<?php 
$file_name		= "index?page=admin_view";
$form_action	= "insert";
$button_value 	= "Add User";
$table_name 	= "admin";
$page_name      = "Add User";
$id	 = "";
$vat = "";
$query_string = "";
$is_active = "1";
$rr_query = mysqli_query($conn, "SELECT IFNULL(MAX(CAST(SUBSTRING(user_reference, 5) AS UNSIGNED)), 0) + 1 AS new_id FROM admin");
$rr_row = mysqli_fetch_assoc($rr_query);
$user_reference = 'REFE' . str_pad($rr_row['new_id'], 3, '0', STR_PAD_LEFT);



################################################## START INSERT ##################################################
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'insert'){
    utility::addslashesOnAllInput(array("query_string"));
	
	$user_reference     = $_REQUEST['user_reference'];
	$first_name         = $_REQUEST['first_name'];
	$surname            = $_REQUEST['surname'];
	$email              = $_REQUEST['email'];
	$password           = md5($_REQUEST['password']); // Make sure to hash passwords
	$group_id         = $_REQUEST['group_id'];
	$post_code          = $_REQUEST['post_code'];
	$mobile             = $_REQUEST['mobile'];
	$home_phone         = $_REQUEST['home_phone'];
	$address_1          = $_REQUEST['address_1'];
	$address_2          = $_REQUEST['address_2'];
	$address_3          = $_REQUEST['address_3'];
	$start_date         = utility::setFormatedDate($_REQUEST['start_date']);
	$leave_date         = utility::setFormatedDate($_REQUEST['leave_date']);
	$ni_number          = $_REQUEST['ni_number'];
	$pension            = $_REQUEST['pension'];
	$clock_no           = $_REQUEST['clock_no'];

	$kin1_name          = $_REQUEST['kin1_name'];
	$kin1_address_1     = $_REQUEST['kin1_address_1'];
	$kin1_address_2     = $_REQUEST['kin1_address_2'];
	$kin1_address_3     = $_REQUEST['kin1_address_3'];
	$kin1_post_code     = $_REQUEST['kin1_post_code'];
	$kin1_relationship  = $_REQUEST['kin1_relationship'];
	$kin1_phone         = $_REQUEST['kin1_phone'];
	$kin1_phone_type    = $_REQUEST['kin1_phone_type'];

	$kin2_name          = $_REQUEST['kin2_name'];
	$kin2_address_1     = $_REQUEST['kin2_address_1'];
	$kin2_address_2     = $_REQUEST['kin2_address_2'];
	$kin2_address_3     = $_REQUEST['kin2_address_3'];
	$kin2_post_code     = $_REQUEST['kin2_post_code'];
	$kin2_relationship  = $_REQUEST['kin2_relationship'];
	$kin2_phone         = $_REQUEST['kin2_phone'];
	$kin2_phone_type    = $_REQUEST['kin2_phone_type'];

	$doctor_name        = $_REQUEST['doctor_name'];
	$doctor_post_code   = $_REQUEST['doctor_post_code'];
	$doctor_phone       = $_REQUEST['doctor_phone'];
	$doctor_address_1   = $_REQUEST['doctor_address_1'];
	$doctor_address_2   = $_REQUEST['doctor_address_2'];
	$doctor_address_3   = $_REQUEST['doctor_address_3'];

	$bank_name          = $_REQUEST['bank_name'];
	$bank_address_1     = $_REQUEST['bank_address_1'];
	$bank_address_2     = $_REQUEST['bank_address_2'];
	$bank_address_3     = $_REQUEST['bank_address_3'];
	$bank_post_code     = $_REQUEST['bank_post_code'];
	$account_no         = $_REQUEST['account_no'];
	$short_code         = $_REQUEST['short_code'];
	$personal_ref_no    = $_REQUEST['personal_ref_no'];
	
    $created_on				= date("Y-m-d H:i:s");	
	$created_by				= $_SESSION['user_id'];
	$updated_by				= $_SESSION['user_id'];
	
	$right_to_work          = isset($_REQUEST['right_to_work']) ? 1 : 0;
	$right_to_work_expiry   = utility::setFormatedDate($_REQUEST['right_to_work_expiry']);
	$documents_reviewed     = isset($_REQUEST['documents_reviewed']) ? 1 : 0;
	
    $is_super_admin= (isset($_REQUEST['is_super_admin'])) ? 1 : 0;
	$is_active= (isset($_REQUEST['is_active'])) ? 1 : 0;
	$admin_color = $config->adminColor;
	
	$sql = "user_reference = '$user_reference', uname = '$first_name', surname = '$surname', email = '$email', password = '$password', group_id = '$group_id', post_code = '$post_code', mobile = '$mobile', home_phone = '$home_phone', address_1 = '$address_1', address_2 = '$address_2', address_3 = '$address_3', start_date = '$start_date', leave_date = '$leave_date', ni_number = '$ni_number', pension = '$pension',clock_no = '$clock_no', kin1_name = '$kin1_name', kin1_address_1 = '$kin1_address_1', kin1_address_2 = '$kin1_address_2', kin1_address_3 = '$kin1_address_3', kin1_post_code = '$kin1_post_code', kin1_relationship = '$kin1_relationship', kin1_phone = '$kin1_phone', kin1_phone_type = '$kin1_phone_type', kin2_name = '$kin2_name', kin2_address_1 = '$kin2_address_1', kin2_address_2 = '$kin2_address_2', kin2_address_3 = '$kin2_address_3', kin2_post_code = '$kin2_post_code', kin2_relationship = '$kin2_relationship', kin2_phone = '$kin2_phone', kin2_phone_type = '$kin2_phone_type', doctor_name = '$doctor_name', doctor_post_code = '$doctor_post_code', doctor_phone = '$doctor_phone', doctor_address_1 = '$doctor_address_1', doctor_address_2 = '$doctor_address_2', doctor_address_3 = '$doctor_address_3', bank_name = '$bank_name', bank_address_1 = '$bank_address_1', bank_address_2 = '$bank_address_2', bank_address_3 = '$bank_address_3', bank_post_code = '$bank_post_code', account_no = '$account_no', short_code = '$short_code', personal_ref_no = '$personal_ref_no', is_active = '$is_active',is_super_admin = '$is_super_admin',admin_color = '$admin_color', right_to_work = '$right_to_work', right_to_work_expiry = '$right_to_work_expiry', documents_reviewed = '$documents_reviewed', created_on = '$created_on', created_by = '$created_by', updated_by = '$updated_by'";
	
	$sql=utility::convert_to_crypt_data($sql,$table_name);
	$sql= "INSERT INTO $table_name SET ".$sql;
    mysqli_query($conn,$sql);
	$document_id = mysqli_insert_id($conn);
	$_SESSION['lead_consultant_session_array'][$document_id] = $name;
	
	if(isset($_FILES['signature']['name']) && $_FILES['signature']['name'] !=""){
		$path = $_FILES['signature']['name'];
		$ext = pathinfo($path, PATHINFO_EXTENSION);
		if($ext == "jpg" || $ext == "jpeg" || $ext == "png"){
			$digits = 5;
			$random_number = rand(pow(10, $digits-1), pow(10, $digits)-1);
			$uploaddir = '../signature/';
			$signature_name_array = explode(".",$_FILES['signature']['name']);
			$signature_name = $document_id."_".$signature_name_array[0]."_".$random_number.".".$signature_name_array[1];
			$uploadfile = $uploaddir . $signature_name;
			move_uploaded_file($_FILES['signature']['tmp_name'], $uploadfile);
			$sql= "UPDATE $table_name SET signature = '$signature_name' WHERE id = $document_id ";
			mysqli_query($conn,$sql);
			
			$tmp = ucfirst(str_replace("_"," ", $table_name));
			$message	= ucfirst("Admin added Successfully");
		}else{
			$message = "Please upload only png or jpg signature";
		}
    }else{
		$message	= ucfirst("User added Successfully");
	}
	
	header("Location: $file_name&message=$message");
    exit();
}
################################################# END INSERT #################################################

