<section class="container" style="padding-bottom: 2rem;">
	<div class="row">
		<div class="col-sm-12">
			<div class="tab-content" id="tabContent">
				<div class="tab-pane fade show active" id="profile" role="tabpanel" aria-labelledby="profile-tab">
					<div class="card bg-light-transparent">
						<div class="card-header bg-light">
							<h4 class="card-title float-left"><?php echo (empty($ID))?'New ':'Edit '; ?>Talent Referral Program Form</h4>
						</div>

						<br>

						<div class="card-header bg-light">
							<h4 class="card-title">Referral Guidelines</h4>
							<hr>
							1. Employees are advised to read through the <a href="<?php echo base_url(); ?>" target="_blank">Talent Referral Programme Guideline</a> first before filing in and submitting this form to People and Culture (PC).<br>
							2. PC will send you this Form once the candidate is confirmed employed.<br>
							3. You are required to submit this form to PC within 5 working days after PC has sent you this form. PC will keep you informed on the necessary process from time to time.<br>
						</div>

						<div class="card-body">
							<div class="card no-shadow">
								<div class="card-body">
									<form id="talentReferralForm" class="form" action="<?php echo ($ID)?base_url('hr/Serviceform/talent_referral_edit'):base_url('hr/Serviceform/talent_referral_save'); ?>" method="POST" onsubmit="return confirm('Do you really want to submit the form?')">
										<input type="text" class="form-control" name="talentReferralID" id="talentReferralID" value="<?php echo $ID; ?>" hidden>

										<h4 class="card-title">Employee's Information</h4>
										<hr>
										<div class="form-row">
											<div class="col-12 col-sm-6">
												<div class="form-group">
													<label>Employee's Name</label>
													<input type="text" class="form-control" name="reqName" id="reqName" value="<?php echo ($ID)?$talentReferralDetail['created_name']:$_SESSION['user']['name']; ?>" readonly>
													<input type="text" class="form-control" name="reqName_id" id="reqName_id" value="<?php echo ($ID)?$talentReferralDetail['createdBY']:$_SESSION['user']['id']; ?>" hidden>
												</div>
											</div>

											<div class="col-12 col-sm-6">
												<div class="form-group">
													<label>Date</label>
													<input type="text" class="form-control" name="reqDate" id="reqDate" value="<?php echo (!empty($ID))?date('d-m-Y',strtotime($talentReferralDetail['createdTS'])):date('d-m-Y'); ?>" readonly>
												</div>
											</div>

											<div class="col-12 col-sm-6">
												<div class="form-group">
													<label>E-mail Address</label>
													<input type="" class="form-control" name="email" id="email" value="<?php echo (!empty($ID))?$talentReferralDetail['created_email']:$_SESSION['user']['email']; ?>" readonly>
												</div>
											</div>

											<div class="col-12 col-sm-6">
												<div class="form-group">
													<label>Department</label>
													<input type="text" class="form-control" name="department" id="department" value="<?php echo ($ID)?$talentReferralDetail['created_dept_name'].' ['.$talentReferralDetail['created_branch_name'].']':$_SESSION['user']['department_name'].' ['.$_SESSION['user']['branch_name'].']'; ?>" readonly>
													<input type="text" class="form-control" name="department_id" id="department_id" value="<?php echo ($ID)?$talentReferralDetail['created_department_id']:$_SESSION['user']['department_id']; ?>" hidden>
												</div>
											</div>

											<div class="col-12">
												<div class="form-group">
													<label>Business Entity</label>
													<input type="text" class="form-control" name="department" id="department" value="<?php echo ($ID)?$talentReferralDetail['created_entity']:$business_entity['name']; ?>" readonly>
													<input type="text" class="form-control" name="department_id" id="department_id" value="<?php echo ($ID)?$talentReferralDetail['entity_id']:$_SESSION['user']['business_entity_id']; ?>" hidden>
												</div>
											</div>
										</div>

										<br>

										<h4 class="card-title">Candidate's Information</h4>
										<hr>
										<div class="form-row">
											<div class="col-12 col-sm-6">
												<div class="form-group">
													<label>Candidate's Name</label>
													<input type="text" class="form-control" name="candidate_name" id="candidate_name" value="<?php echo ($ID)?$talentReferralDetail['candidate_name']:''; ?>" hidden>
													<select class="form-control select" name="candidate_id" id="candidate_id" required onchange="change_candidate_input()">
														<option value="-1" selected disabled>-- Please Select --</option>
														<?php if($candidate_list) foreach($candidate_list as $candidate){ ?>
															<option value="<?php echo $candidate['id']; ?>" <?php echo ($ID)?(($talentReferralDetail['candidate_user_id'] == $candidate['id'])?'selected':''):''; ?>><?php echo $candidate['name'].' - '.$candidate['designation']; ?></option>
														<?php } ?>
													</select>
												</div>
											</div>

											<div class="col-12 col-sm-6">
												<div class="form-group">
													<label>E-mail Address</label>
													<input type="" class="form-control" name="candidate_email" id="candidate_email" value="<?php echo (!empty($ID))?$talentReferralDetail['candidate_email']:''; ?>" required readonly>
												</div>
											</div>

											<div class="col-12 col-sm-6">
												<div class="form-group">
													<label>Telephone Number</label>
													<input type="text" class="form-control" name="candidate_contact" id="candidate_contact" value="<?php echo ($ID)?$talentReferralDetail['candidate_contact']:''; ?>" required readonly>
												</div>
											</div>

											<div class="col-12 col-sm-6">
												<div class="form-group">
													<label>Position Applied</label>
													<input type="text" class="form-control" name="candidate_position" id="candidate_position" value="<?php echo ($ID)?$talentReferralDetail['candidate_position']:''; ?>" required readonly>
												</div>
											</div>

											<div class="col-12 col-sm-6">
												<div class="form-group">
													<label>Business Entity</label>
													<input type="text" class="form-control" name="candidate_entity" id="candidate_entity" value="<?php echo ($ID)?$talentReferralDetail['candidate_entity']:''; ?>" required readonly>
													<input type="text" class="form-control" name="candidate_entity_id" id="candidate_entity_id" value="<?php echo ($ID)?$talentReferralDetail['candidate_entity_id']:''; ?>" hidden>
												</div>
											</div>
										</div>

										<div class=" form-row float-right" <?php echo (isset($mode) && ($mode == 'view'))?'style="display: none;"':''; ?>>
											<a class="btn btn-light" href="<?php echo base_url('hr/serviceform/talent_referral_list/'); ?>">Cancel</a>
											<button type="submit" id="submitBtn" class="btn btn-primary text-right" onclick="change_btn_attr('','talentReferralForm','submitBtn')"><?php echo ($ID)?"Update":"Submit"; ?></button>
										</div>
									</form>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
