<?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="PKKEY" value="<?php echo $data->PKKEY;?>">
	<?php endif;?>
	<!--  -->
	
	<div class="form-group">
		<label><span style="color: red;">*</span> Username</label>
		<?php if(! isset($data)):?>
			<div class="has-feedback">
				<input type="text" name="CODE" onInput="edit_check_unique();" maxlength="10" class="form-control" required="required" autocomplete="OFF">
				<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>
		<?php else:?>
			<p class="form-control-static"><?php echo $data->CODE;?></p>
		<?php endif;?>
	</div>
	
	<div class="form-group">
		<label><span style="color: red;">*</span> Name</label>
		<input type="text" name="NAME" value="<?php if(isset($data)) echo $ci->fm->str_esc($data->NAME, 2);?>" maxlength="100" class="form-control" required="required" autocomplete="OFF">
	</div>
	
	<div class="form-group">
		<label><span style="color: red;">*</span> Group</label>
		<div style="position: relative;">
			<select name="USER_GROUP" class="form-control select2" required="required">
				<option value="">--- Select a value ---</option>
				<?php foreach($all_group as $v):?>
					<option value="<?php echo $v->PKKEY;?>" <?php if(isset($data) && $data->USER_GROUP == $v->PKKEY) echo "selected='selected'";?>>
						<?php echo $v->NAME;?>
					</option>
				<?php endforeach;?>
			</select>
		</div>
	</div>
	
	<div class="form-group">
		<label><span style="color: red;"></span> Active</label>
		<div style="display: block;">
				<input type="hidden" name="ACTIVE" value="0">
				<input type="checkbox" name="ACTIVE" id="" value="1" style="width: 20px; height: 20px;" <?php if(isset($data) && $data->ACTIVE == "1") echo "checked";?>>
			</div>
	</div>
	
	<?php if(isset($data)):?>
		<?php 
			$string = "";
			if($data->CREATE_USER_CODE != "" || $data->DATE_CREATED != ""){
				if($string != "") $string .= "<br>";
				$string .= "Created";
				if($data->CREATE_USER_CODE != "") $string .= " by [".$data->CREATE_USER_CODE."] ".$data->CREATE_USER_NAME;
				if($data->DATE_CREATED != "") $string .= " on ".date("d M Y h:i A", strtotime($data->DATE_CREATED));
				$string .= ".";
			}
			
			if($data->MODIFY_USER_CODE != "" || $data->DATE_MODIFIED != ""){
				if($string != "") $string .= "<br>";
				$string .= "Last modified";
				if($data->MODIFY_USER_CODE != "") $string .= " by [".$data->MODIFY_USER_CODE."] ".$data->MODIFY_USER_NAME;
				if($data->DATE_MODIFIED != "") $string .= " on ".date("d M Y h:i A", strtotime($data->DATE_MODIFIED));
				$string .= ".";
			}
		?>
		<?php if($string != ""):?>
			<div class="form-group">
				<label>User Info</label>
				<p class="form-control-static">
					<?php echo $string;?>
				</p>
			</div>
		<?php endif;?>
	<?php endif;?>
	
	<?php if(! isset($data)):?>
		<div class="form-group">
			<label></label>
			<p style="margin-bottom: 0px; color: #F90;">
				<strong>* Note:</strong>
				The default password is <span style="color: red; font-weight: bold;">1234</span>.
			</p>
		</div>
	<?php endif;?>
	
	<?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_reset_password):?>
				$(".btn_reset").show();
			<?php endif;?>
			<?php if($can_delete && $data->PKKEY != $this->session->userdata('user_id')):?>
				$(".btn_delete").show();
			<?php endif;?>

			<?php if($can_reset_password || ($can_delete && $data->PKKEY != $this->session->userdata('user_id'))):?>
				$("#edit_div_action").show("fast");
			<?php endif;?>
			
			$("#reset_confirmation [name='PKKEY']").val("<?php echo $data->PKKEY;?>");
			$("#delete_modal [name='PKKEY']").val("<?php echo $data->PKKEY;?>");
		<?php endif;?>

		<?php if(isset($data) && ! $can_edit):?>
			$("#edit_ajax_container input").prop("disabled", true);
			$("#edit_ajax_container select").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='PKKEY']").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>