<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"><?php echo (empty($ID))?'New ':'Edit '; ?> Petty Cash / Advance Request Form</h4>
							<h6 class="card-title"><i>*This application for cash advance must be made at least <b>three (3) working days prior</b> to the date of travelling</i></h6>
						</div>
						<div class="card-body">
							<div class="card no-shadow">
								<div class="card-body">
									<?php if(!empty($ID)){ ?>
										<form id="pettyCashAdvanceForm" class="form" action="<?php echo base_url('hr/Serviceform/petty_cash_advance_edit'); ?>" method="POST" onsubmit="return confirm('Confirm to update this form?')">
										<?php }else{ ?>	
											<form id="pettyCashAdvanceForm" class="form" action="<?php echo base_url('hr/Serviceform/petty_cash_advance_save') ?>" method="POST" onsubmit="return confirm('Confirm to submit this form?')" enctype="multipart/form-data">
											<?php }?>

											<input type="text" class="form-control" name="pettyCashAdvanceID" id="pettyCashAdvanceID" value="<?php echo $ID; ?>" hidden>
											<div class="form-row">
												<div class="col-12 col-sm-6">
													<div class="form-group">
														<label>Request Type</label>
														<select class="form-control" name="request_type" id="request_type" required onchange="detail_description();">
															<option value="-1" selected disabled>-- Please Select --</option>
															<option value="0" <?php echo ($ID && $petty_advance_list['request_type'] == 0)?'selected':''; ?>>Petty Cash</option>
															<option value="1" <?php echo ($ID && $petty_advance_list['request_type'] == 1)?'selected':''; ?>>Cash Advance</option>
															<option value="2" <?php echo ($ID && $petty_advance_list['request_type'] == 2)?'selected':''; ?>>Reimbursement</option>
															<?php if($_SESSION['module_data']['role'] == 'admin' || $_SESSION['user']['division_id'] == 5){ ?>
																<option value="3" <?php echo ($ID && $petty_advance_list['request_type'] == 3)?'selected':''; ?>>Perks</option>
															<?php } ?>
														</select>
													</div>	
												</div>

												<div class="col-12 col-sm-6">
													<div class="form-group">
														<label>Business Entity</label>
														<select class="form-control" name="entity_id" id="entity_id" required>
															<option value="-1" selected>-- Please Select --</option>
															<?php if($business_entity) foreach($business_entity as $row){ ?>
																<option value="<?php echo $row['id'] ?>" <?php echo ($ID)?(($petty_advance_list['business_entity_id'] == $row['id'])?'selected':''):(($_SESSION['user']['business_entity_id'] == $row['id'])?'selected':'') ?>>
																	<?php echo $row['name']; ?>
																</option>
															<?php } ?>
														</select>
													</div>
												</div>

												<div class="col-12 col-sm-6">
													<div class="form-group">
														<label>MGS Job ID</label>
														<select class="form-control select" name="job_id" id="job_id" onchange="change_client_input()">
															<option value="-1" selected>-- Please Select --</option>
															<?php if($mgs_job) foreach($mgs_job as $row){ ?>
																<option value="<?php echo $row['job_id'] ?>" <?php echo ($ID)?(($petty_advance_list['job_id'] == $row['job_id'])?'selected':''):'' ?>>
																	<?php echo '[JOB ID: '.str_pad($row['job_id'],5,0,STR_PAD_LEFT).'] '.$row['customerName'].' - '.$row['job_description']; ?>
																</option>
															<?php } ?>
														</select>
													</div>
												</div>

												<div class="col-12 col-sm-6">
													<div class="form-group">
														<label>Client</label>
														<select class="form-control select" name="client_id" id="client_id" required>
															<option value="-1" selected>-- Please Select --</option>
															<?php if($client) foreach($client as $row){ ?>
																<option value="<?php echo $row->id ?>" <?php echo ($ID)?(($petty_advance_list['client_id'] == $row->id)?'selected':''):'' ?>>
																	<?php echo $row->name; ?>
																</option>
															<?php } ?>
														</select>
													</div>
												</div>

												<div class="col-12 col-sm-6">
													<div class="form-group">
														<label>Travel Date From</label>
														<input type="text" class="form-control datepicker" name="date_from" id="date_from" value="<?php echo ($ID)?date('d-m-Y',strtotime($petty_advance_list['travel_date_from'])):'' ?>" required onchange="getNumWorkDays()">
													</div>
												</div>

												<div class="col-12 col-sm-6">
													<div class="form-group">
														<label>Travel Date To</label>
														<input type="text" class="form-control datepicker" name="date_to" id="date_to" value="<?php echo ($ID)?date('d-m-Y',strtotime($petty_advance_list['travel_date_to'])):'' ?>" required>
													</div>
												</div>
												
												<div class="col-12">
													<div class="form-group">
														<label>Member's Name [position][department]</label>
														<select class="form-control select" multiple name="member_id[]" id="member_id" required>
															<option value="-1" disabled>-- Please Select --</option>
															<?php if($listName) foreach($listName as $key => $gID){ 
																$dept_sName =  ($gID['department_name'])?' ['.$gID['department_name'].'] ':' [N/A]';
															?>
																<option value="<?php echo $gID['id']; ?>"><?php echo $gID['name'].$dept_sName; ?></option>
															<?php } ?>
														</select>
													</div>
												</div>

												<div class="col-6 d-none" id="picture_upload">
													<div class="form-group">
														<label>Attachement</label>
														<input type="file" class="form-control" name="picture_upload" id="attachment" disabled>
													</div>
												</div>
												<?php if($ID && !empty($petty_advance_list['photoID'])){ ?>	
													<div class="col-6" id="div_current_Attachment">
														<div class="form-group">
															<label>Uploaded Attachement</label>
															<input type="text" class="form-control" name="current_attachment" id="current_attachment" value="<?php echo $petty_advance_list['photo_title']; ?>" readonly>
														</div>
													</div>
												<?php } ?>
											</div>

											<div class="form-row py-3">
												<div class="col-12">
													<div class="form-group">
														<!-- <h5><b>Description</b></h5> -->
														<!-- <hr> -->
														<input type="text" class="form-control" id="min_id" hidden>
														<input type="text" class="form-control" id="max_id" hidden>
														<div class="form-row">
															<div class="col-10">
																<div class="form-group text-center"><b>DESCRIPTION</b></div>
															</div>
															<div class="col-2">
																<div class="form-group text-center"><b>AMOUNT (RM)</b></div>
															</div>
														</div>

														<hr style="margin-top: 1px; margin-bottom: 2rem;">

														<div class="form-row" id="description_row"></div>

														<hr style="margin-top: 1px; border: 1px solid;">
														<div class="form-row">
															<div class="col-9">
																<div class="form-group text-center">
																	<h6 style="margin-top: 10px;">TOTAL (RM)</h6>
																</div>
															</div>
															<div class="col-3">
																<div class="form-group">
																	<input type="text" class="text-right form-control" name="total_petty_cash_advance" id="total_petty_cash_advance" readonly required value="<?php echo ($ID)?number_format($petty_advance_list['total'],2):number_format(0,2); ?>" style="background: transparent; cursor: not-allowed;">
																</div>
															</div>
														</div>
														<hr style="margin-top: 1px; margin-bottom: 2rem; border: 1px solid;">
													</div>
												</div>
											</div>

											<div class="form-row">
												<div class="col-12">
													<div class="form-group">
														<h5><b>Prepared By</b></h5>
														<hr>
														<div class="form-row">
															<div class="col-12 col-sm-6">
																<div class="form-group">
																	<label>Name</label>
																	<input type="text" class="form-control" name="request_name" id="request_name" value="<?php echo ($ID)?$petty_advance_list['requester_name']:$_SESSION['user']['name']; ?>" readonly>	
																	<input type="text" class="form-control" name="request_name_id" id="request_name_id" value="<?php echo ($ID)?$petty_advance_list['requester_id']:$_SESSION['user']['id']; ?>" hidden>
																</div>
															</div>

															<div class="col-12 col-sm-6">
																<div class="form-group">
																	<label for="email">Email Address</label>
																	<input type="text" class="form-control" name="request_email" id="request_email" value="<?php echo ($ID)?$petty_advance_list['requester_email']:$_SESSION['user']['email']; ?>" readonly>
																</div>
															</div>

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

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

											<div class="form-row">
												<div class="col-12">
													<p>
														*All attachment for this cash advance should be submitted within <b>seven (7) working days</b> after the end of your travel date filled above<br>
														*In the event of surplus on cash advance, the amount of surplus shall be return to company<br>
														*In the event of advance given is insufficient, company will reimburse the differences subject to available attachment<br>
														*This application shall be approved by the head of department (HOD)							
													</p> 
												</div>
											</div>

											<!-- <div class="form-row py-2" <?php //echo (isset($mode) && ($mode == 'view'))?'style="display: none;"':''; ?>>
												<div class="col-12">
													<div class="float-right">
														<a class="btn btn-light" href="<?php //echo base_url('hr/serviceform/petty_cash_advance_list') ?>">Cancel</a>
														<button type="submit" id="submitBtn" class="btn btn-primary"><?php //echo ($ID)?'Update':'Submit'; ?></button>
													</div> 
												</div>
											</div> -->
											<div class="form-row" <?php echo (isset($mode) && ($mode == 'view'))?'style="display: none;"':''; ?>>
												<div class="col-12" style="padding-top: 2rem;">
													<div class="float-left <?php echo ($ID)?'d-none':''; ?>">
														<table>
															<tbody>
																<tr>
																	<td class="text-center">
																		<label class="switch" data-trigger="hover" data-tooltip="tooltip" title="" data-original-title="Send me a copy of this form">
																			<input type="checkbox" name="send_email_checkbox">
																			<span class="slider round"></span>
																		</label>
																	</td>
																	<td class="p-3 py-1"><b><h6 style="">Send me a copy of this form</h6></b></td>
																</tr>
															</tbody>
														</table>
													</div> 
													<div class="float-right">
														<a class="btn btn-light" href="<?php echo base_url('hr/serviceform/petty_cash_advance_list/'); ?>">Cancel</a>
														<button type="submit" id="submitBtn" class="btn btn-primary"><?php echo ($ID)?'Update':'Submit'; ?></button>
													</div> 
												</div>
											</div>
										</form>
									</form>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
