<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">Cash Advance Listing</h4>
							<button class="btn btn-sm btn-outline-dark float-right d-print-none" onclick="window:print()"><i class="fa fa-print"></i> Print</button>
						</div>
						<div class="card-body">
							<div class="card no-shadow">
								<div class="card-body">
									<form id="pettyCashAdvanceForm" class="form" action="<?php echo base_url('hr/Serviceform/save_cash_desc_list'); ?>" method="POST" onsubmit="return confirm('Confirm to update this form?')">
										<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" disabled>
														<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'){ ?>
															<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" disabled>
														<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':''):'' ?>>
																<?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>
													<input type="text"  class="form-control" disabled value="<?php echo ($ID)?(($petty_advance_list['job_id'])?'[JOB ID: '.str_pad($petty_advance_list['job_id'],5,0,STR_PAD_LEFT).'] '.$petty_advance_list['customerName'].' - '.$petty_advance_list['job_description']:''):'' ?>">
												</div>
											</div>

											<div class="col-12 col-sm-6">
												<div class="form-group">
													<label>Client</label>
													<input type="text"  class="form-control" disabled value="<?php echo ($ID)?(($petty_advance_list['client_id'])?$petty_advance_list['customerName']:''):'' ?>">
												</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" value="<?php echo ($ID)?date('d-m-Y',strtotime($petty_advance_list['travel_date_from'])):'' ?>" disabled>
												</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" value="<?php echo ($ID)?date('d-m-Y',strtotime($petty_advance_list['travel_date_to'])):'' ?>" disabled>
												</div>
											</div>
										</div>

										<div class="form-row py-3">
											<div class="col-12">
												<div class="form-group">
													<div class="form-row">
														<div class="col-3">
															<div class="form-group text-center"><b>DESCRIPTION</b></div>
														</div>
														<div class="col-2">
															<div class="form-group text-center"><b>BASE AMOUNT (RM)</b></div>
														</div>
														<div class="col-2">
															<div class="form-group text-center"><b>SST AMOUNT (RM)</b></div>
														</div>
														<div class="col-2">
															<div class="form-group text-center"><b>TOTAL (RM)</b></div>
														</div>
														<div class="col-3">
															<div class="form-group text-center"><b>REMARKS</b></div>
														</div>
													</div>

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

													<div class="form-row">
														<div class="col-1"></div>
														<div class="col-2">
															<div class="form-group">
																Cash Received
															</div>
														</div>
														<div class="col-2"></div>
														<div class="col-2"></div>
														<div class="col-2">
															<div class="form-group">
																<input type="text" class="text-right form-control" name="desc_total__received" id="desc_total__received" readonly required value="<?php echo ($ID)?number_format($petty_advance_list['total_approved'],2,'.',''):number_format(0,2,'.',''); ?>" style="background: transparent; cursor: not-allowed;">
															</div>
														</div>
														<div class="col-3"></div>

														<?php if($description) foreach($description as $row){ if($row['type'] == 1){ ?>
															<div class="col-1 text-center">
																<div class="form-group">
																	<input class="d-print-none" type="checkbox" name="desc_check_<?php echo $row['ID']; ?>" onclick="change_desc_input(<?php echo $row['ID']; ?>)" id="desc_check_<?php echo $row['ID']; ?>">
																</div>
															</div>
															<div class="col-2">
																<div class="form-group">
																	<?php echo $row['description']; ?>
																</div>
															</div>
															<div class="col-2">
																<div class="form-group">
																	<input type="text" class="text-right form-control" name="desc_base__<?php echo $row['ID']; ?>" id="desc_base__<?php echo $row['ID']; ?>" value="<?php echo number_format(0,2,'.',''); ?>" onchange="update_total(<?php echo $row['ID']; ?>)" disabled>
																</div>
															</div>
															<div class="col-2">
																<div class="form-group">
																	<input type="text" class="text-right form-control" name="desc_sst__<?php echo $row['ID']; ?>" id="desc_sst__<?php echo $row['ID']; ?>" value="<?php echo number_format(0,2,'.',''); ?>" onchange="update_total(<?php echo $row['ID']; ?>)" disabled>
																</div>
															</div>
															<div class="col-2">
																<div class="form-group">
																	<input type="text" class="text-right form-control" name="desc_total__<?php echo $row['ID']; ?>" id="desc_total__<?php echo $row['ID']; ?>" readonly value="<?php echo number_format(0,2,'.',''); ?>" style="background: transparent; cursor: not-allowed;">
																</div>
															</div>
															<div class="col-3">
																<div class="form-group">
																	<input type="text" class="form-control" name="desc_remark__<?php echo $row['ID']; ?>" id="desc_remark__<?php echo $row['ID']; ?>" disabled>
																</div>
															</div>
														<?php } } ?>
													</div>

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

													<div class="form-row">
														<div class="col-3">
															<div class="form-group text-center">
																<h6 style="margin-top: 10px;">CASH RETURNED (RM)</h6>
															</div>
														</div>
														<div class="col-2"></div>
														<div class="col-2"></div>
														<div class="col-2">
															<div class="form-group">
																<input type="text" class="text-right form-control" name="total_cash_return" id="total_cash_return" readonly required value="<?php echo 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" style="page-break-before: always;">
											<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</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 py-2" <?php echo ($ID && $petty_advance_list['listing_received_status'] > 0)?'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">Submit</button>
												</div> 
											</div>
										</div>
									</form>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
