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

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

    <div class="row">
        <div class="col-12">
            <div class="card card-info">
                <h3 class="card-header"><?= $boxTitle ?? $pageTitle ?></h3>
                <div class="card-body">
                    <?= formAlert() ?>
                    <form id="groupForm" method="post" class="card-body" action="<?= $formAction ?>">
                        <?= csrf_field() ?>
                        <input type="hidden"
                               name="id_group"
                               value="<?= (isset($obj)) ? $obj['id_group'] : set_value('id_group') ?>"
                        >
                        <input type="hidden"
                               name="token"
                               value="<?= (isset($obj)) ? $obj['token'] : set_value('token') ?>"
                        >
                        <div class="row g-3">
                            <div class="col-md-6">
                                <label for="title" class="form-label text-dark">
                                    <?= lang("App.group_field_title") ?>
                                </label>
                                <input type="text" name="title" id="title" class="form-control"
                                       value="<?= old('title', $group->title) ?>">
                            </div>
                            <div class="col-md-6">
                                <label for="dashboard" class="form-label text-dark">
                                    <?= lang("App.group_field_dashboard") ?>
                                </label>
                                <?php $id_select = (isset($group)) ? $group->dashboard : set_value('dashboard'); ?>
                                <select name="dashboard" id="dashboard" class="select2 form-control">
                                    <option value="user" <?= $id_select == "user" ? 'selected' : '' ?>><?= lang("App.group_label_user") ?></option>
                                    <option value="admin" <?= $id_select == "admin" ? 'selected' : '' ?>><?= lang("App.group_label_admin") ?></option>
                                </select>
                            </div>
                            <div class="col-md-6">
                                <button type="button" name="select_all"
                                        id="select_all"
                                        class="btn btn-primary me-sm-3 me-1">
                                    <i class="la la-toggle-on"></i> <?= lang("App.group_btn_select") ?>
                                </button>
                                <button type="button" name="remove_all"
                                        id="remove_all"
                                        class="btn btn-secondary ">
                                    <i class="la la-toggle-off"></i> <?= lang("App.group_btn_remove") ?>
                                </button>
                            </div>
                        </div>
                        <div class="row g-3 mt-3">
                            <div class="col-md-12">
                                <h5>Role Permissions</h5>
                                <!-- Permission table -->
                                <div class="table-responsive">
                                    <table class="table table-flush-spacing">
                                        <tbody>
                                        <?php foreach (getAllFolder() as $folder): ?>
                                            <?php foreach (getAllClass() as $item): ?>
                                                <?php if (str_contains($item['path'], $folder)): ?>
                                                    <tr>
                                                        <td class="text-nowrap fw-semibold">
                                                            <?= getDictionary($folder) ?? '' ?>
                                                            >> <?= getDictionary($item['name']) ?>
                                                        </td>
                                                        <?php foreach ($item['methods'] as $subitem): ?>
                                                            <?php if (!getIgnoreMethod($subitem)): ?>
                                                                <td>
                                                                    <div class="form-check">
                                                                        <input type="checkbox"
                                                                               id="<?= $item['name'] . '_' . $subitem ?>"
                                                                               name="<?= $item['name'] . '_' . $subitem ?>"
                                                                               class="form-check-input"
                                                                            <?= ruleIsChecked($group->rules, $item['name'], $subitem) ? 'checked' : '' ?>
                                                                        >
                                                                        <label for="<?= $item['name'] . '_' . $subitem ?>"
                                                                               class="form-check-label"><?= getDictionary($subitem) ?></label>
                                                                    </div>
                                                                </td>
                                                            <?php endif; ?>
                                                        <?php endforeach; ?>
                                                    </tr>
                                                <?php endif; ?>
                                            <?php endforeach; ?>
                                        <?php endforeach; ?>

                                        <?php foreach (getAllClass() as $item): ?>
                                            <?php if (empty($item['path'])): ?>
                                                <tr>
                                                    <td class="text-nowrap fw-semibold"><?= getDictionary($item['name']) ?></td>
                                                    <?php foreach ($item['methods'] as $subitem): ?>
                                                        <?php if (!getIgnoreMethod($subitem)): ?>
                                                            <td>
                                                                <div class="form-check">
                                                                    <input type="checkbox"
                                                                           id="<?= $item['name'] . '_' . $subitem ?>"
                                                                           name="<?= $item['name'] . '_' . $subitem ?>"
                                                                           class="form-check-input"
                                                                        <?= ruleIsChecked($group->rules, $item['name'], $subitem) ? 'checked' : '' ?>
                                                                    >
                                                                    <label for="<?= $item['name'] . '_' . $subitem ?>"
                                                                           class="form-check-label"><?= getDictionary($subitem) ?></label>
                                                                </div>
                                                            </td>
                                                        <?php endif; ?>
                                                    <?php endforeach; ?>
                                                </tr>
                                            <?php endif; ?>
                                        <?php 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') ?>

    $('#select_all').on('click', function () {
    $(':checkbox').each(function() {
    this.checked = true;
    });
    });
    $('#remove_all').on('click', function () {
    $(':checkbox').each(function() {
    this.checked = false;
    });
    });

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