<!-- load css dan js -->
<script src="<?php echo ASSETS_URL;?>plugins/form/jquery.validate.js"></script>
<link rel="stylesheet" href="<?php echo ASSETS_URL;?>plugins/select2/select2.min.css" rel="stylesheet" type="text/css"/>
<script src="<?php echo ASSETS_URL;?>plugins/select2/select2.min.js"></script>
<!-- end load css dan js -->

<section class="content-header">
	<h1>
		<?php echo $app_title;?>
		<small><?php echo $app_desc;?></small>
	</h1>
</section>

<section class="content">
	<div class="box box-info"> <!-- start .box -->
		<div class="box-header"> <!-- start .box-header -->
			<?php echo $form_header;?>
		</div> <!-- end .box-header -->
		
		<div class="box-body"> <!-- start .box-body -->
			<form name="frmUser" id="frmUser" method="post" class="form-horizontal">
				<input type="hidden" name="f_user_id" id="f_user_id" value="" >
<!--
				<div class="form-group">
					<label class="col-md-2" style="padding-top:7px;">Relation Employee</label>
					<div class="col-md-6">
					<?php
						echo form_dropdown('f_emp_id',$choose,false,'id="f_emp_id" class="form-control select" style="width:100%"');
					?>
					</div>
				</div>
-->
				<div class="form-group">
					<label class="col-md-2" style="padding-top:7px;">Email <span class="text-red">*</span></label>
					<div class="col-md-6"><input type="text" name="f_user_email" id="f_user_email" class="form-control" placeholder="Email ..." value="" data-rule-required="true" data-rule-email="true"></div>
				</div>
				<div class="form-group">
					<label class="col-md-2" style="padding-top:7px;">Password</label>
					<div class="col-md-6"><input type="password" name="f_user_password" id="f_user_password" class="form-control" placeholder="Password ..." value="" data-rule-required="true" data-rule-minlength="5"></div>
				</div>
				<div class="form-group">
					<label class="col-md-2" style="padding-top:7px;">Re-Type Password</label>
					<div class="col-md-6">
						<input type="password" name="rpassword" id="rpassword" class="form-control" placeholder="Re-Type Password ..." data-rule-equalto="#f_user_password" required>
					</div>
				</div>
				<div class="form-group">
					<label class="col-md-2" style="padding-top:7px;">Name <span class="text-red">*</span></label>
					<div class="col-md-6"><input type="text" name="f_user_name" id="f_user_name" class="form-control" placeholder="Name ..." value="" required></div>
				</div>
<?php if($this->session->userdata('sLevel') != 3) { ?>
				<div class="form-group">
					<label class="col-md-2" style="padding-top:7px;">Level</label>
					<div class="col-md-6">
					<?php
						$level = array(1=>'Super Admin',2=>'Admin',3=>'User',); 
						if($this->session->userdata('sLevel') == 2) {
							unset($level[1]);
						} else if ($this->session->userdata('sLevel') == 3) {
							unset($level[1]);unset($level[2]);
						}
						
						echo form_dropdown('f_user_level',$level,$this->session->userdata('sLevel'),'id="f_user_level" class="form-control select"');
					?>
					</div>
				</div>
				<div class="form-group">
					<label class="col-md-2" style="padding-top:7px;">Role <span class="text-red">*</span></label>
					<div class="col-md-6">
					<?php
						$choose[''] = '';
						echo form_dropdown('f_user_role',$choose,false,'id="f_user_role" class="form-control select" required');
					?>
					</div>
				</div>
				<div class="form-group">
					<label class="col-md-2" style="padding-top:7px;">Active</label>
					<div class="col-md-6">
					<?php
						$active = array(1=>'Active',0=>'Non Active',);
						echo form_dropdown('f_user_active',$active,1,'id="f_user_active" class="form-control select"');
					?>
					</div>
				</div>
<?php } ?>
				<div class="form-group">
					<label class="col-md-2" style="padding-top:7px;">Create</label>
					<div class="col-md-3"><input type="text" name="f_user_create_on" id="f_user_create_on" class="form-control" placeholder="Create Date ..." value="" disabled></div>
					<div class="col-md-3"><input type="text" name="f_user_create_by" id="f_user_create_by" class="form-control" placeholder="Create By ..." value="" disabled></div>
				</div>
				<div class="form-group">
					<label class="col-md-2" style="padding-top:7px;">Update</label>
					<div class="col-md-3"><input type="text" name="f_user_update_on" id="f_user_update_on" class="form-control" placeholder="Update Date ..." value="" disabled></div>
					<div class="col-md-3"><input type="text" name="f_user_update_by" id="f_user_update_by" class="form-control" placeholder="Update by ..." value="" disabled></div>
				</div>	
				<div class="col-md-8" align="center" id="fnButton">
		<?php 	
			if($fAct == 'Add')	echo '<input type="submit" name="submit" value="Save" id="btnSave" class="btn btn-success" />';

			if($fAct == 'Edit')	echo '<input type="submit" name="submit" value="Ubah" id="btnUpdate" class="btn btn-warning" />';
		?>
				&nbsp;&nbsp;&nbsp;
				<a onClick="fnBack()" class="btn btn-danger"> Cancel</a>
				</div>
			</form>

		</div> <!-- end .box-body -->
		<div class="box-footer" align="right"> <!-- start box-footer -->
		  <p>Page rendered in <strong>{elapsed_time}</strong> seconds</p>
		</div> <!-- end .box-footer-->
	</div> <!-- end .box -->
