<script>
    import Paginate from "$/components/Paginate.svelte";
    import { getRolesaplicaciones , getRolesyaplicaciones } from "$/services/rolesyaplicaciones";
    import PageTitle from "$/components/PageTitle.svelte";
    import ModalRol from "./ModalRolaplicacion.svelte";

    const limit = 15;
    let page = 1;
    let offset = 0;
    let count = 0;
    let ordering = 'id_rol'
    let roles = []
    let rol = null
    let loading = false;
    let data2 = []

    $: onPage(page)

    async function onPage(p) {
        try {
            loading = true
            offset = (p - 1) * limit;
            const data = await getRolesaplicaciones({ offset, limit, ordering })
            data2= await getRolesyaplicaciones(1)
            roles = data.results;
            count = data.count;
        } catch (error) {
            alert(error)
        } finally {
            loading = false;
        }
    }

    function onEdita(item) {
        rol = item;
    }

    function onNuevo() {
        rol = {}
    }

    function onOrderBy(field) {
        ordering = ordering === field ? '-' + field : field;
        onPage(page)
    }
</script>

<PageTitle {loading}>rolesyaplicaciones</PageTitle>
<div>
    {data2}
    {#each data2 as rol}
    {rol.id_rol}
    {rol.id_aplicacion}
    {/each}
</div>

<div class="card">
    <div class="card-header">
        <button class="btn btn-primary" on:click|preventDefault={onNuevo}>
            <i class="bi bi-plus-lg"></i> Nuevo
       </button>
    </div>
    <div class="card-body">
        <div class="table-responsive">
            <table class="table table-sm table-bordered">
                <thead>
                    <tr class="table-light">
                        <th style="width:5%">Nro</th>
                        <th>
                            <a href={"#"} on:click|preventDefault={() => onOrderBy('id_aplicacion')}>ID rol</a>
                            {#if ordering === 'id_aplicacion'}<i class="bi bi-caret-up-fill"></i>{/if}
                            {#if ordering === '-id_aplicacion'}<i class="bi bi-caret-down-fill"></i>{/if}
                        </th>
                        <th>
                            <a href={"#"} on:click|preventDefault={() => onOrderBy('id_rol')}>Id app</a>
                            {#if ordering === 'id_rol'}<i class="bi bi-caret-up-fill"></i>{/if}
                            {#if ordering === '-id_rol'}<i class="bi bi-caret-down-fill"></i>{/if}
                        </th>
                        <th style="width:5%">Solo Visualizar</th>
                    </tr>
                </thead>
                <tbody>
                    {#each roles as app, index}
                    <tr>
                        <td class="table-light">{offset + index + 1}</td>
                        <td>{app.id_rol}</td>
                        <td><a href={"#"} on:click|preventDefault={() => onEdita(app)}>{app.id_aplicacion}</a></td>
                        <td>{app.solo_visualizar}</td>
                    </tr>
                    {/each}
                </tbody>
            </table>
        </div>
    </div>
    <div class="card-footer d-flex">
        <a href={"#"} class="btn btn-outline-secondary me-3" on:click|preventDefault={() => onPage(page)}>
            <i class="bi bi-arrow-repeat"></i>
        </a>
        <Paginate
            {offset}
            {limit}
            {count}
            on:page={ev => page = ev.detail}
        />
    </div>
</div>

{#if rol}
    <ModalRol
        {rol}
        on:close={() => rol = null}
        on:refresh={() => onPage(page)}
    />
{/if}


<style>
    .table-responsive {
        max-height: calc(100vh - 300px);
    }
</style>