<?php
include "db.php";
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Customers List - Tameer Al Madina</title>
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">

<style>
* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    background: #F4F6F9;
    font-family: 'Poppins', sans-serif;
    min-height: 100vh;
}

.main-layout {
    display: flex;
    width: 100%;
    min-height: 100vh;
    position: relative;
}

/* Master Fixed Left Sidebar Layout */
aside, .sidebar, [class*="sidebar"] {
    width: 260px;
    min-width: 260px;
    height: 100vh;
    position: fixed;
    top: 0;
    left: 0;
    z-index: 1000;
    transition: all 0.3s ease;
}

.content-wrapper {
    flex: 1;
    margin-left: 260px;
    display: flex;
    flex-direction: column;
    background: #F4F6F9;
    min-width: 0;
    width: calc(100% - 260px);
    transition: all 0.3s ease;
}

/* System Top Headers Section */
.top-navbar {
    height: 60px;
    background: #FFFFFF;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 30px;
    border-bottom: 1px solid #EAEAEA;
}

.navbar-left-flex {
    display: flex;
    align-items: center;
    gap: 15px;
}

.mobile-menu-toggle {
    display: none;
    font-size: 20px;
    color: #0A192F;
    cursor: pointer;
    background: #F4F6F9;
    width: 38px;
    height: 38px;
    align-items: center;
    justify-content: center;
    border-radius: 6px;
    border: 1px solid #E2E8F0;
}

.search-container {
    position: relative;
    width: 300px;
}

.search-container input {
    width: 100%;
    padding: 8px 15px 8px 40px;
    background: #F8F9FA;
    border: 1px solid #E2E8F0;
    border-radius: 8px;
    font-size: 13px;
    outline: none;
}

.search-container i {
    position: absolute;
    left: 15px;
    top: 50%;
    transform: translateY(-50%);
    color: #A0AEC0;
}

.user-profile {
    display: flex;
    align-items: center;
    gap: 20px;
}

.profile-info {
    display: flex;
    align-items: center;
    gap: 10px;
}

.profile-info i.profile-icon {
    font-size: 26px;
    color: #4A5568;
    background: #F4F6F9;
    width: 36px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    border: 1px solid #E2E8F0;
}

.profile-text h4 {
    font-size: 13px;
    color: #1A202C;
    font-weight: 600;
}

.profile-text p {
    font-size: 11px;
    color: #718096;
}

/* Page Fluid Workspace Content */
.page-container {
    padding: 20px 30px;
    flex: 1;
    display: flex;
    flex-direction: column;
}

.form-card {
    background: #FFFFFF;
    border-radius: 12px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.05);
    overflow: hidden;
    width: 100%;
    display: flex;
    flex-direction: column;
    flex: 1;
}

.form-banner {
    background: linear-gradient(135deg, #0A192F 0%, #172A45 100%);
    padding: 15px 30px;
    color: #FFFFFF;
    border-bottom: 4px solid #0d6efd;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.banner-left {
    display: flex;
    align-items: center;
    gap: 12px;
}

.banner-icon {
    background: rgba(255, 255, 255, 0.1);
    width: 38px;
    height: 38px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 6px;
    font-size: 16px;
}

.banner-text h2 {
    font-size: 18px;
    font-weight: 600;
}

.banner-text p {
    font-size: 11px;
    color: #A0AEC0;
}

/* Primary Form Operational Buttons Structure */
.btn-add-customer {
    background: #0d6efd;
    color: #FFFFFF;
    padding: 8px 18px;
    border-radius: 6px;
    text-decoration: none;
    font-weight: 500;
    font-size: 13px;
    display: inline-flex;
    align-items: center;
    gap: 8px;
    transition: background 0.2s ease;
    box-shadow: 0 2px 6px rgba(13, 110, 253, 0.15);
}

.btn-add-customer:hover {
    background: #0a58ca;
}

/* Master Data Grid Design Configuration (Desktop View) */
.table-responsive {
    width: 100%;
    overflow-x: auto;
    flex: 1;
    display: block;
}

table {
    width: 100%;
    border-collapse: collapse;
}

table thead {
    background: #F8F9FA;
    border-bottom: 2px solid #E2E8F0;
    position: sticky;
    top: 0;
    z-index: 10;
}

table th {
    padding: 14px 20px;
    font-size: 12px;
    font-weight: 600;
    color: #4A5568;
    text-align: left;
}

table td {
    padding: 12px 20px;
    border-bottom: 1px solid #E2E8F0;
    font-size: 13px;
    color: #4A5568;
    background: #FFFFFF;
    vertical-align: middle;
}

table tbody tr:hover td {
    background: #F8FAFC;
}

.customer-link {
    color: #0d6efd;
    font-weight: 600;
    text-decoration: none;
    display: inline-flex;
    align-items: center;
    gap: 6px;
}

.customer-link:hover {
    text-decoration: underline;
}

.balance {
    font-weight: 600;
    color: #0A192F;
}

.actions {
    display: flex;
    gap: 6px;
    align-items: center;
}

.action-btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 6px 12px;
    border-radius: 4px;
    font-size: 11px;
    font-weight: 500;
    color: #FFFFFF;
    text-decoration: none;
    white-space: nowrap;
    transition: opacity 0.15s ease;
}

