<?= $this->include("themes/_commonPartialsBs/select2bs5") ?>
<?= $this->extend('themes/vuexy/main/defaultlayout') ?>

<?= $this->section("content") ?>

    <div class="row">
        <div class="col-12">
            <div class="card card-info">
                <h3 class="card-header"><?= lang("RolesPermisos.editRol") ?></h3>
                <div class="card-body">
                    <?= formAlert() ?>
                    <form id="groupForm" method="post" class="card-body" action="<?= $formAction ?>">
                        <?= csrf_field() ?>

                        <div class="row g-3">
                            <input type="hidden"
                                   name="id"
                                   value="<?= old('id', $group->id) ?>"
                            >
                            <div class="col-md-4">
                                <label for="title" class="form-label text-dark">
                                    <?= lang("RolesPermisos.rolName") ?>
                                </label>
                                <input type="text" name="title" id="title" class="form-control"
                                       value="<?= old('title', $group->title) ?>">
                            </div>
                            <div class="col-md-8">
                                <label for="description" class="form-label text-dark">
                                    <?= lang("RolesPermisos.rolDescription") ?>
                                </label>
                                <input type="text" name="description" id="description" class="form-control"
                                       value="<?= old('description', $group->description) ?>">
                            </div>
                        </div>

                        <div class="row g-3 mt-3">
                            <div class="col-md-12">
                                <h5><?= lang("RolesPermisos.rolPermissions") ?></h5>
                                <!-- Permission table -->
                                <div class="table-responsive">
                                    <table class="table table-flush-spacing">
                                        <tbody>
                                        <tr>
                                            <td class="text-nowrap fw-semibold" colspan="1">
                                                <?= lang("RolesPermisos.adminRol") ?>
                                                <i class="ti ti-info-circle" data-bs-toggle="tooltip"
                                                   data-bs-placement="top"
                                                   aria-label="<?= lang("RolesPermisos.adminRolTooltip") ?>"
                                                   data-bs-original-title="<?= lang("RolesPermisos.adminRolTooltip") ?>">
                                                </i>
                                            </td>
                                            <td colspan="3">
                                                <div class="form-check">
                                                    <input class="form-check-input" type="checkbox" name="selectAll">
                                                    <label class="form-check-label" for="selectAll">
                                                        <?= lang("RolesPermisos.selectAll") ?>
                                                    </label>
                                                </div>
                                            </td>
                                        </tr>

                                        <?php foreach ($permisos as $permiso): ?>
                                            <tr>
                                                <td colspan="4" class="text-nowrap fw-semibold">
                                                    <?= $permiso->name ?>
                                                </td>
                                            </tr>
                                            <?php foreach ($permiso->rules as $key => $rules): ?>
                                                <tr>
                                                    <td class="text-nowrap fw-semibold" >
                                                        &emsp;&emsp;<?= lang("RolesPermisos." . convertToCamelCase($key) . "Section") ?>
                                                    </td>
                                                    <td colspan="3">
                                                        <div class="row">
                                                    <?php foreach ($rules as $rule): ?>
                                                        
                                                        <div class="col-md-4">
                                                            <div class="form-check text-wrap">
                                                                <input type="checkbox"
                                                                       id="<?= $key . '_' . $rule ?>"
                                                                       name="<?= $key . '_' . $rule ?>"
                                                                       class="form-check-input"
                                                                    <?= ruleIsChecked($group->rules, $key, $rule) ? 'checked' : '' ?>
                                                                >
                                                                <label for="<?= $key . '_' . $rule ?>" class="form-check-label">
                                                                    <?= lang("RolesPermisos." . $rule . "Permission") ?>
                                                                </label>
                                                            </div>
                                                        </div>
                                                        
                                                    <?php endforeach; ?>
                                                    </div>
                                                    </td>
                                                </tr>
                                            <?php endforeach;
                                        endforeach;
                                        ?>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>

                        <div class="pt-4">
                            <input type="submit" class="btn btn-primary float-start me-sm-3 me-1" name="save"
                                   value="<?= lang("Basic.global.Save") ?>">
                            <?= anchor(route_to("groupIndex"), lang("Basic.global.Cancel"), ["class" => "btn btn-secondary"]) ?>
                        </div>
                    </form>

                </div><!-- /.card-body -->

            </div><!-- //.card -->
        </div><!--//.col -->
    </div><!--//.row -->
<?= $this->endSection() ?>

<?= $this->section('additionalInlineJs') ?>

$('input[name="selectAll"]').click(function() {
    var checkedStatus = this.checked;
    $('input[type="checkbox"]').each(function() {
        this.checked = checkedStatus;
    });
});

<?= $this->endSection() ?>