################################################## START EDIT ##################################################
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'edit'){
	$form_action = "update";
	$button_value = "Update";
	$page_name      = "Edit User";
	$query_string =  (isset($_POST['query_string'])) ? $_POST['query_string'] : $_SERVER['QUERY_STRING'];
	
    $id = $_REQUEST['id'];
    $sql= "SELECT * FROM admin WHERE id = $id";
    $result_edit = mysqli_query($conn,$sql);
    $row_edit = mysqli_fetch_array($result_edit);

    
	$user_reference     = $row_edit['user_reference'];
	$first_name         = $row_edit['uname'];
	$surname            = $row_edit['surname'];
	$email              = $row_edit['email'];
	$password ='';          //= md5($row_edit['password']); // Make sure to hash passwords
	$group_id         = $row_edit['group_id'];
	$post_code          = $row_edit['post_code'];
	$mobile             = $row_edit['mobile'];
	$home_phone         = $row_edit['home_phone'];
	$address_1          = $row_edit['address_1'];
	$address_2          = $row_edit['address_2'];
	$address_3          = $row_edit['address_3'];
	$start_date         = ($row_edit['start_date']);
	$leave_date         = ($row_edit['leave_date']);
	$ni_number          = $row_edit['ni_number'];
	$pension            = $row_edit['pension'];
	$clock_no           = $row_edit['clock_no'];

	$kin1_name          = $row_edit['kin1_name'];
	$kin1_address_1     = $row_edit['kin1_address_1'];
	$kin1_address_2     = $row_edit['kin1_address_2'];
	$kin1_address_3     = $row_edit['kin1_address_3'];
	$kin1_post_code     = $row_edit['kin1_post_code'];
	$kin1_relationship  = $row_edit['kin1_relationship'];
	$kin1_phone         = $row_edit['kin1_phone'];
	$kin1_phone_type    = $row_edit['kin1_phone_type'];

	$kin2_name          = $row_edit['kin2_name'];
	$kin2_address_1     = $row_edit['kin2_address_1'];
	$kin2_address_2     = $row_edit['kin2_address_2'];
	$kin2_address_3     = $row_edit['kin2_address_3'];
	$kin2_post_code     = $row_edit['kin2_post_code'];
	$kin2_relationship  = $row_edit['kin2_relationship'];
	$kin2_phone         = $row_edit['kin2_phone'];
	$kin2_phone_type    = $row_edit['kin2_phone_type'];

	$doctor_name        = $row_edit['doctor_name'];
	$doctor_post_code   = $row_edit['doctor_post_code'];
	$doctor_phone       = $row_edit['doctor_phone'];
	$doctor_address_1   = $row_edit['doctor_address_1'];
	$doctor_address_2   = $row_edit['doctor_address_2'];
	$doctor_address_3   = $row_edit['doctor_address_3'];

	$bank_name          = $row_edit['bank_name'];
	$bank_address_1     = $row_edit['bank_address_1'];
	$bank_address_2     = $row_edit['bank_address_2'];
	$bank_address_3     = $row_edit['bank_address_3'];
	$bank_post_code     = $row_edit['bank_post_code'];
	$account_no         = $row_edit['account_no'];
	$short_code         = $row_edit['short_code'];
	$personal_ref_no    = $row_edit['personal_ref_no'];
	$right_to_work        = $row_edit['right_to_work'];
	$right_to_work_expiry = $row_edit['right_to_work_expiry'];
	$documents_reviewed   = $row_edit['documents_reviewed'];
    $is_super_admin= $row_edit['is_super_admin'];
    $is_active= $row_edit['is_active'];
    $signature= $row_edit['signature'];
	if(isset($_REQUEST['delete_image']) && $_REQUEST['delete_image'] == '1'){	
		$dir = "../signature/";
		if(file_exists($dir.$signature)){ 
			unlink($dir.$signature);				
		}
		$signature ="";
		$db->query("UPDATE admin SET signature='$signature' WHERE id=$id");
		header("location:index?page=add_admin&action=edit&id=".$id);
	
	}
}
################################################# END EDIT ################################################# 

