<?php
    /**
     * @var \App\View\AppView $this
     * @var Icing\Model\Entity\Role[]|\Cake\Collection\CollectionInterface $roles
     * @var Icing\Model\Entity\Role $newRole
     * @var string[] $permissions
     * @var string[] $userGroups
     */

    $this->assign('title', 'Roles');

    $this->Breadcrumbs->prepend('User Groups', ['controller' => 'UserGroups', 'action' => 'index']);
    $this->Breadcrumbs->prepend('Users', ['controller' => 'Users', 'action' => 'index']);

    $this->PageActions->group('top-right')
        ->link(
            '<i class="fa-solid fa-users fa-fw"></i> User Groups',
            ['controller' => 'UserGroups', 'action' => 'index'],
            ['class' => 'btn btn-primary']
        )
        ->link(
            '<i class="fa-solid fa-unlock-keyhole fa-fw"></i> Manage Permissions',
            ['controller' => 'Permissions', 'action' => 'index'],
            ['class' => 'btn btn-primary']
        );
?>

<div class="card">
    <div class="card-header">
        <?= $this->element('Icing.quick_search', ['advanced' => '#RolesFilter']) ?>
    </div>
    <div class="table-responsive">
        <table class="table table-sm table-hover m-0">
            <thead class="thead-light table-nowrap">
            <tr>
                <th><?= $this->Paginator->sort('name') ?></th>
            </tr>
            </thead>
            <tbody>
            <?php foreach ($roles as $role): ?>
                <tr
                    data-fetch="link"
                    data-url="<?= $this->Url->build(['action' => 'edit', $role->id]) ?>"
                >
                    <td><?= h($role->name) ?></td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <div class="card-footer">
        <?= $this->element('Icing.pagination') ?>
    </div>
</div>

<?= $this->Form->create(
    $newRole,
    [
        'type'         => 'get',
        'valueSources' => 'query',
        'novalidate'   => true,
        'floating'     => true,
    ]
) ?>
<div
    class="offcanvas offcanvas-end"
    tabindex="-1"
    id="RolesFilter"
    aria-labelledby="RolesFilterLabel"
    aria-modal="true"
    role="dialog"
>
    <div class="offcanvas-header">
        <h3
            class="offcanvas-title"
            id="rolesFilterLabel"
            tabindex="0"
        >
            Advanced Search
        </h3>
        <div class="d-flex align-items-start">
            <a
                href="<?= $this->Url->build(['?' => array_intersect_key($this->getRequest()->getQuery(), $this->Paginator->params())]) ?>"
                class="p-2 text-body"
                data-bs-toggle="tooltip"
                title="Reset Form"
                data-bs-placement="left"
            >
                <i class="fa-solid fa-rotate-left fa-lg"></i>
            </a>
            <a
                href="javascript: void"
                data-bs-dismiss="offcanvas"
                aria-label="Close"
                class="p-2 text-body"
                data-bs-toggle="tooltip"
                title="Close"
                data-bs-placement="left"
            >
                <i class="fa fa-times fa-lg"></i>
            </a>
        </div>
    </div>

    <div class="offcanvas-body scrollbar">
        <?php
            echo $this->Form->control('search', ['label' => 'Quick Search']);
            echo $this->Form->control('id');
            echo $this->Form->control('name');
            echo $this->Form->control('user_groups._ids', ['options' => $userGroups]);
        ?>
    </div>

    <div class="offcanvas-body flex-grow-0 border-top">
        <div class="d-grid">
            <button
                type="submit"
                class="btn btn-primary"
            >
                Submit
            </button>
        </div>
    </div>
</div>
<?= $this->Form->end() ?>

<?php
    $this->PageActions->group('bottom-right')
        ->link(
            '<i class="fa fa-user-plus fa-fw"></i> New Role',
            ['action' => 'add'],
            ['class' => 'btn btn-success', 'data-fetch' => 'modal'],
        );
?>

