<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 '; ?> Invoice Request Form</h4>
						</div>
						<div class="card-body">
							<div class="card no-shadow">
								<div class="card-body">
									<?php if(!empty($ID)){ ?>
										<form id="invoiceForm" class="form" action="<?php echo ($invoice_list['approval_status'] > 0)?base_url('hr/Serviceform/invoice_edit/'.$mode):base_url('hr/Serviceform/invoice_edit') ?>" method="POST" onsubmit="return confirm('Confirm to update this form?')">
										<?php }else{ ?>	
											<form id="invoiceForm" class="form" action="<?php echo base_url('hr/Serviceform/invoice_save') ?>" method="POST" onsubmit="return confirm('Confirm to submit this form?')">
											<?php }?>

											<input type="text" class="form-control" name="invoiceID" id="invoiceID" value="<?php echo $ID; ?>" hidden>
											<input type="text" class="form-control" name="entityID" id="entityID" value="<?php echo $entity_id; ?>" hidden>

											<div class="form-row">
												<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" required onchange="change_client_input()">
															<option></option>
															<?php if($mgs_job) foreach($mgs_job as $row){ ?>
																<option value="<?php echo $row['job_id'] ?>">
																	<?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>Company Name</label>
														<select class="form-control select" name="client_id" id="client_id" required onchange="change_job_input()" <?php echo ($ID && $invoice_list['approval_status'] > 0)?'disabled':''; ?>>
															<option></option>
															<?php if($client) foreach($client as $row){ ?>
																<option value="<?php echo $row->id; ?>">
																	<?php echo $row->name; ?>
																</option>
															<?php } ?>
														</select>
													</div>
												</div>

												<div class="col-12 col-sm-6">
													<div class="form-group">
														<label>Group</label>
														<input type="text" class="form-control" name="client_group_id" id="client_group_id" <?php echo ($ID && $invoice_list['approval_status'] > 0)?'disabled':'readonly'; ?>>
													</div>
												</div>

												<div class="col-12 col-sm-6">
													<div class="form-group">
														<label>Tel No</label>
														<button type="button" id="addClientPhone" class="btn btn-sm btn-warning float-right" onclick="open_modal('client_phone')" data-trigger="hover" data-tooltip="tooltip" title="Add Client Telephone Number"><i class="fas fa-plus" <?php echo ($ID && $invoice_list['approval_status'] > 0)?'disabled':''; ?>></i></button>
														<select class="form-control" name="client_tel_no" id="client_tel_no" required onchange="change_email_input()" <?php echo ($ID && $invoice_list['approval_status'] > 0)?'disabled':''; ?>>
															<option value="-1" selected disabled>-- Please Select --</option>
														</select>
													</div>
												</div>

												<div class="col-12 col-sm-6">
													<div class="form-group">
														<label>Fax No</label>
														<button type="button" id="addClientFax" class="btn btn-sm btn-warning float-right" onclick="open_modal('client_fax')" data-trigger="hover" data-tooltip="tooltip" title="Add Client Fax"><i class="fas fa-plus" <?php echo ($ID && $invoice_list['approval_status'] > 0)?'disabled':''; ?>></i></button>
														<input type="text" class="form-control" name="client_fax_no" id="client_fax_no" <?php echo ($ID && $invoice_list['approval_status'] > 0)?'disabled':''; ?>>
													</div>
												</div>

												<div class="col-12 col-sm-6">
													<div class="form-group">
														<label>Email</label>
														<button type="button" id="addClientEmail" class="btn btn-sm btn-warning float-right" onclick="open_modal('client_email')" data-trigger="hover" data-tooltip="tooltip" title="Add Client Email Address"><i class="fas fa-plus" <?php echo ($ID && $invoice_list['approval_status'] > 0)?'disabled':''; ?>></i></button>
														<input type="text" class="form-control" name="client_email" id="client_email" required <?php echo ($ID && $invoice_list['approval_status'] > 0)?'disabled':'readonly'; ?>>
													</div>
												</div>

												<div class="col-12">
													<div class="form-group">
														<label>Address</label>
														<button type="button" id="addClientAddress" class="btn btn-sm btn-warning float-right" onclick="open_modal('client_address')" data-trigger="hover" data-tooltip="tooltip" title="Add Client Address"><i class="fas fa-plus" <?php echo ($ID && $invoice_list['approval_status'] > 0)?'disabled':''; ?>></i></button>
														<textarea class="form-control" rows="3" name="client_address" id="client_address" required <?php echo ($ID && $invoice_list['approval_status'] > 0)?'disabled':''; ?>></textarea>
													</div>
												</div>

												<div class="col-12 col-sm-6">
													<div class="form-group">
														<label>Attention To</label>
														<input type="text" class="form-control" name="client_attention_to" id="client_attention_to" value="<?php echo ($ID)?$invoice_list['attention_to']:''; ?>" required <?php echo ($ID && $invoice_list['approval_status'] > 0)?'disabled':''; ?>>
													</div>
												</div>

												<div class="col-12 col-sm-6">
													<div class="form-group">
														<label>PO No</label>
														<input type="text" class="form-control" name="client_po_no" id="client_po_no" value="<?php echo ($ID)?$invoice_list['po_no']:''; ?>" <?php echo ($ID && $invoice_list['approval_status'] > 0)?'disabled':''; ?>>
													</div>
												</div>
												
												<?php if($entity_id == 3){ ?>
													<div class="col-12 col-sm-6">
														<div class="form-group">
															<label>Branch</label>
															<select class="form-control" name="branch_id" id="branch_id" required <?php echo ($ID && $invoice_list['approval_status'] > 0)?'disabled':''; ?>>
																<option value="-1" selected disabled>-- Please Select --</option>
																<?php if($branch) foreach($branch as $row){ ?>
																	<option value="<?php echo $row['id']; ?>" <?php echo ($ID)?(($invoice_list['branch_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>Signing Parner</label>
															<select class="form-control" name="signing_partner" id="signing_partner" <?php echo ($ID && $invoice_list['approval_status'] > 0)?'disabled':''; ?>>
																<option value="-1" selected disabled>-- Please Select --</option>
																<?php if($partner) foreach($partner as $row){ ?>
																	<option value="<?php echo $row['id'] ?>" <?php echo ($ID)?(($invoice_list['sign_partner_id'] == $row['id'])?'selected':''):''; ?>><?php echo $row['name']; ?></option>
																<?php } ?>
															</select>
														</div>
													</div>
												<?php } ?>
											</div>

											<div class="form-row py-3">
												<div class="col-12">
													<div class="form-group">
														<!-- <h5><b>Description</b></h5> -->
														<!-- <hr> -->
														<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">
															<?php if($description) foreach($description as $row){ if($row['type'] == 0){ ?>
																<div class="col-1 text-center">
																	<div class="form-group">
																		<input type="checkbox" name="desc_check_<?php echo $row['ID']; ?>" id="desc_check_<?php echo $row['ID']; ?>" onclick="change_input(<?php echo $row['ID']; ?>)" <?php echo ($ID && $invoice_list['approval_status'] > 0)?'disabled':''; ?>>
																	</div>
																</div>
																<div class="col-3">
																	<div class="form-group">
																		<?php echo $row['description']; ?>
																	</div>
																</div>
																<div class="col-5">
																	<div class="form-group">
																		<input type="text" class="form-control" name="desc_detail__<?php echo $row['ID']; ?>" id="desc_detail__<?php echo $row['ID']; ?>" disabled>
																	</div>
																</div>
																<div class="col-3">
																	<div class="form-group">
																		<input type="text" class="text-right form-control" name="desc_amount__<?php echo $row['ID']; ?>" id="desc_amount__<?php echo $row['ID']; ?>" value="<?php echo number_format(0,2); ?>" onchange="update_total(<?php echo $row['ID']; ?>)" disabled>
																	</div>
																</div>
															<?php } } ?>

															<div class="col-12" style="background-color: #e9ecef; padding-top: 15px; margin-bottom: 20px;"><div class="form-group" style="padding-left: 2rem;">OUT OF POCKET</div></div>

															<?php if($description) foreach($description as $row){ if($row['type'] == 1){ ?>
																<div class="col-1 text-center">
																	<div class="form-group">
																		<input type="checkbox" name="desc_check_<?php echo $row['ID']; ?>" id="desc_check_<?php echo $row['ID']; ?>" onclick="change_input(<?php echo $row['ID']; ?>)" <?php echo ($ID && $invoice_list['approval_status'] > 0)?'disabled':''; ?>>
																	</div>
																</div>
																<div class="col-3">
																	<div class="form-group">
																		<?php echo $row['description']; ?>
																	</div>
																</div>
																<div class="col-5">
																	<div class="form-group">
																		<input type="text" class="form-control" name="desc_detail__<?php echo $row['ID']; ?>" id="desc_detail__<?php echo $row['ID']; ?>" disabled>
																	</div>
																</div>
																<div class="col-3">
																	<div class="form-group">
																		<input type="text" class="text-right form-control" name="desc_amount__<?php echo $row['ID']; ?>" id="desc_amount__<?php echo $row['ID']; ?>" value="<?php echo number_format(0,2); ?>" onchange="update_total(<?php echo $row['ID']; ?>)" disabled>
																	</div>
																</div>
															<?php } } ?>

															<div class="col-1 text-center">
																<div class="form-group">
																	<input type="checkbox" name="desc_check_sst" id="desc_check_sst" <?php echo ($business_entity_detail['is_sst'] == true)?'checked':'disabled style="cursor: not-allowed;"'; ?> <?php echo ($ID && $invoice_list['approval_status'] > 0)?'disabled':''; ?>>
																</div>
															</div>
															<div class="col-3">
																<div class="form-group">
																	SST <?php echo ($business_entity_detail['is_sst'])?'('.($business_entity_detail['tax_amount']*100).'%)':''; ?>
																</div>
															</div>
															<div class="col-5"></div>
															<div class="col-3">
																<div class="form-group">
																	<input type="text" class="text-right form-control" name="desc_amount__sst" id="desc_amount__sst" value="<?php echo number_format(0,2); ?>" <?php echo ($business_entity_detail['is_sst'] == true)?'readonly required style="background: transparent; cursor: not-allowed;"':'disabled style="cursor: not-allowed;"'; ?>>
																</div>
															</div>
														</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_invoice" id="total_invoice" readonly required value="<?php echo ($ID)?number_format($invoice_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)?$invoice_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)?$invoice_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)?$invoice_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)?$invoice_list['dept_name'].' ['.$invoice_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)?$invoice_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($invoice_list['createdTS'])):date("d-m-Y"); ?>" readonly>
																</div>
															</div>
														</div>
													</div>
												</div>
											</div>

											<div class="form-row">
												<div class="col-12">
													<p>
														Kindly Take Note of the followings:-<br>
														i)	Kindly indicate in detail job undertake<br>
														ii)	Kindly ensure amount is in accordance to proposal or quotation issued.<br>
														iii)	Check for major variation from previous invoice(s) issued<br>
														iv)	This form is to be approved by Manager above<br>
													</p> 
												</div>
											</div>

											<!-- <div class="form-row py-2" <?php //echo ($invoice_list['createdBY'] != $_SESSION['user']['id'])?'style="display: none;"':''; ?>>
												<div class="col-12">
													<div class="float-right">
														<a class="btn btn-light" href="<?php //echo base_url('hr/serviceform/invoice_list') ?>">Cancel</a>
														<button type="submit" 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/invoice_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="invoiceApprovedForm" class="form" action="<?php echo base_url('hr/Serviceform/invoice_approved/manager') ?>" method="POST">
											<input type="text" class="form-control" name="invoiceID" id="invoiceID" 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)?(($invoice_list['approval_status'] > 0)?$invoice_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)?(($invoice_list['approval_status'] > 0)?$invoice_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)?(($invoice_list['approval_status'] > 0)?$invoice_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)?(($invoice_list['approval_status'] > 0)?date("d-m-Y h:iA",strtotime($invoice_list['approvedTS'])):date("d-m-Y h:iA")):''; ?>" readonly>
																</div>
															</div>
														</div>
													</div>
												</div>
											</div>
											<div class="col-12" <?php echo ($ID && $invoice_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>
										</form>									
									</div>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
	</section>
<?php } ?>

