<?php
/**
 * @var \App\View\AppView $this
 * @var iterable<\App\Model\Entity\User> $users
 */
?>

<style>
    .booking-style-wrapper {
        font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
        background-color: #ffffff;
        border-radius: 12px;
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
        padding: 24px;
        margin: 20px;
        color: #333;
    }
    .booking-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 24px;
    }
    .booking-header h2 {
        margin: 0;
        font-size: 2rem;
        font-weight: bold;
        color: #1a202c;
    }
    .booking-header p {
        margin: 4px 0 0;
        color: #718096;
        font-size: 17px;
    }
    .booking-filters {
        display: flex;
        gap: 16px;
        margin-bottom: 20px;
        align-items: center;
    }
    .filter-input-group {
        display: flex;
        align-items: center;
    }
    .filter-input-group:last-child {
        margin-left: auto;
        display: flex;
        justify-content: flex-end;
        flex: 1;
    }
    .booking-select {
        padding: 10px 45px 10px 16px;
        border: 1px solid #e2e8f0;
        border-radius: 8px;
        font-size: 17px;
        color: #4a5568;
        outline: none;
        background-color: #fff;
        width: 220px;
        appearance: none;
        -webkit-appearance: none;
        background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' fill='%234a5568' viewBox='0 0 16 16'%3E%3Cpath d='M7.247 11.14 2.451 5.658C1.885 5.013 2.345 4 3.204 4h9.592a1 1 0 0 1 .753 1.659l-4.796 5.48a1 1 0 0 1-1.506 0z'/%3E%3C/svg%3E");
        background-repeat: no-repeat;
        background-position: calc(100% - 16px) center;
    }
    .booking-input {
        padding: 10px 16px;
        border: 1px solid #e2e8f0;
        border-radius: 8px;
        font-size: 17px;
        color: #4a5568;
        outline: none;
        background-color: #fff;
        width: 100%;
        max-width: 500px;
    }
    .btn-new {
        background-color: #4263EB;
        color: #ffffff;
        padding: 10px 20px;
        border-radius: 8px;
        text-decoration: none;
        font-size: 17px;
        font-weight: 500;
    }
    .btn-view {
        background-color: #4263EB;
        color: #ffffff;
        padding: 8px 24px;
        border-radius: 6px;
        text-decoration: none;
        font-size: 17px;
        font-weight: 500;
        display: inline-block;
    }
    .action-links {
        display: flex;
        flex-direction: column;
        gap: 6px;
        margin-right: 12px;
        text-align: right;
    }
    .action-links a { color: #a0aec0; text-decoration: none; font-size: 15px; }
    .action-links a:hover { color: #4263EB; }
    .action-links .link-delete { color: #E53E3E; }
    .action-links .link-delete:hover { color: #C53030; }

    .booking-table {
        width: 100%;
        border-collapse: collapse;
        text-align: left;
    }
    .booking-table th {
        padding: 12px 16px;
        color: #a0aec0;
        font-size: 15px;
        text-transform: uppercase;
        font-weight: 600;
        border-bottom: 2px solid #edf2f7;
    }
    .booking-table th a { color: #a0aec0; text-decoration: none; }
    .booking-table td {
        padding: 16px;
        border-bottom: 1px solid #edf2f7;
        vertical-align: top;
    }
    .booking-table tr:hover { background-color: #f7fafc; }
    .text-main { font-weight: 500; color: #2d3748; font-size: 17px; margin-bottom: 4px; }
    .text-sub { color: #a0aec0; font-size: 15px; }
    .badge {
        display: inline-flex;
        align-items: center;
        padding: 4px 12px;
        border-radius: 20px;
        font-size: 15px;
        font-weight: 500;
    }
    @media (max-width: 768px) {
        .booking-style-wrapper {
            margin: 8px;
            padding: 12px;
        }
        .booking-header {
            flex-direction: column;
            align-items: stretch;
            gap: 10px;
        }
        .booking-header h2 {
            font-size: 1.4rem;
        }
        .booking-filters {
            flex-direction: column;
            gap: 8px;
        }
        .filter-input-group {
            width: 100%;
        }
        .filter-input-group:last-child {
            margin-left: 0;
            flex: none;
        }
        .booking-select,
        .booking-input {
            width: 100%;
            max-width: 100%;
            font-size: 15px;
            padding: 8px 12px;
        }
        .booking-table {
            table-layout: fixed;
            width: 100%;
            font-size: 13px;
        }
        .booking-table th:nth-child(3),
        .booking-table td:nth-child(3) {
            display: none;
        }
        .booking-table th,
        .booking-table td {
            padding: 8px 6px;
            word-break: break-word;
        }
        .text-main {
            font-size: 14px;
        }
        .text-sub {
            font-size: 12px;
        }
        .btn-view {
            padding: 5px 10px;
            font-size: 13px;
        }
        .action-links {
            gap: 4px;
        }
        .action-links a {
            font-size: 12px;
        }
    }
    .badge-admin { background-color: #e6fffa; color: #285e61; }
    .badge-client { background-color: #ebf8ff; color: #2b6cb0; }
    .badge-staff { background-color: #fffaf0; color: #9c4221; }
    .cb-empty { text-align: center; padding: 40px; color: #a0aec0; display: none; }
</style>
<div class="booking-style-wrapper">
    <div class="booking-header">
        <div>
            <h2><?= __('Users') ?></h2>
            <p>Manage all users, view details, and update roles.</p>
        </div>
        <?= $this->Html->link(__('New User'), ['action' => 'add'], ['class' => 'btn-new']) ?>
    </div>
    <div class="booking-filters">
        <div class="filter-input-group">
            <select id="role-filter" class="booking-select" onchange="userFilter()">
                <option value="">All Roles</option>
                <option value="admin">Admin</option>
                <option value="client">Client</option>
                <option value="staff">Staff</option>
            </select>
        </div>
        <div class="filter-input-group">
            <input type="text" id="user-search" class="booking-input" placeholder="Search by email or ID..." oninput="userFilter()">
        </div>
    </div>
    <div style="overflow-x: auto;">
        <table class="booking-table" id="user-table">
            <thead>
            <tr>
                <th><?= $this->Paginator->sort('email', 'User') ?></th>
                <th>Role</th>
                <th><?= $this->Paginator->sort('created', 'Timeline') ?></th>
                <th style="text-align: right;"><?= __('Actions') ?></th>
            </tr>
            </thead>
            <tbody>
            <?php foreach ($users as $user): ?>
                <?php $role = isset($user->role) ? strtolower($user->role) : 'client'; ?>
                <tr class="user-row" data-role="<?= h($role) ?>" data-search="<?= h(strtolower($user->email)) ?> <?= $user->id ?>">
                    <td>
                        <div class="text-main"><?= h($user->email) ?></div>
                        <div class="text-sub">ID: <?= $this->Number->format($user->id) ?></div>
                    </td>
                    <td>
                        <?php
                        $badgeClass = 'badge-client';
                        if ($role === 'admin') $badgeClass = 'badge-admin';
                        if ($role === 'staff') $badgeClass = 'badge-staff';
                        ?>
                        <span class="badge <?= $badgeClass ?>">
                            <?= ucfirst($role) ?>
                        </span>
                    </td>
                    <td>
                        <div class="text-main">Created: <?= h($user->created) ?></div>
                        <div class="text-sub">Updated: <?= h($user->modified) ?></div>
                    </td>
                    <td style="display: flex; justify-content: flex-end; align-items: center;">
                        <div class="action-links">
                            <?= $this->Html->link(__('Edit'), ['action' => 'edit', $user->id]) ?>
                            <?= $this->Form->postLink(__('Delete'), ['action' => 'delete', $user->id], [
                                'confirm' => __('Are you sure?', $user->id),
                                'class' => 'link-delete'
                            ]) ?>
                        </div>
                    </td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
        <div class="cb-empty" id="no-results">No users match your search.</div>
    </div>
</div>
<script>
    function userFilter() {
        const searchQuery = document.getElementById('user-search').value.trim().toLowerCase();
        const roleQuery = document.getElementById('role-filter').value.toLowerCase();
        const rows = document.querySelectorAll('.user-row');
        let visibleCount = 0;
        rows.forEach(row => {
            const rowRole = row.dataset.role || '';
            const rowSearchData = row.dataset.search || '';
            const matchesRole = !roleQuery || rowRole === roleQuery;
            const matchesSearch = !searchQuery || rowSearchData.includes(searchQuery);
            if (matchesRole && matchesSearch) {
                row.style.display = '';
                visibleCount++;
            } else {
                row.style.display = 'none';
            }
        });
        document.getElementById('no-results').style.display = visibleCount === 0 ? 'block' : 'none';
        document.getElementById('user-table').style.display = visibleCount === 0 ? 'none' : 'table';
    }
</script>
