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

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

$contract_id = $_REQUEST['id'];
?>
<!DOCTYPE html>
<html lang="ar" dir="rtl">

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta name="robots" content="index,follow">
	<meta name="author" content="سامح دياب">
	<link rel="icon" href="../images/perfect.ico">
	<title>كشف حساب عقد</title>
	<link rel="stylesheet" href="../dist/css/bootstrap.css" />
	<link rel="stylesheet" href="../css/font-awesome.min.css">
	<link rel="stylesheet" href="../hj2/humanity.calendars.picker.css">
	<link rel="stylesheet" href="../css/myCar.css" />
	<script src="../js/jquery.js"></script>
	<script src="../dist/js/umd/popper.min.js"></script>
	<script src="../dist/js/bootstrap.js"></script>
	<script src="../hj2/jquery.plugin.js"></script>
	<script src="../hj2/jquery.calendars.js"></script>
	<script src="../hj2/jquery.calendars.plus.js"></script>
	<script src="../hj2/jquery.calendars.picker.js"></script>
	<script src="../hj2/jquery.calendars.ummalqura.js"></script>
</head>

<body>
	<div class="container-fluid">
		<br />
		<div class="form-row align-items-center">
			<div class="col text-center">
				<h5 style="font-weight:bold;" id="title"> كشف حساب عقد رقم <?php echo $contract_id; ?></h5>
			</div>
		</div>
		<?php
		$sql = $db->prepare('SELECT aksat_contract.contract_id, aksat_contract.cust_id2,cars.carid,customers.cust_id ,customers.cust_name,customers.phone , customers.phone2, cars.kind ,
			 concat(cars.letters ," ", cars.numbers) as PL , cars.model,aksat_contract.status , aksat_contract.used_area ,aksat_contract.rent_days , aksat_contract.hj
			   FROM cars,aksat_contract,customers
	    WHERE aksat_contract.carid = cars.carid AND aksat_contract.cust_id = customers.cust_id AND aksat_contract.contract_id = ? ');
		$sql->bindParam(1, $contract_id);
		$sql->execute();
		if ($sql->rowCount() > 0) {
			$row = $sql->fetch(PDO::FETCH_ASSOC);
			$cust_id = $row['cust_id'];
		?>
			<div class="form-row" style="margin-top:8px;">
				<div class="col">
					<input type="hidden" id="hjv" name="hjv" value="<?php echo $row['hj']; ?>" />
					<center class="control-label" style="font-size:0.95em; padding:0px; margin:0;font-weight:bold;">
						<?php echo $row['cust_name'] . " ~ " . $row['phone'] . " ~ " . $row['phone2'] . "  ~ سيارة " . $row['kind'] . " " . $row['PL'] . " موديل " . $row['model'];
						$cust22 = $row['cust_id2'];
						?>
					</center>
				</div>
			</div>
		<?php		}?>
	</div>
	<div class="container-fluid" style="margin-top:10px;">
		<?php
		$bb = $db->prepare("SELECT sum(wanted) as tot_wanted , sum(payed) as tot_payed from aksat_in where contract_id = ?  LIMIT 1");
		$bb->bindParam(1, $contract_id);
		$bb->execute();
		if ($bb->rowCount() > 0) {
			$bbc = $bb->fetch(PDO::FETCH_ASSOC);
			$tot_wanted     = $bbc["tot_wanted"];
			$tot_payed     = $bbc["tot_payed"];
		}
		?>
		<div class="form-row align-items-center">
			<div class="col text-center">
				<label class="control-label " style="font-size:0.8em; font-weight:bold;">قيمة السيارة <?php echo $tot_wanted; ?> المدفوع <?php echo $tot_payed; ?> المتبقي من ثمن السيارة <?php echo $tot_wanted - $tot_payed; ?> </label>
			</div>
		</div>
		<br />
		<div class="form-row">
			<table class="table table-bordered table-hover">
				<thead>
					<tr>
						<th class="text-center" width="6%">كود</th>
						<th class="text-center" width="4%">قسط</th>
						<th class="text-center" width="16%">استحقاق</th>
						<th class="text-center" width="7%">المستحق</th>
						<th class="text-center" width="6%">سند</th>
						<th class="text-center" width="18%">تاريخ</th>
						<th class="text-center" width="7%">المدفوع</th>
						<th class="text-center" width="4%">تأخير</th>
						<th class="text-center" width="10%">سداد</th>
						<th class="text-center" width="18%">تعليق</th>
					</tr>
				</thead>
				<tbody>
					<?php
					$output = '';
					$sql = $db->prepare("SELECT *	FROM aksat_in	WHERE contract_id = ? ");
					$sql->bindParam(1, $contract_id);
					$sql->execute();
					if ($sql->rowCount() > 0) {
						while ($row = $sql->fetch()) {
							$bb = $row['payMethod'];
							if (!empty($bb)) {
								switch ($bb) {
									case "1":
										$pay =  "كاش";
										break;
									case "2":
										$pay = "تحويل الراجحي";
										break;
									case "3":
										$pay = "نقاط بيع";
										break;
									case "5":
										$pay = "تحويل الأهلي";
										break;
								}
							} else {
								$pay = "";
							}
							$output .= '<tr><td>' . $row['ak_id'] . '</td>
													<td>' . $row['ak_no'] . '</td>
													<td style="padding:0px; margin:0px;"><input class="hj1 form-control"name="hj1[]" value="' . $row['start'] . '"/></td>
													<td>' . $row['wanted'] . '</td>
													<td>' . $row['printed_no'] . '</td>
													<td style="padding:0px; margin:0px;"> <input class="hj2 form-control"name="hj2[]" value="' . $row['date_payed'] . '"/></td>
													<td>' . $row['payed'] . '</td>
													<td>' . $row['late'] . '</td>
													<td>' . $pay . '</td>
													<td>' . $row['comm'] . '</td>
											</tr>';
						}
					} else {
						$output .= '<tr><td colspan="10">لا يوجد سجلات ...</td></tr>';
					}
					echo $output;
					?>
				</tbody>
				<tfoot></tfoot>
			</table>
		</div>
	</div>
	<script>
		$(document).ready(function() {
			function change_date(date) {
				var date = date.split('-');
				var year = parseInt(date[0], 10);
				var name = 'ummalqura';
				if (year > 1800) {
					name = 'gregorian';
				}
				var month = parseInt(date[1], 10);
				var day = parseInt(date[2], 10);
				try {
					var jd = $.calendars.instance(name).newDate(year, month, day).toJD();
				} catch (e) {
					day = day - 1;
					try {
						var jd = $.calendars.instance(name).newDate(year, month, day).toJD();
					} catch (e) {
						day = day - 1;
						try {
							var jd = $.calendars.instance(name).newDate(year, month, day).toJD();
						} catch (e) {
							alert(e);
							return;
						}
					}
				}
				name == 'ummalqura' ? name = 'gregorian' : name = 'ummalqura';
				var re_date = $.calendars.instance(name).fromJD(jd);
				try {
					re_date = re_date.formatYear() + '-' + re_date.month() + '-' + re_date.day();
				} catch (e) {
					alert(e);
					return null;
				}
				return re_date;
			}
			var ss = $("#hjv").val();
			if (ss == 1) {
				$('.hj1').each(function() {
					var dd = $(this).val();
					dd = change_date(dd);
					$(this).val(dd);
				});

				$('.hj2').each(function() {
					var dd = $(this).val();
					if (dd == "") {} else {
						dd = change_date(dd);
						$(this).val(dd);
					}
				});
			}
			javascript: window.print();
			setTimeout(window.close, 1000);
		});
	</script>
</body>
</html>