<?php
require_once __DIR__ . '/../magic/auth.php';

$auth = bdr_require_privilege([5, 20], '../logout.php');
$user = $auth['user_id'];
$father = $auth['user_id'];
$branch = $auth['branch'];
$conn = $auth['conn'];
$db = $auth['conn'];
$contract_id = $_REQUEST['id'];
$manager = bdr_user_has_privilege($db, $father, 20) ? 1 : 0;
?>
<!DOCTYPE html>
<html lang="ar" dir="rtl">

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta name="robots" content="index,follow">
	<meta name="author" content="سامح دياب">
	<link rel="icon" href="../images/perfect.ico">
	<title>سداد تشغيل</title>
	<link rel="stylesheet" href="../dist/css/bootstrap.css" />
	<link rel="stylesheet" href="../css/font-awesome.min.css">
	<link rel="stylesheet" href="../hj2/humanity.calendars.picker.css">
	<link rel="stylesheet" href="../css/myCar.css" />
	<script src="../js/jquery.js"></script>
	<script src="../dist/js/umd/popper.min.js"></script>
	<script src="../dist/js/bootstrap.js"></script>
	<script src="../hj2/jquery.plugin.js"></script>
	<script src="../hj2/jquery.calendars.js"></script>
	<script src="../hj2/jquery.calendars.plus.js"></script>
	<script src="../hj2/jquery.calendars.picker.js"></script>
	<script src="../hj2/jquery.calendars.ummalqura.js"></script>
</head>
<style>
	.control-label,
	.table,
	.h6,
	h6 {
		color: #CADDD8;
	}
</style>

