<?php $ci = get_instance();?>
<form onSubmit="return edit_validation(this);" action="<?php echo base_url($this->router->fetch_class()."/update")?>" method="POST" style="padding-left: 5px; padding-right: 5px;">
	<!--  -->
	<?php if(isset($data)):?>
		<input type="hidden" name="CODE" value="<?php echo $data->CODE;?>">
	<?php endif;?>
	<!--  -->
<div class = "row">
    <div class="col-md-9">
	    <div class="form-group">
			<label><span style="color: red;">*</span> Staff Code</label>
			<div class="has-feedback">
				<input type="text" name="CODE" onInput="edit_check_unique();" value="<?php if(isset($data)) echo $ci->fm->str_esc($data->CODE, 2);?>" maxlength="10" class="form-control" required="required" autocomplete="OFF" <?php if(isset($data)) echo "readonly";?>>
				<span class="form-control-feedback" aria-hidden="true">
					<i class="fa fa-check check_unique_feedback_ok" style="display: none;"></i>
					<i class="fa fa-spinner fa-pulse check_unique_feedback_loading" style="display: none;"></i>
				</span>
			</div>
		</div>

		<div class = "row">
		    <div class="form-group col-md-6">
		        <label><span style="color: red;">*</span> Name</label>
		        <input type="text" name="NAME"  maxlength="80"  value="<?php if(isset($data)) echo $ci->fm->str_esc($data->NAME, 2);?>" class="form-control" required="required" autocomplete="OFF"/>
		    </div>
		     <div class="form-group col-md-6">
		        <label><span style="color: red;"></span> NRIC</label>
		        <input type="text" name="IC_NO" maxlength="40" value="<?php if(isset($data)) echo $ci->fm->str_esc($data->IC_NO, 2);?>" class="form-control" autocomplete="OFF"/>
		    </div>
	    </div>

	    <div class = "row">
		    <div class="form-group col-md-4">
		        <label><span style="color: red;"></span> Sex</label>
		        <div style="padding-top: 5px;">
			        <label style="padding-right: 20px; padding-left: 20px;"><input type="radio" name="SEX" style="width: 20px; height: 20px;" value="M" <?php if(isset($data) && $data->SEX == "M") echo "checked";?>> Male</label>
					<label style="padding-right: 20px;"><input type="radio" name="SEX" style="width: 20px; height: 20px;" value="F" <?php if(isset($data) && $data->SEX == "F") echo "checked";?>> Female</label>
				</div>
		    </div>
		    <div class="form-group col-md-4">
		        <label><span style="color: red;"></span> Telephone No.</label>
		        <input type="text" name="TEL_NO" maxlength="20" value="<?php if(isset($data)) echo $ci->fm->str_esc($data->TEL_NO, 2);?>" class="form-control" autocomplete="OFF"/>
		    </div>
		     <div class="form-group col-md-4">
		        <label><span style="color: red;"></span> Position</label>
		        <input type="text" name="POSITION" maxlength="40" value="<?php if(isset($data)) echo $ci->fm->str_esc($data->POSITION, 2);?>" class="form-control" autocomplete="OFF"/>
		    </div>
	    </div>

	    <div class = "row">
		    <div class="form-group col-md-8">
		        <label><span style="color: red;"></span> Address</label>
				<textarea name="ADDRESS" class="form-control"><?php if(isset($data)) echo $ci->fm->str_esc($data->ADDRESS, 2);?></textarea>
		    </div>
		    <div class="form-group col-md-4">
		        <label><span style="color: red;"></span> Postcode</label>
		        <input type="text" name="POSTCODE" maxlength="80" value="<?php if(isset($data)) echo $ci->fm->str_esc($data->POSTCODE , 2);?>" class="form-control" autocomplete="OFF"/>
		    </div>
	    </div>

	     <div class = "row">
		    <div class="form-group col-md-6">
		        <label><span style="color: red;"></span> Bank No</label>
		        <input type="text" name="BANK_NO" maxlength="20" value="<?php if(isset($data)) echo $ci->fm->str_esc($data->BANK_NO, 2);?>" class="form-control" autocomplete="OFF"/>
		    </div>
		    <div class="form-group col-md-6">
		        <label><span style="color: red;"></span> Education</label>
		        <input type="text" name="EDUCATION" maxlength="20" value="<?php if(isset($data)) echo $ci->fm->str_esc($data->EDUCATION, 2);?>" class="form-control" autocomplete="OFF"/>
		    </div>
	    </div>

	    <div class = "row">
		    <div class="form-group col-md-4">
		        <label><span style="color: red;"></span> EPF No.</label>
		        <input type="text" name="EPF_NO" maxlength="15" value="<?php if(isset($data)) echo $ci->fm->str_esc($data->EPF_NO, 2);?>" class="form-control" autocomplete="OFF"/>
		    </div>
		    <div class="form-group col-md-4">
		        <label><span style="color: red;"></span> SOCSO No.</label>
		        <input type="text" name="SOCSO_NO" maxlength="15" value="<?php if(isset($data)) echo $ci->fm->str_esc($data->SOCSO_NO, 2);?>" class="form-control" autocomplete="OFF"/>
		    </div>
		    <div class="form-group col-md-4">
		        <label><span style="color: red;"></span> ICM Tax No.</label>
		        <input type="text" name="ICMTAX_NO" maxlength="15" value="<?php if(isset($data)) echo $ci->fm->str_esc($data->ICMTAX_NO, 2);?>" class="form-control" autocomplete="OFF"/>
		    </div>
	    </div>

	    <div class = "row">
		    <div class="form-group col-md-12">
		    	 <label><span style="color: red;"></span> Memo</label>
				<textarea name="MEMO" class="form-control"><?php if(isset($data)) echo $ci->fm->str_esc($data->MEMO, 2);?></textarea>
		    </div>
	    </div>
	</div>
	<div class="col-md-3">
		<fieldset class="the-fieldset">
			<legend class="the-legend" style="font-weight: normal; font-size: 16px;">Date</legend>
				<label><span style="color: red;">*</span> Join Date</label>
				<div class="form-group">
		            <div class="has-feedback" style="max-width: 200px; display: inline-block;">
		            <input type="text" name="JOIN" id="JOIN" value="<?php if(isset($data) && strlen($data->JOIN) > 0) echo $this->fm->str_esc(date("Y-m-d", strtotime($data->JOIN)), 2);?>" maxlength="19" class="form-control datepicker" autocomplete="OFF" required="required">
		            </div>
		        </div><br/>
		        <label><span style="color: red;"></span> Resign Date</label>
				<div class="form-group">
		            <div class="has-feedback" style="max-width: 200px; display: inline-block;">
		            <input type="text" name="RESIGN" id="RESIGN" value="<?php if(isset($data) && strlen($data->RESIGN) > 0) echo $this->fm->str_esc(date("Y-m-d", strtotime($data->RESIGN)), 2);?>" maxlength="19" class="form-control datepicker" autocomplete="OFF">
		            </div>
		        </div><br/>
			</fieldset>
	</div>
