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

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

<?php
// Standard action set an ERP permission matrix cares about. Not every
// module uses every action — cells with no matching permission are blank.
$actionColumns = ['view', 'create', 'edit', 'delete', 'export', 'print', 'approve'];

// Index permissions by [group][action] => permission row, for fast matrix lookup.
$byGroupAction = [];
foreach ($groupedPerms as $group => $perms) {
    foreach ($perms as $p) {
        $parts = explode('.', $p['slug'], 2);
        $action = $parts[1] ?? '';
        $byGroupAction[$group][$action] = $p;
    }
}
?>

<div class="row g-4">
    <div class="col-lg-5">
        <div class="stat-card">
            <h1 class="h5 mb-3">Edit Role</h1>

            <?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('roles/' . $role['id']) ?>" method="post" novalidate>
                <?= csrf_field() ?>
                <div class="mb-3">
                    <label class="form-label">Role Name</label>
                    <input type="text" name="name" class="form-control" value="<?= esc(old('name', $role['name'])) ?>" <?= $role['is_system'] ? 'disabled' : '' ?> required>
                    <?php if ($role['is_system']): ?>
                        <div class="form-text">System role names cannot be changed.</div>
                        <input type="hidden" name="name" value="<?= esc($role['name']) ?>">
                    <?php endif; ?>
                </div>
                <div class="mb-4">
                    <label class="form-label">Description</label>
                    <textarea name="description" class="form-control" rows="3"><?= esc(old('description', $role['description'])) ?></textarea>
                </div>
                <button type="submit" class="btn btn-primary btn-sm">Save Details</button>
                <a href="<?= base_url('roles') ?>" class="btn btn-outline-secondary btn-sm">Back to Roles</a>
            </form>
        </div>
    </div>

    <div class="col-lg-7">
        <div class="stat-card">
            <h2 class="h6 mb-3">Permission Matrix</h2>

            <form action="<?= base_url('roles/' . $role['id'] . '/permissions') ?>" method="post">
                <?= csrf_field() ?>

                <div class="table-responsive">
                    <table class="table table-sm align-middle mb-3">
                        <thead>
                            <tr>
                                <th>Module</th>
                                <?php foreach ($actionColumns as $col): ?>
                                    <th class="text-center text-capitalize"><?= esc($col) ?></th>
                                <?php endforeach; ?>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($groupedPerms as $group => $perms): ?>
                                <tr>
                                    <td class="fw-semibold text-capitalize"><?= esc($group) ?></td>
                                    <?php foreach ($actionColumns as $col): ?>
                                        <td class="text-center">
                                            <?php if (isset($byGroupAction[$group][$col])): ?>
                                                <?php $perm = $byGroupAction[$group][$col]; ?>
                                                <input type="checkbox" class="form-check-input" name="permissions[]"
                                                       value="<?= $perm['id'] ?>"
                                                       <?= in_array($perm['id'], $assignedIds, true) ? 'checked' : '' ?>>
                                            <?php else: ?>
                                                <span class="text-muted">—</span>
                                            <?php endif; ?>
                                        </td>
                                    <?php endforeach; ?>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>

                <button type="submit" class="btn btn-primary btn-sm">
                    <i class="fa-solid fa-floppy-disk me-1"></i>Save Permission Matrix
                </button>
            </form>
        </div>
    </div>
</div>

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