<script>
    import Paginate from "$/components/Paginate.svelte";
    import { getOperadores } from "$/services/operadores";
    import PageTitle from "$/components/PageTitle.svelte";
    import ModalOperador from "./ModalOperador.svelte";
    import { useLocation } from "svelte-navigator";
    import { getPermisosPath } from "$/services/usuarios";

    const limit = 15;
    let page = 1;
    let offset = 0;
    let count = 0;
    let ordering = 'id_operador'
    let operadores = []
    let operador = null
    let verLineas =null 
    let loading = false;
    let escritura = false;
    let location = useLocation()

    getPermisosPath($location.pathname)
        .then(data => escritura = data.escritura)
        .catch(error => console.log({ error }))

    $: onPage(page) 
    async function onPage(p) {
        try {
            loading = true
            offset = (p - 1) * limit;             
            const data = await getOperadores({ offset, limit, ordering })
            operadores = data.results;
            count = data.count;
        } catch (error) {
            globalThis.toast.error(error)
        } finally {
            loading = false;
        }
    }

    function onEdita(item) {
        operador = item;
    }

    function onVerLineas(item){
        verLineas = item;
        operador = item;
    }

    function onNuevo() {
        operador = {}
    }

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

<PageTitle {loading}>Operadores</PageTitle>

<div class="card">
    <div class="card-header">
        {#if escritura}
            <button class="btn btn-primary" on:click|preventDefault={onNuevo}>
                <i class="bi bi-plus-lg"></i> Nuevo
            </button>
        {/if}
    </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_operador')}>ID</a>
                            {#if ordering === 'id_operador'}<i class="bi bi-caret-up-fill"></i>{/if}
                            {#if ordering === '-id_operador'}<i class="bi bi-caret-down-fill"></i>{/if}
                        </th>
                        <th>
                            <a href={"#"} on:click|preventDefault={() => onOrderBy('nombre_operador')}>Nombre</a>
                            {#if ordering === 'nombre_operador'}<i class="bi bi-caret-up-fill"></i>{/if}
                            {#if ordering === '-nombre_operador'}<i class="bi bi-caret-down-fill"></i>{/if}
                        </th>
                        
                        <th>
                            <a href={"#"} on:click|preventDefault={() => onOrderBy('vigente')}>Vigente</a>
                            {#if ordering === 'vigente'}<i class="bi bi-caret-up-fill"></i>{/if}
                            {#if ordering === '-vigente'}<i class="bi bi-caret-down-fill"></i>{/if}
                        </th>
                    </tr>
                </thead>
                <tbody>
                    {#each operadores as app, index}
                    <tr key={index}>
                        <td class="table-light">{offset + index + 1}</td>
                        <td>{app.id_operador}</td>
                        <td><a href={"#"} on:click|preventDefault={() => onEdita(app)}>{app.nombre_operador}</a></td>                        
                        <td>{app.vigente ? '✅':'🚫'}</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
            {limit}
            {count}
            on:page={ev => page = ev.detail}
        />
    </div>
</div>

{#if operador}
    <ModalOperador    
        {operador}
        {escritura}
        on:close={() => operador = null}
        on:refresh={() => onPage(page)}
    />
{/if}

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