</section><!-- /.content -->

<script>
var oTable;
$(document).ready(function(){
	$('#frmUser').validate({
		highlight: function(element, errorClass, validClass) {
			$(element).parents('.form-group').removeClass('has-success').addClass('has-error');
		},
		unhighlight: function(element, errorClass, validClass) {
			$(element).parents('.form-group').removeClass('has-error').addClass('has-success');
		}		
	});

	$('#addUser').click(function()	{
		$('#tableUser_wrapper').hide();
		$('.box-header').hide();
		$('#frmUser').trigger("reset");
		$('#frmUser').show();
		$('#fnButton').html('<input type="submit" name="submit" value="Save" id="btnSave" class="btn btn-success" />&nbsp;&nbsp;&nbsp;<a onClick="fnBack()" class="btn btn-danger"> Cancel</a>');
	});

	var $f_user_role = $('#f_user_role'); 
    $f_user_role.select2({
		placeholder: "-- Pilih ..",
		ajax: {
			url 		: "<?php echo site_url('role_module/fnRole_moduleComboData');?>", // << Example
			dataType	: 'json',
			delay		: 250,
			processResults		: function(data,params) {
				return {
				        results: data.result,
					}
			},
			cache: true
		},
		minimumInputLength: 0,
		//escapeMarkup: function (markup) { return markup; },
		//templateResult: formatAccount,
	});

	var $f_user_level = $('#f_user_level'); 
	$f_user_level.select2();

	//var $f_emp_id = $('#f_emp_id'); 
    //$f_emp_id.select2({
		//placeholder: "-- Pilih ..",
		//ajax: {
			//url 		: "<?php echo site_url('user/fnUserComboDataEmp');?>", // << Example
			//dataType	: 'json',
			//delay		: 250,
			//processResults		: function(data,params) {
				//return {
				        //results: data.result,
					//}
			//},
			//cache: true
		//},
		///*
		//language: {
			//noResults: function() {
				//return "No results found<br/><a href='http://google.com'><i>Create ...</i></a>";
			//}
		//},*/
		//minimumInputLength: 0,
		//escapeMarkup: function (markup) { return markup; },
		//templateResult: formatEmploye,
	//}).on("select2:select", function (e) {
		//$("#f_user_name").val(e.params.data.text);
		//$("#f_user_email").val(e.params.data.email);
	//});

	var $f_user_active = $('#f_user_active'); 
    $f_user_active.select2();

<?php if($fAct == 'Edit') { ;?>
	$('#f_user_password').attr('data-rule-required','false');
	$('#rpassword').removeAttr('required');
	$.get("<?php echo site_url('user/fnUserDataId?id='.$fId);?>",function(data){
		if(data)
		{
			//alert(data.f_emp_id);
			$('#f_user_id').val(data.f_user_id);
			$('#f_user_email').val(data.f_user_email);
			//$('#f_user_password').val(data.f_user_password);
			$('#f_user_name').val(data.f_user_name);
			$('#f_user_level').val(data.f_user_level).trigger('change');

			var $opt_role = $('<option value="'+data.f_user_role+'" selected>'+data.f_role_code+'</option>');
			$('#f_user_role').append($opt_role).trigger('change');
			
			if(data.f_emp_id != null)
			{
				var $opt_emp = $('<option value="'+data.f_emp_id+'" selected>'+data.f_emp_name+'</option>');
				$('#f_emp_id').append($opt_emp).trigger('change');
			}

			$('#f_user_active').val(data.f_user_active);
			$('#f_user_create_on').val(data.f_user_create_on);
			$('#f_user_create_by').val(data.f_user_create_by);
			$('#f_user_update_on').val(data.f_user_update_on);
			$('#f_user_update_by').val(data.f_user_update_by);
		}
	}, "json");

	$('#f_user_password').on('keyup',function(){
		var pass = $(this).val();
		if(pass.length>0) {
			$('#rpassword').attr('required','required');
		} else {
			$('#rpassword').removeAttr('required');
		}
		
	});
	var url = "<?php echo site_url('user/fnUserUpdate?id='.$fId);?>";
<?php } ?>

<?php if($fAct == 'Add') { ;?>
	var url = "<?php echo site_url('user/fnUserSave');?>";
<?php } ?>

	// Menyimpan data
	$('#frmUser').submit(function(e){
		var oForm = $('#frmUser');
		var rec = $('#frmUser').serializeArray();
		rec.push({name: '<?php echo $this->security->get_csrf_token_name(); ?>',value:'<?php echo $this->security->get_csrf_hash();?>'});
		e.preventDefault();
		if(oForm[0].checkValidity()) {
			$.post(url, rec, function(msg){
				if(msg)
				{
					window.location.href = "<?php echo site_url('user');?>";
				}
				else
				{
					infoDialog.realize();
					infoDialog.setTitle('Info').setMessage('<center>Gagal !!</center>').setType(BootstrapDialog.TYPE_DANGER).open();
				}
			}, 'json');
		}
	});
});

function fnBack()
{
	window.location.href = "<?php echo site_url('user');?>";
}

function formatEmploye(result)
{
	if (result.loading) return result.text;
	
	var markup = "<span>"+result.text+"<br/>Division : "+result.segment+"</span>";
	return markup;
}
</script>