<body style="background-color:#051603;">
	<div class="container-fluid fixed-top" style="background-color:black;">
		<br />
		<div class="form-row align-items-center">
			<div class="col-1">
				<button style="margin-right:30px;" type="submit" class="btn btn-outline-danger" id="close2">خروج</button>
			</div>
			<div class="col-11 text-center">
				<h3 style="font-weight:bold; color:#CADDD8;" id="title"> تشغيل عقد رقم <?php echo $contract_id; ?></h3>
			</div>
		</div>
		<?php
		$sql = $db->prepare('SELECT aksat_contract.contract_id, aksat_contract.cust_id2,cars.carid,customers.cust_id ,customers.cust_name,customers.phone , customers.phone2,aksat_contract.tamm_expire , cars.kind ,
			 concat(cars.letters ," ", cars.numbers) as PL , cars.model,aksat_contract.status , aksat_contract.used_area ,aksat_contract.rent_days , aksat_contract.hj
			   FROM cars,aksat_contract,customers
	    WHERE aksat_contract.carid = cars.carid AND aksat_contract.cust_id = customers.cust_id AND aksat_contract.contract_id = ? ');
		$sql->bindParam(1, $contract_id);
		$sql->execute();
		if ($sql->rowCount() > 0) {
			$row = $sql->fetch(PDO::FETCH_ASSOC);
			$date1 = new DateTime($row['tamm_expire']);
			$date2 = new DateTime(date("Y-m-d"));
			$qty   = $date2->diff($date1)->format("%a");
			if ($date1 < $date2) {
				$qty = 3;
			}
			$ddy = date("Y/m/d", strtotime($row['tamm_expire']));
			$cust_id = $row['cust_id'];
		?>
			<div class="form-row" style="margin-top:10px;">
				<div class="col-1">
					<a href="../a/cust_update.php?id=<?php echo $cust_id; ?>" target="_blank"><button type="button" class="btn btn-outline-success btn-sm ">بيانات المستأجر</button></a>
				</div>
				<div class="col-10">
					<center class="control-label" style="font-size:1.3em; padding:0px; margin:0;font-weight:bold; color:#CADDD8;">
						<?php if ($qty > 3) {
							echo $row['cust_name'] . " ~ " . $row['phone'] . " ~ " . $row['phone2'] . " ~ انتهاء تم " . $ddy . " ~ سيارة " . $row['kind'] . " " . $row['PL'] . " موديل " . $row['model'];
						} else {
							echo $row['cust_name'] . " ~" . $row['phone'] . "~" . $row['phone2'] . " ~<span style='color:#DA2D07;font-size:1.3em;'> انتهاء تم " . $ddy . " ~</span>سيارة " . $row['kind'] . " " . $row['PL'] . " موديل " . $row['model'];
						}
						$cust22 = $row['cust_id2'];
						$mycar  = $row['carid'];
						?>
					</center>
				</div>
				<div class="col-1">
					<a href="../a/photo_print.php?carid=<?php echo $row['carid']; ?>" target="_blank"><button type="button" class="btn btn-outline-success btn-sm ">رخصة السيارة</button></a>
				</div>
			</div>
		<?php if ($cust22 > 0) {
				$cust2 = $db->prepare("SELECT cust_id , cust_name , phone , phone2 from customers where cust_id = ?");
				$cust2->bindParam(1, $cust22);
				$cust2->execute();
				if ($cust2->rowCount() > 0) {
					$wow = $cust2->fetch(PDO::FETCH_ASSOC);
					echo '<div class="form-row" style="margin-top:10px;">
			<div class="col-1">
			<a href="../a/cust_update.php?id=' . $cust22 . '" target="_blank"><button type="button" class="btn btn-outline-success btn-sm ">مستأجر إضافي</button></a>
		</div >
			<div class="col-11">
				<center class="control-label" style="font-size:1.3em; font-weight:bold; color:#CADDD8;">';
					echo $wow['cust_name'] . "~" . $wow['phone'] . "~" . $wow['phone2'];
					echo "</center>
			</div>
		</div>
		</div>";
				}
			}
		}

		?>
	</div>
	<p><br /></p>
	<div class="container-fluid" style="margin-top:130px;">
		<div class="form-row">
			<div class="col-2">
				<div class="form-row">
					<div class="col-9">
						<label for="used_area" class="control-label">منطقة الإستخدام</label>
					</div>
					<div class="col-3">
						<button type="button" id="area" class="btn btn-primary" style="padding:5px;background-color:transparent;"><i class="fa fa-tasks" style="font-size: 15px;font-weight:bold;color:#ff9966;" aria-hidden="true"></i></button>
					</div>
				</div>
				<div class="form-row">
					<div class="col-11">
						<input type="text" class="form-control" id="used_area" name="used_area" value="<?php echo $row['used_area']; ?>" />
						<div class="text-danger" style="font-size:12px;" id="used_area_msg"></div>
					</div>
				</div>
				<div class="form-row" style="margin-top:3px;">
					<div class="col-11">
						<input type="hidden" id="hjv" name="hjv" value="<?php echo $row['hj']; ?>" />
						<input type="hidden" id="contract_id" name="contract_id" value="<?php echo $contract_id; ?>" />
						<input type="text" class="form-control" id="rent_days" name="rent_days" value="<?php echo $row['rent_days']; ?>" />
						<div class="text-danger" style="font-size:12px;" id="rent_days_msg"></div>
					</div>
				</div>
				<div class="form-row" style="margin-top:3px;">
					<div class="col-5">
						<label for="wanted" class="control-label">المطلوب</label>
					</div>
					<div class="col-6">
						<input type="number" class="form-control needo " disabled="disabled" readonly />
					</div>
				</div>
				<div class="form-row" style="margin-top:3px;">
					<div class="col-11">
						<button type="button" class="btn btn-outline-primary btn-block" data-toggle="modal" style="float:left;font-weight:bold;" data-backdrop="static" data-keyboard="false" data-target="#tash_in">سداد تشغيل</button>
					</div>
				</div>
				<div class="form-row" style="margin-top:3px;">
					<div class="col-11">
						<button style="font-weight:bold;" type="button" id="contract_update" name="contract_update" class="btn btn-outline-success btn-block">حفظ التعديلات</button>
					</div>
				</div>
				<div class="form-row" style="margin-top:3px;">
					<div class="col-11">
						<button type="button" class="btn btn-outline-dark btn-block" id="add_cust22" data-toggle="modal" style="float:left;font-weight:bold;" data-backdrop="static" data-keyboard="false" data-target="#customer2">إضافة مستأجر</button>
					</div>
				</div>
				<?php
				if ($manager > 0) {
					echo '<div class="form-row" style="margin-top:3px;">
						<div class="col-11">
							<button type="button" style="font-weight:bold;" class="btn btn-outline-danger btn-block" id="contt_close" name="contt_close">إقفال عقد</button>
						</div>
				</div>';
				}
				?>
				<div class="form-row" style="margin-top:3px;">
					<div class="col-11">
						<button type="button" id="rent_print" class="btn btn-outline-success btn-block">طباعة العقد </button>
					</div>
				</div>
				<div class="form-row" style="margin-top:3px;">
					<div class="col-11">
						<a href="aksat_rep.php?id=<?php echo $contract_id; ?>" target="_blank"><button type="button" class="btn btn-outline-success btn-block">كشف حساب </button></a>
					</div>
				</div>
				<div class="form-row" style="margin-top:3px;">
					<div class="col-11">
						<button style="font-weight:bold;" type="button" id="tamm_update" name="tamm_update" class="btn btn-outline-info btn-block">تحديث تم</button>
					</div>
				</div>
				<div class="form-row" style="margin-top:3px;">
					<div class="col-2">
						<label for="tamm" class="control-label">تم</label>
					</div>
					<div class="col-9">
						<input type="number" class="form-control" id="tamm" name="tamm" onchange="ten('tamm')" />
					</div>
					<script>
						function ten(dd) {
							var a = $("#" + dd).val();
							if (a > 175) {
								$("#" + dd).css("background-color", "#FF6B46");
								$("#" + dd).val(175);
							} else {
								$("#" + dd).css("background-color", "");
							}
						};
					</script>
				</div>
				<div class="form-row" style="margin-top:3px;">
					<div class="col-11">
						<input style="font-weight:bold;" type="submit" class="btn btn-outline-danger btn-block" id="close" value=" خروج " />
					</div>
				</div>
			</div>
			<!-- main body start her -->
			<div class="col-10">
				<?php
				$bb = $db->prepare("SELECT sum(wanted) as tot_wanted , sum(payed) as tot_payed from aksat_in where contract_id = ?  LIMIT 1");
				$bb->bindParam(1, $contract_id);
				$bb->execute();
				if ($bb->rowCount() > 0) {
					$bbc = $bb->fetch(PDO::FETCH_ASSOC);
					$tot_wanted     = $bbc["tot_wanted"];
					$tot_payed     = $bbc["tot_payed"];
				}
				?>
				<div class="row" id="msg">
					<!-- //messages -->
				</div>
				<div class="form-row">
					<div class="col-2"></div>
					<div class="col-2"><label class="control-label" style="font-size:1.3em; font-weight:bold; color:#CADDD8;">قيمة السيارة</label></div>
					<div class="col-1"><label class="control-label" style="font-size:1.3em; font-weight:bold; color:#CADDD8;"><?php echo $tot_wanted; ?></label></div>
					<div class="col-1"><label class="control-label" style="font-size:1.3em; font-weight:bold; color:#CADDD8;">المدفوع</label></div>
					<div class="col-1"><label class="control-label" style="font-size:1.3em; font-weight:bold; color:#CADDD8;"><?php echo $tot_payed; ?></label></div>
					<div class="col-3"><label class="control-label" style="font-size:1.3em; font-weight:bold; color:#CADDD8;">المتبقي من قيمة السيارة</label></div>
					<div class="col-1"><label class="control-label" style="font-size:1.3em; font-weight:bold; color:#CADDD8;"><?php echo $tot_wanted - $tot_payed; ?></label></div>
				</div>
				<br />
				<h6>سندات تشغيل</h6>
				<div class="form-row">
					<div class="col">
						<h6></h6>
						<table class="table table-bordered table-hover">
							<thead>
								<tr bgcolor="#1B1C1B">
									<th class="text-center" width="10%">كود</th>
									<th class="text-center" width="10%">المبلغ</th>
									<th class="text-center" width="14%">التاريخ</th>
									<th class="text-center" width="25%">المستلم</th>
									<th class="text-center" width="15%">طريقة الدفع</th>
									<th class="text-center" width="25%">ملاحظات</th>
								</tr>
							</thead>
							<tbody>
								<?php
								$output = '';
								$sql = $db->prepare("SELECT tash_in.tash_id , tash_in.payed , tash_in.date_payed , users.realName , tash_in.payMethod , tash_in.comm
													FROM tash_in , users
													WHERE tash_in.father = users.user_id AND contract_id = ? order by tash_id DESC  ");
								$sql->bindParam(1, $contract_id);
								$sql->execute();
								if ($sql->rowCount() > 0) {
									$ss = 1;
									$sam = 0;
									while ($row = $sql->fetch()) {
										$bb = $row['payMethod'];
										switch ($bb) {
											case "1":
												$pay =  "كاش";
												break;
											case "2":
												$pay = "تحويل الراجحي";
												break;
											case "3":
												$pay = "نقاط بيع";
												break;
											case "5":
													$pay = "تحويل الأهلي";
													break;
										}
										if ($ss == 1) {
											$output .= '<tr>
				 															<td style="background-color:#D8DBD0;"><a  href="../aksat/tash_print.php?sanad=' . $row['tash_id'] . '"target="_blank">' . $row['tash_id'] . '</a></td>';
										} else {
											$output .= '<tr><td>' . $row['tash_id'] . '</td>';
										}
										$output .= '<td>' . $row['payed'] . '</td>
																			<td>' . $row['date_payed'] . '</td>
																			<td>' . $row['realName'] . '</td>
																			<td>' . $pay . '</td>
																			<td>' . $row['comm'] . '</td>
																		</tr>  ';
										$ss = $ss + 1;
										$sam = $sam +	$row['payed'];
									}
								} else {
									$output .= '<tr><td colspan="7">لا يوجد سجلات ...</td></tr>';
								}
								echo $output;
								?>
							</tbody>
							<tfoot></tfoot>
						</table>
					</div>
				</div>
				<h6>سندات أقساط</h6>
				<div class="table-responsive">
					<table class="table table-bordered table-hover">
						<thead>
							<tr bgcolor="#1B1C1B">
								<th class="text-center" width="10%">رقم القسط</th>
								<th class="text-center" width="10%">مبلغ القسط</th>
								<th class="text-center" width="10%">المدفوع</th>
								<th class="text-center" width="12%">من</th>
								<th class="text-center" width="12%">إلي</th>
								<th class="text-center" width="12%">تاريخ السداد</th>
								<th class="text-center" width="10%">تأخير</th>
								<th class="text-center" width="20%">ملاحظات</th>
							</tr>
						</thead>
						<tbody id="myData">
							<?php
							$output = '';
							$sql3 = $db->prepare("SELECT *	FROM aksat_in	WHERE contract_id = ?");
							$sql3->bindParam(1, $contract_id);
							$sql3->execute();
							if ($sql3->rowCount() > 0) {
								$aa = 0;
								$bb = 0;
								while ($do = $sql3->fetch()) {
									$output .= '<tr>
															 <td>' . $do['ak_no'] . '</td>
															 <td>' . $do['wanted'] . '</td>
															 <td>' . $do['payed'] . '</td>
															 <td>' . $do['start'] . '</td>
															 <td>' . $do['endd'] . '</td>
															 <td>' . $do['date_payed'] . '</td>
															 <td>' . $do['late'] . '</td>
															 <td>' . $do['comm'] . '</td>
														 </tr>';
								}
							} else {
								$output .= '<tr><td colspan="8">لا يوجد سجلات ...</td></tr>';
							}
							echo $output;
							?>
						</tbody>
						<tfoot></tfoot>
					</table>
				</div>
				<!-- end main body -->

			</div> <!-- end of invoice table row -->
		</div><!-- end of main row 2 and 10  -->
		<!-- invoice modal -->
		<div class="modal fade" id="tash_in" tabindex="-1" role="dialog" aria-labelledby="exLabel">
			<div class="modal-dialog modal-lg" role="document">
				<div class="modal-content">
					<div class="modal-header" style="background-color:teal ;">
						<div class="col-1 text-right">
							<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span style="color:red;font-size:18px;"><i class="fa fa-times" aria-hidden="true"></i></span></button>
						</div>
						<?php
						$last_kest = $db->prepare("SELECT min(tash_id) as tash_id 
									from tash_in where contract_id = ? AND date_payed IS NULL");
							$last_kest->bindParam(1, $contract_id);
							$last_kest->execute();
					if ($last_kest->rowCount() > 0) {
						$ak = $last_kest->fetch(PDO::FETCH_ASSOC);
							$minID = $ak['tash_id'];}

						$last_kest = $db->prepare("SELECT tash_id , wanted , start
													from tash_in where tash_id = ?");
						$last_kest->bindParam(1, $minID);
						$last_kest->execute();
						if ($last_kest->rowCount() > 0) {
							$ak = $last_kest->fetch(PDO::FETCH_ASSOC);
							$tash_id = $ak['tash_id'];
						}
						?>
						<div class="col-11 text-right">
							<h6 class="modal-title" style="font-weight:bold;color:#FFFAF0; " id="exLabel"> سداد تشغيل</h6>
						</div>
					</div>
					<div class="modal-body">
						<form method="post" class="form-horizontal" id="modal_tash_in">
							<div id="tash_in_msg"></div>
							<div class="form-row">
								<input type="hidden" class="form-control" id="id2" name="id2" />
								<input type="hidden" id="tash_id" name="tash_id" value="<?php echo $tash_id; ?>" />
								<div class="col-1">
									<label for="payed" class="control-label" style="float:left;color:black;">مبلغ التشغيل<span class="text-danger">*</span></label>
								</div>
								<div class="col-2">
									<input type="number" class="form-control" id="payed" name="payed" value="<?php echo $ak['wanted'];  ?>" readonly />
								</div>
								<div class="col-2">
									<label for="printed_no" class="control-label" style="float:left;color:black;">رقم السند</label>
								</div>
								<div class="col-2">
									<input type="text" class="form-control" id="printed_no" name="printed_no" placeholder="المطبوع" />
									<div id="printed_no_msg" class="text-danger" style="font-size:12px;"></div>
								</div>
								<div class="col-2">
									<label for="payMethod" class="control-label " style="float:left;color:black;">طريقة الدفع</label>
								</div>
								<div class="col-2">
									<select class="form-control" id="payMethod" name="payMethod" style="padding:3px;">
										<option selected value="1">كاش</option>
										<option value="2">تحويل بنكي</option>
										<option value="3">نقاط بيع</option>
									</select>
								</div>
							</div>
							<div class="form-row">
								<div class="col-1">
									<label for="comm2" class="control-label" style="color:black;">ملاحظات</label>
								</div>
								<div class="col-10">
									<input type="text" class="form-control" id="comm2" name="comm2" />
									<div id="comm2_msg" class="text-danger" style="font-size:12px;"></div>
								</div>
							</div>
						</form><!-- end of sanad_in form -->
					</div>
					<div class="modal-footer">
						<div class="col-1 text-right">
							<button type="button" class="btn btn-default" data-dismiss="modal">خروج</button>
						</div>
						<div class="col-11 text-right">
							<button type="button" id="tash_in_add" name="tash_in_add" class="btn btn-primary">حفظ <i class="fa fa-cloud" aria-hidden="true"></i></button>
							<a id="tash_print" href="tash_print.php?sanad=<?php echo $tash_id; ?>" target="_blank" style="display: none;"><button type="button" class="btn btn-success">طباعة السند <i class="fa fa-print" aria-hidden="true"></i></button></a>
							<div>
							</div>
						</div>
					</div>
				</div>
			</div><!-- end of sanad_in modal -->
		</div> <!-- modal invoice end -->
		<!-- start add customer2 -->
		<div class="modal fade" id="customer2" tabindex="-1" role="dialog" aria-labelledby="exLabel">
			<div class="modal-dialog modal-lg" role="document">
				<div class="modal-content">
					<div class="modal-header" style="background-color:blue ;">
						<div class="col-1 text-right">
							<button type="button" class="close" data-dismiss="modal" aria-label="Close"><i style="color:red;font-size:18px;" class="fa fa-times" aria-hidden="true"></i></button>
						</div>
						<div class="col-11 text-right">
							<h5 class="modal-title" style="font-weight:bold;color:#F0FFFF; " id="exLabel">إضافة مستاجر</h5>
						</div>
					</div>
					<div class="modal-body">
						<!-- start of add customer2 form -->
						<div class="form-row" style="padding-bottom:20px;">
							<input type="hidden" class="form-control" id="cust_id3" name="cust_id3" />
							<div class="col-4">
								<label for="customer_sr" class="control-label" style="float:left;color:black;">رقم هوية المستاجر<span class="text-danger">*</span></label>
							</div>
							<div class="col-5">
								<input type="number" class="form-control" id="customer_sr" name="customer_sr" placeholder=" رقم الهوية " />
								<div class="text-danger" style="font-size:12px;" id="customer_sr_msg"></div>
							</div>
							<div class="col-3">
								<button type="button" id="cust_sr" class="btn btn-primary">بحث <i style="color:white;" class="fa fa-search" aria-hidden="true"></i></button>
							</div>
						</div>
						<div id="customer2_msg"></div>
						<form method="post" class="form-horizontal" id="modal_customer2" style="display:none;">
							<div class="form-row">
								<div class="col-8">
									<div class="form-row">
										<div class="col-3">
											<label for="cust_name" class="control-label" style="float:left;color:black;">الاسم</label>
										</div>
										<div class="col-6">
											<input type="text" class="form-control" id="cust_name" name="cust_name" placeholder="بدون اسم" />
										</div>
										<div class="col-3">
											<input type="text" class="form-control" id="nation" name="nation" placeholder="مجهول" />
										</div>
									</div>
									<div class="form-row">
										<div class="col-3">
											<label for="idSA" class="control-label" style="float:left;color:black;">رقم الهوية</label>
										</div>
										<div class="col-5">
											<input type="text" class="form-control" id="idSA" name="idSA" />
										</div>
										<div class="col-4">
											<input type="text" class="form-control" id="id_Expire" name="id_Expire" placeholder="بدون" />
										</div>
									</div>
									<div class="form-row">
										<div class="col-3">
											<label for="license" class="control-label" style="float:left;color:black;">الرخصة</label>
										</div>
										<div class="col-5">
											<input type="text" class="form-control" id="license" name="license" placeholder="مجهول" />
										</div>
										<div class="col-4">
											<input type="text" class="form-control" id="lic_expire" name="lic_expire" placeholder="مجهول" />
										</div>
									</div>
									<div class="form-row">
										<div class="col-3">
											<label for="phone" class="control-label" style="float:left;color:black;">جوال</label>
										</div>
										<div class="col-5">
											<input type="text" class="form-control" id="phone" name="phone" placeholder="مجهول" />
										</div>
										<div class="col-4">
											<input type="text" class="form-control" id="phone2" name="phone2" placeholder="مجهول" />
										</div>
									</div>
									<div class="form-row">
										<div class="col-3">
											<label for="comm4" class="control-label" style="float:left;color:black;">ملاحظات</label>
										</div>
										<div class="col-9">
											<input type="text" class="form-control" id="comm4" name="comm4" placeholder="بدون" />
										</div>
									</div>
								</div>
								<div class="col-4">
									<div class="form-row">
										<div class="col-8">
											<input type="text" class="form-control" id="md" name="md" placeholder="بدون مديونية" />
										</div>
										<div class="col-4">
											<input type="text" class="form-control" id="dgree" name="dgree" placeholder="بدون" />
										</div>
									</div>
									<div class="form-row">
										<img id="cust_photo" src="" class="rounded img-fluid img-thumbnail" style="float:left;max-height:50%; max-width:100%;" alt="صورة الرخصة غير متاحة" />
									</div>
								</div>
							</div>
						</form><!-- end of add customer2 form -->
					</div>
					<div class="modal-footer">
						<div class="col-1 text-left">
							<button type="button" class="btn btn-danger" data-dismiss="modal">خروج</button>
						</div>
						<div class="col-11 text-right">
							<button id="cust_add" style="float:right;display:none;" name="cust_add" class="btn btn-primary"> إضافة مستأجر</button>
							<a id="contt_open_print" href="" target="_blank" style="display: none;"><button type="button" class="btn btn-success"> طباعة فتح العقد </button></a>
						</div>
					</div>
				</div>
			</div>
		</div><!-- end of add customer2 modal -->
		<input type="hidden" id="balance" value="<?php echo $ak['wanted'] - $sam; ?>" />
	</div><!-- end of fluid2  -->
	<script>
		$(document).ready(function() {
			$('#close').click(function() {
				close();
			});
			$('#close2').click(function() {
				close();
			});

			$("#rent_print").click(function(event) {
				event.preventDefault();
				var contt = $("#contract_id").val();
				window.open("rent_print.php?contt=" + contt, '_blank');
			});

			$("#tash_in_add").click(function(event) {
				event.preventDefault();
				$("#printed_no_msg").html("");
				$("#comm2_msg").html("");
				var formdata = $("#modal_tash_in").serialize();
				$.ajax({
					url: "../aksat/update_tash.php",
					method: "POST",
					data: formdata,
					success: function(data) {
						if (data.error) {
							$("#printed_no_msg").html(data.error.printed_no);
							$("#comm2_msg").html(data.error.comm);
						} else {
							$('#tash_in_add').css("display", "none");
							$('#tash_print').css("display", "block");
							$("#tash_in_msg").html(data.msg);
							$("#tash_in_msg").delay(3000).slideUp(function() {
								$("#tash_in_msg").html('');
							});
						}
					}
				});
			});
			$('#tash_in').on('show.bs.modal', function(event) {
				var recipient = $("#contract_id").val();
				var modal = $(this);
				modal.find('#id2').val(recipient);
			});

			$("#contract_update").click(function(event) {
				event.preventDefault();
				$("#day_cost_msg").html("");
				$("#kilo_cost_msg").html("");
				$("#free_kilo_msg").html("");
				$("#km_in_msg").html("");
				$("#addtion_cost_msg").html("");
				$("#addtion_desc_msg").html("");
				$("#violations_msg").html("");
				$("#ins_claim_msg").html("");
				$("#comm_msg").html("");
				$.ajax({
					url: "contract_update.php",
					method: "POST",
					data: $("#koki").serialize(),
					dataType: "json",
					success: function(data) {
						if (data.error) {
							$("#day_cost_msg").html(data.error.day_cost);
							$("#kilo_cost_msg").html(data.error.kilo_cost);
							$("#free_kilo_msg").html(data.error.free_kilo);
							$("#km_in_msg").html(data.error.km_in);
							$("#addtion_cost_msg").html(data.error.addtion_cost);
							$("#addtion_desc_msg").html(data.error.addtion_desc);
							$("#violations_msg").html(data.error.violations);
							$("#ins_claim_msg").html(data.error.ins_claim);
							$("#comm_msg").html(data.error.comm);
						} else {
							$("#koki_msg").html(data.msg);
							$("#koki_msg").delay(3000).slideUp(function() {
								$("#koki_msg").html('');
							});
							location.reload();
						}
					}
				});
			});

			$("#tamm_update").click(function(event) {
				event.preventDefault();
				var tamm = $('#tamm').val();
				var contract_id = $("#contract_id").val();
				$.ajax({
					url: "update_aksat.php",
					method: "POST",
					data: {
						tamm: tamm,
						contract_id: contract_id
					},
					dataType: "json",
					success: function(data) {
						location.reload();
					}
				});
			});

			$("#area").click(function(event) {
				event.preventDefault();
				var contract_id = $("#contract_id").val();
				var used_area = $('#used_area').val();
				var rent_days = $('#rent_days').val();
				$("#rent_days_msg").html("");
				$("#used_area_msg").html("");
				$.ajax({
					url: "update_aksat.php",
					method: "POST",
					data: {
						used_area: used_area,
						rent_days: rent_days,
						contract_id: contract_id
					},
					dataType: "json",
					success: function(data) {
						if (data.error) {
							$("#used_area_msg").html(data.error.used_area);
							$("#rent_days_msg").html(data.error.rent_days);
						} else {
							location.reload();
						}
					}
				});
			});

			$("#area_print").click(function(event) {
				event.preventDefault();
				var contt = $("#contract_id").val();
				window.open("print2.php?contt=" + contt, '_blank');
			});

			$("#contt_close").click(function(event) {
				event.preventDefault();
				var carid = "<?php echo $mycar; ?>";
				var contt = $("#contract_id").val();
				$.ajax({
					url: "update_aksat.php",
					method: "POST",
					data: {
						contt: contt,
						carid: carid
					},
					success: function(data) {
						$("#msg").html(data);
						$("#msg").delay(7000).slideUp(function() {
							$("#koki_msg").html('');
						});
					}
				});
			});
			$("#cust_sr").click(function(event) {
				event.preventDefault();
				var idSA = $("#customer_sr").val();
				$("#customer_sr_msg").html("");
				$.ajax({
					url: "../actions/cust_single.php",
					method: "POST",
					data: {
						idSA: idSA
					},
					dataType: "json",
					success: function(data) {
						if (data.error) {
							$("#customer_sr_msg").html(data.error.msg);
						} else {
							$('#modal_customer2').css('display', 'block');
							$('#cust_add').css('display', 'block');
							$("#cust_id3").val(data.cust_id);
							$("#dgree").val(data.dgree);
							$("#cust_name").val(data.cust_name);
							$("#nation").val(data.nation);
							$("#idSA").val(data.idSA);
							$("#id_Expire").val(data.id_Expire);
							$("#license").val(data.license);
							$("#lic_expire").val(data.lic_expire);
							$("#phone").val(data.phone);
							$("#phone2").val(data.phone2);
							$("#comm4").val(data.comm);
							$('#cust_photo').attr("src", "../photo/customers/" + (data.cust_photo));
							$("#customer2_msg").html(data.notfound);
							$("#customer2_msg").delay(6000).slideUp(function() {
								$("#customer2_msg").html('');
							});
						}
					}
				});
			}); //end of customer2 search

			$("#cust_add").click(function(event) {
				event.preventDefault();
				$("#cust_add").prop("disabled", true); //
				$("#cust_add").delay(2000).slideUp(function() {
					$("#cust_add").prop("disabled", false);
				});
				var cust_id2 = $("#cust_id3").val();
				var contract_id = $("#contract_id").val();
				$("#customer_sr_msg").html("");
				$.ajax({
					url: "contract_update.php",
					method: "POST",
					data: {
						cust_id2: cust_id2,
						contract_id: contract_id
					},
					dataType: "json",
					success: function(data) {
						$("#customer2_msg").html(data.msg);
						$('#cust_add').css('display', 'none');
						$('#contt_open_print').css('display', 'block');
						$('#contt_open_print').attr('href', 'print2.php?contt=' + data.contract_id);
						$("#customer2_msg").delay(4000).slideUp(function() {
							$("#customer2_msg").html('');
						});
					}
				});
			}); //end of add customer2

			function convertDates(name, ss, dest) {
				var date = $('#' + ss).val().split('/');
				var year = parseInt(date[0], 10);
				if (name == 'ummalqura') {
					if (year > 1800) {
						return false;
					}
				} else if (name == 'gregorian') {
					if (year < 1800) {
						return false;
					}
				}
				var month = parseInt(date[1], 10);
				var day = parseInt(date[2], 10);
				try {
					var jd = $.calendars.instance(name).newDate(year, month, day).toJD();
				} catch (e) {
					alert(e);
					return;
				}
				name == 'ummalqura' ? showDate('gregorian', jd, dest) : showDate('ummalqura', jd, dest);
			}

			function showDate(name, jd, dest) {
				try {
					$('#' + dest).val('');
					var date = $.calendars.instance(name).fromJD(jd);
					$('#' + dest).val(date.formatYear() + '/' + date.month() + '/' + date.day());
				} catch (e) {
					alert(e);
					return null;
				}
			}
			function change_date(date) {
				var date = date.split('-');
				var year = parseInt(date[0], 10);
				var name = 'ummalqura';
				if (year > 1800) {
					name = 'gregorian';
				}
				var month = parseInt(date[1], 10);
				var day = parseInt(date[2], 10);
				try {
					var jd = $.calendars.instance(name).newDate(year, month, day).toJD();
				} catch (e) {
					day = day - 1;
					try {
						var jd = $.calendars.instance(name).newDate(year, month, day).toJD();
					} catch (e) {
						day = day - 1;
						try {
							var jd = $.calendars.instance(name).newDate(year, month, day).toJD();
						} catch (e) {
							alert(e);
							return;
						}
					}
				}
				name == 'ummalqura' ? name = 'gregorian' : name = 'ummalqura';
				var re_date = $.calendars.instance(name).fromJD(jd);
				try {
					re_date = re_date.formatYear() + '-' + re_date.month() + '-' + re_date.day();
				} catch (e) {
					alert(e);
					return null;
				}
				return re_date;
			}

			function add_date(year, month, day) {
				var name = 'ummalqura';
				if (year > 1800) {
					name = 'gregorian';
				}
				try {
					var jd = $.calendars.instance(name).newDate(year, month, day).toJD();
				} catch (e) {
					day = day - 1;
					try {
						var jd = $.calendars.instance(name).newDate(year, month, day).toJD();
					} catch (e) {
						day = day - 1;
						try {
							var jd = $.calendars.instance(name).newDate(year, month, day).toJD();
						} catch (e) {
							alert(e);
							return;
						}
					}
				}
				var re_date = $.calendars.instance(name).fromJD(jd);
				try {
					re_date = re_date.formatYear() + '-' + re_date.month() + '-' + re_date.day();
				} catch (e) {
					alert(e);
					return null;
				}
				return re_date;
			}
			var ss = $("#hjv").val();
			if (ss == 1) {
				$('.hj1').each(function() {
					var dd = $(this).val();
					dd = change_date(dd);
					$(this).val(dd);
				});

				$('.hj2').each(function() {
					var dd = $(this).val();
					dd = change_date(dd);
					$(this).val(dd);
				});

				$('#fix').css('display', 'block');
			}
			$("#fix").click(function(event) {
				event.preventDefault();
				var date = $("#hj").val();
				date = date.split('-');
				var year = parseInt(date[0], 10);
				var month = parseInt(date[1], 10);
				var day = parseInt(date[2], 10);
				$('.hj1').each(function() {
					var dd = add_date(year, month, day);
					$(this).val(dd);
					month = month + 1;
					if (month == 13) {
						month = 1;
						year = year + 1;
					}
				});
				var date = $("#hjd").val();
				date = date.split('-');
				var year = parseInt(date[0], 10);
				var month = parseInt(date[1], 10);
				var day = parseInt(date[2], 10);
				$('.hj2').each(function() {
					var dd = add_date(year, month, day);
					$(this).val(dd);
					month = month + 1;
					if (month == 13) {
						month = 1;
						year = year + 1;
					}
				});
				$('.hj1').each(function() {
					var dd = $(this).val();
					dd = change_date(dd);
					$(this).val(dd);
				});
				$('.hj2').each(function() {
					var dd = $(this).val();
					dd = change_date(dd);
					$(this).val(dd);
				});
				$.ajax({
					url: "update_aksat.php",
					method: "POST",
					data: $("#update_aksat").serialize(),
					success: function(inv) {
						alert(inv);
					},
					error: function(result) {
						alert(result.statusText);
					}
				});
			}); //end of add matrial invoice ajax code

		});
	</script>

</body>

</html>
