<?php
session_start();
ob_start();
$father = $_SESSION["user_id"];
if (!isset($_SESSION["user_id"])) {
	header("location:../logout.php");
}
include "../actions/dbPDO.php";
include "../op/month_history.php";
if (isset($_REQUEST["dd"])) {
	$myday = $_REQUEST["dd"];
	$car = $_REQUEST["cr"];
}
$first_day = date('Y-m-01', strtotime($myday));
$last_day 	= date('Y-m-t', strtotime($myday));
// $mms  = DateTime::createFromFormat( 'm-Y', $myday );
$mmm = new DateTime($myday);
$mms =  $mmm->format("m Y");

$ac = $db->prepare(" SELECT carid , kind , concat(letters,' ' ,numbers) as PL , model
												from cars  where carid = ? LIMIT 1 ");
$ac->bindParam(1, $car);
$ac->execute();
if ($ac->rowCount() > 0) {
	$day = $ac->fetch(PDO::FETCH_ASSOC);
	$kind = $day['kind'];
	$PL = $day['PL'];
	$model = $day['model'];
}
?>
<!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, 1500);">
	<div class="container-fluid">
		<h6 style="font-weight:bold;font-size:9pt;"> حركة السيارة خلال شهر <?php echo $mms . ' سيارة ' . $kind . ' لوحة رقم ' . $PL . ' موديل ' . $model; ?></h6>
		<div class="form-row" style="padding:0 ;">
			<div class="col-12">
				<table class="table table-bordered">
					<thead>
						<tr>
							<th class="text-center" width="12%">اليوم</th>
							<th class="text-center" width="10%">الحالة</th>
							<th class="text-center" width="12%">رقم العقد</th>
							<th class="text-center" width="10%">الفرع</th>
							<th class="text-center" width="16%">مصروفات</th>
							<th class="text-center" width="40%">ملاحظات</th>
						</tr>
					</thead>
					<tbody id="tstandby">
						<?php
						$output = '';
						$sql = $db->prepare("SELECT actions.ac_date ,actions.contract_id , carstatus.status , branches.b_name
										FROM actions,branches,carstatus
										WHERE actions.branch = branches.branchID AND actions.carstatus = carstatus.status_id
										AND actions.carid = ? AND actions.ac_date between ? AND ? ORDER BY actions.ac_date");
						$sql->bindParam(1, $car);
						$sql->bindParam(2, $first_day);
						$sql->bindParam(3, $last_day);
						$sql->execute();
				if ($sql->rowCount() > 0) {
						while ($row = $sql->fetch()) {
							$output .= '<tr>
											<td>' . $row['ac_date'] . '</td>
	 										<td>' . $row['status'] . '</td>
	 										<td>' . $row['contract_id'] . '</td>
	 										<td>' . $row['b_name'] . '</td>
											<td></td>
	 										<td></td>
	 									</tr>  ';
							}
							$branch = 0;
							$month =  month_history($car, $branch, $first_day, $last_day);
							$output .= '<tr>
											<td>أستعداد' . $month['standby'] . '</td>
											<td>مؤجرة' . $month['at_rent'] . '</td>
											<td>صيانة' . $month['ment'] . '</td>
											<td>إجمالي' . $month['tot_at_branch'] . '</td>
											<td>إيرادات' . $month['money'] . '</td>
											<td> مصروفات </td>
										</tr>  ';
						} else {
							$output .= '<tr><td colspan="6">لا يوجد سجلات ...</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>