<?php
$roleRecord = $roleRecord ?? null;
$formErrors = $formErrors ?? [];
$permissionGroups = $permissionGroups ?? permission_groups();
$selectedPermissionIds = $selectedPermissionIds ?? ($roleRecord ? role_permission_ids((int) $roleRecord['id']) : []);

if (isset($_SESSION['_old']['permission_ids']) && is_array($_SESSION['_old']['permission_ids'])) {
    $selectedPermissionIds = array_map('intval', $_SESSION['_old']['permission_ids']);
}

$fieldValue = static function (string $field, mixed $default = '') use ($roleRecord) {
    if (isset($_SESSION['_old']) && array_key_exists($field, $_SESSION['_old'])) {
        return old($field, $default);
    }

    return $roleRecord[$field] ?? $default;
};
?>
<div class="row g-3">
    <div class="col-md-6">
        <label class="form-label" for="name">Role name</label>
        <input class="form-control <?php echo validation_error($formErrors, 'name') ? 'is-invalid' : ''; ?>" id="name" name="name" value="<?php echo e((string) $fieldValue('name')); ?>" <?php echo $roleRecord && is_system_super_admin_role($roleRecord) ? 'readonly' : ''; ?> required>
        <?php if (validation_error($formErrors, 'name')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'name')); ?></div><?php endif; ?>
    </div>
    <div class="col-12">
        <label class="form-label" for="description">Description</label>
        <textarea class="form-control" id="description" name="description" rows="3"><?php echo e((string) $fieldValue('description')); ?></textarea>
    </div>
    <div class="col-12">
        <label class="form-label d-block">Permissions</label>
        <?php if (validation_error($formErrors, 'permission_ids')): ?>
            <div class="alert alert-danger"><?php echo e((string) validation_error($formErrors, 'permission_ids')); ?></div>
        <?php endif; ?>
        <?php if ($roleRecord && is_system_super_admin_role($roleRecord)): ?>
            <div class="alert alert-info mb-0">Super Admin bypasses permission checks and keeps full access automatically.</div>
        <?php else: ?>
            <div class="row g-3">
                <?php foreach ($permissionGroups as $module => $permissions): ?>
                    <div class="col-md-6 col-xl-4">
                        <div class="feature-tile h-100">
                            <div class="w-100">
                                <h4 class="mb-2"><?php echo e(ucwords(str_replace('_', ' ', $module))); ?></h4>
                                <?php foreach ($permissions as $permission): ?>
                                    <div class="form-check mb-2">
                                        <input class="form-check-input" id="permission_<?php echo e((string) $permission['id']); ?>" name="permission_ids[]" type="checkbox" value="<?php echo e((string) $permission['id']); ?>" <?php echo in_array((int) $permission['id'], $selectedPermissionIds, true) ? 'checked' : ''; ?>>
                                        <label class="form-check-label" for="permission_<?php echo e((string) $permission['id']); ?>"><?php echo e($permission['permission_name']); ?></label>
                                    </div>
                                <?php endforeach; ?>
                            </div>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>
    </div>
</div>