</div>
	
	<?php if((! isset($data) && $can_create) || (isset($data) && $can_edit)):?>
		<div class="form-group" style="margin-top: 30px;">
			<label></label>
			<button type="submit" id="edit_btn_submit" class="btn btn-primary btn-xs-block btn_submit">
				<i class="fa fa-save"></i>
				Save
			</button>
		</div>
	<?php endif;?>
</form>
<script>
	$(document).ready(function(){

		$(".datepicker").datepicker({
            format: "yyyy-mm-dd",
            autoclose: true,
            todayHighlight: true,
            
        });

		$("#edit_ajax_container .select2").select2();

		<?php if(isset($data)):?>
			<?php if($can_delete):?>
				$(".btn_delete").show();
			<?php endif;?>

			<?php if($can_delete):?>
				$("#edit_div_action").show("fast");
			<?php endif;?>
		
			$("#delete_modal [name='CODE']").val("<?php echo $data->CODE;?>");
		<?php endif;?>

		<?php if(isset($data) && ! $can_edit):?>
			$("#edit_ajax_container input").prop("disabled", true);
			$("#edit_ajax_container select").prop("disabled", true);
			$("#edit_ajax_container textarea").prop("disabled", true);
		<?php endif;?>

	});

	function edit_check_unique(){
		var selector = "#edit_ajax_container [name='CODE']";
		$(selector).closest("div").removeClass("has-success");
		$(selector).closest("div").removeClass("has-error");
		$(selector).closest("div").find(".check_unique_feedback_ok").hide("fast");
		$(selector).closest("div").find(".check_unique_feedback_loading").show("fast");
		disable_btn("#edit_btn_submit");

		var unique = $(selector).val();
		checking_unique = unique;

		var params = "";
		params += "&unique=" + unique;
		<?php if(isset($data)):?>
			params += "&pk=" + $("#edit_ajax_container [name='CODE']").val();
		<?php endif;?>

		clearTimeout(timer_check_unique);
		timer_check_unique = setTimeout(function(){
			$.ajax({
				type: "POST",
				url: "<?php echo base_url($this->router->fetch_class());?>/check_unique",
				data: params,
				success: function(result, status, xhr){
					if(checking_unique == unique){
						if(result == "1"){
							$(selector).closest("div").addClass("has-success");
							$(selector).closest("div").find(".check_unique_feedback_ok").show("fast");
							enable_btn("#edit_btn_submit");
						}
						else{
							$(selector).closest("div").addClass("has-error");
						}
					}
				},
				error: function(xhr,status,error){
					if(checking_unique == unique){
					}
				},
				complete: function(xhr,status){
					if(checking_unique == unique){
						$(selector).closest("div").find(".check_unique_feedback_loading").hide("fast");
					}
				},
			});
		}, 500);
	}
	
	function edit_validation(form){
		var error = "";
		
		$(form).find("[required]:not(:disabled)").each(function(){
			if($(this).val() == ""){
				if(error != "") error += "<br>";
				switch($(this).attr("name")){
					case "USERNAME":
						error += "Please fill in the Username!";
						break;
					case "NAME":
						error += "Please fill in the Name!";
						break;
					case "GROUP_ID":
						error += "Please select a Group!";
						break;
					default:
						error += "Please complete the Form!";
						break;
				}
				
				return false;
			}
		});

		if(error == ""){
			disable_btn(".btn_submit");
		}
		else{
			prompt_error(error);
			return false;
		}
	}


</script>