<?php

$pageTitle = 'Quản lý người dùng - PET SPA';
$activeAdminPage = 'users';
$successMessage = $_SESSION['flash_success'] ?? null;
$errorMessage = $_SESSION['flash_error'] ?? null;
unset($_SESSION['flash_success'], $_SESSION['flash_error']);

require BASE_PATH . '/app/views/layouts/header.php';
require BASE_PATH . '/app/views/layouts/navbar.php';
?>

<main class="admin-area">
    <div class="container-fluid">
        <div class="row g-4">
            <div class="col-lg-3 col-xl-2">
                <?php require BASE_PATH . '/app/views/layouts/admin-sidebar.php'; ?>
            </div>
            <div class="col-lg-9 col-xl-10">
                <div class="d-flex justify-content-between align-items-center mb-3">
                    <div>
                        <h1 class="h3 mb-1">Người dùng</h1>
                        <p class="text-muted mb-0"><?= number_format($totalUsers) ?> tài khoản.</p>
                    </div>
                    <a class="btn btn-primary" href="<?= BASE_URL ?>/?url=admin/user/create"><i class="bi bi-person-plus"></i> Thêm người dùng</a>
                </div>

                <?php if ($successMessage): ?><div class="alert alert-success"><?= htmlspecialchars($successMessage) ?></div><?php endif; ?>
                <?php if ($errorMessage): ?><div class="alert alert-danger"><?= htmlspecialchars($errorMessage) ?></div><?php endif; ?>

                <form method="GET" class="card card-body mb-3">
                    <input type="hidden" name="url" value="admin/users">
                    <div class="row g-2">
                        <div class="col-md-5"><input name="search" class="form-control" value="<?= htmlspecialchars($search) ?>" placeholder="Tên, số điện thoại hoặc email"></div>
                        <div class="col-md-2">
                            <select name="role" class="form-select">
                                <option value="">Tất cả vai trò</option>
                                <?php foreach (['customer' => 'Customer', 'owner' => 'Owner', 'admin' => 'Admin'] as $value => $label): ?>
                                    <option value="<?= $value ?>" <?= $role === $value ? 'selected' : '' ?>><?= $label ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <div class="col-md-2">
                            <select name="status" class="form-select">
                                <option value="">Tất cả trạng thái</option>
                                <option value="active" <?= $status === 'active' ? 'selected' : '' ?>>Active</option>
                                <option value="locked" <?= $status === 'locked' ? 'selected' : '' ?>>Locked</option>
                            </select>
                        </div>
                        <div class="col-md-3"><button class="btn btn-dark w-100">Lọc người dùng</button></div>
                    </div>
                </form>

                <div class="card">
                    <div class="table-responsive">
                        <table class="table table-hover align-middle mb-0">
                            <thead><tr><th>ID</th><th>Người dùng</th><th>Liên hệ</th><th>Vai trò</th><th>Trạng thái</th><th>Tạo lúc</th><th></th></tr></thead>
                            <tbody>
                                <?php foreach ($users as $user): ?>
                                    <tr>
                                        <td><?= (int) $user['id'] ?></td>
                                        <td><?= htmlspecialchars($user['full_name']) ?></td>
                                        <td><div><?= htmlspecialchars($user['phone']) ?></div><small><?= htmlspecialchars($user['email'] ?? '') ?></small></td>
                                        <td><?= htmlspecialchars($user['role']) ?></td>
                                        <td><?= htmlspecialchars($user['status']) ?></td>
                                        <td><?= htmlspecialchars($user['created_at']) ?></td>
                                        <td><a class="btn btn-sm btn-outline-primary" href="<?= BASE_URL ?>/?url=admin/user&id=<?= (int) $user['id'] ?>">Chi tiết</a></td>
                                    </tr>
                                <?php endforeach; ?>
                                <?php if ($users === []): ?><tr><td colspan="7" class="text-center text-muted py-4">Không tìm thấy người dùng.</td></tr><?php endif; ?>
                            </tbody>
                        </table>
                    </div>
                </div>

                <?php if ($totalPages > 1): ?>
                    <nav class="mt-3"><ul class="pagination">
                        <?php for ($number = 1; $number <= $totalPages; $number++): ?>
                            <li class="page-item <?= $number === $page ? 'active' : '' ?>">
                                <a class="page-link" href="<?= BASE_URL ?>/?url=admin/users&<?= htmlspecialchars(http_build_query(['search' => $search, 'role' => $role, 'status' => $status, 'page' => $number])) ?>"><?= $number ?></a>
                            </li>
                        <?php endfor; ?>
                    </ul></nav>
                <?php endif; ?>
            </div>
        </div>
    </div>
</main>

<?php require BASE_PATH . '/app/views/layouts/footer.php'; ?>