################################################## START UPDATE ################################################## 
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'update'){    
	utility::addslashesOnAllInput(array("query_string"));
	$id=                 $_REQUEST['id'];    
    $user_reference      = $_REQUEST['user_reference'];
	$first_name         = $_REQUEST['first_name'];
	$surname            = $_REQUEST['surname'];
	$email              = $_REQUEST['email'];
    $update_password = (!empty($_REQUEST['password'])) ? " password= '".md5($_REQUEST['password'])."' , " : "";
	$group_id         = $_REQUEST['group_id'];
	$post_code          = $_REQUEST['post_code'];
	$mobile             = $_REQUEST['mobile'];
	$home_phone         = $_REQUEST['home_phone'];
	$address_1          = $_REQUEST['address_1'];
	$address_2          = $_REQUEST['address_2'];
	$address_3          = $_REQUEST['address_3'];
	$start_date         = utility::setFormatedDate($_REQUEST['start_date']);
	$leave_date         = utility::setFormatedDate($_REQUEST['leave_date']);
	$ni_number          = $_REQUEST['ni_number'];
	$pension            = $_REQUEST['pension'];	
	$clock_no           = $_REQUEST['clock_no'];

	$kin1_name          = $_REQUEST['kin1_name'];
	$kin1_address_1     = $_REQUEST['kin1_address_1'];
	$kin1_address_2     = $_REQUEST['kin1_address_2'];
	$kin1_address_3     = $_REQUEST['kin1_address_3'];
	$kin1_post_code     = $_REQUEST['kin1_post_code'];
	$kin1_relationship  = $_REQUEST['kin1_relationship'];
	$kin1_phone         = $_REQUEST['kin1_phone'];
	$kin1_phone_type    = $_REQUEST['kin1_phone_type'];

	$kin2_name          = $_REQUEST['kin2_name'];
	$kin2_address_1     = $_REQUEST['kin2_address_1'];
	$kin2_address_2     = $_REQUEST['kin2_address_2'];
	$kin2_address_3     = $_REQUEST['kin2_address_3'];
	$kin2_post_code     = $_REQUEST['kin2_post_code'];
	$kin2_relationship  = $_REQUEST['kin2_relationship'];
	$kin2_phone         = $_REQUEST['kin2_phone'];
	$kin2_phone_type    = $_REQUEST['kin2_phone_type'];

	$doctor_name        = $_REQUEST['doctor_name'];
	$doctor_post_code   = $_REQUEST['doctor_post_code'];
	$doctor_phone       = $_REQUEST['doctor_phone'];
	$doctor_address_1   = $_REQUEST['doctor_address_1'];
	$doctor_address_2   = $_REQUEST['doctor_address_2'];
	$doctor_address_3   = $_REQUEST['doctor_address_3'];

	$bank_name          = $_REQUEST['bank_name'];
	$bank_address_1     = $_REQUEST['bank_address_1'];
	$bank_address_2     = $_REQUEST['bank_address_2'];
	$bank_address_3     = $_REQUEST['bank_address_3'];
	$bank_post_code     = $_REQUEST['bank_post_code'];
	$account_no         = $_REQUEST['account_no'];
	$short_code         = $_REQUEST['short_code'];
	$personal_ref_no    = $_REQUEST['personal_ref_no'];
	$is_super_admin= (isset($_REQUEST['is_super_admin'])) ? 1 : 0;
	$is_active= (isset($_REQUEST['is_active'])) ? 1 : 0;
	
	$right_to_work          = isset($_REQUEST['right_to_work']) ? 1 : 0;
	$right_to_work_expiry   = utility::setFormatedDate($_REQUEST['right_to_work_expiry']);
	$documents_reviewed     = isset($_REQUEST['documents_reviewed']) ? 1 : 0;
	
    $updated_on				= date("Y-m-d H:i:s");
	
	$hidden_signature 	= $_REQUEST['hidden_signature'];
	if(isset($_FILES['signature']['name']) && $_FILES['signature']['name'] !=""){
		$path = $_FILES['signature']['name'];
		$ext = pathinfo($path, PATHINFO_EXTENSION);
		if($ext == "jpg" || $ext == "jpeg" || $ext == "png"){
			$digits = 5;
			$random_number = rand(pow(10, $digits-1), pow(10, $digits)-1);
			$uploaddir = '../signature/';
			$old_signature	=	$uploaddir.$hidden_signature;	
			if(file_exists($old_signature)){
				unlink($old_signature);
			}
			$signature_name_array = explode(".",$_FILES['signature']['name']);
			$signature_name = $id."_".$signature_name_array[0]."_".$random_number.".".$signature_name_array[1];
			$uploadfile = $uploaddir . $signature_name;
			move_uploaded_file($_FILES['signature']['tmp_name'], $uploadfile);
			
			$message	= ucfirst("Signature updated Successfully");
		}else{
			$signature_name = $hidden_signature;
			$message = "Please upload only png or jpg signature";
		}
    }else{
		$message = "Admin User updated Successfully";
		$signature_name = $hidden_signature;
	}
	
	$sql = "user_reference = '$user_reference', uname = '$first_name', surname = '$surname', email = '$email', $update_password group_id = '$group_id', post_code = '$post_code', mobile = '$mobile', home_phone = '$home_phone', address_1 = '$address_1', address_2 = '$address_2', address_3 = '$address_3', start_date = '$start_date', leave_date = '$leave_date', ni_number = '$ni_number', pension = '$pension',clock_no = '$clock_no', kin1_name = '$kin1_name', kin1_address_1 = '$kin1_address_1', kin1_address_2 = '$kin1_address_2', kin1_address_3 = '$kin1_address_3', kin1_post_code = '$kin1_post_code', kin1_relationship = '$kin1_relationship', kin1_phone = '$kin1_phone', kin1_phone_type = '$kin1_phone_type', kin2_name = '$kin2_name', kin2_address_1 = '$kin2_address_1', kin2_address_2 = '$kin2_address_2', kin2_address_3 = '$kin2_address_3', kin2_post_code = '$kin2_post_code', kin2_relationship = '$kin2_relationship', kin2_phone = '$kin2_phone', kin2_phone_type = '$kin2_phone_type', doctor_name = '$doctor_name', doctor_post_code = '$doctor_post_code', doctor_phone = '$doctor_phone', doctor_address_1 = '$doctor_address_1', doctor_address_2 = '$doctor_address_2', doctor_address_3 = '$doctor_address_3', bank_name = '$bank_name', bank_address_1 = '$bank_address_1', bank_address_2 = '$bank_address_2', bank_address_3 = '$bank_address_3', bank_post_code = '$bank_post_code', account_no = '$account_no', short_code = '$short_code', personal_ref_no = '$personal_ref_no', is_active = '$is_active',is_super_admin = '$is_super_admin',admin_color = '$admin_color',right_to_work = '$right_to_work', right_to_work_expiry = '$right_to_work_expiry', documents_reviewed = '$documents_reviewed', updated_on = '$updated_on'";
	
	$sql= "UPDATE $table_name SET ".$sql." WHERE id = '".$id."'";
	mysqli_query($conn,$sql);	
	$_SESSION['lead_consultant_session_array'][$id] = $name;
	
	$query_string = (isset($_REQUEST['query_string']) && !empty($_REQUEST['query_string'])) ? $_REQUEST['query_string'] : "";
  	$query_string = str_ireplace("page=add_admin", "page=admin_view", $query_string);
	$query_string = "&".$query_string;
	//$tmp = ucfirst(str_replace("_"," ", $table_name));
    header("Location: $file_name&message=$message$query_string");
    exit();
}
################################################# END UPDATE ################################################# 
?>

