<?php
/**
 * SalonPro X - Edit Role View
 * @var object $role Role data
 * @var array $groupedPermissions Permissions grouped by module
 * @var array $rolePermIds Array of permission IDs assigned to this role
 */
?>
<div class="row">
    <div class="col-lg-8">
        <div class="card glass-card">
            <div class="card-header">
                <h5 class="card-title">Edit Role: <?= e($role->name) ?></h5>
            </div>
            <div class="card-body">
                <form method="POST" action="<?= url('roles/update/' . $role->id) ?>" class="needs-validation" novalidate>
                    <?= csrf_field() ?>

                    <div class="mb-3">
                        <label class="form-label">Role Name <span class="text-danger">*</span></label>
                        <input type="text" name="name" class="form-control" required 
                               value="<?= e($role->name) ?>">
                    </div>

                    <div class="mb-3">
                        <label class="form-label">Description</label>
                        <textarea name="description" class="form-control" rows="2"><?= e($role->description ?? '') ?></textarea>
                    </div>

                    <div class="mb-4">
                        <label class="form-label">Permissions</label>
                        <div class="row">
                            <?php foreach ($groupedPermissions as $module => $perms): ?>
                                <div class="col-md-6 mb-3">
                                    <div class="permission-group glass-card p-3">
                                        <h6 class="text-gold mb-2">
                                            <i class="fas fa-<?= match($module) {
                                                'Dashboard' => 'th-large',
                                                'Authentication' => 'lock',
                                                'Roles' => 'shield-alt',
                                                default => 'circle'
                                            } ?> me-1"></i>
                                            <?= e($module) ?>
                                        </h6>
                                        <div class="form-check mb-2">
                                            <input type="checkbox" class="form-check-input select-all" data-module="<?= e($module) ?>">
                                            <label class="form-check-label small text-muted">Select All</label>
                                        </div>
                                        <?php foreach ($perms as $perm): ?>
                                            <div class="form-check form-check-inline">
                                                <input type="checkbox" name="permissions[]" 
                                                       value="<?= $perm->id ?>" 
                                                       class="form-check-input perm-<?= e($module) ?>"
                                                       id="perm_<?= $perm->id ?>"
                                                       <?= in_array($perm->id, $rolePermIds) ? 'checked' : '' ?>>
                                                <label class="form-check-label small" for="perm_<?= $perm->id ?>">
                                                    <?= e($perm->name) ?>
                                                </label>
                                            </div>
                                        <?php endforeach; ?>
                                    </div>
                                </div>
                            <?php endforeach; ?>
                        </div>
                    </div>

                    <button type="submit" class="btn btn-gold">
                        <i class="fas fa-save me-1"></i>Update Role
                    </button>
                    <a href="<?= url('roles') ?>" class="btn btn-ghost ms-2">Cancel</a>
                </form>
            </div>
        </div>
    </div>
</div>

<?php $inlineScripts = <<<JS
$('.select-all').on('change', function() {
    var module = $(this).data('module');
    $('.perm-' + module).prop('checked', this.checked);
});
JS;
?>
