<?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)) {
}




?>
<!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-3 mb-3"></div>
<div class="col-md-2 mb-3">
    <h1 style="color: #cce5ff;" >العملاء</h1>
</div>
<div class="col-md-3 mb-3"></div>
<div class="col-md-2 mb-3">
    <button type="submit" id="addCustomerBtn" class="btn btn-success">إضافة عميل جديد</button>
</div>
</div>
    <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="customerTable">
        <thead class="sticky-top top-0">
            <tr class="table-info">
                <th class="text-center" width="4%">رقم العميل</th>
                <th>رقم السجل</th>
                <th>اسم العميل</th>
                <th>رقم الهاتف</th>
                <th>العنوان</th>
                <th>انتهاء الرخصة</th>
                <th>عقود نشطة</th>
            </tr>
        </thead>
        <tbody>
            <?php
            $activeCustomers = [] ;
            $activeSql = "SELECT DISTINCT `cust_id` FROM `contracts` WHERE `cont_status` = 1
                        UNION
                      SELECT DISTINCT `cust_id2` FROM `contracts` WHERE `cont_status` = 1";
            $activeResult = $conn->query($activeSql);
            $activeResult->execute();
            while ($comany = $activeResult->fetch()){
                $activeCustomers[] = $comany['cust_id'];
            }

            $sql = "SELECT cust_id , idSA , cust_name ,phone , address ,lic_expire 
            from customers where LEFT(idSA, 1) IN (1 , 2) ORDER BY cust_id DESC LIMIT 150" ;
             $stmt = $conn->prepare($sql);
             $stmt->execute();
             if($stmt->rowCount() > 0){
                $nn = 1 ;
                $active = "لا توجد عقود" ; $style ="";
                while ($customer = $stmt->fetch()){
                    $cust_id = (int) $customer['cust_id'];
                    if (in_array($cust_id, $activeCustomers)){$active = "يوجد عقود نشطة" ; $style = "background-color: green;" ;}
                    $rows = null;
                    $rows = "<tr>";
                    $rows.= "<td> <a href='customerEdit.php?id=$cust_id' class='btn btn-primary btn-sm fw-bold w-100' target=_blank>" . $nn . "</a></td>";
                    $rows.= "<td>" . $customer["idSA"] . "</td>";
                    $rows.= "<td>" . $customer["cust_name"] . "</td>";
                    $rows.= "<td>" . $customer["phone"] . "</td>";
                    $rows.= "<td>" . $customer["address"] . "</td>";
                    $rows.= "<td>" . $customer["lic_expire"] . "</td>";
                    $rows.= "<td> <a href='cust_contracts.php?cust=$cust_id' class='btn btn-primary btn-sm fw-bold w-100' style='$style' target=_blank>" . $active . "</a></td>";
                    $rows.= "</tr>";
                    echo $rows;
                    $nn = 1 + $nn ;
                    $active = "لا توجد عقود" ; $style ="";
                }

             }else{
                echo "<tr><td colspan='7'>لا توجد عملاء</td></tr>";
             }
            ?>
        </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('customerTable');
        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 addCustomerButton = document.getElementById("addCustomerBtn");
            addCustomerButton.addEventListener("click", function () {
                window.location.href = 'Customer.php';
            });

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