<?= $this->extend('layouts/enterprise') ?>

<?= $this->section('content') ?>

<div class="row g-4">
    <div class="col-lg-8">
        <h1 class="h5 mb-3">Permissions</h1>

        <?php foreach ($grouped as $group => $perms): ?>
            <div class="stat-card mb-3">
                <h2 class="h6 text-capitalize mb-3"><?= esc($group) ?></h2>
                <div class="table-responsive">
                    <table class="table table-sm mb-0">
                        <thead>
                            <tr><th>Slug</th><th>Label</th><th>Type</th><th class="text-end">Actions</th></tr>
                        </thead>
                        <tbody>
                            <?php foreach ($perms as $p): ?>
                                <tr>
                                    <td><code><?= esc($p['slug']) ?></code></td>
                                    <td><?= esc($p['label']) ?></td>
                                    <td><?= $p['is_system'] ? '<span class="badge text-bg-secondary">System</span>' : '<span class="badge text-bg-light border">Custom</span>' ?></td>
                                    <td class="text-end">
                                        <?php if (! $p['is_system']): ?>
                                            <form action="<?= base_url('permissions/' . $p['id'] . '/delete') ?>" method="post" onsubmit="return confirm('Delete this permission?');">
                                                <?= csrf_field() ?>
                                                <button type="submit" class="btn btn-sm btn-outline-danger"><i class="fa-solid fa-trash"></i></button>
                                            </form>
                                        <?php endif; ?>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        <?php endforeach; ?>
    </div>

    <div class="col-lg-4">
        <div class="stat-card">
            <h2 class="h6 mb-3">Add Custom Permission</h2>

            <?php if (session()->getFlashdata('errors')): ?>
                <div class="alert alert-danger py-2 small">
                    <ul class="mb-0 ps-3">
                        <?php foreach ((array) session()->getFlashdata('errors') as $err): ?>
                            <li><?= esc(is_array($err) ? implode(', ', $err) : $err) ?></li>
                        <?php endforeach; ?>
                    </ul>
                </div>
            <?php endif; ?>

            <form action="<?= base_url('permissions') ?>" method="post" novalidate>
                <?= csrf_field() ?>
                <div class="mb-2">
                    <label class="form-label small">Module (group)</label>
                    <input type="text" name="group" class="form-control form-control-sm" placeholder="e.g. inventory" required>
                </div>
                <div class="mb-2">
                    <label class="form-label small">Action</label>
                    <input type="text" name="action" class="form-control form-control-sm" placeholder="e.g. approve" required>
                </div>
                <div class="mb-3">
                    <label class="form-label small">Label (optional)</label>
                    <input type="text" name="label" class="form-control form-control-sm" placeholder="Approve Inventory">
                </div>
                <button type="submit" class="btn btn-sm btn-primary w-100">Add Permission</button>
            </form>
        </div>
    </div>
</div>

<?= $this->endSection() ?>
