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

$auth = bdr_require_privilege(20, '../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 "../op/wanted.php";


$initialContractData = bdr_cont2debt_data($conn, $contract_id);

function bdr_cont2debt_data(PDO $conn, $contract_id): array
{
	$output = array();

	$wanted = new Wanted($conn, $contract_id);
	$pay = $wanted->wanted();

	$output["total_in"] = $pay['total_in'] ?? 0;
	$output["total_out"] = $pay['total_out'] ?? 0;
	$output["payed"] = $pay['payed'] ?? 0;
	$output["courts_in"] = $pay['courts_in'] ?? 0;
	$output["days_cost"] = $pay['days_cost'] ?? 0;
	$output["kilos_cost"] = $pay['kilos_cost'] ?? 0;
	$output["hours_cost"] = $pay['hours_cost'] ?? 0;
	$output["km_qty"] = $pay['km_qty'] ?? 0;
	$output["used_km"] = $pay['used_km'] ?? 0;
	$output["days_qty"] = $pay['days_qty'] ?? 0;
	$output["hour_qty"] = $pay['hour_qty'] ?? 0;
	$output["hour_cost"] = $pay['hour_cost'] ?? 0;
	$output["rent_cost"] = $pay['rent_cost'] ?? 0;
	$output["tot_addtion"] = $pay['tot_addtion'] ?? 0;
	$output["cutt"] = $pay["cutt"] ?? 0;
	$output["tot_cut"] = $pay['tot_cut'] ?? 0;
	$output["wesam_ins"] = $pay['wesam_ins'] ?? 0;
	$output["open_kilo"] = $pay['open_kilo'] ?? 0;
	$output["net_cost"] = $pay['net_cost'] ?? 0;
	$output["vat5"] = $pay['vat5'] ?? 0;
	$output["vat15"] = $pay['vat15'] ?? 0;
	$output["vat_req"] = $pay['vat_req'] ?? 0;
	$output["daysCost5"] = $pay["daysCost5"] ?? 0;
	$output["after_vat"] = $pay['after_vat'] ?? 0;
	$output["wanted"] = $pay['wanted'] ?? 0;

	$cont = $conn->prepare("SELECT * FROM contracts WHERE contract_id = ? LIMIT 1");
	$cont->bindParam(1, $contract_id);
	$cont->execute();
	if ($cont->rowCount() < 1) {
		return array('error' => 'contract_not_found');
	}

	$c_row = $cont->fetch(PDO::FETCH_ASSOC);
	$carid = $c_row["carid"];

	$output["addtion_desc"] = $c_row["addtion_desc"];
	$output["contract_id"] = $c_row["contract_id"];
	$output["used_area"] = $c_row["used_area"];
	$output["rent_days"] = $c_row["rent_days"];
	$output["carid"] = $c_row["carid"];
	$output["cust_id"] = $c_row["cust_id"];
	$output["cust_id2"] = $c_row["cust_id2"];
	$output["date_out"] = $c_row["date_out"];
	$output["date_in"] = $c_row["date_in"];
	$output["day_cost"] = $c_row["day_cost"];
	$output["km_out"] = $c_row["km_out"];
	$output["km_in"] = $c_row["km_in"];
	$output["time_out"] = $c_row["time_out"];
	$output["time_in"] = $c_row["time_in"];
	$output["free_kilo"] = $c_row["free_kilo"];
	$output["kilo_cost"] = $c_row["kilo_cost"];
	$output["father"] = $c_row["father"];
	$output["branch"] = $c_row["branch"];
	$output["free_dd"] = $c_row["free_dd"];
	$output["tamm"] = $c_row["tamm"];
	$output["addtion_cost"] = $c_row["addtion_cost"];
	$output["violations"] = $c_row["violations"];
	$output["ins_claim"] = $c_row["ins_claim"];
	$output["next_cust"] = $c_row["next_cust"];
	$output["open_kilo_day"] = $c_row["open_kilo"];
	$output["wesam_ins_day"] = $c_row["wesam_ins"];
	$output["deduction"] = $c_row["deduction"];
	$output["cont_status"] = $c_row["cont_status"];
	$output["operating_comm"] = $c_row["operating_comm"];
	$output["dept_comm"] = $c_row["dept_comm"];
	$output["open_comm"] = $c_row["open_comm"];
	$output["comm"] = $c_row["comm"];

	$car = $conn->prepare("SELECT km3, minn, freekilo FROM cars WHERE carid = ? LIMIT 1");
	$car->bindParam(1, $carid);
	$car->execute();
	if ($car->rowCount() > 0) {
		$row = $car->fetch(PDO::FETCH_ASSOC);
		$output["km3"] = $row["km3"];
		$output["minn"] = $row["minn"];
		$output["freekm"] = $row["freekilo"];
	}

	return $output;
}
?>
<!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">
	<link rel="icon" href="../images/perfect.ico">
	<title>عقد<?= $contract_id ?></title>
	<link rel="stylesheet" href="../dist/css/bootstrap.min.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>

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

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

		a:hover {
			color: green;
		}
	</style>
	<?php
	$sql = $conn->prepare('SELECT contracts.contract_id,contracts.cust_id2,contracts.branch,cars.carid,customers.cust_id ,customers.cust_name,customers.phone ,
				 customers.phone2, cars.kind , cars.numbers , cars.model,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);
		$cont_status  	= $row['cont_status'];
		$cust_id 		= $row['cust_id'];
		$current_branch = $row['branch'];
		$carid			= $row['carid'];
		$phone 			= $row['phone'];
		$phone2 		= $row['phone2'];
		$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="container-fluid" style="background-color:black;">
			<br />
			<div class="form-row align-items-center">
				<div class="col-1">
					<input style="margin-right:20px;" type="submit" class="btn btn-danger" id="close2" value=" خروج " />
				</div>
				<div class="col-1">
				<input style="margin-right:-40px; color:firebrick; font-size: 15px;background-color: #262626;" type="submit" class="btn btn-outline-danger btn-block" id="state5" value=" محامي "/>
				</div>
				<div class="col-1">
					<button id="company" class="btn btn-outline-primary btn-block">تبديل</button>
				</div>
				<div class="col-6 text-center">
					<h3 style="font-weight:bold; color:#EDDFDF;" id="title">متابعة عقد رقم <?= $contract_id ?></h3>
				</div>
				<div class="col-1">
					<?php
					if ($cont_status == 4) {
						echo '<input style="margin-left:20px;" type="submit" class="btn btn-danger btn-block" id="state" value=" محكمة "/>';
					} else if ($cont_status == 3) {
						echo '<input style="margin-left:20px;" type="submit" class="btn btn-warning btn-block" id="state" value=" مديون "/>';
					} else if($cont_status == 2){
						echo '<input style="margin-left:20px;" type="submit" class="btn btn-success btn-block" id="state" value=" مغلق "/>';
					} else if($cont_status == 5){
						echo '<input style="margin-left:20px;" type="submit" class="btn btn-outline-danger btn-block" id="state" value=" محامي "/>';
					}
					?>
				</div>
				<div class="col-1">
					<button id="branch_btn" class="btn btn-outline-primary btn-block">تعديل</button>
				</div>
				<div class="col-1">
				<select class="form-control" id="current_branch" name="current_branch" style="padding:3px;">
					<?php
						$sql = $conn->prepare("SELECT branchID , b_name from branches where branchID > 1");
						$sql->execute();
					if ($sql->rowCount() > 0) {
					while ($ro = $sql->fetch()) {
						$branchid   = $ro["branchID"];
							$b_name     = $ro["b_name"];
						if ($branchid == $current_branch) {
						echo '<option selected="selected" value="' . $branchid . '" >' . $b_name . '</option>';
						} else {
							echo "<option value= $branchid >$b_name</option>";
								}
								}
							} 
						?>
				</select>
			</div>
		</div>


			<div class="form-row align-items-center" style="margin-top:10px;">
				<div class="col-1">
					<a href="cust_update.php?id=<?= $cust_id; ?>"><button type="button" class="btn btn-outline-success btn-sm">بيانات المستأجر</button></a>
				</div>
				<div class="col-10 text-center">
					<center class="control-label" style="font-size:1.3em; font-weight:bold; color:#EDDFDF;">
						<?= $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>~ سيارة " . $row['kind'] . " " . $row['numbers'] . " موديل " . $row['model'];
						$cust22 = $row['cust_id2'];
						?>
					</center>
				</div>
				<div class="col-1 text-left">
					<a href="../a/car_update.php?id=<?= $row['carid']; ?>"><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 '<br /><div class="form-row align-items-center">
						<div class="col-1 col-md-1">
						<a href="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:#EDDFDF;">';
				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>
							</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_db     = $monn["debt"];
	}
		?>
		</div>
		<p><br /></p>
		<div class="container-fluid">
			<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>
					<div class="row-form">
						<div class="col">
							<input type="text" class="form-control" id="used_area" name="used_area" disabled="disabled" readonly />
						</div>
					</div>
					<div class="form-row">
						<div class="col-11">
							<input type="text" class="form-control" id="rent_days" name="rent_days" disabled="disabled" readonly />
						</div>
					</div>
					<div class="form-row">
						<div class="col-11">
							<input type="text" class="form-control" id="tamm" name="tamm" disabled="disabled" readonly />
						</div>
					</div>
					<div class="form-row">
						<div class="col-5">
							<label for="wanted" class="control-label" style="margin-top:5px;">المطلوب</label>
						</div>
						<div class="col-6">
							<input type="number" class="form-control wanted" disabled="disabled" readonly />
						</div>
					</div>
					<br />
					<div class="form-row">
						<div class="col-11">
							<button type="button" id="contract_update" name="contract_update" class="btn btn-outline-success btn-block">حفظ التعديلات <i style="margin-right:10px;font-size:16px;font-weight:bold;" class="fa fa-cloud" aria-hidden="true"></i></button>
						</div>
					</div>
					<div class="form-row">
						<div class="col-11">
							<button type="button" class="btn btn-outline-primary btn-block" id="add_cust22" data-toggle="modal" style="font-weight:bold;" data-backdrop="static" data-keyboard="false" data-target="#customer2">إضافة مستأجر <i style="margin-right:10px;font-size:18px;color:blue;" class="fa fa-user" aria-hidden="true"></i></button>
						</div>
					</div>
					<div class="form-row">
						<div class="col-11">
							<a id="print3" href="" target="_blank"><button type="button" class="btn btn-outline-success btn-block">طباعة العقد <i style="margin-right:10px;font-size:16px;" class="fa fa-print" aria-hidden="true"></i></button></a>
						</div>
					</div>
					<div class="row" style="padding:2px;">
						<div class="col-11">
							<a id="newIssue" href="" target="_blank"><button type="button" class="btn btn-outline-dark btn-block">رفع معاملة<span style="margin-right:20px;" class="glyphicon glyphicon-print" aria-hidden="true"></span></button></a>
						</div>
					</div>
					<div class="form-row">
						<div class="col-11">
							<a id="acc" href="" target="_blank"><button type="button" class="btn btn-outline-success btn-block">كشف حساب <i style="margin-right:10px;font-size:16px;" class="fa fa-print" aria-hidden="true"></i></button></a>
						</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> -->
					<div class="form-row">
						<div class="col-11">
							<button type="button" class="btn btn-outline-danger btn-block" id="close">خروج <i style="margin-right:10px;font-size:16px;font-weight:bold;" class="fa fa-times" aria-hidden="true"></i></button>
						</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="<?= $contract_id ?>" />
						<input type="hidden" id="carid" name="carid" />
						<input type="hidden" id="minn" name="minn" />
						<input type="hidden" id="km3" name="km3" />
						<input type="hidden" id="freekm" name="freekm" />
						<input type="hidden" id="free_dd" name="free_dd" />
						<input type="hidden" id="gpsid" name="gpsid" value="<?= $gpsid ?>" />
						<input type="hidden" id="cont_status" name="cont_status" value="<?= $cont_status ?>" />
						<div class="form-row">
							<div class="col-2">
							</div>
							<div class="col-2 text-center">
								<label for="date_in" class="control-label ">الخروج</label>
							</div>
							<div class="col-2 text-center">
								<label class="control-label">الدخول</label>
							</div>
							<div class="col-1  text-center">
								<label class="control-label">عدد</label>
							</div>
							<div class="col-1 text-center">
								<label class="control-label">سعر</label>
							</div>
							<div class="col-2 text-center">
								<label for="days_cost" class="control-label">إجمالي</label>
							</div>
							<div class="col-2 text-center">
								<label for="" class="control-label  text-danger"><?= $debt_db ?></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" readonly />
							</div>
							<script>
								$(function() {
									var calendar = $.calendars.instance('');
									$('#date_in').calendarsPicker({
										calendar: calendar
									});
								});
							</script>
							<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" />
								<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" disabled="disabled" readonly />
							</div>
							<div class="col-2">
								<input type="text" class="form-control" id="time_in" name="time_in" />
							</div>
							<div class="col-1">
								<input type="number" class="form-control" id="hour_qty" name="hour_qty" disabled="disabled" readonly />
							</div>
							<div class="col-1">
								<input type="number" onchange="money()" class="form-control" id="hour_cost" name="hour_cost" disabled="disabled" readonly />
							</div>
							<div class="col-2">
								<input type="number" class="form-control" id="hours_cost" name="hours_cost" disabled="disabled" readonly />
							</div>
							<div class="col-2">
								<input type="number" class="form-control" id="used_kilo" name="used_kilo" disabled="disabled" 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 class="text-danger" style="font-size:12px;" id="addtion_desc_msg"></div>
									</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="4" 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" 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="2" id="operating_comm" name="operating_comm" disabled="disabled" readonly placeholder="تعليمات التشغيل"></textarea>
									</div>
								</div>
							</div>
							<div class="col-5">
								<div class="form-row">
									<div class="col-2" style="margin-right:15px;">
										<label for="addtion_cost" class="control-label" style="float:left;">تعويضات</label>
									</div>
									<div class="col-4">
										<input type="number" class="form-control" onchange="money()" 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:15px;">
										<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-xs-2 col-md-2" style="margin-right:15px;">
										<label for="ins_claim" class="control-label" style="float:left;">تأمينات</label>
									</div>
									<div class="col-4">
										<input type="number" class="form-control" onchange="money()" id="ins_claim" name="ins_claim" />
										<div class="text-danger" style="font-size:12px;" id="ins_claim_msg"></div>
									</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" disabled="disabled" readonly />
								</div>
							</div>
								<div class="form-row">
									<div class="col-2" style="margin-right:16px;">
										<label for="deduction" class="control-label" style="float:left;">الخصم</label>
									</div>
									<div class="col-4">
										<input type="number" class="form-control" id="deduction" name="deduction" />
									</div>
									<div class="col-2">
										<label for="cutt" class="control-label text-danger">اشعارات خصم</label>
									</div>
									<div class="col-3">
										<input type="number" class="form-control" id="cutt" name="cutt" disabled="disabled" readonly />
									</div>
								</div>
								<div class="form-row">
									<div class="col-2" style="margin-right:16px;">
										<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="courts_in" class="control-label text-primary">تحصيل محكمة</label>
									</div>
									<div class="col-3">
										<input type="number" class="form-control" id="courts_in" name="courts_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" name="wanted" disabled="disabled" readonly />
									</div>
								</div>
							</div>
						</div>
						<div class="form-row">
							<div class="col-1">
								<label for="open_comm" class="control-label" style="float:left;">ملاحظات</label>
							</div>
							<div class="col-10">
								<input type="text" class="form-control" id="open_comm" name="open_comm" disabled="disabled" readonly />
								<!-- <div class="text-danger" style="font-size:12px;" id="comm_msg"></div> -->
							</div>
						</div>
						<p><br /></p>
						<script>
							function money() {
								var a = parseFloat($("#day_cost").val()) || 0;
								var hq = parseInt($("#hour_qty").val()) || 0;
								var b = parseInt($("#days_qty").val()) || 0;
								$("#days_cost").val((a * b).toFixed(2));
								if (hq > 9) {
									$("#hours_cost").val(a);
								}
								if ((b == 0) && (hq > 6)) {
									$("#hours_cost").val(a);
								}
								var h = parseInt($("#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 ww = parseFloat($("#wanted").val()) || 0;
								if (ww > 200) {
									$(".wanted").css("background-color", "#ffcccc");
								} else if (ww < 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 = myfree();
								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 mykm(out) {
								var a = parseInt($("#kilo_cost").val()) || 0;
								var inn = parseInt($("#km_in").val()) || 0;
								var b = parseInt($("#km3").val()) || 0;
								if (inn != "") {
									if (out >= inn) {
										$("#km_in").val(out).css("background-color", "#ffcccc");
									} else {
										$("#km_in").val(inn).css("background-color", "");
									}
								}
								if (a < b) {
									$("#kilo_cost").val(a).css("background-color", "#ffcccc");
									return a;
								} else if (a == b) {
									$("#kilo_cost").css("background-color", "#ffcccc");
									return a;
								} else {
									$("#kilo_cost").css("background-color", "");
									return a;
								}
							}

							function myfree() {
								var a = parseInt($("#free_kilo").val()) || 250;
								var b = parseInt($("#freekm").val()) || 250;
								if (a > b) {
									$("#free_kilo").val(a).css("background-color", "#ffcccc");
									return a;
								} else if (a == b) {
									$("#free_kilo").css("background-color", "#ffcccc");
									return a;
								} else {
									$("#free_kilo").css("background-color", "");
									return a;
								}
							}
						</script>
					</form>
					<script>
					(function(data) {
						function setContractValue(id, value) {
							var element = document.getElementById(id);
							if (!element) {
								return;
							}
							element.value = value == null ? '' : value;
						}
						function setContractHref(id, value) {
							var element = document.getElementById(id);
							if (!element) {
								return;
							}
							element.setAttribute('href', value);
						}
						if (!data || data.error) {
							return;
						}
						setContractValue('carid', data.carid);
						setContractValue('rent_days', data.rent_days);
						setContractValue('used_area', data.used_area);
						setContractValue('free_dd', data.free_dd);
						setContractValue('freekm', data.freekm);
						setContractValue('minn', data.minn);
						setContractValue('km3', data.km3);
						setContractValue('tamm', data.tamm);
						setContractValue('cont_status', data.cont_status);
						setContractValue('date_out', data.date_out);
						setContractValue('date_in', data.date_in);
						setContractValue('end', data.date_in);
						setContractValue('days_qty', data.days_qty);
						setContractValue('day_cost', data.day_cost);
						setContractValue('days_cost', data.days_cost);
						setContractValue('km_out', data.km_out);
						setContractValue('km_in', data.km_in);
						setContractValue('free_kilo', data.free_kilo);
						setContractValue('kilo_qty', data.km_qty);
						setContractValue('kilo_cost', data.kilo_cost);
						setContractValue('kilos_cost', data.kilos_cost);
						setContractValue('used_kilo', data.used_km);
						setContractValue('time_out', data.time_out);
						setContractValue('time_in', data.time_in);
						setContractValue('hour_qty', data.hour_qty);
						setContractValue('hour_cost', data.hour_cost);
						setContractValue('hours_cost', data.hours_cost);
						setContractValue('addtion_cost', data.addtion_cost);
						setContractValue('addtion_desc', data.addtion_desc);
						setContractValue('violations', data.violations);
						setContractValue('ins_claim', data.ins_claim);
						setContractValue('deduction', data.deduction);
						setContractValue('wesam_ins', data.wesam_ins);
						setContractValue('open_kilo', data.open_kilo);
						setContractValue('cutt', data.cutt);
						setContractValue('courts_in', data.courts_in);
						setContractValue('payed', data.payed);
						setContractValue('net_cost', data.net_cost);
						setContractValue('vat5', data.vat5);
						setContractValue('vat15', data.vat15);
						setContractValue('vat_req', data.vat_req);
						setContractValue('after_vat', data.after_vat);
						setContractValue('wanted', data.wanted);
						setContractValue('comm', data.comm);
						setContractValue('dept_comm', data.dept_comm);
						setContractValue('operating_comm', data.operating_comm);
						setContractValue('open_comm', data.open_comm);
						setContractHref('print3', '../rent/print/closeContract.php?contt=' + data.contract_id);
						setContractHref('acc', 'account_statement.php?dd=' + data.contract_id);
						setContractHref('car_check', '../rent/car_check.php?dd=' + data.contract_id);
						window.contractDataLoaded = true;
					})(<?= json_encode($initialContractData, JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE); ?>);
					</script>
					<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="5%">م</th>
								<th class="text-center" width="17%">المبلغ</th>
								<th class="text-center" width="15%">التاريخ</th>
								<th class="text-center" width="9%">كود</th>
								<th class="text-center" width="22%">المستلم</th>
								<th class="text-center" width="14%">الفرع</th>
								<th class="text-center" width="18%">طريقة الدفع</th>
							</tr>
						</thead>
						<tbody>
							<?php
							$output = '';
							$sql = $conn->prepare("SELECT invoice.in_id, invoice.money_in, invoice.sanad_date, users.realName, invoice.payMethod, branches.b_name
						FROM invoice
						LEFT JOIN users ON users.user_Id = invoice.father
						LEFT JOIN branches ON branches.branchID = invoice.branch
						WHERE invoice.contract_id = ? order by invoice.sanad_date DESC ");
							$sql->bindParam(1, $contract_id);
							try {
								$sql->execute();
							} catch (PDOException $e) {
								error_log('cont2debt sanad_in load failed: ' . $e->getMessage());
								$output .= '<tr><td colspan="7">تعذر تحميل سندات القبض من قاعدة البيانات.</td></tr>';
								$sql = false;
							}
							if ($sql && $sql->rowCount() > 0) {
								$ss = 1;
								while ($row = $sql->fetch()) {
									$in_id = $row['in_id'];
									$bb = $row['payMethod'];
									switch ($bb) {
										case "1":
											$pay =  "كاش";
											$options = '<option selected value="1">كاش</option><option value="2" >تحويل الراجحي</option><option value="3" >نقاط بيع</option><option value="5" >تحويل الأهلي</option>';
											break;
										case "2":
											$pay = "تحويل الراجحي";
											$options = '<option value="1">كاش</option><option selected value="2" >تحويل الراجحي</option><option value="3" >نقاط بيع</option><option value="5" >تحويل الأهلي</option>';
											break;
										case "3":
											$pay = "نقاط بيع";
											$options = '<option value="1">كاش</option><option value="2" >تحويل الراجحي</option><option selected value="3" >نقاط بيع</option><option value="5" >تحويل الأهلي</option>';
											break;
										case "5":
											$pay = "تحويل الأهلي";
											$options = '<option value="1">كاش</option><option value="2" >تحويل الراجحي</option><option value="3" >نقاط بيع</option><option selected  value="5" >تحويل الأهلي</option>';
											break;
									}
									$output .= '<tr>
							 					<td style="padding:0px;margin:0px;" ><button id="' . $in_id . '" style="font-weight:bold ;font-size:14px; padding:2;" class="btn btn-danger btn-ms btn-block in_edit ">' . $ss . '</button></td>
							 					<td style="padding:0px;margin:0px;"><input class="form-control" id="mon' . $in_id . '" name="mon' . $in_id . '" value="' . $row['money_in'] . '"/></td>
							 					<td>' . $row['sanad_date'] . '</td>
												<td ><a  href="../rent/print/sanadin.php?sn=' . $in_id . '"target="_blank">'. $in_id . '</a></td>
							 					<td>' . $row['realName'] . '</td>
							 					<td>' . $row['b_name'] . '</td>
							 					<td style="padding:0px;margin:0px;" contenteditable="true"><select id="pay' . $in_id . '" class="form-control payMM" style="background-color:transparent;padding:3px;margine:0px;" >
							 						' . $options . '</select></td>
							 				</tr>  ';
									$ss = $ss + 1;
								}
							} else {
								$output .= '<tr><td colspan="7">لا يوجد سجلات ...</td></tr>';
							}
							echo $output;
							?>
						</tbody>
						<tfoot></tfoot>
					</table>
				</div>
				<div class="col-6">
					<div class="row">
						<div class="col-4">
							<button type="button" style="font-weight:bold;" id="vat_add" class="btn btn-outline-success btn-block">اصدار فاتورة ضريبية</button>
						</div>
						<div class="col-3">
							<input type="text" class="form-control" id="end" name="end" readonly />
						</div>
						<script>
							$(function() {
								var calendar = $.calendars.instance('');
								$('#end').calendarsPicker({
									calendar: calendar
								});
							});
						</script>
						<div class="col-4">
							<button type="button" style="font-weight:bold;" id="vat_ref" class="btn btn-outline-warning btn-block">تحديث أخر فاتورة ضريبية</button>
						</div>
					</div>
					<br /><br />
					<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="10%">المبلغ</th>
										<th class="text-center" width="20%">بداية</th>
										<th class="text-center" width="20%">نهاية الفترة</th>
										<th class="text-center" width="10%">يومي</th>
										<th class="text-center" width="10%">عدد أيام</th>
										<th class="text-center" width="20%">اشعار خصم</th>
									</tr>
								</thead>
								<tbody id="tstandby">
									<?php
									$output = '';
									$sql = $conn->prepare("SELECT vat_id , after_vat , start , end, day_cost ,days_qty FROM vat WHERE contract_id = ? 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>
									<td style="padding:0px;margin:0;"><button type="button" class="btn btn-outline-danger btn-block debit_vat" vatid="' . $vat_id . '" data-toggle="modal" style="font-weight:bold;" data-backdrop="static" data-keyboard="false" data-target="#debit_vat">اشعار خصم</button></td>
									</tr>  ';
										}
									} else {
										$output .= '<tr><td colspan="6">لا يوجد سجلات ...</td></tr>';
									}
									echo $output;
									?>
								</tbody>
								<tfoot></tfoot>
							</table>
						</div>
					</div>
					<div class="form-row">
						<div class="col">
							<h6 style="color:#FF5722;">اشعارات الخصم</h6>
							<table class="table table-bordered table-hover ">
								<thead>
									<tr>
										<th class="text-center" width="6%">م</th>
										<th class="text-center" width="16%">المبلغ</th>
										<th class="text-center" width="68%">التعليق</th>
										<th class="text-center" width="10%">طباعة</th>
									</tr>
								</thead>
								<tbody>
									<?php
									$output = '';
									$sql = $conn->prepare("SELECT * FROM debit_vat WHERE contract_id = ? ORDER BY debit_id DESC");
									$sql->bindParam(1, $contract_id);
									$sql->execute();
									if ($sql->rowCount() > 0) {
										$ss = 1;
									while ($row = $sql->fetch()) {
										$debit_id = $row['debit_id'];
										$output .= '<tr>
						 						<td> ' . $ss . '</td>
						 						<td>' . $row['after_vat'] . '</td>
						 						<td>' . $row['comm'] . '</td>
							 					<td style="padding:0px;margin:0px;"><a id="print3" href="..\rent\print\debit_print.php?cr=' . $debit_id . '" target="_blank"><button type="button" class="btn btn-outline-success btn-block">طباعة <i style="margin-right:10px;font-size:18px;" class="fa fa-print"></i></button></a></td>
						   					</tr>';
											$ss = $ss + 1;
										}
									} else {
										$output .= '<tr><td colspan="4">لا يوجد سجلات ...</td></tr>';
									}
									echo $output;
									?>
								</tbody>
								<tfoot></tfoot>
							</table>
						</div>
					</div>
					<br>
					<div class="form-row">
						<div class="col">
							<h6>سندات الصرف</h6>
							<table class="table table-bordered table-hover ">
								<thead>
									<tr>
										<th class="text-center" width="6%">م</th>
										<th class="text-center" width="14%">المبلغ</th>
										<th class="text-center" width="14%">التاريخ</th>
										<th class="text-center" width="10%">كود</th>
										<th class="text-center" width="20%">موظف الفرع</th>
										<th class="text-center" width="12%">الفرع</th>
										<th class="text-center" width="12%">المردود</th>
									</tr>
								</thead>
						<tbody>
			<?php
					$output = '';
					$sql = $conn->prepare("SELECT outvoice.out_id, outvoice.money_out, outvoice.sanad_out, users.realName, branches.b_name, outvoice.payOutMethod
					FROM outvoice
					LEFT JOIN users ON users.user_Id = outvoice.father
					LEFT JOIN branches ON branches.branchID = outvoice.branch
					WHERE outvoice.contract_id = ? order by outvoice.sanad_out DESC  ");
					$sql->bindParam(1, $contract_id);
					try {
						$sql->execute();
					} catch (PDOException $e) {
						error_log('cont2debt sanad_out load failed: ' . $e->getMessage());
						$output .= '<tr><td colspan="7">تعذر تحميل سندات الصرف من قاعدة البيانات.</td></tr>';
						$sql = false;
					}
				if ($sql && $sql->rowCount() > 0) {
					$ss = 1;
				while ($row = $sql->fetch()) {
					   $out_id = $row['out_id'];
					   $bb = $row['payOutMethod'];
					switch ($bb) {
							case "1":
								$pay =  "كاش";
								$options = '<option selected value="1">كاش</option><option value="2" >بنكي </option>';
								break;
							case "2":
								$pay = "بنكي";
								$options = '<option value="1">كاش</option><option selected value="2" >بنكي </option>';
							}
					$output .= '<tr>
						 	<td style="padding:0px;margin:0px;"><button id="' . $out_id . '" style="font-weight:bold ;font-size:14px; padding:2;" class="btn btn-danger btn-ms btn-block out_edit ">' . $ss . '</button></td>
						 	<td style="padding:0px;margin:0px;"><input class="form-control" id="out' . $out_id . '" name="out' . $out_id . '" value="' . $row['money_out'] . '"/></td>
						 	<td>' . $row['sanad_out'] . '</td>
							 <td ><a  href="../rent/print/sanadout.php?sn=' . $out_id . '"target="_blank">'. $out_id . '</a></td>
						 	<td>' . $row['realName'] . '</td>
						 	<td>' . $row['b_name'] . '</td>
							 <td style="padding:0px;margin:0px;" contenteditable="true"><select id="payOut' . $out_id . '" class="form-control" style="background-color:transparent;padding:3px;margine:0px;" >
							 ' . $options . '</select></td>
						</tr>  ';
											$ss = $ss + 1;
										}
									} else {
										$output .= '<tr><td colspan="6">لا يوجد سجلات ...</td></tr>';
									}
									echo $output;
									?>
								</tbody>
								<tfoot></tfoot>
							</table>
						</div>
					</div>
				</div>
			</div> <!-- end of invoice table row -->
			<div class="modal fade" id="debit_vat" 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 debit_vat form -->
							<form method="post" class="form-horizontal" id="modal_debit_vat">
								<div id="debit_vat_msg"></div>
								<div class="form-row">
									<input type="hidden" class="form-control" id="id4" name="id4" />
									<input type="hidden" class="form-control" id="debit_id" name="debit_id" />
									<input type="hidden" class="form-control" id="vat_id2" name="vat_id2" />
									<div class="col-1">
										<label for="debit_money" 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="debit_money" name="debit_money" placeholder="بالريال" />
										<div class="text-danger" style="font-size:12px;" id="debit_money_msg"></div>
									</div>
									
								</div>
								<div class="form-row">
									<div class="col-1">
									<label for="comm" class="control-label text-primary">سبب الخصم</label>
									</div>
									<div class="col-11">
										<input type="text" class="form-control" id="comm" name="comm" placeholder="سبب الخصم" require />
									</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="debit_vat_add" name="debit_vat_add" class="btn btn-primary" data-dismiss="modal">حفظ <i class="fa fa-cloud" aria-hidden="true"></i></button>
								<a id="debit_vat_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 debit_vat 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('../rent/print/newCont_api.php?ct=' + contt)
				.then((data) => data.json())
				.then((data) => {
					document.getElementById('carid').value = data.carid;
					document.getElementById('rent_days').value = data.rent_days;
					document.getElementById('used_area').value = data.used_area;
					document.getElementById('free_dd').value = data.free_dd;
					document.getElementById('freekm').value = data.freekm;
					document.getElementById('minn').value = data.minn;
					document.getElementById('km3').value = data.km3;
					document.getElementById('tamm').value = data.tamm;

					document.getElementById('date_out').value = data.date_out;
					document.getElementById('date_in').value = data.date_in;
					document.getElementById('end').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').value = data.km_in;
					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('deduction').value = data.deduction;
					document.getElementById('wesam_ins').value = data.wesam_ins;
					document.getElementById('open_kilo').value = data.open_kilo;
					document.getElementById('cutt').value = data.cutt;
					document.getElementById('courts_in').value = data.courts_in;

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

					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("print3").setAttribute("href", "../rent/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", "../rent/car_check.php?dd=" + data.contract_id);
				}).catch(err => {
					console.log(err);
					if (window.contractDataLoaded) {
						return;
					}
					var msg = document.getElementById('koki_msg');
					if (msg) {
						msg.innerHTML = "<div class='alert alert-danger'>تعذر تحميل بيانات العقد. فضلا سجل الدخول مرة أخرى أو راجع رقم العقد.</div>";
					}
				});
		</script>
		<script>
			function gpsCheck() {
			event.preventDefault();
				let device_id = document.getElementById('gpsid').value;
				let from_date = document.getElementById('date_out').value;
				let to_date = 	document.getElementById('date_in').value;
				let from_time = document.getElementById('time_out').value;
				let to_time = document.getElementById('time_in').value;
				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() {
				$('#close').click(function() {
					close();
				});
				$('#close2').click(function() {
					close();
				});
				$("#debit_vat_add").click(function(event) {
					event.preventDefault();
					$("#debit_money_msg").html("");
					$.ajax({
						url: "debit_vat_action.php",
						method: "POST",
						
						data: $("#modal_debit_vat").serialize(),
						dataType: "json",
						success: function(data) {
							if (data.error) {
								$("#debit_money_msg").html(data.error.debit_money);
								$("#debit_vat_msg").delay(2500).slideUp(function() {
								$("#debit_vat_msg").html('');});
							} else {
								$('#debit_vat_add').css("display", "none");
								$('#debit_vat_print').css("display", "block");
								$("#debit_vat_msg").html(data.msg);
								$("#debit_id").val(data.debit_id);
								$("#debit_vat_print").attr("href", "../rent/print/debit_print.php?cr=" + data.debit_id);
								$("#debit_vat_msg").delay(2500).slideUp(function() {
								$("#debit_vat_msg").html('');
								});
								
							}
						}
						
					});
					location.reload();
				});


	$(".debit_vat").click('show.bs.modal', function(event) {
					event.preventDefault();
					var recipient = $("#contract_id").val();
					var vatid = $(this).attr('id');
					var id = $(this).attr('vatid');
					$('#vat_id2').val(id);
					$('#id4').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("");
					$.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);
							} else {
								$("#koki_msg").html(data.msg);
								$("#koki_msg").delay(3000).slideUp(function() {
									$("#koki_msg").html('');
								});
								location.reload();
							}
						}
					});
				});
				$("#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();
					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', '../rent/print2.php?contt=' + data.contract_id);
							$("#customer2_msg").delay(5000).slideUp(function() {
								$("#customer2_msg").html('');
							});
						}
					});
				}); //end of add customer2
				$(".in_edit").click(function(event) {
					event.preventDefault();
					if (confirm("هل تريد تحديث قيمة  و طريقة دفع هذا السند؟")) {
						var id = $(this).attr('id');
						var money_in = $("#mon" + id).val();
						var payMethod = $("#pay" + id).val();
						$.ajax({
							url: "sanad_in_update.php",
							method: "POST",
							data: {
								id: id,
								money_in: money_in,
								payMethod: payMethod
							},
							success: function(data) {
								location.reload();
							}
						});
					} else {
						return false;
					}
				}); //end of add customer2
				$(".out_edit").click(function(event) {
					event.preventDefault();
					if (confirm("هل تريد تعديل قيمة سند الصرف؟")) {
						var id = $(this).attr('id');
						var money_out = $("#out" + id).val();
						var payOutMethod = $("#payOut" + id).val();
						$.ajax({
							url: "sanad_out_update.php",
							method: "POST",
							data: {
								id: id,
								money_out: money_out ,
								payOutMethod : payOutMethod
							},
							success: function(data) {
								location.reload();
							}
						});
					} else {
						return false;
					}
				}); //end of add customer2

				$("#vat_add").click(function(event) {
					event.preventDefault();
					var contract_id = $("#contract_id").val();
					var end = $("#end").val();
					$.ajax({
						url: "../qrt/vat_actions.php",
						method: "POST",
						data: {
							contract_id: contract_id,
							end: end
						},
						success: function(data) {
							$("#koki_msg").html(data);
							$("#koki_msg").delay(2000).slideUp(function() {
								location.reload();
							});
						}
					});
				});
		$("#vat_ref").click(function(event) {
					event.preventDefault();
					var contract_id = $("#contract_id").val();
					var end = $("#end").val();
					$.ajax({
						url: "../qrt/vat_actions.php",
						method: "POST",
						data: {
							lastVat: contract_id,
							end: end
						},
						success: function(data) {
							$("#koki_msg").html(data);
							$("#koki_msg").delay(2000).slideUp(function() {
								location.reload();
							}
							);
						}
					});
		}); //end of Add vat inovice
				$(".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
				$("#newIssue").click(function(event) {
					event.preventDefault();
					var newIssue = $("#contract_id").val();
					$.ajax({
						url: "contract_update.php",
						method: "POST",
						data: {
							newIssue: newIssue
						},
						dataType: "json",
						success: function(data) {
							$("#koki_msg").html(data.msg);
							$("#koki_msg").delay(3000).slideUp(function() {
								$("#koki_msg").html('');
							});
						}
					});
				});
		$("#branch_btn").click(function(event) {
				event.preventDefault();
				if (confirm("هل تريد نقل العقد لفرع اخر ؟")) {
					var contract_id = $("#contract_id").val();
					var branch = $("#current_branch").val();
					var carid = $("#carid").val();
					var date_in = $("#date_in").val();
					$.ajax({
						url: "contract_update.php",
						method: "POST",
						data: {
							contract_id	: contract_id,
							branch		: branch,
							date_in 	: date_in,
							carid 		: carid
						},
					success: function(data) {
							location.reload();
						}
					});
			} else {return false;	}
			});
				$("#state").click(function(event) {
					event.preventDefault();
					var id = $("#contract_id").val();
					var state = $("#cont_status").val();
					$.ajax({
						url: "contract_update.php",
						method: "POST",
						data: {
							id: id,
							state: state
						},
						dataType: "json",
						success: function(data) {
							$("#koki_msg").html(data.msg);
							$("#koki_msg").delay(2000).slideUp(function() {
								location.reload();
							});
						}
					});
				});
				// متابعة محامي
				$("#state5").click(function(event) {
					event.preventDefault();
					if (confirm("هل تريد تحويل العقد لمتابعة محامي ؟")) {
					var id = $("#contract_id").val();
					var state5 = $("#cont_status").val();
					$.ajax({
						url: "contract_update.php",
						method: "POST",
						data: {
							id: id,
							state5: state5
						},
						dataType: "json",
						success: function(data) {
							$("#koki_msg").html(data.msg);
							$("#koki_msg").delay(2000).slideUp(function() {
								location.reload();
							});
						}
					});
				} else {return false;	}
				});
				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;
					}
				}

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

			}); // end of script
		</script>
</body>

</html>
