<?php

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

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

// تحقق من وجود المستخدم في الجلسة
if (empty($_SESSION["user_id"])) {
	header("Location: ../logout.php");
	exit;
}

$branch = $_SESSION["branch"] ?? null; // استخدام null إذا لم تكن معرفة


try {

	// تحقق من صلاحيات المستخدم
} catch (PDOException $e) {
	// معالجة الخطأ بطريقة آمنة
	error_log("Database error: " . $e->getMessage());
	header("Location: ../error.php");
	exit;
}

if (isset($_REQUEST["day"])) {
	$myday = $_REQUEST["day"];
} else {
	$myday = date("Y-m-d");
}
$first_day = date('Y-m-01', strtotime($myday));
?>
<!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.min.css" />
	<link rel="stylesheet" href="../dist/css/day.css" />
</head>
<!-- onload="javascript: window.print(); setTimeout(window.close, 1000);" -->

<body>
	<div class="container-fluid">
		<div class="row" style="padding:5 ;">
			<div class="col-xs-10" style="float:right;">
				<h6 style="font-weight:bold;padding:3px;font-size:10pt;">حركة عمل السبت من <?= $first_day ?></h6>
			</div>
			<div class="col-xs-2" style="float:right;">
			</div>
		</div>
		<div class="table-responsive">
			<table class="table table-bordered table-hover">
				<thead>
					<tr>
						<th class="text-center" rowspan="2" width="5%"> م </th>
						<th class="text-center" rowspan="2" width="10%">تاريخ الأسبوع</th>
						<th class="text-center" colspan="2" width="14%">فلسطين</th>
						<th class="text-center" colspan="2" width="14%">الأربعين</th>
						<th class="text-center" colspan="2" width="14%">الفيحاء</th>
						<th class="text-center" colspan="2" width="14%">صقر قريش</th>
						<th class="text-center" rowspan="2" width="5%">اجمالي</th>
						<th class="text-center" rowspan="2" width="30%">ملاحظات</th>
					</tr>
					<tr>
						<th class="text-center" width="7%"> فتح </th>
						<th class="text-center" width="7%">اغلاق</th>
						<th class="text-center" width="7%"> فتح </th>
						<th class="text-center" width="7%">اغلاق</th>
						<th class="text-center" width="7%"> فتح </th>
						<th class="text-center" width="7%">اغلاق</th>
						<th class="text-center" width="7%"> فتح </th>
						<th class="text-center" width="7%">اغلاق</th>
					</tr>
				</thead>
				<tbody>
	<?php
	$output = '';
	$st = $conn->prepare("WITH RECURSIVE dates AS (
			SELECT DATE(?) AS dt
			UNION ALL
			SELECT DATE_ADD(dt, INTERVAL 1 DAY)
			FROM dates
			WHERE dt < CURDATE()
			)
			SELECT dt
			FROM dates
			WHERE DAYOFWEEK(dt) = 7;");
					$st->execute([$first_day]);
					if ($st->rowCount() > 0) {
						$nn = 0;
						$a = $b= $c = $d = $e =$f =$g = $h = $toty =0;
		while ($row = $st->fetch()) {
							$nn++;
				$saturday = $row['dt'];
				$master = $conn->prepare("SELECT COUNT(contract_id) as qty 
							FROM contracts 
						WHERE date_out = ? 
						AND time_out BETWEEN '06:00' AND '12:00' AND open_at = ?;");

				$back = $conn->prepare("SELECT COUNT(contract_id) as qty 
					FROM contracts 
					WHERE date_in = ? 
					AND time_in BETWEEN '06:00' AND '12:00' AND branch = ?;");

				$master->execute([$saturday,2]); 
					$dat = $master->fetch();
					$plastine_out = $dat ? $dat['qty'] : 0;
				
				$back->execute([$saturday,2]); 
					$dat = $back->fetch();
					$plastine_back = $dat ? $dat['qty'] : 0;
					//
				$master->execute([$saturday,3]); 
					$dat = $master->fetch();
					$arbeen_out = $dat ? $dat['qty'] : 0;
				
				$back->execute([$saturday,3]); 
					$dat = $back->fetch();
					$arbeen_back = $dat ? $dat['qty'] : 0;
//fayhaa
					$master->execute([$saturday,7]); 
					$dat = $master->fetch();
					$fay_out = $dat ? $dat['qty'] : 0;
				
				$back->execute([$saturday,7]); 
					$dat = $back->fetch();
					$fay_back = $dat ? $dat['qty'] : 0;
					//Saquer
					$master->execute([$saturday,9]); 
					$dat = $master->fetch();
					$saq_out = $dat ? $dat['qty'] : 0;
				
				$back->execute([$saturday,9]); 
					$dat = $back->fetch();
					$saq_back = $dat ? $dat['qty'] : 0;
$tot = $plastine_out+$plastine_back+$arbeen_out+$arbeen_back+$fay_out+$fay_back+$saq_out + $saq_back;
				$output .= '<tr>
								<td>' .$nn. '</td>
								<td>' .$saturday. '</td>
								<td>'.$plastine_out.'</td>
								<td>'.$plastine_back.'</td>
								<td>'.$arbeen_out.'</td>
								<td>'.$arbeen_back.'</td>
								<td>'.$fay_out.'</td>
								<td>'.$fay_back.'</td>
								<td>'.$saq_out.'</td>
								<td>'.$saq_back.'</td>
								<td>'.$tot.'</td>
								<td class="text-center" ></td>
							</tr>  ';
							$a= $a+$plastine_out;
							$b= $b+$plastine_out;
							$c =$c +$arbeen_out;
							$d =$d + $arbeen_back;
							$e =$e + $fay_out;
							$f = $f + $fay_back ;
							$g = $g+  $saq_out;
							$h = $h + $saq_back ;
							$toty =  $toty+$tot ;

						}
			$output .= '<tr><td colspan="11" ></td></tr>';
			$output .= '<tr>
							<td colspan="2"></td>
							<td>'.$a.'</td>
							<td>'.$b.'</td>
							<td>'.$c.'</td>
							<td>'.$d.'</td>
							<td>'.$e.'</td>
							<td>'.$f.'</td>
							<td>'.$g.'</td>
							<td>'.$h.'</td>
							<td>'.$toty.'</td>
							<td ></td>
						</tr>  ';
					} else {
						$output .= '<tr><td colspan="11">لا يوجد سجلات ...</td></tr>';
					}
					echo $output;
					?>
				</tbody>
				<tfoot></tfoot>
			</table>
		</div>
	</div>
	<script src="../js/jquery.js"></script>
	<script src="../dist/js/umd/popper.min.js"></script>
	<script src="../dist/js/bootstrap.min.js"></script>
</body>

</html>