<?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="form-group">
		<label><span style="color: red;">*</span> Type 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="form-group">
		<label><span style="color: red;"></span> Description</label>
		<input type="text" name="DESCRIPT" maxlength="50" value="<?php if(isset($data)) echo $ci->fm->str_esc($data->DESCRIPT, 2);?>" class="form-control" autocomplete="OFF">
	</div>

	<div class="form-group">
		<label><span style="color: red;"></span> Remarks</label>
		<textarea name="REMARKS" class="form-control"><?php if(isset($data)) echo $ci->fm->str_esc($data->REMARKS, 2);?></textarea>
	</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(){
		$("#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>