<script>
    // imagenes
    import IconParada from "$/assets/parada.png";
    // libs
    import { createEventDispatcher } from "svelte";
    import FormParadero from "./FormParadero.svelte";
    import FormParaderoDispositivos from "./FormParaderoDispositivos.svelte";
    import FormParaderoServicios from "./FormParaderoServicios.svelte";
    import { useLocation } from "svelte-navigator";
    import { getPermisosPath } from "$/services/usuarios";
    const dispatch = createEventDispatcher();
    export let parada = null;
    let canvas = null;
    let tab = 0;
    let escritura = false;
    let location = useLocation()

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

    $: init(!!parada);

    function init(show) {
        if (!canvas) return;
        if (show) {
            canvas.classList.add("show");
        } else {
            canvas.classList.remove("show");
            tab = 0;
        }
    }
</script>

<div
    class="offcanvas offcanvas-end"
    tabindex="-1"
    bind:this={canvas}
    aria-labelledby="offcanvasParaderoLabel"
    style="visibility: inherit;"
>
    <div class="offcanvas-header d-block pb-0">
        <div class="d-flex justify-content-between">
            <h5 class="offcanvas-title text-info" id="offcanvasParaderoLabel">
                <img src={IconParada} alt="Icono de Paradero" width="64" />
                Paradero #{parada?.id_paradero} {parada?.location}
            </h5>
            <button
                type="button"
                class="btn-close"
                aria-label="Close"
                on:click|preventDefault={() => dispatch("close")}
            />
        </div>

        <nav class="mb-0" style="--bs-nav-tabs-link-active-border-color: var(--bs-dark)">
            <ul class="nav nav-tabs">
                <li class="nav-item">
                    <a
                        class={"nav-link" + (tab === 0 ? " active" : "")}
                        href={"#"}
                        on:click|preventDefault={() => (tab = 0)}>
                        General
                    </a>
                </li>
                <li class="nav-item">
                    <a
                        class={"nav-link" + (tab === 1 ? " active" : "")}
                        href={"#"}
                        on:click|preventDefault={() => (tab = 1)}>
                        Dispositivos
                    </a>
                </li>
                <li class="nav-item">
                    <a
                        class={"nav-link" + (tab === 2 ? " active" : "")}
                        href={"#"}
                        on:click|preventDefault={() => (tab = 2)}>
                        Servicios
                    </a>
                </li>
            </ul>
        </nav>
    </div>
    <div class="offcanvas-body bg-white py-3">
        {#if tab === 0}
            <FormParadero {parada} {escritura} />
        {/if}
        {#if tab === 1}
            <FormParaderoDispositivos {parada} {escritura} />
        {/if}
        {#if tab === 2}
            <FormParaderoServicios {parada} />
        {/if}
    </div>
</div>

{#if parada}
    <div
        class="offcanvas-backdrop fade show"
        on:click|preventDefault={() => dispatch("close")}
        on:keydown={() => {}}
    />
{/if}

{#if parada}
    <style>
        html {
            overflow-y: hidden;
        }
        
        .offcanvas {
            --bs-offcanvas-width: 500px;
            --bs-offcanvas-bg: var(--bs-gray-200);
        }

        .nav-tabs {
            --bs-nav-tabs-border-color: var(--bs-white);
            --bs-nav-tabs-border-width: 0;
        }
        
        .nav-tabs .nav-link.active {
            --bs-nav-tabs-link-active-bg: var(--bs-white);
            --bs-nav-tabs-link-active-color: var(--bs-secondary);
            --bs-nav-tabs-link-active-border-color: var(--bs-white);
        }
    </style>
{/if}