</section>

<!-- manager approval -->
<?php if($_SESSION['user']['grade'] >= 31){ ?>
	<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">Approved By</h4>
							</div>
							<div class="card-body">
								<div class="card no-shadow">
									<div class="card-body">
										<form id="pettyCashAdvanceApprovedForm" class="form" action="<?php echo base_url('hr/Serviceform/petty_cash_advance_approved/manager') ?>" method="POST">
											<input type="text" class="form-control" name="pettyCashAdvanceID" id="pettyCashAdvanceID" value="<?php echo $ID; ?>" hidden>
											<div class="form-row py-3">
												<div class="col-12">
													<div class="form-group">
														<div class="form-row">
															<div class="col-12 col-sm-6">
																<div class="form-group">
																	<label>Name</label>
																	<input type="text" class="form-control" name="approve_name" id="approve_name" value="<?php echo ($ID)?(($petty_advance_list['approval_status'] > 0)?$petty_advance_list['approved_by_name']:$_SESSION['user']['name']):''; ?>" readonly>	
																	<input type="text" class="form-control" name="approve_name_id" id="approve_name_id" value="<?php echo ($ID)?(($petty_advance_list['approval_status'] > 0)?$petty_advance_list['approved_by_staff_id']:$_SESSION['user']['id']):''; ?>" hidden>
																</div>
															</div>

															<div class="col-12 col-sm-6">
																<div class="form-group">
																	<label for="email">Email Address</label>
																	<input type="text" class="form-control" name="approve_email" id="approve_email" value="<?php echo ($ID)?(($petty_advance_list['approval_status'] > 0)?$petty_advance_list['approved_by_email']:$_SESSION['user']['email']):''; ?>" readonly>
																</div>
															</div>

															<div class="col-12 col-sm-6">
																<div class="form-group">
																	<label for="dateapprv">Date Approve</label>
																	<input type="text" class="form-control" name="dateapprv" id="dateapprv" value="<?php echo ($ID)?(($petty_advance_list['approval_status'] > 0)?date("d-m-Y h:iA",strtotime($petty_advance_list['approvedTS'])):date("d-m-Y h:iA")):''; ?>" readonly>
																</div>
															</div>
														</div>
													</div>
												</div>
											</div>
											<?php if($petty_advance_list['approval_status'] == 0){ ?>
												<div class="col-12" <?php echo ($ID && $petty_advance_list['prepared_status'] > 0)?'style="display: none;"':''; ?>>
													<div class="float-right">
														<button type="submit" class="btn btn-danger" name="approveBtn" id="rejectBtn" value="2">Reject</button>
														<button type="submit" class="btn btn-success" name="approveBtn" id="approveBtn" value="1">Approved</button>
													</div> 
												</div>
											<?php } ?>
										</form>									
									</div>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
	</section>
<?php } ?>

