<script>
    import Paginate from "$/components/Paginate.svelte";
    import { getRedTransporte } from "$/services/red_transporte";
    import PageTitle from "$/components/PageTitle.svelte";
    import ModalRedTransporte from "./ModalRedTransporte.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_red'
    let redes = []
    let red = 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 getRedTransporte({ offset, limit, ordering })
            redes = data.results;
            count = data.count;
        } catch (error) {
            globalThis.toast.error(error)
        } finally {
            loading = false;
        }
    }

    function onEdita(item) {
        red = item;
    }

    function onNuevo() {
        red = {}
    }

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

<PageTitle {loading}>Redes de Transporte</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 style="width: 5%">
                            <a href={"#"} on:click|preventDefault={() => onOrderBy('id_red')}>ID</a>
                            {#if ordering === 'id_red'}<i class="bi bi-caret-up-fill"></i>{/if}
                            {#if ordering === '-id_red'}<i class="bi bi-caret-down-fill"></i>{/if}
                        </th>
                        <th>
                            <a href={"#"} on:click|preventDefault={() => onOrderBy('nombre_red')}>Nombre</a>
                            {#if ordering === 'nombre_red'}<i class="bi bi-caret-up-fill"></i>{/if}
                            {#if ordering === '-nombre_red'}<i class="bi bi-caret-down-fill"></i>{/if}
                        </th>
                        
                        <th>
                            <a href={"#"} on:click|preventDefault={() => onOrderBy('descripcion')}>Descripcion</a>
                            {#if ordering === 'descripcion'}<i class="bi bi-caret-up-fill"></i>{/if}
                            {#if ordering === '-descripcion'}<i class="bi bi-caret-down-fill"></i>{/if}
                        </th>

                        <th>
                            <a href={"#"} on:click|preventDefault={() => onOrderBy('url_gtfs_rt')}>URL_GTFS_RT</a>
                            {#if ordering === 'url_gtfs_rt'}<i class="bi bi-caret-up-fill"></i>{/if}
                            {#if ordering === '-url_gtfs_rt'}<i class="bi bi-caret-down-fill"></i>{/if}
                        </th>
                        <th>
                            <a href={"#"} on:click|preventDefault={() => onOrderBy('api_key')}>api_key</a>
                            {#if ordering === 'api_key'}<i class="bi bi-caret-up-fill"></i>{/if}
                            {#if ordering === '-api_key'}<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 redes as app, index}
                    <tr>
                        <td class="table-light">{offset + index + 1}</td>
                        <td>{app.id_red}</td>
                        <td><a href={"#"} on:click|preventDefault={() => onEdita(app)}>{app.nombre_red}</a></td>
                        <td>{app.descripcion}</td>
                        <td>{app.url_gtfs_rt}</td>
                        <td>{app.api_key}</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
            forcePage={page}
            {limit}
            {count}
            on:page={ev => page = ev.detail}
        />
    </div>
</div>

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

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