<?php $editing = !empty($role); $isSystem = $role['is_system'] ?? false; ?>
<div class="max-w-4xl mx-auto">
    <div class="flex items-center gap-3 mb-6"><a href="?page=roles" class="text-gray-400 hover:text-gray-600"><svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 19l-7-7 7-7"/></svg></a><h1 class="text-2xl font-bold text-gray-900"><?= $editing ? ($isSystem ? 'View Role' : 'Edit Role') : 'Create Role' ?></h1></div>
    <form method="POST" action="?page=roles&action=<?= $editing ? 'update&id='.$role['id'] : 'store' ?>" class="space-y-6"><?= CSRF::field() ?>
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6">
            <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                <div><label class="block text-sm font-medium text-gray-700 mb-1">Role Name <span class="text-red-500">*</span></label><input type="text" name="name" value="<?= htmlspecialchars($role['name'] ?? '') ?>" required <?= $isSystem ? 'disabled' : '' ?> class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500 <?= $isSystem ? 'bg-gray-50' : '' ?>"></div>
                <div><label class="block text-sm font-medium text-gray-700 mb-1">Description</label><input type="text" name="description" value="<?= htmlspecialchars($role['description'] ?? '') ?>" <?= $isSystem ? 'disabled' : '' ?> class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500 <?= $isSystem ? 'bg-gray-50' : '' ?>"></div>
            </div>
        </div>

        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6">
            <h3 class="text-sm font-semibold text-gray-500 uppercase tracking-wider mb-4">Permissions</h3>
            <?php
            $grouped = []; foreach ($permissions ?? [] as $p) { $grouped[$p['module'] ?? 'general'][] = $p; }
            if (empty($grouped)): ?>
                <p class="text-sm text-gray-400">No permissions defined. Run the installer to seed default permissions.</p>
            <?php else: ?>
            <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
                <?php foreach ($grouped as $module => $perms): ?>
                    <div>
                        <h4 class="text-xs font-bold text-gray-700 uppercase mb-2"><?= ucwords(str_replace('_',' ',$module)) ?></h4>
                        <div class="space-y-1.5">
                            <?php foreach ($perms as $p): ?>
                                <label class="flex items-center gap-2 text-sm <?= $isSystem ? 'opacity-60' : '' ?>">
                                    <input type="checkbox" name="permissions[]" value="<?= $p['id'] ?>" <?= in_array($p['id'], $rolePermissions ?? []) ? 'checked' : '' ?> <?= $isSystem ? 'disabled' : '' ?> class="rounded border-gray-300 text-indigo-600 focus:ring-indigo-500">
                                    <span class="text-gray-700"><?= htmlspecialchars($p['name']) ?></span>
                                </label>
                            <?php endforeach; ?>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
            <?php endif; ?>
        </div>

        <?php if (!$isSystem): ?>
        <div class="flex justify-end gap-3"><a href="?page=roles" class="px-4 py-2 border border-gray-300 text-gray-700 rounded-lg hover:bg-gray-50 text-sm font-medium">Cancel</a><button type="submit" class="px-6 py-2 bg-indigo-600 text-white rounded-lg hover:bg-indigo-700 text-sm font-medium"><?= $editing ? 'Update' : 'Create' ?></button></div>
        <?php endif; ?>
    </form>
</div>