<!-- for finance use -->
<?php if($ID && $petty_advance_list['approval_status'] > 0 && $finance_division){ ?>
	<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">For Finance Division Use Only</h4>
							</div>
							<div class="card-body">
								<div class="card no-shadow">
									<div class="card-body">
										<form id="pettyCashAdvanceApprovedForm" class="form" action="<?php echo base_url('hr/Serviceform/petty_cash_advance_approved/finance') ?>" method="POST">
											<input type="text" class="form-control" name="pettyCashAdvanceID" id="pettyCashAdvanceID" value="<?php echo $ID; ?>" hidden>
											<div class="form-row py-3">
												<div class="col-12">
													<div class="form-group">
														<div class="form-row">
															<div class="col-12 col-sm-6">
																<div class="form-group">
																	<label>Name</label>
																	<input type="text" class="form-control" name="finance_name" id="finance_name" value="<?php echo ($ID)?(($petty_advance_list['prepared_status'] > 0)?$petty_advance_list['prepared_by_name']:$_SESSION['user']['name']):''; ?>" readonly>	
																	<input type="text" class="form-control" name="finance_name_id" id="finance_name_id" value="<?php echo ($ID)?(($petty_advance_list['prepared_status'] > 0)?$petty_advance_list['prepared_by_staff_id']:$_SESSION['user']['id']):''; ?>" hidden>
																</div>
															</div>

															<div class="col-12 col-sm-6">
																<div class="form-group">
																	<label for="email">Email Address</label>
																	<input type="text" class="form-control" name="finance_email" id="finance_email" value="<?php echo ($ID)?(($petty_advance_list['prepared_status'] > 0)?$petty_advance_list['prepared_by_email']:$_SESSION['user']['email']):''; ?>" readonly>
																</div>
															</div>

															<div class="col-12 col-sm-6">
																<div class="form-group">
																	<label for="finance_date">Date of Transfer</label>
																	<input type="text" class="form-control" name="finance_date" id="finance_date" value="<?php echo ($ID)?(($petty_advance_list['prepared_status'] > 0)?date("d-m-Y h:iA",strtotime($petty_advance_list['preparedTS'])):date("d-m-Y h:iA")):''; ?>" readonly>
																</div>
															</div>

															<div class="col-12 col-sm-6">
																<div class="form-group">
																	<label for="advice_no">Remittance Advice No</label>
																	<input type="text" class="form-control" name="advice_no" id="advice_no" value="<?php echo ($ID)?(($petty_advance_list['prepared_status'] > 0)?$petty_advance_list['advice_no']:''):''; ?>">
																</div>
															</div>

															<div class="col-12 col-sm-6">
																<div class="form-group">
																	<label for="finance_total_approved">Amount Approved</label>
																	<input type="text" class="form-control" name="finance_total_approved" id="finance_total_approved" value="<?php echo ($ID)?(($petty_advance_list['total_approved'] > 0)?number_format($petty_advance_list['total_approved'],2):''):''; ?>" required onchange="check_max_amount(<?php echo ($ID)?$petty_advance_list['total']:0; ?>)">
																</div>
															</div>
														</div>
													</div>
												</div>
											</div>
											<div class="col-12">
												<div class="float-right">
													<button type="submit" class="btn btn-danger" name="financeCompleteBtn" id="financeDuplicateBtn" value="2">Duplicate</button>
													<button type="submit" class="btn btn-success" name="financeCompleteBtn" id="financeCompleteBtn" value="1">Complete</button>
												</div> 
											</div>
										</form>
									</div>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
	</section>
<?php } ?>

