<script setup>
import { useI18n } from 'vue-i18n';
import { useQuasar } from 'quasar';
import { useRouter } from 'vue-router';

import VnSearchbar from 'src/components/ui/VnSearchbar.vue';
import VnPaginate from 'src/components/ui/VnPaginate.vue';
import CardList from 'src/components/ui/CardList.vue';
import VnLv from 'src/components/ui/VnLv.vue';
import SupplierSummaryDialog from './Card/SupplierSummaryDialog.vue';
import SupplierListFilter from './SupplierListFilter.vue';

import { useStateStore } from 'stores/useStateStore';

const stateStore = useStateStore();
const router = useRouter();
const quasar = useQuasar();
const { t } = useI18n();

function navigate(id) {
    router.push({ path: `/supplier/${id}` });
}

const redirectToCreateView = () => {
    router.push({ name: 'SupplierCreate' });
};

const viewSummary = (id) => {
    quasar.dialog({
        component: SupplierSummaryDialog,
        componentProps: {
            id,
        },
    });
};
</script>

<template>
    <template v-if="stateStore.isHeaderMounted()">
        <Teleport to="#searchbar">
            <VnSearchbar
                data-key="SuppliersList"
                :limit="20"
                :label="t('Search suppliers')"
            />
        </Teleport>
    </template>

    <QPage class="column items-center q-pa-md">
        <QDrawer v-model="stateStore.rightDrawer" side="right" :width="256" show-if-above>
            <QScrollArea class="fit text-grey-8">
                <SupplierListFilter data-key="SuppliersList" />
            </QScrollArea>
        </QDrawer>
        <div class="vn-card-list">
            <VnPaginate data-key="SuppliersList" url="Suppliers/filter" auto-load>
                <template #body="{ rows }">
                    <CardList
                        v-for="row of rows"
                        :key="row.id"
                        :title="row.socialName"
                        :id="row.id"
                        @click="navigate(row.id)"
                    >
                        <template #list-items>
                            <VnLv label="NIF/CIF" :value="row.nif" />
                            <VnLv label="Alias" :value="row.alias" />
                            <VnLv
                                :label="t('supplier.list.payMethod')"
                                :value="row.payMethod"
                            />
                            <VnLv
                                :label="t('supplier.list.payDeadline')"
                                :title-label="t('invoiceOut.list.created')"
                                :value="row.payDem"
                            />
                            <VnLv
                                :label="t('supplier.list.payDay')"
                                :value="row.payDay"
                            />
                            <VnLv
                                :label="t('supplier.list.account')"
                                :value="row.account"
                            />
                        </template>
                        <template #actions>
                            <QBtn
                                :label="t('components.smartCard.openSummary')"
                                @click.stop="viewSummary(row.id)"
                                color="primary"
                            />
                        </template>
                    </CardList>
                </template>
            </VnPaginate>
        </div>
        <QPageSticky :offset="[20, 20]">
            <QBtn fab icon="add" color="primary" @click="redirectToCreateView()" />
            <QTooltip>
                {{ t('supplier.list.newSupplier') }}
            </QTooltip>
        </QPageSticky>
    </QPage>
</template>

<i18n>
en:
    Search suppliers: Search suppliers

es:
    Search suppliers: Buscar proveedores
</i18n>