<!-- for finance use -->
<?php if($ID && $invoice_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="invoiceApprovedForm" class="form" action="<?php echo base_url('hr/Serviceform/invoice_approved/finance') ?>" method="POST">
											<input type="text" class="form-control" name="invoiceID" id="invoiceID" 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)?(($invoice_list['prepared_status'] > 0)?$invoice_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)?(($invoice_list['prepared_status'] > 0)?$invoice_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)?(($invoice_list['prepared_status'] > 0)?$invoice_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 Issued</label>
																	<input type="text" class="form-control" name="finance_date" id="finance_date" value="<?php echo ($ID)?(($invoice_list['prepared_status'] > 0)?date("d-m-Y h:iA",strtotime($invoice_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_inv_no">Invoice No</label>
																	<input type="text" class="form-control" name="finance_inv_no" id="finance_inv_no" value="<?php echo ($ID)?(($invoice_list['prepared_status'] > 0)?$invoice_list['inv_form_no']:''):''; ?>" required>
																</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 } ?>

<!-- add client phone modal -->
<div class="modal fade" id="modal_add_client_info" role="dialog" aria-hidden="true">
	<div class="modal-dialog modal-lg">
		<div class="modal-content">
			<div class="modal-header">
				<h5 class="modal-title" id="modal_add_client_info_title"></h5>
				<button type="button" class="close" data-dismiss="modal" aria-label="Close">
					<span aria-hidden="true">&times;</span>
				</button>
			</div>

			<div class="modal-body">
				<input type="text" class="form-control" name="client_ID" id="client_ID" hidden>	

				<div id="modal_add_client_info_content"></div>
				
				<!-- <div class="row p-3">
					<div class="col-12">
						<label>Name</label>
						<input type="text" class="form-control" id="rep_name" name="rep_name" required>
					</div>
				</div>

				<div class="row p-3">
					<div class="col-12">
						<label>Phone</label>
						<input type="text" class="form-control" id="phone_mobile" name="phone_mobile" required>
					</div>
				</div> -->

				<div class="float-right">
					<button type="button" class="btn btn-light" data-dismiss="modal">Cancel</button>
					<button type="button" id="saveClientInfoBtn" class="btn btn-primary">Save</button>
				</div>
			</div>
		</div>
	</div>