</section>

<?php if($ID && ($_SESSION['module_data']['role'] == 'admin' || $_SESSION['user']['division_id'] == 5)){ ?>
	<section class="container" style="padding-bottom: 2rem;">
		<div class="row">
			<div class="col-sm-12">
				<div class="tab-content" id="tabContent">
					<div class="tab-pane fade show active" id="profile" role="tabpanel" aria-labelledby="profile-tab">
						<div class="card bg-light-transparent">
							<div class="card-header bg-light">
								<h4 class="card-title float-left">For People Centre Use Only</h4>
							</div>
							<div class="card-body">
								<div class="card no-shadow">
									<div class="card-body">
										<form id="talentReferralFormStatus" class="form" action="<?php echo base_url('hr/Serviceform/talent_referral_update_status'); ?>" method="POST" >
											<input type="text" class="form-control" name="talentReferral_ID" id="talentReferral_ID" value="<?php echo $ID; ?>" hidden>
											<div class="form-row">
												<div class="col-12 col-sm-6">
													<div class="form-group">
														<label>Date Received the Application</label>
														<input type="text" class="form-control datepicker" name="application_date" id="application_date" value="<?php echo ($ID && strtotime($talentReferralDetail['application_date']) > 0)?date('d-m-Y',strtotime($talentReferralDetail['application_date'])):''; ?>" required>
													</div>
												</div>

												<div class="col-12 col-sm-6">
													<div class="form-group">
														<label>Joining Date</label>
														<input type="text" class="form-control datepicker" name="joining_date" id="joining_date" value="<?php echo ($ID && strtotime($talentReferralDetail['joining_date']) > 0)?date('d-m-Y',strtotime($talentReferralDetail['joining_date'])):''; ?>" <?php echo ($talentReferralDetail['status'] >= 1)?'required':''; ?>>
													</div>
												</div>

												<div class="col-12 col-sm-6">
													<div class="form-group">
														<label>Processed By</label>
														<input type="text" class="form-control" name="processedBYName" id="processedBYName" value="<?php echo ($ID && $talentReferralDetail['processedBY'] > 0)?$talentReferralDetail['processedBY_name']:$_SESSION['user']['name']; ?>" readonly>
														<input type="text" class="form-control" name="processedBY" id="processedBY" value="<?php echo ($ID)?$talentReferralDetail['processedBY']:$_SESSION['user']['id']; ?>" hidden>
													</div>
												</div>

												<div class="col-12 col-sm-6">
													<div class="form-group">
														<label>Date</label>
														<input type="text" class="form-control datepicker" name="processedTS" id="processedTS" value="<?php echo ($ID && strtotime($talentReferralDetail['processedTS']) > 0)?date('d-m-Y',strtotime($talentReferralDetail['processedTS'])):date('d-m-Y'); ?>" readonly>
													</div>
												</div>

												<div class="col-12">
													<div class="form-group">
														<label>Remark(s)</label>
														<input type="text" class="form-control" name="remark" id="remark" value="<?php echo ($ID)?$talentReferralDetail['remark']:''; ?>" <?php echo ($talentReferralDetail['status'] >= 1)?'readonly':''; ?> required>
													</div>
												</div>
											</div>

											<?php if($talentReferralDetail['status'] >= 2){ ?>
												<div class="form-row">
													<div class="col-12 col-sm-6">
														<div class="form-group">
															<label>Verified By</label>
															<input type="text" class="form-control" name="verifiedBYName" id="verifiedBYName" value="<?php echo ($talentReferralDetail['verifiedBY'] > 0)?$talentReferralDetail['verifiedBY_name']:$_SESSION['user']['name']; ?>" readonly>
															<input type="text" class="form-control" name="verifiedBY" id="verifiedBY" value="<?php echo ($ID)?$talentReferralDetail['verifiedBY']:$_SESSION['user']['id']; ?>" hidden>
														</div>
													</div>

													<div class="col-12 col-sm-6">
														<div class="form-group">
															<label>Date</label>
															<input type="text" class="form-control datepicker" name="verifiedTS" id="verifiedTS" value="<?php echo (strtotime($talentReferralDetail['verifiedTS']) > 0)?date('d-m-Y',strtotime($talentReferralDetail['verifiedTS'])):date('d-m-Y'); ?>" readonly>
														</div>
													</div>
												</div>
												
												<div class="form-row">
													<div class="col-12 col-sm-3">
														<div class="form-group">
															<label>1st Payment Status</label>
															<div class="row form-contol">
																<div class="col-sm-4">
																	<input type="radio" name="payment_status_1" id="payment_status_1" value="1" <?php echo ($ID && $talentReferralDetail['payment_status_1'] == 1)?'checked':''; ?> <?php echo ($talentReferralDetail['status'] >= 2)?'readonly style="cursor: not-allowed"':''; ?> required> Paid
																</div>
																<div class="col-sm-5">
																	<input type="radio" name="payment_status_1" id="payment_status_1" value="0" <?php echo ($ID && $talentReferralDetail['payment_status_1'] == 2)?'checked':''; ?> <?php echo ($talentReferralDetail['status'] >= 2)?'readonly style="cursor: not-allowed"':''; ?> required checked> Unpaid
																</div>
															</div>
														</div>
													</div>

													<div class="col-12 col-sm-9">
														<div class="form-group">
															<label>1st Payment Remark</label>
															<input type="text" class="form-control" name="payment_remark_1" id="payment_remark_1" value="<?php echo ($ID && $talentReferralDetail['payment_remark_1'])?$talentReferralDetail['payment_remark_1']:''; ?>" <?php echo ($talentReferralDetail['status'] >= 2)?'readonly style="cursor: not-allowed"':'';?>>
														</div>
													</div>
												</div>

												<div class="form-row">
													<div class="col-12 col-sm-3">
														<div class="form-group">
															<label>2nd Payment Status</label>
															<div class="row form-contol">
																<div class="col-sm-4">
																	<input type="radio" name="payment_status_2" id="payment_status_2" value="1" <?php echo ($ID && $talentReferralDetail['payment_status_2'] == 1)?'checked':''; ?> required> Paid
																</div>
																<div class="col-sm-5">
																	<input type="radio" name="payment_status_2" id="payment_status_1" value="0" <?php echo ($ID && $talentReferralDetail['payment_status_2'] == 2)?'checked':''; ?> required checked> Unpaid
																</div>
															</div>
														</div>
													</div>

													<div class="col-12 col-sm-9">
														<div class="form-group">
															<label>2nd Payment Remark</label>
															<input type="text" class="form-control" name="payment_remark_2" id="payment_remark_2" value="<?php echo ($ID && $talentReferralDetail['payment_remark_2'])?$talentReferralDetail['payment_remark_2']:''; ?>">
														</div>
													</div>
												</div>
											<?php } ?>


											<div class=" form-row float-right">
												<?php if($talentReferralDetail['status'] != 4){ ?>	
													<button type="submit" name="statusBtn" value="3" class="changeStatusBtn btn btn-danger btn-sm" style="width: 200px;" onclick="change_btn_attr('changeStatusBtn','talentReferralFormStatus')">Reject</button>
												<?php }else{ ?>
													<a href="<?php echo base_url('hr/serviceform/talent_referral_list'); ?>" class="btn btn-secondary btn-sm" style="width: 200px;">Back</a>
												<?php } ?>

												<?php if($talentReferralDetail['status'] == 0){ ?>
													<button type="submit" name="statusBtn" value="1" class="changeStatusBtn btn btn-primary btn-sm" style="width: 200px;" onclick="change_btn_attr('changeStatusBtn','talentReferralFormStatus')">Process</button>
												<?php } elseif($talentReferralDetail['status'] == 1){ ?>
													<button type="submit" name="statusBtn" value="2" class="changeStatusBtn btn btn-primary btn-sm" style="width: 200px;" onclick="change_btn_attr('changeStatusBtn','talentReferralFormStatus')">Verified</button>
												<?php }elseif($talentReferralDetail['status'] == 2){ ?>
													<button type="submit" name="statusBtn" value="4" class="changeStatusBtn btn btn-success btn-sm" style="width: 200px;" onclick="change_btn_attr('changeStatusBtn','talentReferralFormStatus')">Completed</button>
												<?php } ?>
											</div>
										</form>
									</div>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
	</section>
<?php } ?>

