<?php

$pageTitle = 'Phân quyền - PET SPA';
$activeAdminPage = 'permissions';
$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="admin-layout">
            <div class="admin-sidebar-column">
                <?php require BASE_PATH . '/app/views/layouts/admin-sidebar.php'; ?>
            </div>

            <section class="admin-main">
                <div class="admin-page-header">
                    <div>
                        <h1>Phân quyền</h1>
                        <p>Quyền này đồng thời ẩn menu và chặn truy cập trực tiếp bằng URL.</p>
                    </div>
                </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="post" action="<?= BASE_URL ?>/?url=admin/permissions/update" class="admin-card permission-card">
                    <input type="hidden" name="_token" value="<?= htmlspecialchars($_SESSION['csrf_token'] ?? '') ?>">

                    <div class="table-responsive">
                        <table class="table align-middle mb-0 permission-matrix">
                            <thead>
                                <tr><th>Chức năng</th><th class="text-center">Customer</th><th class="text-center">Owner</th></tr>
                            </thead>
                            <tbody>
                                <?php foreach ($matrixRows as $row): ?>
                                    <tr>
                                        <td><?= htmlspecialchars($row['label']) ?></td>
                                        <?php foreach (['customer', 'owner'] as $role): ?>
                                            <?php $key = $row[$role]; ?>
                                            <td class="text-center">
                                                <?php if ($key !== null): ?>
                                                    <input
                                                        class="form-check-input permission-toggle"
                                                        type="checkbox"
                                                        name="permissions[<?= $role ?>][]"
                                                        value="<?= htmlspecialchars($key) ?>"
                                                        aria-label="<?= htmlspecialchars($row['label'] . ' ' . $role) ?>"
                                                        <?= !empty($matrix[$role][$key]) ? 'checked' : '' ?>
                                                    >
                                                <?php else: ?>
                                                    <span class="text-muted">—</span>
                                                <?php endif; ?>
                                            </td>
                                        <?php endforeach; ?>
                                    </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>

                    <div class="mt-4 d-flex justify-content-between align-items-center gap-3 flex-wrap">
                        <p class="text-muted small mb-0">Admin luôn có toàn quyền và không bị áp dụng ma trận này.</p>
                        <button class="btn btn-primary"><i class="bi bi-check2-circle me-1"></i>Lưu phân quyền</button>
                    </div>
                </form>
            </section>
        </div>
    </div>
</main>

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