<?php

declare(strict_types=1);

ob_start();

?>

<div class="d-flex justify-content-between align-items-center mb-4">

    <div>

        <h2 class="mb-1">
            Role Permissions
        </h2>

        <p class="text-muted mb-0">

            Assign permissions to:

            <strong>

                <?= htmlspecialchars(
                    $role['role_name'],
                    ENT_QUOTES,
                    'UTF-8'
                ) ?>

            </strong>

        </p>

    </div>

    <a
        href="/public/roles"
        class="btn btn-outline-secondary">

        <i class="bi bi-arrow-left me-1"></i>

        Back

    </a>

</div>


<?php if (!empty($error)): ?>

    <div class="alert alert-danger">

        <?= htmlspecialchars(
            $error,
            ENT_QUOTES,
            'UTF-8'
        ) ?>

    </div>

<?php endif; ?>


<form
    method="POST"
    action="/public/roles/permissions">

    <input
        type="hidden"
        name="id"
        value="<?= (int) $role['id'] ?>">


    <div class="card shadow-sm border-0">

        <div class="card-header bg-white py-3">

            <div class="d-flex justify-content-between align-items-center">

                <h5 class="mb-0">
                    Available Permissions
                </h5>

                <div>

                    <button
                        type="button"
                        class="btn btn-sm btn-outline-primary"
                        id="selectAll">

                        Select All

                    </button>

                    <button
                        type="button"
                        class="btn btn-sm btn-outline-secondary"
                        id="clearAll">

                        Clear All

                    </button>

                </div>

            </div>

        </div>


        <div class="card-body">

            <?php if (empty($permissions)): ?>

                <div class="text-center text-muted py-4">

                    No active permissions available.

                </div>

            <?php else: ?>

                <div class="row g-3">

                    <?php foreach ($permissions as $permission): ?>

                        <?php

                        $permissionId =
                            (int) $permission['id'];

                        $checked = in_array(
                            $permissionId,
                            $assignedPermissionIds,
                            true
                        );

                        ?>

                        <div class="col-lg-4 col-md-6">

                            <div class="border rounded p-3 h-100">

                                <div class="form-check">

                                    <input
                                        class="form-check-input permission-checkbox"
                                        type="checkbox"
                                        name="permissions[]"
                                        value="<?= $permissionId ?>"
                                        id="permission<?= $permissionId ?>"
                                        <?= $checked
                                            ? 'checked'
                                            : '' ?>>

                                    <label
                                        class="form-check-label"
                                        for="permission<?= $permissionId ?>">

                                        <strong>

                                            <?= htmlspecialchars(
                                                $permission['permission_name'],
                                                ENT_QUOTES,
                                                'UTF-8'
                                            ) ?>

                                        </strong>

                                    </label>

                                </div>

                                <?php if (
                                    !empty(
                                        $permission['description']
                                    )
                                ): ?>

                                    <div class="small text-muted mt-2 ms-4">

                                        <?= htmlspecialchars(
                                            $permission['description'],
                                            ENT_QUOTES,
                                            'UTF-8'
                                        ) ?>

                                    </div>

                                <?php endif; ?>

                            </div>

                        </div>

                    <?php endforeach; ?>

                </div>

            <?php endif; ?>

        </div>


        <div class="card-footer bg-white text-end py-3">

            <button
                type="submit"
                class="btn btn-primary">

                <i class="bi bi-shield-check me-1"></i>

                Save Permissions

            </button>

        </div>

    </div>

</form>


<script>

document
    .getElementById('selectAll')
    .addEventListener(
        'click',
        function () {

            document
                .querySelectorAll(
                    '.permission-checkbox'
                )
                .forEach(
                    function (checkbox) {
                        checkbox.checked = true;
                    }
                );
        }
    );

document
    .getElementById('clearAll')
    .addEventListener(
        'click',
        function () {

            document
                .querySelectorAll(
                    '.permission-checkbox'
                )
                .forEach(
                    function (checkbox) {
                        checkbox.checked = false;
                    }
                );
        }
    );

</script>

<?php

$content = ob_get_clean();

$title = 'Role Permissions';

include APP_PATH .
    '/Modules/Administration/Views/layouts/admin.php';