</div>


<script>
	$(document).ready(function()
	{
		var client_id = $('#client_id').val();
		if(client_id > 0)
		{
			$('#addClientPhone').removeAttr('disabled');
			$('#addClientPhone').css('cursor','pointer');
			$('#addClientFax').removeAttr('disabled');
			$('#addClientFax').css('cursor','pointer');
			$('#addClientAddress').removeAttr('disabled');
			$('#addClientAddress').css('cursor','pointer');
			$('#addClientEmail').removeAttr('disabled');
			$('#addClientEmail').css('cursor','pointer');
		}else{
			$('#addClientPhone').attr('disabled','disabled');
			$('#addClientPhone').css('cursor','not-allowed');
			$('#addClientFax').attr('disabled','disabled');
			$('#addClientFax').css('cursor','not-allowed');
			$('#addClientAddress').attr('disabled','disabled');
			$('#addClientAddress').css('cursor','not-allowed');
			$('#addClientEmail').attr('disabled','disabled');
			$('#addClientEmail').css('cursor','not-allowed');
		}

		var job_id = '<?php echo ($ID)?$invoice_list['job_id']:''; ?>';
		var client_id = '<?php echo ($ID)?$invoice_list['client_id']:''; ?>';
		
		if(job_id > 0)
		{
			change_client_input(job_id);
		}else{
			change_job_input(client_id,job_id);
		}
		if('<?php echo $ID; ?>' > 0) edit_description_value();
		
	});

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

		var group_id 	= '';
		var tel_no 		= '';
		var fax_no 		= '';
		var email 		= '';
		var address 	= '';

		
		if(job_id > 0)
		{
			// $('#client_id').select2();
			$('#client_id').val('-1');
			$('#job_id').select2();
			$('#job_id').val(job_id);
			$('#job_id').select2({
				placeholder: "-- Please Select --",
				theme: 'bootstrap4',
			});

			$('#client_tel_no').html('');
			$('#client_tel_no').append('<option value="-1" selected disabled>-- Please Select --</option>');
			$('#client_id').select2();
			$('#client_id').select2('destroy');

			$.ajax({
				url:'<?php echo base_url('hr/serviceform/invoice_get_client_detail') ?>',
				data: {
					job_id 		: job_id,
				},
				type: 'post',
				dataType: 'json',
				success: (data) => {
					if(data.status == "success") {
						var job_detail 			= data.job_detail;
						var client_detail 		= data.client_detail;
						var client_contact_no 	= data.client_contact_no;

						$.each(client_contact_no, function(key,content){
							if(content.phone != '' && content.phone != null)
							{
								if('<?php echo $ID; ?>' > 0)
								{
									if('<?php echo $invoice_list['client_contact']; ?>' == content.phone)
									{
										var selected = 'selected';
									}else{
										var selected = '';
									}
								}
								var tel_temp = '<option value="'+content.phone+'" '+selected+'>'+content.rep_name+' - '+content.phone+'</option>';
								$('#client_tel_no').append(tel_temp);
							}
						});

						if(client_detail.groupName != '' && client_detail.groupName != null) group_id = client_detail.groupName;
						if(client_detail.fax != '' && client_detail.fax != null) fax_no = client_detail.fax;
						if(client_detail.email != '' && client_detail.email != null) email = client_detail.email;
						if(client_detail.address1 != '' && client_detail.address1 != null) address = client_detail.address1+' '+client_detail.address2;

						$('#client_group_id').val(group_id);

						if(('<?php echo $ID; ?>' != '' && '<?php echo $ID; ?>' != null) && (job_detail[0]['customerID'] == '<?php echo $invoice_list['client_id']; ?>'))
						{
							$('#client_fax_no').val('<?php echo ($invoice_list['client_fax'])?$invoice_list['client_fax']:''; ?>');
							$('#client_address').val(`<?php echo ($invoice_list['client_address'])?$invoice_list['client_address']:''; ?>`);
						}else{
							$('#client_fax_no').val(fax_no);
							$('#client_email').val(email);
							$('#client_address').val(address);
						}

						$('#addClientPhone').removeAttr('disabled');
						$('#addClientPhone').css('cursor','pointer');
						$('#addClientFax').removeAttr('disabled');
						$('#addClientFax').css('cursor','pointer');
						$('#addClientAddress').removeAttr('disabled');
						$('#addClientAddress').css('cursor','pointer');
						$('#addClientEmail').removeAttr('disabled');
						$('#addClientEmail').css('cursor','pointer');

						$('#client_id').val(job_detail[0]['customerID']);
						$('#client_id').css('pointer-events','none');
						$('#client_id').attr('readonly','readonly');

           				change_email_input(job_detail[0]['customerID'],'<?php echo ($ID)?$invoice_list['client_contact']:''; ?>');
						// $('#client_id').select2({
						// placeholder: "-- Please Select --", 	
						// theme: 'bootstrap4',
						// });
					}else{
						$('#client_id').css('pointer-events','none');
						$('#client_id').attr('readonly','readonly');
						$('#addClientPhone').attr('disabled','disabled');
						$('#addClientPhone').css('cursor','not-allowed');
						$('#addClientFax').attr('disabled','disabled');
						$('#addClientFax').css('cursor','not-allowed');
						$('#addClientAddress').attr('disabled','disabled');
						$('#addClientAddress').css('cursor','not-allowed');
						$('#addClientEmail').attr('disabled','disabled');
						$('#addClientEmail').css('cursor','not-allowed');

						$('#client_fax_no').val(fax_no);
						$('#client_email').val(email);
						$('#client_address').val(address);

						iziToast.warning({
		                    position: 'bottomRight',
		                    message: 'No Client Detail Found. Please Try Again. Thank You.',
		                    timeout: '3000',
		                });
					}
              	}
           	});
		}else{
			$('#addClientPhone').attr('disabled','disabled');
			$('#addClientPhone').css('cursor','not-allowed');
			$('#addClientFax').attr('disabled','disabled');
			$('#addClientFax').css('cursor','not-allowed');
			$('#addClientAddress').attr('disabled','disabled');
			$('#addClientAddress').css('cursor','not-allowed');
			$('#addClientEmail').attr('disabled','disabled');
			$('#addClientEmail').css('cursor','not-allowed');

			$('#client_tel_no').html('');
			$('#client_tel_no').append('<option value="-1" selected disabled>-- Please Select --</option>');
			$('#client_group_id').val(group_id);
			$('#client_fax_no').val(fax_no);
			$('#client_email').val(email);
			$('#client_address').val(address);
			$('#client_attention_to').val('');
			$('#client_po_no').val('');

			$('#client_id').val('-1');
			$('#client_id').removeAttr('readonly');
			$('#client_id').select2({
				placeholder: "-- Please Select --",
				theme: 'bootstrap4',
			});
		}
	}

	function change_job_input(clientID = null, jobID = null)
	{
		var group_id 	= '';
		var tel_no 		= '';
		var fax_no 		= '';
		var email 		= '';
		var address 	= '';

		if(clientID > 0)	var client_id = clientID;
		else 	var client_id = $('#client_id').val();
		
		// $('#job_id').select2();
		// $('#job_id').select2('destroy');
		$('#client_id').select2();
		$('#client_id').select2('destroy');
		$('#client_tel_no').html('');
		$('#client_tel_no').append('<option value="-1" selected disabled>-- Please Select --</option>');
		
		if(client_id > 0)
		{
			$('#addClientPhone').removeAttr('disabled');
			$('#addClientPhone').css('cursor','pointer');
			$('#addClientFax').removeAttr('disabled');
			$('#addClientFax').css('cursor','pointer');
			$('#addClientAddress').removeAttr('disabled');
			$('#addClientAddress').css('cursor','pointer');
			$('#addClientEmail').removeAttr('disabled');
			$('#addClientEmail').css('cursor','pointer');

			$.ajax({
				url:'<?php echo base_url('hr/serviceform/invoice_get_client_detail') ?>',
				data: {
					client_id 	: client_id,
				},
				type: 'post',
				dataType: 'json',
				success: (data) => {
					if(data.status == "success") {
						var client_detail 		= data.client_detail;
						var client_contact_no 	= data.client_contact_no;

						$.each(client_contact_no, function(key,content){
							if(content.phone != '' && content.phone != null)
							{
								if('<?php echo $ID; ?>' > 0)
								{
									if('<?php echo $invoice_list['client_contact']; ?>' == content.phone)
									{
										var selected = 'selected';
									}else{
										var selected = '';
									}
								}else{
									var selected = '';
								}

								var tel_temp = '<option value="'+content.phone+'" '+selected+'>'+content.rep_name+' - '+content.phone+'</option>';
								$('#client_tel_no').append(tel_temp);
							}
						});

						if(client_detail.groupName != '' && client_detail.groupName != null) group_id = client_detail.groupName;
						if(client_detail.fax != '' && client_detail.fax != null) fax_no = client_detail.fax;
						if(client_detail.email != '' && client_detail.email != null) email = client_detail.email;
						if(client_detail.address1 != '' && client_detail.address1 != null) address = client_detail.address1+' '+client_detail.address2;

						$('#client_group_id').val(group_id);

						if('<?php echo $ID; ?>' != '' && '<?php echo $ID; ?>' != null)
						{
							$('#client_fax_no').val('<?php echo ($invoice_list['client_fax'])?$invoice_list['client_fax']:''; ?>');
							$('#client_address').val(`<?php echo ($invoice_list['client_address'])?$invoice_list['client_address']:''; ?>`);
						}else{
							$('#client_fax_no').val(fax_no);
							$('#client_email').val(email);
							$('#client_address').val(address);
						}

						$('#client_id').val(client_id);

						$('#client_id').select2({
							placeholder: "-- Please Select --",
							theme: 'bootstrap4',
						});
           				
           				change_email_input(client_id, '<?php echo ($ID)?$invoice_list['client_contact']:''; ?>');
					}
              	}
           	});

			if(jobID > 0)
			{
				$('#job_id').select2({
					placeholder: "-- Please Select --",
					theme: 'bootstrap4',
				});
			}else
			{				
				// $('#job_id').removeAttr('required');
				// $('#job_id').css('pointer-events','none');
				// $('#job_id').attr('readonly','readonly');
			}         	
		}else{
			$('#addClientPhone').attr('disabled','disabled');
			$('#addClientPhone').css('cursor','not-allowed');
			$('#addClientFax').attr('disabled','disabled');
			$('#addClientFax').css('cursor','not-allowed');
			$('#addClientAddress').attr('disabled','disabled');
			$('#addClientAddress').css('cursor','not-allowed');
			$('#addClientEmail').attr('disabled','disabled');
			$('#addClientEmail').css('cursor','not-allowed');

			$('#client_group_id').val(group_id);
			$('#client_fax_no').val(fax_no);
			$('#client_email').val(email);
			$('#client_address').val(address);
			$('#client_id').val('-1');

			$('#client_id').select2({
				placeholder: "-- Please Select --",
				theme: 'bootstrap4',
			});

			$('#job_id').attr('required','required');
			$('#job_id').val('-1');
			// $('#job_id').removeAttr('readonly');
			// $('#job_id').select2({
			// placeholder: "-- Please Select --",	
			// theme: 'bootstrap4',
			// 	disabled: false,
			// });
			$('#job_id').select2({
				placeholder: "-- Please Select --",
				theme: 'bootstrap4',
			});
		}
	}

	function open_modal(mode)
	{
		var client_id 		= $('#client_id').val();
		var modal_content 	= '';
		var modal_title		= '';

		$('#modal_add_client_info_title').html('');
		$('#modal_add_client_info_content').html('');
		$('#saveClientInfoBtn').removeAttr('onclick');

		if(client_id > 0)
		{
			if(mode == 'client_phone')
			{
				modal_title = 'Add Client Telephone Number';
				modal_content = 
				'<div class="row pb-3">\
					<div class="col-12">\
						<label>Name</label>\
						<input type="text" class="form-control" id="rep_name" name="rep_name" required>\
					</div>\
				</div>\
				<div class="row pb-3">\
					<div class="col-12">\
						<label>Phone</label>\
						<input type="text" class="form-control" id="phone_mobile" name="phone_mobile" required>\
					</div>\
				</div>';
				
				$('#saveClientInfoBtn').attr('onclick','save_client_phone()');
			}else if(mode == 'client_fax')
			{
				modal_title = 'Add Client Fax Number';
				modal_content = 
				'<div class="row pb-3">\
					<div class="col-12">\
						<label>Fax No</label>\
						<input type="text" class="form-control" id="fax_num" name="fax_num" required>\
					</div>\
				</div>';

				$('#saveClientInfoBtn').attr('onclick','save_client_info('+"'client_fax'"+','+"'client_fax_no'"+','+"'fax_num'"+')');
			}else if(mode == 'client_email')
			{
				modal_title = 'Add Client Email Address';
				modal_content = 
				'<div class="row pb-3">\
					<div class="col-12">\
						<label>Email</label>\
						<input type="text" class="form-control" id="email_address" name="email_address" required>\
					</div>\
				</div>';

				$('#saveClientInfoBtn').attr('onclick','save_client_info('+"'client_email'"+','+"'client_email'"+','+"'email_address'"+')');
			}else if(mode == 'client_address')
			{
				modal_title = 'Add Client Address';
				modal_content = 
				'<div class="row pb-3">\
					<div class="col-12">\
						<label>Address</label>\
						<textarea class="form-control" id="address" name="address" rows="3" required></textarea>\
					</div>\
				</div>';

				$('#saveClientInfoBtn').attr('onclick','save_client_info('+"'client_address'"+','+"'client_address'"+','+"'address'"+')');
			}
			
			$('#client_ID').val(client_id);
			$('#modal_add_client_info_title').append(modal_title);
			$('#modal_add_client_info_content').append(modal_content);
			$('#modal_add_client_info').modal('show');
		}
	}

	function change_email_input(clientID = null, client_contact = null)
	{
		if(client_contact != '' && client_contact != null && client_contact != undefined)
		{
			var contact = client_contact;
		}else{
			var contact = $('#client_email').val();
		}

		if(client_contact != '' && client_contact != null && client_contact != undefined)
		{
			var client_id = clientID;
		}else{
			var client_id = $('#client_id').val();
		}

		$.ajax({
			url:'<?php echo base_url('hr/serviceform/invoice_get_client_detail') ?>',
			data: {
				client_id 		: client_id,
				client_contact 	: client_contact,
			},
			type: 'post',
			dataType: 'json',
			success: (data) => {
				if(data.status == "success") {
					var client_contact_no 	= data.client_contact_no;
					var client_tel_no 		= $('#client_tel_no').val();

					$.each(client_contact_no, function(key,content){
						if(content.phone != '' && content.phone != null)
						{
							if(client_tel_no == content.phone)
							{
								$('#client_email').val(content.email);
							}

						}
					});
				}
          	}
       	});
	}

	function save_client_phone()
	{
		$('#modal_add_client_info').modal('hide');

		var client_id 		= $('#client_ID').val();
		var rep_name 		= $('#rep_name').val();
		var phone_mobile 	= $('#phone_mobile').val();

		$.ajax({
			url:'<?php echo base_url('hr/serviceform/invoice_save_client_phone') ?>',
			data: {
				client_id 		: client_id,
				rep_name 		: rep_name,
				phone_mobile 	: phone_mobile,
			},
			type: 'post',
			dataType: 'json',
			success: (data) => {
				if(data.status == "success") {
					iziToast.success({
	                    position: 'bottomRight',
	                    message: data.msg,
	                });

	                $('#client_tel_no').html('');
					$('#client_tel_no').append('<option value="-1" selected disabled>-- Please Select --</option>');
					var client_contact_no 	= data.client_contact_no;

					$.each(client_contact_no, function(key,content){
						if(content.phone != '' && content.phone != null)
						{
							if(content.phone == phone_mobile)
							{
								var selected = 'selected';
							}else{
								var selected = '';
							}
							var tel_temp = '<option value="'+content.phone+'" '+selected+'>'+content.rep_name+' - '+content.phone+'</option>';
							$('#client_tel_no').append(tel_temp);
						}
					});
				}else{
					iziToast.warning({
	                    position: 'bottomRight',
	                    message: data.msg
	                });

	                $('#client_tel_no').html('');
					$('#client_tel_no').append('<option value="-1" selected disabled>-- Please Select --</option>');
					var client_contact_no 	= data.client_contact_no;

					$.each(client_contact_no, function(key,content){
						if(content.phone != '' && content.phone != null)
						{
							var tel_temp = '<option value="'+content.phone+'">'+content.rep_name+' - '+content.phone+'</option>';
							$('#client_tel_no').append(tel_temp);
						}
					});
				}
          	}
       	});
	}

	function change_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');

			if('<?php echo $entity_id; ?>' == 3 && description_id == 1)
			{
				$('#signing_partner').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');

			if('<?php echo $entity_id; ?>' == 3 && description_id == 1)
			{
				$('#signing_partner').removeAttr('required');
			}

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

			update_total(description_id);
		}
	}

	function update_total(description_id)
	{
		var description_first_id = '<?php echo min($description)['ID']; ?>';
		var description_last_id = '<?php echo max($description)['ID']; ?>';
		// var description_amount = $('#desc_amount__'+description_id).val();

		if('<?php echo $business_entity_detail['is_sst']; ?>' == true) var sst_amount = '<?php echo $business_entity_detail['tax_amount']; ?>';
		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;
			}
		}
			
		for(var i = description_first_id; i <= 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;
			}
		}

		if(total > 0)
		{
			var sst = total * sst_amount; 
		}else{
			var sst = 0.00;
		}

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

		if('<?php echo $business_entity_detail['is_sst']; ?>' == true)
		{
			$('#desc_amount__sst').val(parseFloat(sst).toFixed(2).replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,"));
			$('#total_invoice').val(parseFloat(total + sst).toFixed(2));
		}else{
			$('#total_invoice').val(parseFloat(total).toFixed(2).replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,"));
		}
	}

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

		$.each(desc_id,function(key, row){
			$('#desc_check_'+row).prop('checked', true);
			$('#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).replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,"));
		});
	}

	function save_client_info(mode, input_id, modal_input_id)
	{
		$('#modal_add_client_info').modal('hide');

		var client_id 	= $('#client_ID').val();
		var input_value	= $('#'+modal_input_id).val();
		
		$.ajax({
			url:'<?php echo base_url('hr/serviceform/invoice_save_client_info') ?>',
			data: {
				mode 		: mode,
				client_id 	: client_id,
				input_id 	: input_id,
				input_value : input_value,
			},
			type: 'post',
			dataType: 'json',
			success: (data) => {
				if(data.status == "success") {
					iziToast.success({
	                    position: 'bottomRight',
	                    message: data.msg,
	                });

	    //             $('#client_tel_no').html('');
					// $('#client_tel_no').append('<option value="-1" selected disabled>-- Please Select --</option>');
					// var client_contact_no 	= data.client_contact_no;

					// $.each(client_contact_no, function(key,content){
					// 	if(content.phone != '' && content.phone != null)
					// 	{
					// 		if(content.phone == phone_mobile)
					// 		{
					// 			var selected = 'selected';
					// 		}else{
					// 			var selected = '';
					// 		}
					// 		var tel_temp = '<option value="'+content.phone+'" '+selected+'>'+content.rep_name+' - '+content.phone+'</option>';
					// 		$('#client_tel_no').append(tel_temp);
					// 	}
					// });
				}else{
					iziToast.warning({
	                    position: 'bottomRight',
	                    message: data.msg
	                });

	                $('#client_tel_no').html('');
					$('#client_tel_no').append('<option value="-1" selected disabled>-- Please Select --</option>');
					var client_contact_no 	= data.client_contact_no;

					$.each(client_contact_no, function(key,content){
						if(content.phone != '' && content.phone != null)
						{
							var tel_temp = '<option value="'+content.phone+'">'+content.rep_name+' - '+content.phone+'</option>';
							$('#client_tel_no').append(tel_temp);
						}
					});
				}
          	}
       	});
	}
</script>