<script setup>
import { ref, onMounted, onUnmounted } from 'vue';
import { useI18n } from 'vue-i18n';
import { useRouter } from 'vue-router';
import { useStateStore } from 'stores/useStateStore';
import { downloadFile } from 'src/composables/downloadFile';
import { toDate, toCurrency } from 'src/filters/index';
import VnPaginate from 'src/components/ui/VnPaginate.vue';
import VnSearchbar from 'src/components/ui/VnSearchbar.vue';
import VnLv from 'src/components/ui/VnLv.vue';
import CardList from 'src/components/ui/CardList.vue';
import InvoiceInFilter from './InvoiceInFilter.vue';
import { getUrl } from 'src/composables/getUrl';
import InvoiceInSummary from './Card/InvoiceInSummary.vue';
import { useSummaryDialog } from 'src/composables/useSummaryDialog';

const stateStore = useStateStore();
const router = useRouter();
const { viewSummary } = useSummaryDialog();
let url = ref();
const { t } = useI18n();

onMounted(async () => {
    stateStore.rightDrawer = true;
    url.value = await getUrl('');
});
onUnmounted(() => (stateStore.rightDrawer = false));

function navigate(id) {
    router.push({ path: `/invoice-in/${id}` });
}
</script>

<template>
    <template v-if="stateStore.isHeaderMounted()">
        <Teleport to="#searchbar">
            <VnSearchbar
                data-key="InvoiceInList"
                :label="t('Search invoice')"
                :info="t('You can search by invoice reference')"
            />
        </Teleport>
        <Teleport to="#actions-append">
            <div class="row q-gutter-x-sm">
                <QBtn
                    flat
                    @click="stateStore.toggleRightDrawer()"
                    round
                    dense
                    icon="menu"
                >
                    <QTooltip bottom anchor="bottom right">
                        {{ t('globals.collapseMenu') }}
                    </QTooltip>
                </QBtn>
            </div>
        </Teleport>
    </template>
    <QDrawer v-model="stateStore.rightDrawer" side="right" :width="256" show-if-above>
        <QScrollArea class="fit text-grey-8">
            <InvoiceInFilter data-key="InvoiceInList" />
        </QScrollArea>
    </QDrawer>
    <QPage class="column items-center q-pa-md">
        <div class="vn-card-list">
            <VnPaginate
                data-key="InvoiceInList"
                url="InvoiceIns/filter"
                order="issued DESC, id DESC"
                auto-load
            >
                <template #body="{ rows }">
                    <CardList
                        v-for="row of rows"
                        :key="row.id"
                        :title="row.supplierRef"
                        @click="navigate(row.id)"
                        :id="row.id"
                    >
                        <template #list-items>
                            <VnLv
                                :label="t('invoiceIn.list.supplierRef')"
                                :value="row.supplierRef"
                            />
                            <VnLv
                                :label="t('invoiceIn.list.supplier')"
                                :value="row.supplierName"
                            />
                            <VnLv
                                :label="t('invoiceIn.list.serialNumber')"
                                :value="row.serialNumber"
                            />
                            <VnLv
                                :label="t('invoiceIn.list.serial')"
                                :value="row.serial"
                            />
                            <VnLv
                                :label="t('invoiceIn.list.issued')"
                                :value="toDate(row.issued)"
                            />
                            <VnLv :label="t('invoiceIn.list.awb')" :value="row.awbCode" />
                            <VnLv
                                :label="t('invoiceIn.list.amount')"
                                :value="toCurrency(row.amount)"
                            />
                            <VnLv
                                :label="t('invoiceIn.list.isBooked')"
                                :value="!!row.isBooked"
                            />
                        </template>
                        <template #actions>
                            <QBtn
                                :label="t('components.smartCard.openCard')"
                                @click.stop="navigate(row.id)"
                                class="bg-vn-dark"
                                outline
                                type="reset"
                            />

                            <QBtn
                                :label="t('components.smartCard.openSummary')"
                                @click.stop="viewSummary(row.id, InvoiceInSummary)"
                                color="primary"
                                type="submit"
                                class="q-mt-sm"
                            />
                            <QBtn
                                :label="t('Download')"
                                class="q-mt-sm"
                                @click.stop="downloadFile(row.dmsFk)"
                                type="submit"
                                color="primary"
                            />
                        </template>
                    </CardList>
                </template>
            </VnPaginate>
        </div>
    </QPage>
    <QPageSticky position="bottom-right" :offset="[20, 20]">
        <QBtn
            color="primary"
            icon="add"
            size="lg"
            round
            :href="`${url}invoice-in/create`"
        />
    </QPageSticky>
</template>

<i18n>
es:
    Search invoice: Buscar factura recibida
    You can search by invoice reference: Puedes buscar por referencia de la factura
    Download: Descargar
</i18n>