<style>
em{
	color:red;
}
</style>
<section class="content-header">
	<div class="container-fluid">
		<div class="row mb-2">
			<div class="col-sm-6">
				<h1>
				<?=$page_name;?>
				</h1>
			</div>
			 <div class="col-sm-6">
		<?
			$query_string 	=  (isset($_POST['query_string'])) ? $_POST['query_string'] : $_SERVER['QUERY_STRING'];
			if(isset($from) && $from == "view_enquiry_progress"){
				$query_string = str_ireplace("page=add_admin
				", "page=view_enquiry_progress", $query_string);
			}else {
				$query_string = str_ireplace("page=add_admin", "page=admin_view", $query_string);
			}
		?>
		<button type="button" id="back" name="back" value="back"  style="float:right;margin-right:5px;margin-bottom:8px;" class="btn btn-primary" onclick="window.location.href = 'index?<?=$query_string;?>'">Back</button>
	  </div>
		</div>
	</div><!-- /.container-fluid -->
</section>
<section class="content">
	<div class="row">
		<div class="col-12">
			<!-- general form elements -->
               <div class="card">
                <form method="post" name="form1" id="form1" action="index?page=add_admin&action=<? echo $form_action; ?>" onsubmit="return validateForm(this)" enctype="multipart/form-data">
                  <div class="card-body">
				      <div class="form-section mb-3">
						<h4 class="" style="margin-top: 0; margin-bottom: 0; color:#9c4893">Details</h4>
						<hr style="background-color: #9c4893; height:1px; margin-top: 0; margin-bottom: 1;">
						<div class="row">
								<div class="col-lg-3">
									<label for="user_reference">
									User Reference <em>*</em>
									<span id="user_reference_validate" class="text-danger"></span>
								</label>

								<input type="text" id="user_reference" name="user_reference" 
								class="form-control" value="<?php echo $user_reference; ?>" 
								placeholder="User Reference" >
								</div>

								<div class="col-lg-3">
									<label for="first_name">First Name <em>*</em></label>
									<input type="text" id="first_name" name="first_name" class="form-control" value="<?php echo $first_name; ?>" placeholder="First Name" >
									<div class="blank"></div>
								</div>

								<div class="col-lg-3">
									<label for="surname">Surname <em>*</em></label>
									<input type="text" id="surname" name="surname" class="form-control" value="<?php echo $surname; ?>" placeholder="Surname" >
									<div class="blank"></div>
								</div>
								
								<div class="col-lg-3">
									<label for="password">Password <em>*</em></label>
									<input type="password" id="password" name="password" class="form-control" value="<?php echo $password; ?>" placeholder="Password" >
									<div class="blank"></div>
								</div>
                                <div class="col-lg-3">
									<label for="clock_no">Clock No.<em>*</em></label>
									<input type="text" id="clock_no" name="clock_no" class="form-control" value="<?php echo $clock_no; ?>" placeholder="Clock No." >
									<div class="blank"></div>
								</div>
								<div class="col-lg-3">
									<label for="email">Email Address <em></em><span id="email_validate" class="text-danger"></span></label>
									<input type="email" id="email" name="email" class="form-control" value="<?php echo $email; ?>" placeholder="Email Address">
									<div class="blank"></div>
								</div>
                                <div class="col-lg-3">
									<label for="mobile">Mobile <em></em></label>
									<input type="text" id="mobile" name="mobile" class="form-control" value="<?php echo $mobile; ?>" placeholder="Mobile Number" pattern="^\+?[0-9\s()-]*$" oninput="this.value = this.value.replace(/[^0-9+\s()-]/g, '')">
									<div class="blank"></div>
								</div>
								<div class="col-lg-3">
									<label for="home_phone">Home Telephone <em></em></label>
									<input type="text" id="home_phone" name="home_phone" class="form-control" value="<?php echo $home_phone; ?>" placeholder="Home Telephone" pattern="[0-9 ]*">
								</div>

								<div class="col-lg-3">
									<label for="group_id">Department <em>*</em></label>
									<?php Admin::getGroupList("group_id", $group_id, "100%", false, "form-control "); ?>
									<div class="blank"></div>
								</div>

							<div class="col-lg-3">
								<label for="start_date">Start Date  <em>*</em></label>
								<input type="text" class="form-control datepicker" id="start_date" name="start_date" value="<?php (isset($start_date) && $start_date !="") ? utility::getFormatedDate($start_date) : "";?>" placeholder="DD-MM-YYYY" autocomplete="off" >
							</div>

							<div class="col-lg-3">
								<label for="leave_date">Leave Date <em></em></label>
								<input type="text" class="form-control datepicker" id="leave_date" name="leave_date" value="<?php (isset($leave_date) && $leave_date !="") ? utility::getFormatedDate($leave_date) : "";?>" placeholder="DD-MM-YYYY" autocomplete="off">
							</div>

							<div class="col-lg-3">
								<label for="ni_number">N.I. Number <em>*</em><span class="" id="validation_message"></span></label>
								<input type="text" id="ni_number" name="ni_number" class="form-control" value="<?php echo $ni_number; ?>" placeholder="N.I. Number" oninput="validateNINumber()" >
							</div>
                            <div class="col-lg-3">
								<label for="address_1">Address 1 <em></em></label>
								<input type="text" id="address_1" name="address_1" class="form-control" value="<?php echo $address_1; ?>" placeholder="Address 1">
							</div>

							<div class="col-lg-3">
								<label for="address_2">Address 2 <em></em></label>
								<input type="text" id="address_2" name="address_2" class="form-control" value="<?php echo $address_2; ?>" placeholder="Address 2">
							</div>

							<div class="col-lg-3">
								<label for="address_3">Address 3 <em></em></label>
								<input type="text" id="address_3" name="address_3" class="form-control" value="<?php echo $address_3; ?>" placeholder="Address 3">
							</div>
                            <div class="col-lg-3">
									<label for="post_code">Post Code <em></em></label>
									<input type="text" id="post_code" name="post_code" class="form-control" value="<?php echo $post_code; ?>" placeholder="Post Code" oninput="this.value = this.value.toUpperCase();">
									<div class="blank"></div>
								</div>
							<div class="col-lg-3">
								<label for="pension">Pension <em></em></label>
								<? $gsi->getPensionTypeList("pension", $pension, "100%", false, "form-control"); ?>
							</div>
						</div>
					</div>
					
                      <div class="form-section">
						<h4 class="" style="margin-top: 0; margin-bottom: 0;color:#9c4893;">Next of Kin Details 1</h4>
						<hr style="background-color: #9c4893; height:1px; margin-top: 0; margin-bottom: 1;">

						<div class="row">
							<div class="col-lg-3">
								<label for="kin1_name">Name <em></em></label>
								<input type="text" id="kin1_name" name="kin1_name" class="form-control" value="<?php echo $kin1_name; ?>" placeholder="Next of Kin Name">
							</div>

							<div class="col-lg-3">
								<label for="kin1_relationship">Relationship to Yourself <em></em></label>
								<input type="text" id="kin1_relationship" name="kin1_relationship" class="form-control" value="<?php echo $kin1_relationship; ?>" placeholder="Relationship" >
							</div>

							<div class="col-lg-3">
								<label for="kin1_phone">Phone <em></em></label>
								<input type="tel" id="kin1_phone" name="kin1_phone" class="form-control" value="<?php echo $kin1_phone; ?>" placeholder="Phone Number" pattern="^\+?[0-9\s()-]*$" oninput="this.value = this.value.replace(/[^0-9+\s()-]/g, '')" >
							</div>

							<div class="col-lg-3">
								<label for="kin1_phone_type">Phone Type <em></em></label>
								<? $gsi->getKinPhoneTypeList("kin1_phone_type", $kin1_phone_type, "100%", false, "form-control", ""); ?>
							</div>
							<div class="col-lg-3">
								<label for="kin1_address_1">Address 1 <em></em></label>
								<input type="text" id="kin1_address_1" name="kin1_address_1" class="form-control" value="<?php echo $kin1_address_1; ?>" placeholder="Address 1" >
							</div>

							<div class="col-lg-3">
								<label for="kin1_address_2">Address 2 <em></em></label>
								<input type="text" id="kin1_address_2" name="kin1_address_2" class="form-control" value="<?php echo $kin1_address_2; ?>" placeholder="Address 2" >
							</div>

							<div class="col-lg-3">
								<label for="kin1_address_3">Address 3 <em></em></label>
								<input type="text" id="kin1_address_3" name="kin1_address_3" class="form-control" value="<?php echo $kin1_address_3; ?>" placeholder="Address 3" >
							</div>
							<div class="col-lg-3">
								<label for="kin1_post_code">Post Code <em></em></label>
								<input type="text" id="kin1_post_code" name="kin1_post_code" class="form-control" value="<?php echo $kin1_post_code; ?>" placeholder="Post Code"  oninput="this.value = this.value.toUpperCase();">
							</div>

						</div>
					</div>
                     <div class="form-section mt-2">
						<h4 class="" style="margin-top: 0; margin-bottom: 0;color:#9c4893;">Next of Kin Details 2</h4>
						<hr style="background-color: #9c4893; height:1px; margin-top: 0; margin-bottom: 1;">

						<div class="row">
							<div class="col-lg-3">
								<label for="kin2_name">Name <em></em></label>
								<input type="text" id="kin2_name" name="kin2_name" class="form-control" value="<?php echo $kin2_name; ?>" placeholder="Next of Kin Name" >
							</div>

							<div class="col-lg-3">
								<label for="kin2_relationship">Relationship to Yourself <em></em></label>
								<input type="text" id="kin2_relationship" name="kin2_relationship" class="form-control" value="<?php echo $kin2_relationship; ?>" placeholder="Relationship" >
							</div>

							<div class="col-lg-3">
								<label for="kin2_phone">Phone <em></em></label>
								<input type="tel" id="kin2_phone" name="kin2_phone" class="form-control" value="<?php echo $kin2_phone; ?>" placeholder="Phone Number" pattern="^\+?[0-9\s()-]*$" oninput="this.value = this.value.replace(/[^0-9+\s()-]/g, '')" >
							</div>

							<div class="col-lg-3">
								<label for="kin2_phone_type">Phone Type <em></em></label>
								<? $gsi->getKinPhoneTypeList("kin2_phone_type", $kin2_phone_type, "100%", false, "form-control", ""); ?>
							</div>
							<div class="col-lg-3">
								<label for="kin2_address_1">Address 1 <em></em></label>
								<input type="text" id="kin2_address_1" name="kin2_address_1" class="form-control" value="<?php echo $kin2_address_1; ?>" placeholder="Address 1">
							</div>

							<div class="col-lg-3">
								<label for="kin2_address_2">Address 2 <em></em></label>
								<input type="text" id="kin2_address_2" name="kin2_address_2" class="form-control" value="<?php echo $kin2_address_2; ?>" placeholder="Address 2" >
							</div>

							<div class="col-lg-3">
								<label for="kin2_address_3">Address 3 <em></em></label>
								<input type="text" id="kin2_address_3" name="kin2_address_3" class="form-control" value="<?php echo $kin2_address_3; ?>" placeholder="Address 3">
							</div>
							<div class="col-lg-3">
								<label for="kin2_post_code">Post Code <em></em></label>
								<input type="text" id="kin2_post_code" name="kin2_post_code" class="form-control" value="<?php echo $kin2_post_code; ?>" placeholder="Post Code" oninput="this.value = this.value.toUpperCase();">
							</div>

						</div>
					</div>
					<div class="form-section mt-2">
						<h4 class="" style="margin-top: 0; margin-bottom: 0;color:#9c4893;">Doctor Details</h4>
						<hr style="background-color: #9c4893; height:1px; margin-top: 0; margin-bottom: 1;">

						<div class="row">
							<div class="col-lg-3">
								<label for="doctor_name">Name <em></em></label>
								<input type="text" id="doctor_name" name="doctor_name" class="form-control" value="<?php echo $doctor_name; ?>" placeholder="Name" >
							</div>
                            <div class="col-lg-3">
								<label for="doctor_phone">Phone <em></em></label>
								<input type="tel" id="doctor_phone" name="doctor_phone" class="form-control" value="<?php echo $doctor_phone; ?>" placeholder="Phone Number" pattern="^\+?[0-9\s()-]*$" oninput="this.value = this.value.replace(/[^0-9+\s()-]/g, '')" >
							</div>

							
							<div class="col-lg-3">
								<label for="doctor_address_1">Address 1 <em></em></label>
								<input type="text" id="doctor_address_1" name="doctor_address_1" class="form-control" value="<?php echo $doctor_address_1; ?>" placeholder="Address 1" >
							</div>

							<div class="col-lg-3">
								<label for="doctor_address_2">Address 2 <em></em></label>
								<input type="text" id="doctor_address_2" name="doctor_address_2" class="form-control" value="<?php echo $doctor_address_2; ?>" placeholder="Address 2" >
							</div>

							<div class="col-lg-3">
								<label for="doctor_address_3">Address 3 <em></em></label>
								<input type="text" id="doctor_address_3" name="doctor_address_3" class="form-control" value="<?php echo $doctor_address_3; ?>" placeholder="Address 3">
							</div>

							<div class="col-lg-3">
								<label for="doctor_post_code">Post Code <em></em></label>
								<input type="text" id="doctor_post_code" name="doctor_post_code" class="form-control" value="<?php echo $doctor_post_code; ?>" placeholder="Post Code" oninput="this.value = this.value.toUpperCase();">
							</div>

						</div>
					</div>
					<div class="form-section mt-2">
						<h4 class="" style="margin-top: 0; margin-bottom: 0;color:#9c4893;">Bank Details</h4>
						<hr style="background-color: #9c4893; height:1px; margin-top: 0; margin-bottom: 1;">

						<div class="row">
							<div class="col-lg-3">
								<label for="bank_name">Bank Name <em>*</em></label>
								<input type="text" id="bank_name" name="bank_name" class="form-control" value="<?php echo $bank_name; ?>" placeholder="Bank Name" >
							</div>
                            <div class="col-lg-3">
								<label for="account_no">Account Number <em>*</em><span id="account_validation_message"></span></label>
								<input type="text" id="account_no" name="account_no" class="form-control" value="<?php echo $account_no; ?>" placeholder="Account Number" oninput="validateAccountNo()" >
							</div>

							<div class="col-lg-3">
								<label for="short_code">Sort Code <em>*</em><span id="sort_code_validation_message"></span></label>
								<input type="text" id="short_code" name="short_code" class="form-control" value="<?php echo $short_code; ?>" placeholder="Sort Code" oninput="formatSortCode()" maxlength="8" >
							</div>

							<div class="col-lg-3">
								<label for="personal_ref_no">Personal Reference No. <em>*</em></label>
								<input type="text" id="personal_ref_no" name="personal_ref_no" class="form-control" value="<?php echo $personal_ref_no; ?>" placeholder="Personal Reference No." >
							</div>
							<div class="col-lg-3">
								<label for="bank_address_1">Bank Address 1 <em>*</em></label>
								<input type="text" id="bank_address_1" name="bank_address_1" class="form-control" value="<?php echo $bank_address_1; ?>" placeholder="Bank Address 1" >
							</div>

							<div class="col-lg-3">
								<label for="bank_address_2">Bank Address 2 <em>*</em></label>
								<input type="text" id="bank_address_2" name="bank_address_2" class="form-control" value="<?php echo $bank_address_2; ?>" placeholder="Bank Address 2" >
							</div>

							<div class="col-lg-3">
								<label for="bank_address_3">Bank Address 3 <em>*</em></label>
								<input type="text" id="bank_address_3" name="bank_address_3" class="form-control" value="<?php echo $bank_address_3; ?>" placeholder="Bank Address 3" >
							</div>

							<div class="col-lg-3">
								<label for="bank_post_code">Post Code <em>*</em></label>
								<input type="text" id="bank_post_code" name="bank_post_code" class="form-control" value="<?php echo $bank_post_code; ?>" placeholder="Bank Post Code" oninput="this.value = this.value.toUpperCase();" >
							</div>

						</div>
					</div>
					<div class="form-section mt-2">
						<h4 style="margin-top: 0; margin-bottom: 0;color:#9c4893;">Right to Work Details</h4>
						<hr style="background-color: #9c4893; height:1px; margin-top: 0; margin-bottom: 1;">

						<div class="row">
							<div class="col-lg-3">
								<label for="right_to_work">Right to Work</label><br>
								<input type="checkbox" id="right_to_work" name="right_to_work"
									<?php echo (isset($right_to_work) && $right_to_work == 1) ? "checked" : ""; ?>>
							</div>

							<div class="col-lg-3">
								<label for="right_to_work_expiry">Right to Work Expiry</label>
								<input type="text" class="form-control datepicker" id="right_to_work_expiry" name="right_to_work_expiry"
									value="<?php echo isset($right_to_work_expiry) ? utility::getFormatedDate($right_to_work_expiry) : ''; ?>"
									placeholder="DD-MM-YYYY" autocomplete="off">
							</div>

							<div class="col-lg-3">
								<label for="documents_reviewed">Documents Reviewed</label><br>
								<input type="checkbox" id="documents_reviewed" name="documents_reviewed"
									<?php echo (isset($documents_reviewed) && $documents_reviewed == 1) ? "checked" : ""; ?>>
							</div>
						</div>
					</div>
					<div class="form-section mt-2">
						<h4 class="" style="margin-top: 0; margin-bottom: 0;color:#9c4893;"></h4>
						<hr style="background-color: #9c4893; height:1px; margin-top: 0; margin-bottom: 1;">
						<div class="row">
						 <div class="col-lg-6">
						  <? if($_SESSION['is_super_admin']){ ?>
							<div class="form-group">
							  <label for="Super admin" class="control-label">Super admin</label>
							  <div class="">
								<input class="flat-red" type="checkbox" id="is_super_admin" name="is_super_admin" <? echo ($is_super_admin) ? "checked" : ""; ?>>
							  </div>
							</div>
							<? } ?>
							<div class="form-group">
							  <label for="Active" class="control-label">Active</label>
							  <div class="">
								<input class="flat-red" type="checkbox" id="is_active" name="is_active" <? echo ($is_active) ? "checked" : ""; ?>>
							  </div>
							</div>
							
							</div>
						  <div class="col-lg-6">
					
						    <div class="form-group hide">
							  <label for="Signature" class="control-label">Profile Image</label>
							  <div class="">
								<input type="file" name="signature" id="signature" />
								<div style="float:left;margin:5px 0 5px 155px;">
								<?		
								$dir = "../signature/";
								if(isset($_REQUEST['action']) && $_REQUEST['action']=='edit'){
								 if (is_file($dir.$signature)) {					 
										$image_path ='<img  width="100" src="../'.$dir.$signature.'"><br><a id="delete_image" href="">Delete</a>';  					 	
									}else{
										$image_path ='<img width="100" src="../'.$dir.'no-image.gif">';	
									}
									echo $image_path;
								}?>
								<input type="hidden" name="hidden_signature" value="<? echo $signature;?>">
								</div>
							  </div>
                           </div>
						  </div>
						</div>
						</div>

					   </div><!-- /.box-body -->
                  <div class="card-footer">
					<input type="hidden" id="id" name="id" value="<?php echo $id;?>">
					<input type="hidden" id="from" name="from" value="<?php echo $from;?>">
					<input type="hidden" id="query_string" name="query_string" value="<?php echo $query_string;?>">
                    <button type="submit" id="submit" name="submit" value="<?php echo $button_value;?>" class="btn btn-primary"><?php echo $button_value;?>
						<i class="fas fa-sync-alt fa-spin" style="display:none;" id="page_load_div"></i>
					</button>
                  </div>
                </form>
              </div><!-- /.box -->
		</div>
	</div>
</section>
<script type="text/javascript">
	 function validateNINumber() {
		const niNumberInput = document.getElementById('ni_number').value;
		const niPattern = /^[A-Z]{2}[0-9]{6}[A-Z]$/;
		const validationMessage = document.getElementById('validation_message');
		
		if (niPattern.test(niNumberInput)) {
			validationMessage.textContent = "Valid NI number";
			validationMessage.style.color = "green";
		} else {
			validationMessage.textContent = "Invalid NI number formate(use AB123456C)";
			validationMessage.style.color = "red";
		}
	}
	
	 function validateAccountNo() {
        const accountNoInput = document.getElementById('account_no').value;
		const accountPattern = /^[0-9]{8}$/;
		const validationMessage = document.getElementById('account_validation_message');
		
		if (accountPattern.test(accountNoInput)) {
			validationMessage.textContent = "Valid Account Number";
			validationMessage.style.color = "green";
		} else {
			validationMessage.textContent = "Invalid Account Number format (use 12345678)";
			validationMessage.style.color = "red";
		}
	}

		function formatSortCode() {
			var input = document.getElementById('short_code');
			var value = input.value.replace(/[^0-9]/g, ''); 
			var formattedValue = '';

			if (value.length > 0) {
				formattedValue = value.slice(0, 2);
			}
			if (value.length > 2) {
				formattedValue += '-' + value.slice(2, 4);
			}
			if (value.length > 4) {
				formattedValue += '-' + value.slice(4, 6);
			}

			input.value = formattedValue;
		}

	$("#delete_image").click(function(){		
		if(confirm("Are you sure! You want to delete the picture?")){
			var new_url = window.location+'&delete_image=1';		
			$(location).attr('href', new_url);
			return false;
		}else{
			return false;
		}
   });
	
	$("#form1").on("blur","#uname",function(){	
		var uname = $(this).val();
		var id = $("#id").val();
		if(uname != ""){
		var form_data   = {'uname':uname,'id':id};       
			$.ajax({
				async: false,
				type: "POST",
				url: site_url+"/modules/adminuser/index.php?page=ajax_actions&action=validate_username",
				data: form_data,
				success: function(result){          
					var resultObj = jQuery.parseJSON(result); 
					if(resultObj.status == "failure"){
						//alert("This username already in system");
						$("#uname_validate").show();
						$("#uname_validate").html("This username already in system.");
						//$("#uname").val("");
						$("#uname").focus();
					}else{
						$("#uname_validate").html("");
						$("#uname_validate").hide();
					}
				}
			});
		}
	});
	$("#form1").on("blur","#email",function(){	
		var email = $(this).val();
		var id = $("#id").val();
		if(email != ""){
		var form_data   = {'email':email,'id':id};       
			$.ajax({
				async: false,
				type: "POST",
				url: site_url+"/adminuser/index?page=ajax_actions&action=validate_email",
				data: form_data,
				success: function(result){          
					var resultObj = jQuery.parseJSON(result); 
					if(resultObj.status == "failure"){
						//alert("This email already in system");
						$("#email_validate").show();
						$("#email_validate").html("This email already in system.");
						//$("#email").val("");
						$("#email").focus();
					}else{
						$("#email_validate").html("");
						$("#email_validate").hide();
					}
				}
			});
		}
	});
	$("#form1").on("blur","#user_reference",function(){	
    var user_reference = $(this).val();
    var id = $("#id").val();

    if(user_reference != ""){
        $.ajax({
            type: "POST",
            url: site_url+"/adminuser/index?page=ajax_actions&action=validate_user_reference",
            data: {user_reference: user_reference, id: id},
            success: function(result){          
                var resultObj = JSON.parse(result); 

                if(resultObj.status == "failure"){
                    $("#user_reference_validate").show().html("This User Reference already exists.");
                    $("#user_reference").focus();
                }else{
                    $("#user_reference_validate").hide().html("");
                }
            }
        });
    }
});
 $(document).ready(function() {
	  $("#form1").submit(function() {
	

   
		var is_error =false;
		$("#form1 .").each(function(index) {
			if($(this).val() == ''){
				var field_name = $(this).attr("id");
				alert("Please enter "+field_name);				
				$(this).focus();
				is_error = true;
				return false;		
			}				
		});
		if(is_error){
			return false;
		}
		var uname = $("#uname").val();
		var email = $("#email").val();
		if(uname.match(' ')){
			alert('Spaces are not allowed in username!');
			$('#uname').focus();
			return false;
		}else if(email.match(' ')){
			alert('Spaces are not allowed in email!');
			$('#email').focus();
			return false;
		}else{
			$("#submit").attr("disabled", true);
			$('#page_load_div').show();
			return true;
		}
	});	
});
</script>