<?php
require_once "../magic/session_config.php";
bdr_start_session();
ob_start();
$father = $_SESSION["user_id"];
$branch = $_SESSION['branch'];
if (!isset($_SESSION["user_id"])) {
	header("location:../logout.php");
}
include "../actions/dbPDO.php";
include "../op/aksat_wanted.php";
$today = date("Y-m-d");
?>
<!DOCTYPE html>
<html lang="ar" dir="rtl">

<head>
	<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
	<title>الأقساط مستحقة السداد</title>
	<link rel="stylesheet" href="../dist/css/bootstrap.css" />
	<link rel="stylesheet" href="../dist/css/day.css" media="print" />
</head>

<body onload="javascript: window.print(); setTimeout(window.close, 1000);">
	<div class="container-fluid">
		<h6 style="font-weight:bold;font-size:9pt;">الأقسط المستحقة حتي <?php echo $today; ?></h6>
		<div class="form-row" style="padding:0 ;">
			<div class="col">
	<table class="table table-bordered table-hover">
		<thead>
			<tr class="oh" height="30">
				<th class="align-middle text-center" width="5%">عقد</th>
				<th class="align-middle text-center" width="23%">الإسم</th>
				<th class="align-middle text-center" width="6%">متأخرات</th>
				<th class="align-middle text-center" width="9%">الجوال</th>
				<th class="align-middle text-center" width="12%">استحقاق</th>
				<th class="align-middle text-center" width="8%">طراز</th>
				<th class="align-middle text-center" width="5%">رقم</th>
				<th class="align-middle text-center" width="4%">موديل</th>
				<th class="align-middle text-center" width="7%">المدفوع</th>
				<th class="align-middle text-center" width="7%">إجمالي</th>
				<th class="align-middle text-center" width="4%">تأخير</th>
				<th class="align-middle text-center" width="10%">التفويض</th>
			</tr>
		</thead>
<tbody>
	<?php
	$output = '';
	$sql = $db->prepare("SELECT customers.cust_name ,customers.phone,cars.kind , cars.numbers , cars.model , aksat_contract.day_cost , aksat_contract.tamm_expire ,
				aksat_contract.contract_id , aksat_contract.start , aksat_contract.kest
				from customers , aksat_contract , cars
			 WHERE aksat_contract.cust_id = customers.cust_id AND aksat_contract.carid = cars.carid
				AND aksat_contract.status = 1 AND aksat_contract.branch = ? order by aksat_contract.contract_id ");
						$sql->bindParam(1, $branch);
						$sql->execute();
						if ($sql->rowCount() > 0) {
							while ($row = $sql->fetch()) {
								$ss = $row['contract_id'];
								$wk = next_wanted($ss);
								$tot_debt    = $wk["tot_debt"];
								$tot_payed   = $wk["tot_payed"];
								$tot_wanted  = $wk["tot_wanted"];
								$late        = $wk["late"];
								$next_ak_id  = $wk["next_ak_id"];
								$next_wanted = $wk["next_wanted"];
								$next_start  = $wk["next_start"];
								$next_ak_no  = $wk["next_ak_no"];
								$date1 = new DateTime($row['tamm_expire']);
								$date2 = new DateTime(date("Y-m-d"));
								$qty = $date2->diff($date1)->format("%a");

								if ($late > 0) {
									$style = 'style="color:#DA2D07; background-color:#F9F5F5;font-weight:bold;"';
								} else {
									$style = '';
									continue;
								}
				$output .= '<tr height="25"><td>' . $ss . '</td>
							<td>' . $row['cust_name'] . '</td>
							<td ' . $style . '>' . $tot_debt . '</td>
							<td>' . $row['phone'] . '</td>
							<td ' . $style . '>' . $next_start . '</td>
							<td class="text-center" >' . $row['kind'] . '</td>
							<td class="text-center" >' . $row['numbers'] . '</td>
							<td class="text-center" >' . $row['model'] . '</td>
							<td class="text-center" >' . $tot_payed . '</td>
							<td class="text-center" >' . $tot_wanted . '</td>
							<td class="text-center" ' . $style . ' >' . $late . '</td>';
		if (($qty < 3) || ($date1 < $date2)) {
			$output .= '    <td class="text-center" style="color:#DA2D07;background-color:#F0F7E4;" >' . $row['tamm_expire'] . '</td>   </tr>';
		} else {
			$output .= '    <td class="text-center">' . $row['tamm_expire'] . '</td>  </tr>';
		}
	}
						} else {
							$output .= '<tr><td colspan="13">لا يوجد سجلات ...</td></tr>';
						}
						echo $output;
						?>
					</tbody>
					<tfoot></tfoot>
				</table>
			</div>
		</div> <!-- end of table row -->
	</div><!-- main fluied container -->
	<script src="../js/jquery.js"></script>
	<script src="../dist/js/umd/popper.min.js"></script>
	<script src="../dist/js/bootstrap.js"></script>
</body>

</html>