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

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

if (!isset($_SESSION)) {
}
$contract_id = $_REQUEST['id'];



$branchCONT = $conn->prepare('SELECT father,cont_status from contracts WHERE contract_id = ?');
$branchCONT->bindParam(1, $contract_id);
$branchCONT->execute();
if ($branchCONT->rowCount() < 1) {
	header( "refresh:1;url=../profile.php" );
}else{
	$row = $branchCONT->fetch(PDO::FETCH_ASSOC);
		$state = $row['cont_status'];
		if($state > 4){
	if (!bdr_user_has_privilege($conn, $father, 34)) {
		header( "refresh:1;url=../not_auth.php" );
		}		
			
		}	
}
?>
<!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.min.css" />
	<link rel="stylesheet" href="../css/font-awesome.min.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>
</head>
<style>
	.control-label,
	.table,
	.h6,
	h6 {
		color: #A9A9A7;
	}

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

	a:hover {
		color: green;
	}
</style>

<body style="background-color:#002933;">
	<div class="container-fluid" style="background-color:black;">
		<br />
		<div class="form-row align-items-center">
			<div class="col-2">
				<input style="margin-right:20px;" type="submit" class="btn btn-danger" id="close2" value=" خروج " />
			</div>
			<div class="col-8 text-center">
				<h3 style="font-weight:bold; color:#EDDFDF;" id="title">متابعة عقد رقم <?php echo $contract_id; ?></h3>
			</div>
			<div class="col-2 text-left">
				<button  type="submit" class="btn btn-outline-danger" id="checkin">تشييك</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, 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);
			$cust_id = $row['cust_id'];
			$phone = $row['phone'];
			$phone2 = $row['phone2'];
			$whats = substr($phone, 1);
			$whats = "9660" . $whats;
			$whats2 = "";
			if (!empty($phone2)) {
				$whats2 = substr($phone2, 1);
				$whats2 = "9660" . $whats2;
			}
		?>
			<div class="form-row align-items-center" style="margin-top:10px;">
				<div class="col-1">
					<a href="cust_update.php?id=<?php echo $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;">
						<?php 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>~ سيارة " . $row['kind'] . " " . $row['numbers'] . " موديل " . $row['model'];
						$cust22 = $row['cust_id2'];
						?>
					</center>
				</div>
				<div class="col-1 text-left">
					<a href="../ment/car_contracts.php?dd=<?php echo $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="../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:#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     = $monn["debt"];
		}
		?>
	</div>
	<p><br /></p>
	<style>
		.control-label,
		.table,
		.h6,
		h6 {
			color: #EDDFDF;
		}
	</style>
	<div class="container-fluid">
		<div class="row">
			<div class="col-2">
				<div class="row">
					<div class="col-11">
						<label for="used_area" class="control-label">منطقة الإستخدام</label>
					</div>
				</div>
				<div class="form-row">
					<div class="col">
						<input type="text" class="form-control" id="used_area" name="used_area" style="float:right;" disabled="disabled" readonly />
					</div>
				</div>
				<div class="form-row">
					<div class="col">
						<input type="text" class="form-control" id="rent_days" name="rent_days" disabled="disabled" readonly />
					</div>
				</div>
				<div class="form-row">
						<div class="col-12">
							<input type="text" class="form-control" id="tamm" name="tamm" disabled="disabled" readonly placeholder="رقم العقد الإلكتروني" />
						</div>
				</div>
				<div class="form-row">
					<div class="col-5">
						<label for="wanted" class="control-label" style="margin-top:5px;padding:2px;">المطلوب</label>
					</div>
					<div class="col-6">
						<input type="number" class="form-control wanted" style="float:right;" disabled="disabled" readonly />
					</div>
				</div>
				<div class="form-row" style="padding:2px;">
					<div class="col-11">
						<button type="button" class="btn btn-outline-primary btn-block" id="case_new_btn" data-toggle="modal" style="float:left;font-weight:bold;" data-backdrop="static" data-keyboard="false" data-target="#case_new">تسجيل معاملة</button>
					</div>
				</div>
				<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">
						<a id="print3" href="" target="_blank"><button type="button" class="btn btn-outline-success btn-block">طباعة العقد <span style="margin-right:20px;" class="glyphicon glyphicon-print" aria-hidden="true"></span></button></a>
					</div>
				</div>
				<!--<div class="form-row" style="margin-top:3px;">-->
				<!--	<div class="col-11">-->
				<!--		<button type="button" class="btn btn-outline-danger btn-block" data-toggle="modal" style="float:left;font-weight:bold;" data-backdrop="static" data-keyboard="false" data-target="#court_in">سند قبض</button>-->
				<!--	</div>-->
				<!--</div>-->
				<div class="row" style="padding:2px;">
					<div class="col-11">
						<a id="acc" href="" target="_blank"><button type="button" class="btn btn-outline-success btn-block">كشف حساب <span style="margin-right:20px;" class="glyphicon glyphicon-print" aria-hidden="true"></span></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" 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="row" style="padding:2px;">
					<div class="col-11">
						<input style="float:right ;font-weight:bold;" type="button" 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="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" />
					<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 for="" 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" class="form-control" id="day_cost" name="day_cost" disabled="disabled" readonly />
						</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" class="form-control" id="km_in" name="km_in" disabled="disabled" readonly />
						</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" class="form-control" id="kilo_cost" name="kilo_cost" disabled="disabled" readonly />
						</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" class="form-control" id="free_kilo" name="free_kilo" disabled="disabled" readonly />
						</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" disabled="disabled" readonly />
						</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" 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="dept_comm" class="control-label" style="float:left;">الإدارة</label>
								</div>
								<div class="col-9">
									<textarea class="form-control" rows="2" 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="3" id="operating_comm" name="operating_comm" placeholder="تعليمات التشغيل"></textarea>
								</div>
							</div>
							<div class="form-row">
								<div class="col-3">
									<label for="open_comm" class="control-label" style="float:left;">ملاحظات</label>
								</div>
								<div class="col-9">
									<textarea class="form-control" rows="2" id="open_comm" name="open_comm" disabled="disabled" readonly placeholder="عند فتح العقد"></textarea>
								</div>
							</div>
						</div>
						<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()" id="addtion_cost" name="addtion_cost" disabled="disabled" readonly />
									<div class="text-danger" style="font-size:12px;" id="addtion_cost_msg"></div>
								</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" disabled="disabled" readonly />
									<div class="text-danger" style="font-size:12px;" id="violations_msg"></div>
								</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" />
								</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 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="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>
							<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="comm" class="control-label" style="float:left;">ملاحظات</label>
						</div>
						<div class="col-10">
							<input type="text" class="form-control" id="comm" name="comm" name="wanted" disabled="disabled" readonly />
							<div class="text-danger" style="font-size:12px;" id="comm_msg"></div>
						</div>
					</div>
					<p><br /></p>
				</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="12%">المبلغ</th>
							<th class="text-center" width="15%">التاريخ</th>
							<th class="text-center" width="12%">كود</th>
							<th class="text-center" width="24%">المستلم</th>
							<th class="text-center" width="13%">الفرع</th>
							<th class="text-center" width="18%">طريقة الدفع</th>
						</tr>
					</thead>
					<tbody id="tstandby">
						<?php
						$output = '';
						$sql = $conn->prepare("SELECT in_id , money_in , sanad_date , realName , payMethod ,b_name  FROM sanad_in
					WHERE contract_id = ? order by sanad_date DESC ");
						$sql->bindParam(1, $contract_id);
						$sql->execute();
						if ($sql->rowCount() > 0) {
							$ss = 1;
							while ($row = $sql->fetch()) {
								$bb = $row['payMethod'];
								switch ($bb) {
									case "1":
										$pay = "كاش";
										break;
									case "2":
										$pay = "تحويل الراجحي";
										break;
									case "3":
										$pay = "نقاط بيع";
										break;
									case "5":
										$pay = "تحويل الأهلي";
										break;
								}
								$output .= '<tr>
							<td>' . $ss . '</td>
							<td>' . $row['money_in'] . '</td>
							<td>' . $row['sanad_date'] . '</td>
							<td>' . $row['in_id'] . '</td>
							<td>' . $row['realName'] . '</td>
							<td>' . $row['b_name'] . '</td>
							<td>' . $pay . '</td>
						</tr>  ';
								$ss = $ss + 1;
							}
						} else {
							$output .= '<tr><td colspan="7">لا يوجد سجلات ...</td></tr>';
						}
						echo $output;
						?>
					</tbody>
					<tfoot></tfoot>
				</table>
			</div>
			<div class="col-6" style="float:right;">
			<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 class="row">
				<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 id="tstandby">
						<?php
					$output = '';
					$sql = $conn->prepare("SELECT out_id , money_out , sanad_out , realName , b_name ,payOutMethod FROM sanad_out WHERE contract_id = ? order by sanad_out DESC  ");
					$sql->bindParam(1, $contract_id);
					$sql->execute();
				if ($sql->rowCount() > 0) {
					$ss = 1;
				while ($row = $sql->fetch()) {
					   $out_id = $row['out_id'];
					   $bb = $row['payOutMethod'];
					switch ($bb) {
							case "1":
								$pay =  "كاش";
								break;
							case "2":
								$pay = "بنكي";
							}
					$output .= '<tr>
						 	<td style="padding:0px;margin:0px;"><button style="font-weight:bold ;font-size:14px; padding:2;" class="btn btn-danger btn-ms btn-block">' . $ss . '</button></td>
						 	<td>' . $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>' . $pay . '</td>
						</tr>  ';
											$ss = $ss + 1;
										}
									} else {
										$output .= '<tr><td colspan="6">لا يوجد سجلات ...</td></tr>';
									}
									echo $output;
									?>
					</tbody>
					<tfoot></tfoot>
				</table>
						</div>
			</div>
		</div> <!-- end of invoice table row -->
	</div><!-- end of fluid2  -->
	<!-- create new case for jaudgment  -->
	<div class="modal fade" id="case_new" 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" style="background-color:#000 ;">
					<!-- start of case_new form -->
					<form method="post" class="form-horizontal" id="modal_case_new">
						<div id="case_new_msg"></div>
						<div class="form-row">
							<input type="hidden" class="form-control" id="id2" name="id2" />
							<input type="hidden" id="case_id" name="case_id" />
							<div class="col-1 text-left">
								<label for="case_no" class="control-label">رقم المعاملة</label>
							</div>
							<div class="col-2">
								<input type="number" class="form-control" id="case_no" name="case_no" />
							</div>
							<div class="col-1 text-left">
								<label for="money_wanted" class="control-label">المبلغ</label>
							</div>
							<div class="col-2">
								<input type="number" class="form-control" id="money_wanted" name="money_wanted" />
							</div>
							<div class="col-2"></div>
							<div class="col-2">
								<label for="start" class="control-label text-center">تاريخ الرفع</label>
							</div>
							<div class="col-2">
								<input type="text" class="form-control" id="start" name="start" value="<?php echo date("Y-m-d"); ?>" />
							</div>
						</div>
						<div class="form-row" style="padding:5px;">
							<div class="col-1">
								<label for="court_id" class="control-label" style="float:left;">المحكمة</label>
							</div>
							<div class="col-6">
								<select class="form-control" id="court_id" name="court_id" style="padding:3px;">
									<?php
									$sql = $conn->prepare("SELECT court_id , concat(name,' # ',address) as tname from courts");
									$sql->execute();
									if ($sql->rowCount() > 0) {
										while ($row = $sql->fetch()) {
											$court_id   = $row["court_id"];
											$tname  	= $row["tname"];
											echo "<option value= $court_id >$tname</option>";
										}
									} ?>
								</select>
							</div>
							<div class="col-1">
								<label for="floor" class="control-label" style="float:left;">الدور</label>
							</div>
							<div class="col-1">
								<input type="number" class="form-control" id="floor" name="floor" value="2" />
							</div>
							<div class="col-1">
								<label for="zone" class="control-label" style="float:left;">الدائرة</label>
							</div>
							<div class="col-1">
								<input type="number" class="form-control" id="zone" name="zone" value="2" />
							</div>
						</div>
						<div class="row" style="padding:5px;">
							<div class="col-1">
								<label for="start_comm" class="control-label" style="float:left;">ملاحظات</label>
							</div>
							<div class="col-11">
								<textarea class="form-control" rows="8" id="start_comm" name="start_comm" placeholder="عند بدأ المعاملة"></textarea>
							</div>
						</div>
					</form><!-- end of case_new form -->
				</div>
				<div class="modal-footer" style="background-color:teal ;">
					<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="case_new_add" name="case_new_add" class="btn btn-primary">حفظ <i class="fa fa-cloud" aria-hidden="true"></i></button>
						<!-- <a id="case_new_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 case_new modal -->
	</div> <!-- modal case end -->
	</div>
	<script>
		var contt = document.getElementById('contract_id').value;
		fetch('../rent/print/newCont_api.php?ct=' + contt)
			.then((data) => data.json())
			.then((data) => {
				// $('#print3').attr('href','../rent/print/closeContract.php?contt='+data.contract_id);
				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('wesam_ins').value = data.wesam_ins;
				document.getElementById('open_kilo').value = data.open_kilo;
				document.getElementById('deduction').value = data.deduction;
				document.getElementById('cutt').value = data.cutt;

				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('courts_in').value = data.courts_in;

				document.getElementById('dept_comm').value = data.dept_comm;
				document.getElementById('operating_comm').value = data.operating_comm;
				document.getElementById('open_comm').value = data.open_comm;
				$('#newIssue').attr('href', '../jaudge/newIssue.php?id=' + data.contract_id);
				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);
				if (data.cont_status == 4) {
					$("#case_new_btn").css("display", "block");
					$("#newIssue").css("display", "none");
				}
			}).catch(err => console.log(err));
	</script>
	<script>
		$(document).ready(function() {
			$('#close').click(function() {
				close();
			});
			$('#close2').click(function() {
				close();
			});
			$("#checkin").click(function(event) {
				event.preventDefault();
				window.open("../rent/print/checkin.php", '_blank');
			});
			$("#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('');
						});
					}
				});
			});

			$("#contract_update").click(function(event) {
				event.preventDefault();
				$("#operating_comm_msg").html("");
				$("#ins_claim_msg").html("");
				$.ajax({
					url: "contract_update.php",
					method: "POST",
					data: $("#koki").serialize(),
					dataType: "json",
					success: function(data) {
						if (data.error) {
							$("#operating_comm_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();
						}
					}
				});
			});
			$("#case_new_add").click(function(event) {
				event.preventDefault();
				var form = new FormData();
				form = $("#modal_case_new").serialize();
				$.ajax({
					url: "case_new.php",
					method: "POST",
					data: form,
					success: function(data) {
						$("#case_new_msg").html(data);
					},
					error: function(data) {
						alert(data.statusText);
					}
				})
			});
			$(".vat_print").click(function(event) {
				event.preventDefault();
				var dd = $(this).attr('id');
				window.open("../rent/print/vat_print.php?vt=" + dd, '_blank');
			});
			$("#court_in_add").click(function(event) {
				event.preventDefault();
				$.ajax({
					url: "court_in_action.php",
					method: "POST",
					data: $("#modal_sanad_in").serialize(),
					dataType: "json",
					success: function(data) {
						$('#court_in_add').css("display", "none");
						$('#sanad_in_print').css("display", "block");
						$("#sanad_in_msg").html(data.msg);
						$("#sanad_id").val(data.sanad_id);
						$("#sanad_in_print").attr("href", "print/courtsin.php?sn=" + data.sanad_id);
					}
				});
			});
			$('#court_in').on('show.bs.modal', function(event) {
				var recipient = $("#contract_id").val();
				var modal = $(this);
				modal.find('#id2').val(recipient);
			});
		}); // end of script
	</script>
</body>

</html>