<script>
	$(document).ready(function()
	{	
		if('<?php echo $ID; ?>' > 0)
		{
			// change_input('<?php //echo $petty_advance_list['request_type']; ?>');
			detail_description();
			// update_description_value();
			update_member();
		}
		
	});

	function change_client_input(jobID = null)
	{
		if(jobID > 0)
		{
			var job_id 		= jobID;
		}else{
			var job_id 		= $('#job_id').val();
		}

		if(job_id > 0)
		{
			$('#job_id').select2();
			$('#job_id').val(job_id);
			$('#job_id').select2({
				theme: 'bootstrap4',
			});

			$('#client_id').select2();
			$('#client_id').select2('destroy');

			$.ajax({
				url:'<?php echo base_url('hr/serviceform/petty_cash_advance_get_client') ?>',
				data: {
					job_id 		: job_id,
				},
				type: 'post',
				dataType: 'json',
				success: (data) => {
					if(data.status == "success") {
						var job_detail 	= data.job_detail;
						$('#client_id').val(job_detail[0]['customerID']);
						$('#client_id').attr('readonly','readonly');
						$('#client_id').css('pointer-events','none');
					}else{

					}
              	}
           	});

		}else{
			$('#client_id').val('-1');
			$('#client_id').select2({
				theme: 'bootstrap4',
			});
			$('#client_id').css('pointer-events','');
		}
	}

	function change_desc_input(description_id)
	{
		var description_checkbox = $('#desc_check_'+description_id).is(':checked');

		if(description_checkbox == true)
		{
			$('#desc_amount__'+description_id).removeAttr('disabled');
			$('#desc_amount__'+description_id).attr('required','required');
			$('#desc_detail__'+description_id).removeAttr('disabled');
			$('#desc_detail__'+description_id).attr('required','required');		
		}else{
			$('#desc_amount__'+description_id).removeAttr('required');
			$('#desc_amount__'+description_id).attr('disabled','disabled');
			$('#desc_detail__'+description_id).removeAttr('required');
			$('#desc_detail__'+description_id).attr('disabled','disabled');

			$('#desc_detail__'+description_id).val('');
			$('#desc_amount__'+description_id).val(0);

			update_total(description_id);
		}
	}

	// function change_input(request_type)
	function change_input()
	{
		var request_type = $('#request_type').val();
		<?php foreach($description as $key => $row){ ?>
			if(request_type == '<?php echo $row['type']; ?>')
			{
				$('#desc_check_'+'<?php echo $row['ID']; ?>').removeAttr('disabled');
				if($('#desc_check_'+'<?php echo $row['ID']; ?>').is(':checked'))
				{
					$('#desc_check_'+'<?php echo $row['ID']; ?>').prop('checked',false);
					$('#desc_detail__'+'<?php echo $row['ID']; ?>').attr('disabled','disabled');
					$('#desc_amount__'+'<?php echo $row['ID']; ?>').attr('disabled','disabled');
					$('#desc_detail__'+'<?php echo $row['ID']; ?>').val('');
					$('#desc_amount__'+'<?php echo $row['ID']; ?>').val((0).toFixed(2));					
				}
			}else{
				$('#desc_check_'+'<?php echo $row['ID']; ?>').attr('disabled','disabled');
				if($('#desc_check_'+'<?php echo $row['ID']; ?>').is(':checked'))
				{
					$('#desc_check_'+'<?php echo $row['ID']; ?>').prop('checked',false);
					$('#desc_detail__'+'<?php echo $row['ID']; ?>').attr('disabled','disabled');
					$('#desc_amount__'+'<?php echo $row['ID']; ?>').attr('disabled','disabled');
					$('#desc_detail__'+'<?php echo $row['ID']; ?>').val('');
					$('#desc_amount__'+'<?php echo $row['ID']; ?>').val((0).toFixed(2));					
				}
			}

		<?php } ?>
		// update_total();

		if(request_type == 0){
			$('#entity_id').removeAttr('required');
			$('#client_id').removeAttr('required');
			$('#date_from').removeAttr('required');
			$('#date_to').removeAttr('required');
			$('#member_id').removeAttr('required');
			$('#attachment').attr('disabled','disabled');
			$('#attachment').removeAttr('required');
		}else if(request_type == 2){
			$('#entity_id').removeAttr('required');
			$('#client_id').removeAttr('required');
			$('#date_from').removeAttr('required');
			$('#date_to').removeAttr('required');
			$('#member_id').removeAttr('required');
			$('#attachment').removeAttr('disabled');
			$('#attachment').attr('required','required');
		}else if(request_type == 3){
			$('#entity_id').removeAttr('required');
			$('#client_id').removeAttr('required');
			$('#date_from').removeAttr('required');
			$('#date_to').removeAttr('required');
			$('#member_id').removeAttr('required');
			$('#attachment').attr('disabled','disabled');
			$('#attachment').removeAttr('required');
		}else{
			$('#entity_id').attr('required','required');
			$('#client_id').attr('required','required');
			$('#date_from').attr('required','required');
			$('#date_to').attr('required','required');
			$('#member_id').attr('required','required');
			$('#attachment').attr('disabled','disabled');
			$('#attachment').removeAttr('required');
		}
	}

	function update_total(description_id = null)
	{
		var description_first_id = $('#min_id').val();
		var description_last_id = $('#max_id').val();
		var total = 0;

		if(description_id > 0)
		{
			var description_amount = $('#desc_amount__'+description_id).val().replace(",","");

			if(description_amount != '' && description_amount != null && description_amount > 0)
			{
				var amount = parseFloat(description_amount);
			}else{
				var amount = 0.00;
			}

			$('#desc_amount__'+description_id).val(parseFloat(amount).toFixed(2).replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,"));
			
		}

		for(var i = parseInt(description_first_id); i <= parseInt(description_last_id); i++)
		{
			var desc_amount = $('#desc_amount__'+i).val().replace(",","");

			if(desc_amount != '' && desc_amount != null && desc_amount > 0)
			{
				total += parseFloat(desc_amount);
			}else{
				total += 0;
			}
		}

		$('#total_petty_cash_advance').val(parseFloat(total).toFixed(2).replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,"));
	}

	function update_description_value()
	{
		var desc_id = ('<?php echo $petty_advance_list['description_id']; ?>').split('->');
		var desc_remark = ('<?php echo $petty_advance_list['description_remark']; ?>').split('->');
		var desc_amount = ('<?php echo $petty_advance_list['description_amount']; ?>').split('->');

		$.each(desc_id,function(key, row){
			$('#desc_check_'+row).prop('checked', true);
			$('#desc_check_'+row).prop('disabled',false);
			$('#desc_detail__'+row).prop('disabled',false);
			$('#desc_amount__'+row).prop('disabled',false);
			$('#desc_amount__'+row).prop('required',true);
			$('#desc_detail__'+row).val(desc_remark[key]);
			$('#desc_amount__'+row).val(parseFloat(desc_amount[key]).toFixed(2));
		});

		update_total();
	}

	function getNumWorkDays() 
	{
		var request_type 	= $('#request_type').val();
		var dateFrom 		= moment($('#date_from').val(), "DD-MM-YYYY");
        var today 			= moment().startOf('day');
        var diff 			= dateFrom.diff(today, 'days');
		
		if('<?php echo $mode; ?>' != 'view')
		{
			if(request_type == 1){ 
			    if(diff < 3)
			    {
			    	iziToast.warning({
			            position: 'bottomRight',
			            message: 'Application for cash advance must be made at <b>least three (3) working days</b> prior to the date of travelling',
			        });

					$('#submitBtn').removeAttr("type").attr("type", "button");
					$('#submitBtn').prop('disabled',true);
					$('#submitBtn').css('cursor','not-allowed');
			    }else{
			    	iziToast.success({
			            position: 'bottomRight',
			            message: 'Success',
			        });

					$("#date_to").datepicker("destroy");

				    $( "#date_to").datepicker({
			            uiLibrary: 'bootstrap4',
			            format: 'dd-mm-yyyy',
			            autoclose: true,
			            changeMonth: true, 
			            changeYear: false,
			            startDate :dateFrom.format('DD-MM-YYYY'),
			        });

					$('#submitBtn').removeAttr("type").attr("type", "submit");
					$('#submitBtn').prop('disabled',false);
					$('#submitBtn').css('cursor','pointer');
			    }
			}else{
				$('#submitBtn').removeAttr("type").attr("type", "submit");
				$('#submitBtn').prop('disabled',false);
				$('#submitBtn').css('cursor','pointer');
			}
		}
	}

	function update_member()
	{
		var petty_advance_list 	= '<?php echo $petty_advance_list['member_id']; ?>';

		// if(petty_advance_list.member_id != '' && petty_advance_list.member_id != null && petty_advance_list.member_id != undefined)
		if(petty_advance_list != '' && petty_advance_list != null && petty_advance_list != undefined)
		{
			var member_id_split = (petty_advance_list).split("->");
			$('#member_id').select2("val",[member_id_split]);
		}
	}

	function detail_description()
	{
		var request_type = $('#request_type').val();

		if(request_type == 2)
		{
			$('#picture_upload').removeClass('d-none');
		}else{
			$('#picture_upload').addClass('d-none');
		}

		$('#description_row').html('');

		$.ajax({
			url:'<?php echo base_url('hr/serviceform/petty_cash_advance_get_description') ?>',
			data: {
				type 		: request_type,
			},
			type: 'post',
			dataType: 'json',
			success: (data) => {
				if(data.status == "Success") {
					var description	= data.description;
					
					$.each(description,function(key,row){
						var temp_input = `<div class="col-1 text-center">
							<div class="form-group">
								<input type="checkbox" name="desc_check_${row.ID}" onclick="change_desc_input(${row.ID})" id="desc_check_${row.ID}" disabled>
							</div>
						</div>
						<div class="col-3">
							<div class="form-group">
								${row.description}
							</div>
						</div>
						<div class="col-5">
							<div class="form-group">
								<input type="text" class="form-control" name="desc_detail__${row.ID}" id="desc_detail__${row.ID}" disabled>
							</div>
						</div>
						<div class="col-3">
							<div class="form-group">
								<input type="text" class="text-right form-control" name="desc_amount__${row.ID}" id="desc_amount__${row.ID}" value="<?php echo number_format(0,2); ?>" onchange="update_total(${row.ID})" disabled>
							</div>
						</div>`;

						$('#description_row').append(temp_input);
					});

					// update_total();
					// change_input();
					// update_description_value();

					$('#min_id').val(data.min_id);
					$('#max_id').val(data.max_id);

					change_input();
					<?php if($ID){ ?>
						update_description_value();
					<?php } ?>
				}
          	}
       	});
	}

	function check_max_amount(total_request)
	{
		var total_request = parseFloat(total_request);
		var total_approved = parseFloat($('#finance_total_approved').val());

		$('#finance_total_approved').val(total_approved.toFixed(2).toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,"));

		if(total_approved > total_request)
		{
			iziToast.warning({
	            position: 'bottomRight',
	            message: 'Approved amount cannot exceed requested amount.',
	        });

	        $('#financeDuplicateBtn').removeAttr("type").attr("type", "button");
			$('#financeDuplicateBtn').prop('disabled',true);
			$('#financeDuplicateBtn').css('cursor','not-allowed');

			$('#financeCompleteBtn').removeAttr("type").attr("type", "button");
			$('#financeCompleteBtn').prop('disabled',true);
			$('#financeCompleteBtn').css('cursor','not-allowed');
		}else{
			$('#financeDuplicateBtn').removeAttr("type").attr("type", "submit");
			$('#financeDuplicateBtn').prop('disabled',false);
			$('#financeDuplicateBtn').css('cursor','pointer');

			$('#financeCompleteBtn').removeAttr("type").attr("type", "submit");
			$('#financeCompleteBtn').prop('disabled',false);
			$('#financeCompleteBtn').css('cursor','pointer');

		}
	}
</script>