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

$auth = bdr_require_privilege(1, '../logout.php');
$user = $auth['user_id'];
$father = $auth['user_id'];
$branch = $auth['branch'];
$conn = $auth['conn'];
$db = $auth['conn'];

$contract_id = $_REQUEST['id'];
require_once "../magic/Whats.php";


$whats = new Whats();
// $whats->sendNextMessage();

$branchCONT = $conn->prepare('SELECT printed,father,cont_status FROM contracts WHERE branch = ? AND contract_id = ?');
$branchCONT->bindParam(1, $branch);
$branchCONT->bindParam(2, $contract_id);
$branchCONT->execute();
if ($branchCONT->rowCount() < 1) {
	header("location:../logout.php");
} else {
	$ddy = $branchCONT->fetch(PDO::FETCH_ASSOC);
	if ($ddy['cont_status'] > 1) {
		// if ($ddy['printed'] > 0) {
		// 	header("location:cont2debt.php?id=$contract_id");
		// }
		header("location:cont2debt.php?id=$contract_id");
	}
}
?>
<!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> عقد <?php echo $contract_id; ?></title>
	<link rel="stylesheet" href="../dist/css/bootstrap.css" />
	<link rel="stylesheet" href="../css/all.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>

<body style="background-color:#051603;">
	<style>
		.control-label,
		.table,
		.h6,
		h6 {
			color: #A9A9A7;
		}

		a {
			font-size: 1.1em;
			font-weight: bold;
			color: #EDDFDF;
		}

		a:hover {
			color: green;
		}

		.error-message {
			color: #dc3545;
			font-size: 0.875rem;
			margin-top: 0.25rem;
			display: none;
		}

		.error-message.active {
			display: block;
		}
	</style>
	<div class="container-fluid fixed-top" style="background-color:black;">
		<br />
		<div class="form-row align-items-center">
			<div class="col-2">
				<button style="margin-right:30px;" type="submit" class="btn btn-outline-danger" id="close2">خروج</button>
			</div>
			<div class="col-8 text-center">
				<h3 style="font-weight:bold; color:#CADDD8;">متابعة عقد رقم <?php echo $contract_id; ?></h3>
			</div>
			<div class="col-1">
				<!-- <button id="media" class="btn btn-outline-primary btn-block">طلب تصوير</button> -->
			</div>
			<div class="col-1">
				<button style="margin-right:15px;" type="submit" class="btn btn-outline-danger" id="check">تشييك</button>
			</div>
		</div>
		<?php
		$sql = $conn->prepare('SELECT contracts.contract_id,contracts.cust_id2,cars.carid,customers.cust_id ,customers.cust_name,customers.phone , customers.phone2,contracts.tamm_expire , cars.kind ,
				 concat(cars.letters ," ", cars.numbers) as PL , cars.model,cars.color,contracts.cont_status
			   FROM cars,contracts,customers
	    		WHERE contracts.carid = cars.carid AND contracts.cust_id = customers.cust_id 
				AND contracts.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'];
			$phone = $row['phone'];
			$phone2 = $row['phone2'];
			$carid = $row['carid'];
			$whats = substr($phone, 1);
			$whats = "9660" . $whats;
			$whats2 = "";
			if (!empty($phone2)) {
				$whats2 = substr($phone2, 1);
				$whats2 = "9660" . $whats2;
			}
			$gpsid = 0;
			$gp = $conn->prepare("SELECT gpsid from naql where carid = $carid LIMIT 1");
			$gp->execute();
			if ($gp->rowCount() > 0) {
				$gps = $gp->fetch(PDO::FETCH_ASSOC);
				$gpsid = $gps['gpsid'];
			}
		?>
			<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
						$car_info = $row['kind'] . " " . $row['PL'] . " " . $row['color'] . " " . $row['model'];
						if ($qty > 3) {
							echo $row['cust_name'] . " ~<a href='https://api.whatsapp.com/send?phone=$whats' target='_blank'>" . $phone . "</a>~ <a href='https://api.whatsapp.com/send?phone=$whats2' target='_blank'>" . $phone2 . "</a>~ انتهاء تم " . $ddy . "~ سيارة " . $car_info;
						} else {
							echo $row['cust_name'] . " ~<a href='https://api.whatsapp.com/send?phone=$whats' target='_blank'>" . $phone . "</a>~ <a href='https://api.whatsapp.com/send?phone=$whats2' target='_blank'>" . $phone2 . "</a>~<span style='color:#DA2D07;font-size:1.3em;'> انتهاء تم " . $ddy . "~ سيارة " . $car_info;
						}
						$cust22 = $row['cust_id2'];
						?>
					</center>
				</div>
				<div class="col-1">
					<a href="../rent/car_contracts.php?dd=<?php echo $carid; ?>" target="_blank"><button type="button" class="btn btn-outline-success btn-sm ">عقود السيارة</button></a>
				</div>
			</div>
		<?php if ($cust22 > 0) {
				$cust2 = $conn->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);
					$ph1 = $wow['phone'];
					$ph2  = $wow['phone2'];
					$wh1 = substr($ph1, 1);
					$wh1 = "9660" . $wh1;
					$wh2 = "";
					if (!empty($ph2)) {
						$wh2 = substr($ph2, 1);
						$wh2 = "9660" . $wh2;
					}
					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'] . "~ <a href='https://api.whatsapp.com/send?phone=$wh1' target='_blank'>" . $ph1 . "</a>~<a href='https://api.whatsapp.com/send?phone=$wh2' target='_blank'>" . $ph2 . "</a>";
					echo "</center>
			</div>
		</div>";
				}
			}
		}
		$debt = $conn->prepare("SELECT sum(wanted) as debt from contracts where cust_id = ?  LIMIT 1");
		$debt->bindParam(1, $cust_id);
		$debt->execute();
		if ($debt->rowCount() > 0) {
			$monn = $debt->fetch(PDO::FETCH_ASSOC);
			$debt     = $monn["debt"];
		}
		?>
	</div>
	<p><br /></p>
	<div class="container-fluid" style="margin-top:130px;">
		<div class="form-row">
			<div class="col-2">
				<div class="form-row">
					<input type="hidden" id="car_info" value="<?php echo $car_info; ?>" />
					<div class="col-9">
						<label for="used_area" class="control-label">منطقة الإستخدام</label>
					</div>
					<div class="col-3">
						<button type="button" id="areaBtn" 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>
						<button type="button" id="area_print" class="btn btn-success" style="padding:5px;background-color:transparent;display:none;"><i class="fa fa-print" style="font-size:15px;font-weight:bold;color:#AFF966;" aria-hidden="true"></i></button>
					</div>
				</div>
				<div class="form-row">
					<div class="col">
						<input type="text" class="form-control" id="used_area" name="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">
						<input type="text" class="form-control" id="rent_days" name="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">
						<input type="text" class="form-control" id="tamm" name="tamm"/>
					</div> -->
					<div class="col">
						<input type="text" class="form-control" id="tamm" name="tamm" placeholder="عقد إلكتروني 15 رقم" />
						<div id="errorMessage" class="error-message text-danger">خطأ في رقم العقد لابد أن يكون 15 رقم </div>
					</div>
				</div>
				<script>
					const $input = $('#tamm');
					const $error = $('#errorMessage');

					// Listen to input event for live validation
					$input.on('input', function() {
						const value = $(this).val().trim();

						// Show error if there's input but less than 15 characters
						if (value.length > 0 && value.length < 15) {
							$error.addClass('active');
						} else {
							$error.removeClass('active');
						}
					});
				</script>
				<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 wanted" 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="#sanad_in">سند قبض</button>
					</div>
				</div>
				<div class="form-row" style="margin-top:3px;">
					<div class="col-11">
						<button type="button" class="btn btn-outline-danger btn-block" id="sanad_out_btn" data-toggle="modal" style="float:left;font-weight:bold;" data-backdrop="static" data-keyboard="false" data-target="#sanad_out">سند صرف</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>
				<div class="form-row" style="margin-top:3px;">
					<div class="col-11">
						<button type="button" style="font-weight:bold; display:none ;" 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">
						<a id="print3" href="" 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">
						<a id="acc" href="" 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_expire" class="control-label">تم</label>
					</div>
					<script>
						$(function() {
							var calendar = $.calendars.instance();
							$('#tamm_expire').calendarsPicker({
								calendar: calendar
							});
						});
					</script>
					<div class="col-9">
						<input type="text" class="form-control" id="tamm_expire" name="tamm_expire" onchange="mkhh('tamm_expire')" placeholder="تحديث تم" />
						<div id="tamm_msg"></div>
					</div>
				</div>
				<!-- <div class="form-row" style="margin-top:3px;">
					<div class="col-11">
						<button class="btn btn-outline-success btn-block sms" smsKind="2" sanad='1'> طلب دفعة <i class="fas fa-sms"></i></button>
					</div>
				</div>
				<div class="form-row" style="margin-top:3px;">
					<div class="col-11">
						<button class="btn btn-outline-danger btn-block sms" smsKind="3" sanad='1'> تغيير زيت <i class="fas fa-sms"></i></button>
					</div>
				</div>
				<div class="form-row" style="margin-top:3px;">
					<div class="col-11">
						<button class="btn btn-outline-primary btn-block sms" smsKind="4" sanad='1'> مراجعة الفرع <i class="fas fa-sms"></i></button>
					</div>
				</div> -->
				<div class="form-row" style="margin-top:3px;">
					<div class="col-11">
						<a id="car_check" href="" 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">
						<input style="font-weight:bold;" type="submit" class="btn btn-outline-danger btn-block" id="close" value=" خروج " />
					</div>
				</div>
			</div>
			<div class="col-10">
				<form id="koki" method="post" class="form-horizontal" style="float:center;font-weight: bold; font-size: 16px; color: #000;">
					<div id="koki_msg"></div>
					<input type="hidden" id="contract_id" name="contract_id" value="<?php echo $contract_id; ?>" />
					<input type="hidden" id="km3" name="km3" />
					<input type="hidden" id="free_dd" name="free_dd" />
					<input type="hidden" id="cont_status" name="cont_status" />
					<input type="hidden" id="carid" name="carid" value="<?php echo $carid; ?>" />
					<input type="hidden" id="branch" name="branch" value="<?php echo $branch; ?>" />
					<input type="hidden" id="gpsid" name="gpsid" value="<?php echo $gpsid; ?>" />

					<div class="form-row">
						<div class="col-2"></div>
						<div class="col-2">
							<label for="date_in" class="control-label text-center">الخروج</label>
						</div>
						<div class="col-2">
							<label class="control-label text-center">الدخول</label>
						</div>
						<div class="col-1">
							<label class="control-label text-center">عدد</label>
						</div>
						<div class="col-1">
							<label class="control-label text-center">سعر</label>
						</div>
						<div class="col-2">
							<label for="days_cost" class="control-label text-center">إجمالي</label>
						</div>
						<div class="col-2 text-center">
							<label class="control-label  text-danger"><?php echo $debt; ?></label>
						</div>
					</div>
					<div class="form-row">
						<div class="col-2">
							<label for="date_in" class="control-label" style="float:left;">التاريخ</label>
						</div>
						<div class="col-2">
							<input type="text" class="form-control" id="date_out" name="date_out" disabled="disabled" readonly />
						</div>
						<div class="col-2">
							<input type="text" class="form-control" id="date_in" name="date_in" disabled="disabled" readonly />
						</div>
						<div class="col-1">
							<input type="number" class="form-control" id="days_qty" name="days_qty" disabled="disabled" readonly />
						</div>
						<div class="col-1">
							<input type="number" onchange="money()" class="form-control" id="day_cost" name="day_cost" readonly />
							<div class="text-danger" style="font-size:12px;" id="day_cost_msg"></div>
						</div>
						<div class="col-2">
							<input type="number" class="form-control" id="days_cost" name="days_cost" disabled="disabled" readonly />
						</div>
						<div class="col-2">
							<label for="free_kilo" class="control-label">الكيلو المسموح</label>
						</div>
					</div>
					<div class="form-row">
						<div class="col-2">
							<label for="km_in" class="control-label" style="float:left;">الكيلو</label>
						</div>
						<div class="col-2">
							<input type="number" class="form-control" id="km_out" name="km_out" disabled="disabled" readonly />
						</div>
						<div class="col-2">
							<input type="number" onchange="money()" class="form-control" id="km_in" name="km_in" />
							<div class="text-danger" style="font-size:12px;" id="km_in_msg"></div>
						</div>
						<div class="col-1">
							<input type="number" class="form-control" id="kilo_qty" name="kilo_qty" disabled="disabled" readonly />
						</div>
						<div class="col-1">
							<input type="number" onchange="money()" class="form-control" id="kilo_cost" name="kilo_cost" />
							<div class="text-danger" style="font-size:12px;" id="kilo_cost_msg"></div>
						</div>
						<div class="col-2">
							<input type="number" class="form-control" id="kilos_cost" name="kilos_cost" disabled="disabled" readonly />
						</div>
						<div class="col-2">
							<input type="number" onchange="money()" class="form-control" id="free_kilo" name="free_kilo" />
							<div class="text-danger" style="font-size:12px;" id="free_kilo_msg"></div>
						</div>
					</div>
					<div class="form-row">
						<div class="col-2">
							<label for="date_in" class="control-label" style="float:left;">الساعة</label>
						</div>
						<div class="col-2">
							<input type="text" class="form-control" id="time_out" name="time_out" readonly />
						</div>
						<div class="col-2">
							<input type="text" class="form-control" id="time_in" name="time_in" readonly />
						</div>
						<div class="col-1">
							<input type="number" class="form-control" id="hour_qty" name="hour_qty" readonly />
						</div>
						<div class="col-1">
							<input type="number" class="form-control" id="hour_cost" name="hour_cost" readonly />
						</div>
						<div class="col-2">
							<input type="number" class="form-control" id="hours_cost" name="hours_cost" readonly />
						</div>
						<div class="col-2">
							<input type="number" class="form-control" id="used_kilo" name="used_kilo" readonly />
						</div>
					</div>
					<br />
					<div class="form-row">
						<div class="col-7">
							<div class="form-row">
								<div class="col-3">
									<label for="addtion_desc" class="control-label" style="float:left;">تعويضات</label>
								</div>
								<div class="col-9">
									<textarea class="form-control" rows="3" id="addtion_desc" name="addtion_desc" placeholder="بيان الإضافي و التعويضات و المخالفات"></textarea>
								</div>
							</div>
							<div class="form-row">
								<div class="col-3">
									<label for="comm" class="control-label" style="float:left;">ملاحظات</label>
								</div>
								<div class="col-9">
									<textarea type="text" class="form-control" rows="2" id="comm" name="comm" placeholder=" ملاحظات العقد"></textarea>
								</div>
							</div>
							<div class="form-row">
								<div class="col-3">
									<label for="dept_comm" class="control-label" style="float:left;">الإدارة</label>
								</div>
								<div class="col-9">
									<textarea class="form-control" rows="4" id="dept_comm" name="dept_comm" disabled="disabled" readonly placeholder="تعليمات الإدارة"></textarea>
								</div>
							</div>
							<div class="form-row">
								<div class="col-3">
									<label for="operating_comm" class="control-label" style="float:left;">التشغيل</label>
								</div>
								<div class="col-9">
									<textarea class="form-control" rows="4" id="operating_comm" name="operating_comm" disabled="disabled" readonly placeholder="تعليمات التشغيل"></textarea>
								</div>
							</div>
						</div>
						<br />
						<div class="col-5">
							<div class="form-row">
								<div class="col-2" style="margin-right:16px;">
									<label for="addtion_cost" class="control-label" style="float:left;">تعويضات</label>
								</div>
								<div class="col-4">
									<input type="number" class="form-control" onchange="money()" style="float:right;" id="addtion_cost" name="addtion_cost" />
									<div class="text-danger" style="font-size:12px;" id="addtion_cost_msg"></div>
								</div>
								<div class="col-2">
									<!-- <label for="gpsKM" class="control-label text-primary">فحص</label> -->
								</div>
								<div class="col-3">
									<!-- <label for="" class="control-label  text-danger" id="gpsKM">0</label> -->
								</div>
							</div>
							<div class="form-row">
								<div class="col-2" style="margin-right:16px;">
									<label for="violations" class="control-label" style="float:left;">مخالفات</label>
								</div>
								<div class="col-4">
									<input type="number" class="form-control" onchange="money()" id="violations" name="violations" />
									<div class="text-danger" style="font-size:12px;" id="violations_msg"></div>
								</div>
								<div class="col-2">
									<!-- <label for="maxKM" class="control-label text-primary">اعلي</label> -->
								</div>
								<div class="col-3">
									<!-- <label for="" class="control-label  text-danger" id="maxKM">0</label> -->
								</div>
							</div>
							<div class="form-row">
								<div class="col-2" style="margin-right:16px;">
									<label for="ins_claim" class="control-label" style="float:left;">تأمينات</label>
								</div>
								<div class="col-4">
									<input type="number" class="form-control" id="ins_claim" name="ins_claim" disabled="disabled" readonly />
								</div>
								<div class="col-1"></div>
								<div class="col-4">
									<!-- <button id="gps" onclick="gpsCheck()" name="gps" class="btn btn-primary btn-block">فحص الكيلومتر</button> -->
								</div>
							</div>
							<div class="form-row">
								<div class="col-2" style="margin-right:16px;">
									<label for="wesam_ins" class="control-label" style="float:left;">تأمين ذهبي</label>
								</div>
								<div class="col-4">
									<input type="number" class="form-control" id="wesam_ins" name="wesam_ins" disabled="disabled" readonly />
								</div>
								<div class="col-2">
									<label for="open_kilo" class="control-label">كيلو مفتوح</label>
								</div>
								<div class="col-3">
									<input type="number" class="form-control" id="open_kilo" name="open_kilo" readonly />
								</div>
							</div>
							<div class="form-row">
								<div class="col-2" style="margin-right:16px;">
									<label for="deduction" class="control-label text-danger" style="float:left;">الخصم</label>
								</div>
								<div class="col-4">
									<input type="number" class="form-control" onchange="money()" id="deduction" name="deduction" />
									<div class="text-danger" style="font-size:12px;" id="deduction_msg"></div>
								</div>
							</div>
							<div class="form-row">
								<div class="col-2" style="margin-right:16px;margin-left:0px;">
									<label for="net_cost" class="control-label text-primary" style="float:left;">إجمالي إيجار</label>
								</div>
								<div class="col-4">
									<input type="number" class="form-control" id="net_cost" name="net_cost" disabled="disabled" readonly />
								</div>
								<div class="col-2">
									<!-- <label for="vat_req" class="control-label">5% ضريبة</label> -->
								</div>
								<div class="col-3">
									<input type="hidden" class="form-control" id="vat_req" name="vat_req" disabled="disabled" readonly />
									<!-- <input type="number" class="form-control" id="vat5" name="vat5" disabled="disabled" readonly /> -->
								</div>
							</div>
							<div class="form-row">
								<div class="col-2" style="margin-right:16px;padding:0px;padding-top:8px;">
									<label for="vat15" class="control-label text-danger" style="float:left;">ضريبة %15</label>
								</div>
								<div class="col-4">
									<input type="number" class="form-control" id="vat15" name="vat15" disabled="disabled" readonly />
								</div>
							</div>
							<div class="form-row">
								<div class="col-2" style="margin-right:16px;">
									<label for="after_vat" class="control-label text-success" style="float:left;">الإجمالي</label>
								</div>
								<div class="col-4">
									<input type="number" class="form-control" id="after_vat" name="after_vat" disabled="disabled" readonly />
								</div>
							</div>
							<div class="form-row">
								<div class="col-2" style="margin-right:16px;">
									<label for="payed" class="control-label" style="float:left;">المدفوع</label>
								</div>
								<div class="col-4">
									<input type="number" class="form-control" id="payed" name="payed" disabled="disabled" readonly />
								</div>
								<div class="col-2">
									<label for="total_in" class="control-label">قبض</label>
								</div>
								<div class="col-3">
									<input type="number" class="form-control" id="total_in" name="total_in" disabled="disabled" readonly />
								</div>
							</div>
							<div class="form-row">
								<div class="col-2" style="margin-right:16px;">
									<label for="wanted" class="control-label" style="float:left;">المطلوب </label>
								</div>
								<div class="col-4">
									<input type="number" class="form-control wanted" id="wanted" readonly />
								</div>
								<div class="col-2">
									<label for="total_out" class="control-label">صرف</label>
								</div>
								<div class="col-3">
									<input type="number" class="form-control" id="total_out" name="total_out" disabled="disabled" readonly />
								</div>
							</div>
						</div>
					</div>
					<div class="form-row">
						<div class="col-2">
							<label for="open_comm" class="control-label" style="float:left;">ملاحظات فتح العقد</label>
						</div>
						<div class="col-10">
							<input class="form-control" id="open_comm" disabled="disabled" readonly placeholder="عند فتح العقد" readonly />
						</div>
					</div>
					<p><br /></p>
					<script>
						function money() {
							var a = parseFloat($("#day_cost").val()) || 0;
							var b = parseInt($("#days_qty").val()) || 0;
							$("#days_cost").val((a * b).toFixed(2));
							var hqty = parseInt($("#hour_qty").val()) || 0;
							var hprice = (a / 12);
							hprice = hprice.toFixed(2);
							$("#hour_cost").val(hprice);
							var hmoney = 0;
							if (hqty > 1) {
								if (hqty < 6) {
									hmoney = (hqty - 1) * (a / 12);
								} else {
									hmoney = a;
								}
							}
							hmoney = hmoney.toFixed(2);

							$("#hours_cost").val(hmoney);
							var h = parseFloat($("#hours_cost").val()) || 0;
							var d = parseFloat($("#addtion_cost").val()) || 0;
							d = parseFloat((d).toFixed(2));
							var v = parseInt($("#violations").val()) || 0;
							var i = parseInt($("#ins_claim").val()) || 0;
							mykilo(h, a);
							var k = parseInt($("#kilos_cost").val()) || 0;
							var open_kilo = parseFloat($("#open_kilo").val()) || 0;
							var wesam_ins = parseFloat($("#wesam_ins").val()) || 0;
							var dd = ddy() || 0;
							$("#net_cost").val((((a * b) + h + d + v + i + k + open_kilo + wesam_ins) - dd).toFixed(2));
							var nn = parseFloat($("#net_cost").val()) || 0;
							var vat15 = nn * 0.15;
							$("#vat15").val(vat15);
							var vat_req = parseFloat(vat15.toFixed(2));
							$("#vat_req").val(vat_req);
							var after_vat = nn + vat_req;
							$("#after_vat").val(after_vat);
							var mm = parseFloat($("#payed").val()) || 0;
							mm = parseFloat((mm).toFixed(2));
							var wanted = parseFloat((after_vat - mm).toFixed(2));
							$(".wanted").val(wanted);
							if (wanted > 200) {
								$(".wanted").css("background-color", "#ffcccc");
							} else if (wanted < 0) {
								$(".wanted").css("background-color", "#ccffcc");
							} else {
								$(".wanted").css("background-color", "");
							}
						};

						function mykilo(hh, aa) {
							var free = 0;
							var a = parseInt($("#km_out").val());
							var b = parseInt($("#km_in").val()) || 0;
							if ((b - a) > 0) {
								$("#used_kilo").val(b - a);
							} else {
								$("#used_kilo").val(0);
							}
							var s = mykm(a);
							var d = parseInt($("#days_qty").val()) || 0;
							var f = parseInt($("#free_kilo").val()) || 250;
							if (hh == aa) {
								free = (1 + d) * f;
							} else {
								free = f * d;
							}
							if (free > (b - a)) {
								$("#kilos_cost").val(0);
								$("#kilo_qty").val(0);
							} else {
								var koky = ((b - (a + free)) * s) / 100;
								koky = Math.trunc(koky);
								$("#kilos_cost").val(koky);
								$("#kilo_qty").val(b - (a + free));
							}
						};

						function ddy() {
							var dd = parseFloat($("#deduction").val()) || 0;
							dd = parseFloat((dd).toFixed(2));
							var free_dd = parseInt($("#free_dd").val()) || 0;
							if (dd > free_dd) {
								$("#deduction").val(free_dd).css("background-color", "#ffcccc");
								return free_dd;
							} else if (dd == free_dd) {
								$("#deduction").css("background-color", "#ffcccc");
								return free_dd;
							} else if (dd < 0) {
								$("#deduction").val(0).css("background-color", "");
								return 0;
							} else {
								$("#deduction").val(dd).css("background-color", "");
								return dd;
							}
						};

						function mykm(out) {
							var a = parseInt($("#kilo_cost").val()) || 0;
							var inn = parseInt($("#km_in").val()) || 0;
							var b = parseInt($("#km3").val()) || 0;
							var open_kilo = parseFloat($("#open_kilo").val()) || 0;

							var cont_status = parseInt($("#cont_status").val()) || 0;
							if (inn != "") {
								if (cont_status == 1) {
									document.getElementById("contt_close").style.display = "block";
								}
								if (out >= inn) {
									$("#km_in").val(out).css("background-color", "#ffcccc");
									alert("يوجد خطأ بعداد الدخول ..... راجع من فضلك !!!");
									$("#contt_close").css("display", "none");
								} else {
									$("#km_in").val(inn).css("background-color", "");
								}
							}
							if (open_kilo > 0) {
								$("#kilo_cost").val(0).css("background-color", "#2ECC71");
							} else {
								if (a < b) {
									$("#kilo_cost").val(b).css("background-color", "#ffcccc");
									return b;
								} else if (a == b) {
									$("#kilo_cost").css("background-color", "#ffcccc");
									return a;
								} else {
									$("#kilo_cost").css("background-color", "");
									return a;
								}
							}

						}

						function mkhh(ss) {
							var date = $('#' + ss).val().split('/');
							var year = parseInt(date[2], 10);
							if (year < 100) {
								year = year + 1400;
							}
							var month = parseInt(date[1], 10);
							if (month > 12) {
								month = 12;
							} else if (month < 1) {
								month = 1;
							}
							var day = parseInt(date[0], 10);
							if (day > 29) {
								day = 29;
							} else if (day < 1) {
								day = 1;
							}
							$('#' + ss).val(year + "/" + month + "/" + day);
						}
					</script>
				</form>
				<div id="msg"></div>
			</div>
		</div><!-- end of main row 2 and 10  -->
		<div class="form-row">
			<div class="col-6">
				<h6>سندات القبض</h6>
				<table class="table table-bordered table-hover">
					<thead>
						<tr>
							<th class="text-center" width="6%">م</th>
							<th class="text-center" width="10%">المبلغ</th>
							<th class="text-center" width="18%">التاريخ</th>
							<th class="text-center" width="9%">كود</th>
							<th class="text-center" width="23%">المستلم</th>
							<th class="text-center" width="14%">الفرع</th>
							<th class="text-center" width="19%">طريقة الدفع</th>
						</tr>
					</thead>
					<tbody id="tstandby">
						<?php
						$output = '';
						$pay =  "كاش";
						$sql = $conn->prepare("SELECT in_id , money_in , sanad_date , realName , payMethod ,b_name  FROM  sanad_in
		WHERE contract_id = ? order by in_id DESC ");
						$sql->bindParam(1, $contract_id);
						$sql->execute();
						if ($sql->rowCount() > 0) {
							$ss = 1;
							while ($row = $sql->fetch()) {
								$qty = 2;
								$bb = $row['payMethod'];
								$sanad = $row['in_id'];
								if ($ss == 1) {
									$date1 = new DateTime($row['sanad_date']);
									$date2 = new DateTime(date("Y-m-d"));
									$qty   = $date2->diff($date1)->format("%a");
								}
								switch ($bb) {
									case "1":
										$pay =  "كاش";
										break;
									case "2":
										$pay = "تحويل الراجحي";
										break;
									case "3":
										$pay = "نقاط بيع";
										break;
									case "5":
										$pay = "تحويل الأهلي";
										break;
								}
								if (($ss < 4) && ($qty == 0)) {

									$output .= '<tr><td style="background-color:#699F72;"><a  href="print/sanadin.php?sn=' . $row['in_id'] . '"target="_blank">' . $ss . '</a></td>';
								} else {
									$output .= '<tr><td>' . $ss . '</td>';
								}

								$output .= '<td>' . $row['money_in'] . '</td>
								<td>' . $row['sanad_date'] . '</td>
								<td>' . $sanad . '</td>
								<td>' . $row['realName'] . '</td>
								<td>' . $row['b_name'] . '</td>';
								if (($ss == 1) && ($qty == 0)) {
									$smsKind = 1;
									// $inv = SMSapi::in_outSMS($conn, $sanad, $smsKind);
									// if ($inv > 0) {
									$output .= '<td>' . $pay . '</td>';
									// } else {
									// 	$output .= '<td><button class="btn btn-outline-success btn-block form-control sms" smsKind="1" sanad=' . $sanad . '> ' . $pay . ' <i class="fas fa-sms"></i></button></td>';
									// }
								} else {
									$output .= '<td>' . $pay . '</td>';
								}
								$output .= '</tr>  ';
								$ss = $ss + 1;
							}
						} else {
							$output .= '<tr><td colspan="7">لا يوجد سجلات ...</td></tr>';
						}
						echo $output;
						?>
					</tbody>
					<tfoot></tfoot>
				</table>
			</div>
			<div class="col-6">
				<h6>سندات الصرف</h6>
				<table class="table table-bordered table-hover ">
					<thead>
						<tr>
							<th class="text-center" width="6%">م</th>
							<th class="text-center" width="12%">المبلغ</th>
							<th class="text-center" width="15%">التاريخ</th>
							<th class="text-center" width="12%">كود</th>
							<th class="text-center" width="25%">موظف الفرع</th>
							<th class="text-center" width="15%">الفرع</th>
						</tr>
					</thead>
					<tbody>
						<?php
						$output = '';
						$sql = $conn->prepare("SELECT out_id , money_out , sanad_out , realName , b_name FROM sanad_out WHERE contract_id = ? order by out_id DESC  ");
						$sql->bindParam(1, $contract_id);
						$sql->execute();
						if ($sql->rowCount() > 0) {
							$ss = 1;
							while ($row = $sql->fetch()) {
								$output .= '<tr>
 						<td style="background-color:#D8DBD0;"><a href="print/sanadout.php?sn=' . $row['out_id'] . '"target="_blank">' . $ss . '</a></td>
						<td>' . $row['money_out'] . '</td>
						<td>' . $row['sanad_out'] . '</td>
						<td>' . $row['out_id'] . '</td>
						<td>' . $row['realName'] . '</td>
						<td>' . $row['b_name'] . '</td>
					</tr>  ';
								$ss = $ss + 1;
							}
						} else {
							$output .= '<tr><td colspan="6">لا يوجد سجلات ...</td></tr>';
						}
						echo $output;
						?>
					</tbody>
					<tfoot></tfoot>
				</table>
				<!-- // -->
				<div class="row">
					<div class="col">
						<h6>فواتير ضريبية</h6>
						<table class="table table-bordered table-hover ">
							<thead>
								<tr>
									<th class="text-center" width="10%">فاتورة</th>
									<th class="text-center" width="12%">المبلغ</th>
									<th class="text-center" width="22%">بداية</th>
									<th class="text-center" width="26%">نهاية الفترة</th>
									<th class="text-center" width="10%">يومي</th>
									<th class="text-center" width="15%">عدد أيام</th>
								</tr>
							</thead>
							<tbody>
								<?php
								$output = '';
								$sql = $conn->prepare("SELECT vat_id , after_vat , start , end, day_cost ,days_qty FROM vat WHERE contract_id = ? AND vat_id < 26307 order by vat_id DESC  ");
								$sql->bindParam(1, $contract_id);
								$sql->execute();
								if ($sql->rowCount() > 0) {
									while ($row = $sql->fetch()) {
										$vat_id = $row['vat_id'];
										$output .= '<tr>
													<td style="padding:0px;margin:0px;"><button id="' . $vat_id . '" style="font-weight:bold ;font-size:14px; padding:2;" class="btn btn-danger btn-ms btn-block vat_print">' . $vat_id . '</button></td>
													<td >' . $row['after_vat'] . '</td>
													<td>' . $row['start'] . '</td>
													<td>' . $row['end'] . '</td>
													<td>' . $row['day_cost'] . '</td>
													<td>' . $row['days_qty'] . '</td>
													</tr>  ';
									}
								} else {
									$output .= '<tr><td colspan="6">لا يوجد سجلات ...</td></tr>';
								}
								echo $output;
								?>
							</tbody>
							<tfoot></tfoot>
						</table>
					</div>
				</div>
				<!-- // -->
			</div>
		</div> <!-- end of invoice table row -->

		<!-- invoice modal -->
		<div class="modal fade" id="sanad_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>
						<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">
						<!-- start of sanad_in form -->
						<form method="post" class="form-horizontal" id="modal_sanad_in">
							<div id="sanad_in_msg"></div>
							<div class="form-row">
								<input type="hidden" class="form-control" id="id2" name="id2" />
								<input type="hidden" id="in_id" name="in_id" />
								<div class="col-2">
									<label for="money_in" 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="money_in" name="money_in" placeholder="بالريال" />
									<div id="money_in_msg" class="text-danger" style="font-size:12px;"></div>
								</div>
								<div class="col-2"></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>
										<option value="5">تحويل الأهلي</option>
									</select>
								</div>
							</div>
							<div class="form-row" style="margin-top: 10PX;">
								<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>
							</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="sanad_in_add" name="sanad_in_add" class="btn btn-primary">حفظ <i class="fa fa-cloud" aria-hidden="true"></i></button>
							<a id="sanad_in_print" href="" 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 sanad out -->
		<div class="modal fade" id="sanad_out" 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:Maroon ;">
						<div class="col-1 text-right">
							<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span style="color:blue;font-size:18px;"><i class="fa fa-times" aria-hidden="true"></i></span></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 sanad_out form -->
						<form method="post" class="form-horizontal" id="modal_sanad_out">
							<div id="sanad_out_msg"></div>
							<div class="form-row">
								<input type="hidden" class="form-control" id="id3" name="id3" />
								<input type="hidden" id="out_id" name="out_id" />
								<div class="col-2">
									<label for="money_out" 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="money_out" name="money_out" placeholder="بالريال" />
									<div class="text-danger" style="font-size:12px;" id="money_out_msg"></div>
								</div>
								<div class="col-4">
								</div>
							</div>
							<div class="form-row" style="margin-top:10px;">
								<div class="col-2">
									<label for="comm3" class="control-label" style="float:left;color:black;">ملاحظات</label>
								</div>
								<div class="col-10">
									<input type="text" class="form-control" id="comm3" name="comm3" />
								</div>
							</div>
						</form><!-- end of sanad_out 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="sanad_out_add" name="sanad_out_add" class="btn btn-primary">حفظ <i class="fa fa-cloud" aria-hidden="true"></i></button>
							<a id="sanad_out_print" href="" 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><!-- end of sanad_out modal -->
		<!-- 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 -->
	</div><!-- end of fluid2  -->

	<script>
		var contt = document.getElementById('contract_id').value;

		fetch('print/newCont_api.php?ct=' + contt)
			.then((data) => data.json())
			.then((data) => {
				document.getElementById('rent_days').value = data.rent_days;
				document.getElementById('used_area').value = data.used_area;
				document.getElementById('tamm').value = data.tamm;
				document.getElementById('free_dd').value = data.free_dd;
				document.getElementById('km3').value = data.km3;

				document.getElementById('date_out').value = data.date_out;
				document.getElementById('date_in').value = data.date_in;
				document.getElementById('days_qty').value = data.days_qty;
				document.getElementById('day_cost').value = data.day_cost;
				document.getElementById('days_cost').value = data.days_cost;

				document.getElementById('km_out').value = data.km_out;
				document.getElementById('km_in').setAttribute("min", data.km_out);
				document.getElementById('km_in').value = data.km_in;
				if (data.km_in > 1) {
					document.getElementById("contt_close").style.display = "block";
				}
				document.getElementById('free_kilo').value = data.free_kilo;
				document.getElementById('kilo_qty').value = data.km_qty;
				document.getElementById('kilo_cost').value = data.kilo_cost;
				document.getElementById('kilos_cost').value = data.kilos_cost;

				document.getElementById('used_kilo').value = data.used_km;

				document.getElementById('time_out').value = data.time_out;
				document.getElementById('time_in').value = data.time_in;
				document.getElementById('hour_qty').value = data.hour_qty;
				document.getElementById('hour_cost').value = data.hour_cost;
				document.getElementById('hours_cost').value = data.hours_cost;

				document.getElementById('addtion_cost').value = data.addtion_cost;
				document.getElementById('addtion_desc').value = data.addtion_desc;
				document.getElementById('violations').value = data.violations;
				document.getElementById('ins_claim').value = data.ins_claim;
				document.getElementById('wesam_ins').value = data.wesam_ins;
				document.getElementById('open_kilo').value = data.open_kilo;
				document.getElementById('deduction').value = data.tot_cut;

				document.getElementById('payed').value = data.payed;
				document.getElementById('net_cost').value = data.net_cost;
				document.getElementById('vat_req').value = data.vat_req;
				// document.getElementById('vat5').value = data.vat5;
				document.getElementById('vat15').value = data.vat15;
				document.getElementById('after_vat').value = data.after_vat;
				document.getElementById('wanted').value = data.wanted;
				document.getElementsByClassName('wanted').value = data.wanted;
				document.getElementById('total_in').value = data.total_in;
				document.getElementById('total_out').value = data.total_out;

				document.getElementById('comm').value = data.comm;
				document.getElementById('dept_comm').value = data.dept_comm;
				document.getElementById('operating_comm').value = data.operating_comm;
				document.getElementById('open_comm').value = data.open_comm;
				document.getElementById('cont_status').value = data.cont_status;

				document.getElementById("print3").setAttribute("href", "print/closeContract.php?contt=" + data.contract_id);
				document.getElementById("acc").setAttribute("href", "account_statement.php?dd=" + data.contract_id);
				document.getElementById("car_check").setAttribute("href", "car_check.php?dd=" + data.contract_id);

				if (data.cont_status == 1) {
					document.getElementById("contt_close").style.display = "none";
					document.getElementById("print3").style.display = "none";
					document.getElementById("sanad_out_btn").style.display = "none";
				} else {
					document.getElementById("print3").style.display = "block";
				}
				if ((data.wanted < 0) && (data.cont_status != 1)) {
					document.getElementById("sanad_out_btn").style.display = "block";
				}
			}).catch(err => console.log(err));
	</script>
	<script>
		// function gpsCheck() {
		// 	event.preventDefault();
		// 	let device_id = document.getElementById('gpsid').value;
		// 	let from_date = document.getElementById('date_out').value;
		// 	let to_date = new Date().toLocaleDateString();
		// 	let from_time = document.getElementById('time_out').value;
		// 	let to_time = new Date().toLocaleTimeString().replace('PM', '');
		// 	var proxyUrl = 'https://cors-anywhere.herokuapp.com/',
		// 		targetUrl = "https://track.gps14.com/api/get_history?lang=en&user_api_hash=$2y$10$pKVV2ghYQNdtdjlTtdf9r.fMrV/R2AUFoe6aH5vn.J52b.LIhpelm&device_id=" + device_id + "&from_date=" + from_date + "&from_time=" + from_time + "&to_date=" + to_date + "&to_time=" + to_time;

		// 	fetch(proxyUrl + targetUrl)
		// 		.then(respond => {
		// 			return respond.json()
		// 		}).then(json => {
		// 			console.log(json.distance_sum);
		// 			document.getElementById('gpsKM').innerHTML = json.distance_sum;
		// 			document.getElementById('maxKM').innerHTML = json.top_speed;
		// 		})
		// }
	</script>
	<script>
		$(document).ready(function() {
			var branch = $('#branch').val();
			$('#close').click(function() {
				close();
			});
			$('#close2').click(function() {
				close();
			});
			// $("#sanad_in_add").click(function(event) {
			// 	event.preventDefault();
			// 	$("#money_in_msg").html("");
			// 	$.ajax({
			// 		url: "sanad_in_action.php",
			// 		method: "POST",
			// 		data: $("#modal_sanad_in").serialize(),
			// 		dataType: "json",
			// 		success: function(data) {
			// 			if (data.error) {
			// 				$("#money_in_msg").html(data.error.money_in);
			// 			} else {
			// 				$('#sanad_in_add').css("display", "none");
			// 				$('#sanad_in_print').css("display", "block");
			// 				$("#sanad_in_msg").html(data.msg);
			// 				$("#in_id").val(data.in_id);
			// 				$("#sanad_in_print").attr("href", "print/sanadin.php?sn=" + data.in_id);
			// 				$("#sanad_in_msg").delay(3000).slideUp(function() {
			// 					$("#sanad_in_msg").html('');
			// 					$.ajax({
			// 						url: "sanad_in_action.php",
			// 						method: "POST",
			// 						data: {in_id:data.in_id},
			// 						dataType: "json",
			// 						success: function(data) {},
			// 					});
			// 					// alert(data.in_id);
			// 				});
			// 				// location.reload();
			// 			}
			// 		}
			// 	});
			// });
			$("#sanad_in_add").click(function(event) {
				event.preventDefault();
				$("#money_in_msg").html(""); // Clear previous messages

				// First AJAX request
				$.ajax({
					url: "sanad_in_action.php",
					method: "POST",
					data: $("#modal_sanad_in").serialize(),
					dataType: "json",
					success: function(data) {
						if (data.error) {
							$("#money_in_msg").html(data.error.money_in);
						} else {
							// Update UI elements
							$('#sanad_in_add').hide();
							$('#sanad_in_print').show();
							$("#sanad_in_msg").html(data.msg);
							$("#in_id").val(data.in_id);
							$("#sanad_in_print").attr("href", "print/sanadin.php?sn=" + data.in_id);

							// Start delay for second request
							$("#sanad_in_msg").delay(3000).slideUp(function() {
								$(this).html(''); // Clear message after slideUp
							});
						}
					},
					error: function(jqXHR, textStatus, errorThrown) {
						console.error("First request failed:", textStatus, errorThrown);
						$("#money_in_msg").html("An error occurred. Please try again.");
					}
				});
			});
			$("#sanad_out_add").click(function(event) {
				event.preventDefault();
				$("#money_out_msg").html("");
				$.ajax({
					url: "sanad_out_action.php",
					method: "POST",
					data: $("#modal_sanad_out").serialize(),
					dataType: "json",
					success: function(data) {
						if (data.error) {
							$("#money_out_msg").html(data.error.money_out);
						} else {
							$('#sanad_out_add').css("display", "none");
							$('#sanad_out_print').css("display", "block");
							$("#sanad_out_msg").html(data.msg);
							$("#out_id").val(data.out_id);
							$("#sanad_out_print").attr("href", "print/sanadout.php?sn=" + data.out_id);
							$("#sanad_out_msg").delay(3000).slideUp(function() {
								$("#sanad_out_msg").html('');
							});
						}
					}
				});
			});

			$('#sanad_in').on('show.bs.modal', function(event) {
				var recipient = $("#contract_id").val();
				var modal = $(this);
				modal.find('#id2').val(recipient);
			});
			$("#sanad_in").on("hidden.bs.modal", function() {
				money();
			});
			$('#sanad_out').on('show.bs.modal', function(event) {
				var recipient = $("#contract_id").val();
				var ww = $(".wanted").val();
				ww = ww * -1;
				var modal = $(this);
				modal.find('#id3').val(recipient);
				modal.find('#money_out').val(ww);
			});
			$("#sanad_out").on("hidden.bs.modal", function() {
				money();
			});
			$("#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("");
				$("#violations_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);
							$("#violations_msg").html(data.error.violations);
							$("#koki_msg").html(data.msg);
						} else {
							$("#koki_msg").html(data.msg);
							$("#koki_msg").delay(3000).slideUp(function() {
								$("#koki_msg").html('');
							});
							location.reload();
						}
					}
				});
			});

			$("#tamm_update").click(function(event) {
				event.preventDefault();
				$("#tamm_update").prop("disabled", true);
				$("#tamm_update").delay(2000).slideUp(function() {
					$("#tamm_update").prop("disabled", false);
				});
				// var dd = $('#tamm_expire').val();
				// // if (dd != 0) {
				// // 	convertDates('ummalqura', 'tamm_expire', 'tamm_expire');
				// // }
				var contract_id = $("#contract_id").val();
				var tamm_expire = $('#tamm_expire').val();
				$("#tamm_msg").html("");
				$.ajax({
					url: "contract_update.php",
					method: "POST",
					data: {
						tamm_expire: tamm_expire,
						contract_id: contract_id
					},
					dataType: "json",
					success: function(data) {
						if (data.error) {
							$("#tamm_msg").html(data.error.tamm);
						} else {
							$("#koki_msg").html(data.msg);
							$("#koki_msg").delay(3000).slideUp(function() {
								$("#koki_msg").html('');
							});
							location.reload();
						}
					}
				});
			});

			$("#areaBtn").click(function(event) {
				event.preventDefault();
				var contract_id = $("#contract_id").val();
				var used_area = $('#used_area').val();
				var rent_days = $('#rent_days').val();
				var tamm = $('#tamm').val();
				$.ajax({
					url: "contract_update.php",
					method: "POST",
					data: {
						used_area: used_area,
						rent_days: rent_days,
						tamm: tamm,
						contract_id: contract_id
					},
					dataType: "json",
					success: function(data) {
						if (data.error) {} else {
							$("#koki_msg").html(data.msg);
							$('#area').css('display', 'none');
							$('#area_print').css('display', 'block');
							$("#koki_msg").delay(3000).slideUp(function() {
								$("#koki_msg").html('');
							});
						}
					}
				});
			});

			// send sms
			// $(".sms").click(function() {
			// 	event.preventDefault();
			// 	var msg = [];
			// 	msg[1] = "هل تريد ارسال تم قبض مبلغ ؟؟";
			// 	msg[2] = "هل تريد طلب دفعة ايجار ؟؟؟";
			// 	msg[3] = " هل تريد ارسال طلب تغيير زيت ؟؟ ";
			// 	msg[4] = " هل تريد طلب مراجعة الفرع ؟؟";
			// 	msg[5] = "هل تريد ارسال تم صرف مبلغ ؟؟";
			// 	msg[6] = "هل تريد ارسال تم تطبيق 15 % ضريبة قيمة مضافة ؟؟";
			// 	var smsKind = $(this).attr("smsKind");
			// 	if (confirm(msg[smsKind])) {
			// 		var sanad = $(this).attr("sanad");
			// 		var contract_id = $("#contract_id").val();
			// 		$.ajax({
			// 			url: "../op/SMSapi.php",
			// 			method: "POST",
			// 			data: {
			// 				smsKind: smsKind,
			// 				sanad: sanad,
			// 				contract_id: contract_id,
			// 				father: father
			// 			},
			// 			//  dataType	:	"json",
			// 			success: function(data) {
			// 				$("#koki_msg").delay(1000).slideUp(function() {
			// 					window.location.href = data;
			// 				});
			// 				// alert(data);
			// 			}
			// 		});
			// 	} else {
			// 		return false;
			// 	}
			// });

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

			$("#contt_close").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("");
				$("#violations_msg").html("");
				$("#deduction_msg").html("");
				var contract_id = $("#contract_id").val();
				$.ajax({
					url: "cont2close_action.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);
							$("#violations_msg").html(data.error.violations);
							$("#deduction_msg").html(data.error.deduction_msg);
						} else {
							$("#koki_msg").html(data.msg);
							$("#koki_msg").delay(3000).slideUp(function() {
								$("#koki_msg").html('');
							});
							window.location.href = 'cont2debt.php?id=' + contract_id;
						}
					}
				});
			});

			$("#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', 'print/newContract.php?contt=' + data.contract_id);
						$("#customer2_msg").delay(4000).slideUp(function() {
							$("#customer2_msg").html('');
						});
					}
				});
			}); //end of add customer2
			$("#check").click(function(event) {
				event.preventDefault();
				var sn = $("#contract_id").val();
				window.open("print/check.php?sn=" + sn, '_blank');
			});
			$(".vat_print").click(function(event) {
				event.preventDefault();
				var dd = $(this).attr('id');
				window.open("../rent/print/vat_print.php?vt=" + dd, '_blank');
			}); //end of vat print button action

			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;
				}
			}
		}); // end of script
	</script>
</body>

</html>