<script setup>
import { useI18n } from 'vue-i18n';
import { useStateStore } from 'stores/useStateStore';
import InvoiceInDescriptor from './InvoiceInDescriptor.vue';
import LeftMenu from 'components/LeftMenu.vue';
import VnSearchbar from 'components/ui/VnSearchbar.vue';
import VnSubToolbar from 'src/components/ui/VnSubToolbar.vue';
import { useArrayData } from 'src/composables/useArrayData';
import { onMounted, watch } from 'vue';
import { useRoute } from 'vue-router';
import useCardSize from 'src/composables/useCardSize';

const stateStore = useStateStore();
const { t } = useI18n();
const route = useRoute();

const filter = {
    include: [
        {
            relation: 'supplier',
            scope: {
                include: {
                    relation: 'contacts',
                    scope: {
                        where: {
                            email: { neq: null },
                        },
                    },
                },
            },
        },
        {
            relation: 'invoiceInDueDay',
        },
        {
            relation: 'company',
        },
        {
            relation: 'currency',
        },
    ],
};
const arrayData = useArrayData('InvoiceIn', {
    url: `InvoiceIns/${route.params.id}`,
    filter,
});

onMounted(async () => await arrayData.fetch({ append: false }));
watch(
    () => route.params.id,
    async (newId, oldId) => {
        if (newId) {
            arrayData.store.url = `InvoiceIns/${newId}`;
            await arrayData.fetch({ append: false });
        }
    }
);
</script>
<template>
    <Teleport to="#searchbar" v-if="stateStore.isHeaderMounted()">
        <VnSearchbar
            data-key="InvoiceInList"
            url="InvoiceIns/filter"
            :label="t('Search invoice')"
            :info="t('You can search by invoice reference')"
        />
    </Teleport>
    <QDrawer v-model="stateStore.leftDrawer" show-if-above :width="256">
        <QScrollArea class="fit">
            <InvoiceInDescriptor />
            <QSeparator />
            <LeftMenu source="card" />
        </QScrollArea>
    </QDrawer>
    <QPageContainer>
        <QPage>
            <VnSubToolbar />
            <div :class="useCardSize()">
                <RouterView></RouterView>
            </div>
        </QPage>
    </QPageContainer>
</template>

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