<div class="row justify-content-center">
    <div class="col-lg-10">
        <div class="card">
            <div class="card-header"><h5 class="mb-0"><?= $role ? 'Edit Role: ' . e($role['role_name_en']) : 'Create Role' ?></h5></div>
            <div class="card-body">
                <form method="POST" action="<?= $role ? '/admin/roles/' . $role['id'] : '/admin/roles' ?>" data-ajax="true">
                    <?= csrf_field() ?>
                    <div class="row g-3 mb-4">
                        <?php if (!$role): ?>
                        <div class="col-md-4">
                            <label class="form-label">Role Code <span class="text-danger">*</span></label>
                            <input type="text" name="role_code" class="form-control" required>
                            <div class="invalid-feedback" data-field="role_code"></div>
                        </div>
                        <?php endif; ?>
                        <div class="col-md-4">
                            <label class="form-label">Name (EN) <span class="text-danger">*</span></label>
                            <input type="text" name="role_name_en" class="form-control" value="<?= e($role['role_name_en'] ?? '') ?>" required>
                        </div>
                        <div class="col-md-4">
                            <label class="form-label">Name (AR) <span class="text-danger">*</span></label>
                            <input type="text" name="role_name_ar" class="form-control" value="<?= e($role['role_name_ar'] ?? '') ?>" required>
                        </div>
                        <div class="col-md-6">
                            <div class="form-check form-switch">
                                <input class="form-check-input" type="checkbox" name="is_active" value="1" <?= ($role['is_active'] ?? 1) ? 'checked' : '' ?>>
                                <label class="form-check-label">Active</label>
                            </div>
                        </div>
                    </div>
                    <h5 class="mb-3">Permission Matrix</h5>
                    <div class="mb-3">
                        <button type="button" class="btn btn-sm btn-outline-success" id="btn-check-all">Check All</button>
                        <button type="button" class="btn btn-sm btn-outline-secondary" id="btn-uncheck-all">Uncheck All</button>
                    </div>
                    <?php foreach ($modules as $moduleName => $perms): ?>
                    <div class="card mb-3">
                        <div class="card-header py-2"><strong><?= e(ucfirst($moduleName)) ?></strong></div>
                        <div class="card-body py-2">
                            <div class="row">
                                <?php foreach ($perms as $p): ?>
                                <div class="col-md-4 col-lg-3">
                                    <div class="form-check">
                                        <input class="form-check-input perm-checkbox" type="checkbox" name="permissions[]" value="<?= $p['id'] ?>" id="perm-<?= $p['id'] ?>"
                                            <?= in_array($p['id'], $rolePermissions) ? 'checked' : '' ?>>
                                        <label class="form-check-label small" for="perm-<?= $p['id'] ?>" title="<?= e($p['permission_code']) ?>">
                                            <?= e($p['permission_name_en']) ?>
                                        </label>
                                    </div>
                                </div>
                                <?php endforeach; ?>
                            </div>
                        </div>
                    </div>
                    <?php endforeach; ?>
                    <div class="d-flex gap-2">
                        <button type="submit" class="btn btn-primary"><i class="fas fa-save me-1"></i>Save</button>
                        <a href="/admin/roles" class="btn btn-secondary">Cancel</a>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<?php $__view->push('scripts') ?>
<script>
$('#btn-check-all').click(function(){$('.perm-checkbox').prop('checked',true);});
$('#btn-uncheck-all').click(function(){$('.perm-checkbox').prop('checked',false);});
</script>
<?php $__view->endPush() ?>