.action-btn:hover {
    opacity: 0.9;
}

.ledger-btn { background: #28A745; }
.edit-btn { background: #0A192F; }
.delete-btn { background: #DC3545; }

/* Mobile View Responsive Cards Layout */
.mobile-cards-container {
    display: none;
    padding: 15px;
    background: #F8F9FA;
    flex-direction: column;
    gap: 12px;
    overflow-y: auto;
}

.mobile-card {
    background: #FFFFFF;
    border-radius: 8px;
    padding: 15px;
    border: 1px solid #E2E8F0;
    box-shadow: 0 2px 5px rgba(0,0,0,0.02);
}

.card-header-flex {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    border-bottom: 1px dashed #E2E8F0;
    padding-bottom: 8px;
    margin-bottom: 10px;
}

.card-title-area h3 {
    font-size: 14px;
    color: #0d6efd;
    font-weight: 600;
}

.card-title-area span {
    font-size: 11px;
    color: #A0AEC0;
}

.card-balance-badge {
    background: #EBF8FF;
    color: #2B6CB0;
    padding: 4px 8px;
    border-radius: 4px;
    font-size: 12px;
    font-weight: 600;
}

.card-body-details {
    display: flex;
    flex-direction: column;
    gap: 6px;
    margin-bottom: 12px;
}

.detail-row {
    display: flex;
    font-size: 12px;
}

.detail-row .label {
    width: 95px;
    color: #718096;
    font-weight: 500;
}

.detail-row .value {
    color: #2D3748;
    flex: 1;
}

.card-actions-flex {
    display: flex;
    gap: 6px;
    border-top: 1px solid #F1F5F9;
    padding-top: 10px;
}

.card-actions-flex .action-btn {
    flex: 1;
    justify-content: center;
    padding: 8px;
    font-size: 12px;
}

.no-data {
    text-align: center;
    padding: 40px !important;
    color: #A0AEC0;
    font-size: 14px;
}

.no-data i {
    font-size: 24px;
    margin-bottom: 8px;
    display: block;
    color: #CBD5E1;
}

.sidebar-overlay {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.4);
    z-index: 998;
}

@media (min-width: 992px) {
    body { overflow: hidden; }
    .page-container { height: calc(100vh - 60px); }
}

@media (max-width: 1024px) and (min-width: 769px) {
    aside, .sidebar, [class*="sidebar"] { width: 240px; min-width: 240px; }
    .content-wrapper { margin-left: 240px; width: calc(100% - 240px); }
}

@media (max-width: 768px) {
    body { overflow-y: auto !important; }
    .mobile-menu-toggle { display: inline-flex; }
    aside, .sidebar, [class*="sidebar"] { left: -260px !important; }
    aside.active, .sidebar.active, [class*="sidebar"].active { left: 0 !important; }
    .content-wrapper { margin-left: 0 !important; width: 100% !important; }
    .top-navbar { padding: 0 15px; }
    .search-container { width: 140px; }
    .page-container { padding: 10px; }
    .form-banner { padding: 12px 15px; flex-direction: column; gap: 15px; align-items: flex-start; }
    .btn-add-customer { width: 100%; justify-content: center; }
    
    /* Responsive Transformation Rules */
    .table-responsive { display: none; }
    .mobile-cards-container { display: flex; }
}
</style>
</head>
<body>

<div class="sidebar-overlay" id="sidebarOverlay" onclick="toggleMobileSidebar()"></div>

<div class="main-layout">
    
    <?php 
    $sidebar_path = "/home/tameeralmadina/public_html/TAM/sidebar";
    if (file_exists($sidebar_path)) {
        include $sidebar_path;
    } elseif (file_exists($sidebar_path . ".php")) {
        include $sidebar_path . ".php";
    }
    ?>

    <div class="content-wrapper">
        
        <div class="top-navbar">
            <div class="navbar-left-flex">
                <div class="mobile-menu-toggle" onclick="toggleMobileSidebar()">
                    <i class="fa-solid fa-bars"></i>
                </div>
                <div class="search-container">
                    <i class="fa-solid fa-magnifying-glass"></i>
                    <input type="text" id="customerSearchInput" onkeyup="filterCustomers()" placeholder="Search parameters here...">
                </div>
            </div>
            
            <div class="user-profile">
                <div class="profile-info">
                    <i class="fa-solid fa-circle-user profile-icon"></i>
                    <div class="profile-text">
                        <h4>Admin</h4>
                        <p>Administrator</p>
                    </div>
                </div>
            </div>
        </div>

        <div class="page-container">
            <div class="form-card">
                
                <div class="form-banner">
                    <div class="banner-left">
                        <div class="banner-icon">
                            <i class="fa-solid fa-users"></i>
                        </div>
                        <div class="banner-text">
                            <h2>Customers Accounts Registry</h2>
                            <p>Manage consumer profile statements, structural opening values and ledger operations</p>
                        </div>
                    </div>
                    <a href="customers_add.php" class="btn-add-customer">
                        <i class="fa-solid fa-user-plus"></i> Add New Customer
                    </a>
                </div>

                <?php
                $customers_data = [];
                $result = $conn->query("SELECT * FROM customers ORDER BY id DESC");
                if($result && $result->num_rows > 0) {
                    while($row = $result->fetch_assoc()) {
                        $customers_data[] = $row;
                    }
                }
                ?>

                <div class="table-responsive">
                    <table id="desktopTable">
                        <thead>
                            <tr>
                                <th style="width: 6%;">ID</th>
                                <th style="width: 20%;">Customer Name</th>
                                <th style="width: 14%;">Contact Details</th>
                                <th style="width: 18%;">Email Address</th>
                                <th style="width: 18%;">Business Entity</th>
                                <th style="width: 12%;">Opening Balance</th>
                                <th style="width: 12%; text-align: center;">System Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                        <?php if(!empty($customers_data)): ?>
                            <?php foreach($customers_data as $row): 
                                $opening_balance = number_format((float)$row['opening_balance'], 3, '.', ',');
                            ?>
                                <tr class="customer-row">
                                    <td><strong>#<?= $row['id'] ?></strong></td>
                                    <td class="search-name">
                                        <a href='ledger_view.php?type=customer&id=<?= $row['id'] ?>' class='customer-link'>
                                            <i class='fa-regular fa-folder-open' style='font-size:11px;'></i> <?= htmlspecialchars($row['name']) ?>
                                        </a>
                                    </td>
                                    <td><?= htmlspecialchars($row['contact']) ?></td>
                                    <td><?= htmlspecialchars($row['email']) ?></td>
                                    <td class="search-business"><?= htmlspecialchars($row['business_name']) ?></td>
                                    <td class='balance'>OMR <?= $opening_balance ?></td>
                                    <td>
                                        <div class='actions' style='justify-content: center;'>
                                            <a href='ledger_view.php?type=customer&id=<?= $row['id'] ?>' class='action-btn ledger-btn'>
                                                <i class='fa-solid fa-book-open'></i> Ledger
                                            </a>
                                            <a href='customers_edit.php?id=<?= $row['id'] ?>' class='action-btn edit-btn'>
                                                <i class='fa-regular fa-pen-to-square'></i> Edit
                                            </a>
                                            <a href='customers_delete.php?id=<?= $row['id'] ?>' class='action-btn delete-btn' onclick='return confirm("Are you certain you want to remove this profile reference registration?")'>
                                                <i class='fa-regular fa-trash-can'></i> Drop
                                            </a>
                                        </div>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        <?php else: ?>
                            <tr>
                                <td colspan='7' class='no-data'>
                                    <i class='fa-solid fa-user-slash'></i> No registered consumer directories matches current indices parameters.
                                </td>
                            </tr>
                        <?php endif; ?>
                        </tbody>
                    </table>
                </div>

                <div class="mobile-cards-container" id="mobileCardsContainer">
                    <?php if(!empty($customers_data)): ?>
                        <?php foreach($customers_data as $row): 
                            $opening_balance = number_format((float)$row['opening_balance'], 3, '.', ',');
                        ?>
                            <div class="mobile-card customer-card">
                                <div class="card-header-flex">
                                    <div class="card-title-area">
                                        <h3 class="search-name"><?= htmlspecialchars($row['name']) ?></h3>
                                        <span>ID: #<?= $row['id'] ?></span>
                                    </div>
                                    <div class="card-balance-badge">
                                        OMR <?= $opening_balance ?>
                                    </div>
                                </div>
                                <div class="card-body-details">
                                    <div class="detail-row">
                                        <span class="label"><i class="fa-solid fa-briefcase" style="width:15px; color:#A0AEC0;"></i> Business:</span>
                                        <span class="value search-business"><?= htmlspecialchars($row['business_name']) ?></span>
                                    </div>
                                    <div class="detail-row">
                                        <span class="label"><i class="fa-solid fa-phone" style="width:15px; color:#A0AEC0;"></i> Contact:</span>
                                        <span class="value"><?= htmlspecialchars($row['contact']) ?></span>
                                    </div>
                                    <div class="detail-row">
                                        <span class="label"><i class="fa-solid fa-envelope" style="width:15px; color:#A0AEC0;"></i> Email:</span>
                                        <span class="value"><?= htmlspecialchars($row['email']) ?></span>
                                    </div>
                                </div>
                                <div class="card-actions-flex">
                                    <a href='ledger_view.php?type=customer&id=<?= $row['id'] ?>' class='action-btn ledger-btn'>
                                        <i class='fa-solid fa-book-open'></i> Ledger
                                    </a>
                                    <a href='customers_edit.php?id=<?= $row['id'] ?>' class='action-btn edit-btn'>
                                        <i class='fa-regular fa-pen-to-square'></i> Edit
                                    </a>
                                    <a href='customers_delete.php?id=<?= $row['id'] ?>' class='action-btn delete-btn' onclick='return confirm("Are you certain you want to remove this profile reference registration?")'>
                                        <i class='fa-regular fa-trash-can'></i> Drop
                                    </a>
                                </div>
                            </div>
                        <?php endforeach; ?>
                    <?php else: ?>
                        <div class="no-data" style="background:#FFF; border-radius:8px; border:1px solid #E2E8F0;">
                            <i class='fa-solid fa-user-slash'></i> No registered consumer directories matches current indices parameters.
                        </div>
                    <?php endif; ?>
                </div>

            </div>
        </div>
    </div>
</div>

<script>
// Sidebar Toggle logic enhanced
function toggleMobileSidebar() {
    const sidebarElements = document.querySelectorAll('aside, .sidebar, [class*="sidebar"]');
    const overlay = document.getElementById('sidebarOverlay');
    
    sidebarElements.forEach(sidebar => {
        sidebar.classList.toggle('active');
    });
    
    if(sidebarElements[0] && sidebarElements[0].classList.contains('active')) {
        overlay.style.display = 'block';
    } else {
        overlay.style.display = 'none';
    }
}

// Instant Live Search Filter for both Desktop Table & Mobile Cards
function filterCustomers() {
    const input = document.getElementById('customerSearchInput');
    const filter = input.value.toLowerCase();
    
    // 1. Filter Desktop Rows
    const tableRows = document.querySelectorAll('#desktopTable .customer-row');
    tableRows.forEach(row => {
        const nameText = row.querySelector('.search-name').textContent.toLowerCase();
        const businessText = row.querySelector('.search-business').textContent.toLowerCase();
        if (nameText.includes(filter) || businessText.includes(filter)) {
            row.style.display = "";
        } else {
            row.style.display = "none";
        }
    });
    
    // 2. Filter Mobile Cards
    const mobileCards = document.querySelectorAll('#mobileCardsContainer .customer-card');
    mobileCards.forEach(card => {
        const nameText = card.querySelector('.search-name').textContent.toLowerCase();
        const businessText = card.querySelector('.search-business').textContent.toLowerCase();
        if (nameText.includes(filter) || businessText.includes(filter)) {
            card.style.display = "";
        } else {
            card.style.display = "none";
        }
    });
}
</script>
</body>
</html>