<script setup>
import { ref, computed, onMounted, onUnmounted } from 'vue';
import { useI18n } from 'vue-i18n';
import { useStateStore } from 'stores/useStateStore';
import { downloadFile } from 'src/composables/downloadFile';
import { toDate, toCurrency } from 'src/filters/index';
import InvoiceInFilter from './InvoiceInFilter.vue';
import InvoiceInSummary from './Card/InvoiceInSummary.vue';
import { useSummaryDialog } from 'src/composables/useSummaryDialog';
import SupplierDescriptorProxy from 'src/pages/Supplier/Card/SupplierDescriptorProxy.vue';
import RightMenu from 'src/components/common/RightMenu.vue';
import InvoiceInSearchbar from 'src/pages/InvoiceIn/InvoiceInSearchbar.vue';
import VnTable from 'src/components/VnTable/VnTable.vue';
import VnSelect from 'src/components/common/VnSelect.vue';
import VnInput from 'src/components/common/VnInput.vue';
import VnInputDate from 'src/components/common/VnInputDate.vue';

const stateStore = useStateStore();
const { viewSummary } = useSummaryDialog();
const { t } = useI18n();

onMounted(async () => (stateStore.rightDrawer = true));
onUnmounted(() => (stateStore.rightDrawer = false));

const tableRef = ref();
const cols = computed(() => [
    {
        align: 'left',
        name: 'id',
        label: 'Id',
    },
    {
        align: 'left',
        name: 'supplierFk',
        label: t('invoiceIn.list.supplier'),
        columnFilter: {
            component: 'select',
            attrs: {
                url: 'Suppliers',
                fields: ['id', 'name'],
            },
        },
        columnClass: 'expand',
    },
    {
        align: 'left',
        name: 'supplierRef',
        label: t('invoiceIn.list.supplierRef'),
    },

    {
        align: 'left',
        name: 'serialNumber',
        label: t('invoiceIn.list.serialNumber'),
    },
    {
        align: 'left',
        name: 'serial',
        label: t('invoiceIn.list.serial'),
    },
    {
        align: 'left',
        label: t('invoiceIn.list.issued'),
        name: 'issued',
        component: null,
        columnFilter: {
            component: 'date',
        },
        format: (row, dashIfEmpty) => dashIfEmpty(toDate(row.issued)),
    },
    {
        align: 'left',
        name: 'isBooked',
        label: t('invoiceIn.list.isBooked'),
        columnFilter: false,
    },
    {
        align: 'left',
        name: 'awbCode',
        label: t('invoiceIn.list.awb'),
    },
    {
        align: 'left',
        name: 'amount',
        label: t('invoiceIn.list.amount'),
        format: ({ amount }) => toCurrency(amount),
    },
    {
        align: 'right',
        name: 'tableActions',
        actions: [
            {
                title: t('components.smartCard.openSummary'),
                icon: 'preview',
                type: 'submit',
                action: (row) => viewSummary(row.id, InvoiceInSummary),
            },
            {
                title: t('globals.download'),
                icon: 'download',
                type: 'submit',
                isPrimary: true,
                action: (row) => downloadFile(row.dmsFk),
            },
        ],
    },
]);
</script>
<template>
    <InvoiceInSearchbar />
    <RightMenu>
        <template #right-panel>
            <InvoiceInFilter data-key="InvoiceInList" />
        </template>
    </RightMenu>
    <VnTable
        ref="tableRef"
        data-key="InvoiceInList"
        url="InvoiceIns/filter"
        :order="['issued DESC', 'id DESC']"
        :create="{
            urlCreate: 'InvoiceIns',
            title: t('globals.createInvoiceIn'),
            onDataSaved: ({ id }) => tableRef.redirect(id),
            formInitialData: {},
        }"
        redirect="invoice-in"
        :columns="cols"
        :right-search="false"
        :disable-option="{ card: true }"
        :auto-load="!!$route.query.params"
    >
        <template #column-supplierFk="{ row }">
            <span class="link" @click.stop>
                {{ row.supplierName }}
                <SupplierDescriptorProxy :id="row.supplierFk" />
            </span>
        </template>
        <template #more-create-dialog="{ data }">
            <VnSelect
                v-model="data.supplierFk"
                url="Suppliers"
                :fields="['id', 'nickname']"
                :label="t('Supplier')"
                option-value="id"
                option-label="nickname"
                :filter-options="['id', 'name']"
                :required="true"
            >
                <template #option="scope">
                    <QItem v-bind="scope.itemProps">
                        <QItemSection>
                            <QItemLabel>{{ scope.opt?.nickname }}</QItemLabel>
                            <QItemLabel caption> #{{ scope.opt?.id }} </QItemLabel>
                        </QItemSection>
                    </QItem>
                </template>
            </VnSelect>
            <VnInput
                :label="t('invoiceIn.summary.supplierRef')"
                v-model="data.supplierRef"
            />
            <VnSelect
                url="Companies"
                :label="t('Company')"
                :fields="['id', 'code']"
                v-model="data.companyFk"
                option-value="id"
                option-label="code"
                :required="true"
            />
            <VnInputDate :label="t('invoiceIn.summary.issued')" v-model="data.issued" />
        </template>
    </VnTable>
</template>
