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

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

if (!isset($_SESSION)) {
}

$cust_id = $_REQUEST['cust'];

require_once "../op/wanted.php";


// Get customer data
require_once("../magic/Customers.php");
$table = "customers";
$customer = new Customers($dataBase, $table);
$cust = $customer->singleCust($cust_id);

if (!$cust) {
    echo "لا يوجد مستأجر بهذا الرقم";
    exit;
}
?>
<!DOCTYPE html>
<html lang="ar" dir="rtl">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>عقود العميل</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.rtl.min.css" rel="stylesheet">
    <link rel="stylesheet" href="./../css25/myCar.css" />
    <link rel="stylesheet" href="./../css25/drop.css" />
    <link rel="icon" href="./../images/perfect.ico">
    <style>
      
    </style>
</head >
<body style="background-color: #00324d;">

<div class="container mt-5" >
<div class="row">
<div class="col-md-2 mb-3">
    <button type="submit" id="homeBtn" class="btn btn-primary">الشاشة الرئيسية</button>
</div>
<div class="col-md-8 mb-8 text-center">
    <h3 style="color: #cce5ff;" >عقود / <?= $cust['cust_name'] ; ?></h3>
</div>
<div class="col-md-2 mb-3">
    <button type="submit" id="customersBtn" class="btn btn-success">شاشة العملاء</button>
</div>
</div>
<hr/>
    <div class="mb-3">
        <input type="text" id="searchInput" class="form-control" placeholder="بحث...">
    </div>
    <!-- table-striped -->
    <div class="panel-body" style="background-color:#00324d ; color:#cce5ff;">
    <!-- <div class="table-responsive"> -->
    <table class="table table-responsive table-bordered table-hover" id="contractsTable">
        <thead class="sticky-top top-0">
            <tr class="table-info">
                <th class="text-center" width="4%">م</th>
				<th class="text-center" width="7%">رقم العقد</th>
				<th class="text-center" width="7%">متأخرات</th>
				<th class="text-center" width="9%">إغلاق العقد</th>
				<th class="text-center" width="8%">طراز</th>
				<th class="text-center" width="6%">رقم</th>
				<th class="text-center" width="5%">موديل</th>
				<th class="text-center" width="5%">يومي</th>
				<th class="text-center" width="5%">أيام</th>
				<th class="text-center" width="6%">إضافي</th>
				<th class="text-center" width="30%">بيان الإضافي</th>
				<th class="text-center" width="7%">المدفوع</th>
            </tr>
        </thead>
        <tbody>
            <?php
		$output = '';
		$sql = $conn->prepare("SELECT contracts.contract_id ,contracts.date_in ,contracts.day_cost , contracts.addtion_desc, cars.kind , cars.numbers , cars.model , contracts.day_cost
						from  contracts , cars
						WHERE contracts.carid = cars.carid
						AND (contracts.cust_id = ? OR contracts.cust_id2 = ?)   ORDER BY contracts.date_out DESC LIMIT 55");
							$sql->bindParam(1, $cust_id);
							$sql->bindParam(2, $cust_id);
							$sql->execute();
							if ($sql->rowCount() > 0) {
								$nn = 0;
								while ($row = $sql->fetch()) {
									$nn++;
									$ss = $row['contract_id'];
									$wanted    = new  Wanted($conn, $ss);
									$pay       = $wanted->wanted();
									$ww        = $pay["wanted"];
									$tot_add   = $pay["tot_addtion"];
									$days_qty  = $pay["days_qty"];
									$output .='<tr><td>'.$nn.'</td>';
									if ($ww > 0) {
										$output .= '<td>
									<a href="../jaudge/cont2debt.php?id=' . $ss . '"target="_blank"><button type="button" style="font-weight:bold ; font-size:14px; padding:2;" class="btn btn-danger btn-xs btn-block ">' . $row['contract_id'] . '</button></a>
									</td>
								 <td style="color:#DA2D07; background-color:#F0F7E4;font-weight:bold;">' . $ww . '</td>';
									} else {
										$output .= '<td>
										<a href="../jaudge/cont2debt.php?id=' . $ss . '"target="_blank"><button type="button" style="font-weight:bold ; font-size:14px; padding:2;" class="btn btn-danger btn-xs btn-block ">' . $row['contract_id'] . '</button></a>
										</td>
									 <td>' . $ww . '</td>';
									}
									$output .= '
												<td class = "table-info">' . $row['date_in'] . '</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" >' . $row['day_cost'] . '</td>
												<td class="text-center" >' . $days_qty . '</td>
												<td class="text-center" >' . $tot_add . '</td>
												<td class="text-center" >' . $row['addtion_desc'] . '</td>
												<td class="text-center" >' . $pay["payed"] . '</td>
									</tr>';
								}
							} else {
								$output .= '<tr><td colspan="12">لا يوجد سجلات ...</td></tr>';
							}
							echo $output;
							?>
        </tbody>
    </table>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<script>
    document.addEventListener('DOMContentLoaded', function() {
        const searchInput = document.getElementById('searchInput');
        const table = document.getElementById('contractsTable');
        const rows = table.getElementsByTagName('tr');

        searchInput.addEventListener('input', function() {
            const searchTerm = searchInput.value.toLowerCase();

            for (let i = 1; i < rows.length; i++) {
                let row = rows[i];
                let text = row.textContent.toLowerCase();

                if (text.includes(searchTerm)) {
                    row.style.display = '';
                } else {
                    row.style.display = 'none';
                }
            }
        });
    });
    const customersBtn = document.getElementById("customersBtn");
            customersBtn.addEventListener("click", function () {
                window.location.href = 'customerFlow.php';
            });

   const homeButton = document.getElementById("homeBtn");
            homeButton.addEventListener("click", function () {
                window.location.href = '../profile.php';
            });
</script>
<?php
ob_end_flush();
?>
</body>
</html>