</section>

<!-- manager approval -->
<?php if($_SESSION['user']['grade'] >= 31 || $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">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_listing_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['listing_approval_status'] > 0)?$petty_advance_list['listing_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['listing_approval_status'] > 0)?$petty_advance_list['listing_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['listing_approval_status'] > 0)?$petty_advance_list['listing_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['listing_approval_status'] > 0)?date("d-m-Y h:iA",strtotime($petty_advance_list['approvedTS'])):date("d-m-Y h:iA")):''; ?>" readonly>
																</div>
															</div>

															<div class="col-12 col-sm-6 d-none d-print-block">
																<div class="form-group">
																	<label for="mgr_status">Status</label>
																	<input type="text" class="form-control" name="mgr_status" id="mgr_status" value="<?php echo ($ID)?(($petty_advance_list['listing_received_status'] > 0)?(($petty_advance_list['listing_received_status'] == 1)?'Approved':'Reject'):''):'' ?>" readonly>
																</div>
															</div>
														</div>
													</div>
												</div>
											</div>
											<?php if($petty_advance_list['approved_by_staff_id'] == $_SESSION['user']['id']){ ?>	
												<div class="col-12" <?php echo ($ID && $petty_advance_list['listing_received_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['listing_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_listing_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['listing_received_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['listing_received_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['listing_received_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 Received</label>
																	<input type="text" class="form-control" name="finance_date" id="finance_date" value="<?php echo ($ID)?(($petty_advance_list['listing_received_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="finance_total_received">Amount Received</label>
																	<input type="text" class="form-control" name="finance_total_received" id="finance_total_received" value="<?php echo ($ID)?(($petty_advance_list['listing_received_status'] > 0)?number_format($petty_advance_list['balance_received'],2):''):''; ?>" required>
																</div>
															</div>
														</div>
													</div>
												</div>
											</div>
											<div class="col-12 d-print-none">
												<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']; ?>');
			update_description_value();
		}
		
	});

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

		if(description_checkbox == true)
		{
			$('#desc_base__'+description_id).removeAttr('disabled');
			$('#desc_base__'+description_id).attr('required','required');
			$('#desc_sst__'+description_id).removeAttr('disabled');
			$('#desc_total__'+description_id).attr('required','required');
			$('#desc_remark__'+description_id).removeAttr('disabled');
			$('#desc_remark__'+description_id).attr('required','required');		
		}else{
			$('#desc_base__'+description_id).removeAttr('required');
			$('#desc_base__'+description_id).attr('disabled','disabled');
			$('#desc_sst__'+description_id).attr('disabled','disabled');
			$('#desc_total__'+description_id).removeAttr('required');
			$('#desc_remark__'+description_id).removeAttr('required');
			$('#desc_remark__'+description_id).attr('disabled','disabled');

			$('#desc_base__'+description_id).val(0);
			$('#desc_sst__'+description_id).val(0);
			$('#desc_total__'+description_id).val(0);
			$('#desc_remark__'+description_id).val('');

			update_total(description_id);
		}
	}

	function change_input(request_type)
	{
		<?php foreach($description as $key => $row){ ?>
			if($('#desc_check_'+'<?php echo $row['ID']; ?>').is(':checked'))
			{
				$('#desc_check_'+'<?php echo $row['ID']; ?>').prop('checked',false);
				$('#desc_remark__'+'<?php echo $row['ID']; ?>').attr('disabled','disabled');
				$('#desc_remark__'+'<?php echo $row['ID']; ?>').val('');
				$('#desc_base__'+'<?php echo $row['ID']; ?>').attr('disabled','disabled');
				$('#desc_base__'+'<?php echo $row['ID']; ?>').val((0).toFixed(2));
				$('#desc_sst__'+'<?php echo $row['ID']; ?>').attr('disabled','disabled');
				$('#desc_sst__'+'<?php echo $row['ID']; ?>').val((0).toFixed(2));
				$('#desc_total__'+'<?php echo $row['ID']; ?>').val((0).toFixed(2));					
			}

			update_total();
		<?php } ?>
	}

	function update_total(description_id = null)
	{
		var description_first_id 	= '<?php echo ($description)?$description[0]['ID']:''; ?>';
		var description_last_id 	= '<?php echo end($description)['ID']; ?>';
		var description_amount 		= $('#desc_base__'+description_id).val();
		var description_sst 		= $('#desc_sst__'+description_id).val();
		var total 					= 0;
		var total_balance 			= 0;
		var total_base 				= 0;
		var total_sst 				= 0;

		if(description_id > 0)
		{
			if(description_amount != '' && description_amount != null && description_amount > 0)
			{
				var base_amount = $('#desc_base__'+description_id).val();
			}else{
				var base_amount = 0.00;
			}

			if(description_sst != '' && description_sst != null && description_sst > 0)
			{
				var sst_amount = $('#desc_sst__'+description_id).val();
			}else{
				var sst_amount = 0.00;
			}

			var desc_total = parseFloat(base_amount) + parseFloat(sst_amount);

			$('#desc_base__'+description_id).val(parseFloat(base_amount).toFixed(2));
			$('#desc_sst__'+description_id).val(parseFloat(sst_amount).toFixed(2));
			$('#desc_total__'+description_id).val(desc_total.toFixed(2));
		}
			
		for(var i = description_first_id; i <= description_last_id; i++)
		{
			if($('#desc_total__'+i).val() != '' && $('#desc_total__'+i).val() != null && $('#desc_total__'+i).val() > 0)
			{
				total_base 	+= parseFloat($('#desc_base__'+i).val());
				total_sst 	+= parseFloat($('#desc_sst__'+i).val());
				total 		+= parseFloat($('#desc_total__'+i).val());
			}else{
				total_base 	+= 0;
				total_sst 	+= 0;
				total 		+= 0;
			}
		}

		total_balance = parseFloat('<?php echo $petty_advance_list['total_approved']; ?>') - parseFloat(total);

		$('#total_base_amount').val(parseFloat(total_base).toFixed(2));
		$('#total_sst_amount').val(parseFloat(total_sst).toFixed(2));
		$('#total').val(parseFloat(total).toFixed(2));
		$('#total_cash_return').val(parseFloat(total_balance).toFixed(2));
	}

	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 base_amount = ('<?php echo $petty_advance_list['base_amount']; ?>').split('->');
		var sst_amount 	= ('<?php echo $petty_advance_list['sst_amount']; ?>').split('->');
		var total 		= ('<?php echo $petty_advance_list['total']; ?>').split('->');

		$.each(desc_id,function(key, row){
			$('#desc_check_'+row).prop('checked', true);
			$('#desc_check_'+row).prop('disabled',false);

			$('#desc_remark__'+row).prop('disabled',false);
			$('#desc_remark__'+row).prop('required',true);
			$('#desc_remark__'+row).val(desc_remark[key]);

			$('#desc_base__'+row).prop('disabled',false);
			$('#desc_base__'+row).prop('required',true);
			$('#desc_base__'+row).val(parseFloat(base_amount[key]).toFixed(2));

			$('#desc_sst__'+row).prop('disabled',false);
			$('#desc_sst__'+row).val(parseFloat(sst_amount[key]).toFixed(2));

			$('#desc_total__'+row).prop('required',true);
			$('#desc_total__'+row).val(parseFloat(total[key]).toFixed(2));
		});

		update_total();
	}
</script>