<script type="text/javascript">
	function change_btn_attr(btn_class, form_id, btn_id)
	{
		if(btn_class)
		{
			$('.'+btn_class).append('<span class="fa fa-spinner fa-spin"></span>');
			$('.'+btn_class).attr({'type':'button','disabled':'disabled'});
		}

		if(btn_id)
		{
			$('#'+btn_id).append('<span class="fa fa-spinner fa-spin"></span>');
			$('#'+btn_id).attr({'type':'button','disabled':'disabled'});
		}

		$("#"+form_id).submit();
	}

	function change_candidate_input()
	{
		var candidate_id = $('#candidate_id').val();

		$.ajax({
			url:'<?php echo base_url('hr/serviceform/getCandidateDetail'); ?>',
			data: {
				candidate_id : candidate_id,
			},
			type: 'post',
			dataType: 'json',
			success: (data) => {
				if(data.status == "success") {
					console.log(data);
					$('#candidate_name').val(data.result.name);
					$('#candidate_email').val(data.result.user_email);
					
					if(data.result.user_phone != null && data.result.user_phone != "" && data.result.user_phone != "N/A") 
					{
						$('#candidate_contact').val(data.result.user_phone);
						$('#candidate_contact').attr({'readonly':'readonly'});
						$('#candidate_contact').removeAttr('required');
					}else{
						$('#candidate_contact').val();
						$('#candidate_contact').attr({'required':'required'});
						$('#candidate_contact').removeAttr('readonly');
					}

					$('#candidate_position').val(data.result.designation);
					$('#candidate_entity').val(data.result.entity_name);
					$('#candidate_entity_id').val(data.result.business_entity_id);

					localStorage.setItem("status", data.status);
					localStorage.setItem("msg", data.msg);
				}else{
					$('#candidate_name').val('');
					$('#candidate_email').val('');
					$('#candidate_contact').val('');
					$('#candidate_position').val('');
					$('#candidate_entity').val('');
					$('#candidate_entity_id').val('');

					localStorage.setItem("status", data.status);
					localStorage.setItem("msg", data.msg);
				}
			}
		});
	}
</script>