<script setup>
import axios from 'axios';
import VnSection from 'src/components/common/VnSection.vue';
import { ref, computed } from 'vue';
import { useI18n } from 'vue-i18n';
import { useState } from 'src/composables/useState';
import { onBeforeMount } from 'vue';

import EntryFilter from './EntryFilter.vue';
import VnTable from 'components/VnTable/VnTable.vue';
import SupplierDescriptorProxy from 'src/pages/Supplier/Card/SupplierDescriptorProxy.vue';
import VnSelectTravelExtended from 'src/components/common/VnSelectTravelExtended.vue';
import { toDate } from 'src/filters';

const { t } = useI18n();
const tableRef = ref();
const defaultEntry = ref({});
const state = useState();
const user = state.getUser();
const dataKey = 'EntryList';

const entryQueryFilter = {
    include: [
        {
            relation: 'suppliers',
            scope: {
                fields: ['id', 'name'],
            },
        },
        {
            relation: 'travels',
            scope: {
                fields: ['id', 'ref'],
            },
        },
        {
            relation: 'companies',
            scope: {
                fields: ['id', 'code'],
            },
        },
    ],
};

const columns = computed(() => [
    {
        label: 'Ex',
        toolTip: t('entry.list.tableVisibleColumns.isExcludedFromAvailable'),
        name: 'isExcludedFromAvailable',
        component: 'checkbox',
        width: '35px',
    },
    {
        label: 'Pe',
        toolTip: t('entry.list.tableVisibleColumns.isOrdered'),
        name: 'isOrdered',
        component: 'checkbox',
        width: '35px',
    },
    {
        label: 'Le',
        toolTip: t('entry.list.tableVisibleColumns.isConfirmed'),
        name: 'isConfirmed',
        component: 'checkbox',
        width: '35px',
    },
    {
        label: 'Re',
        toolTip: t('entry.list.tableVisibleColumns.isReceived'),
        name: 'isReceived',
        component: 'checkbox',
        width: '35px',
    },
    {
        label: t('entry.list.tableVisibleColumns.landed'),
        name: 'landed',
        component: 'date',
        columnField: {
            component: null,
        },
        format: (row, dashIfEmpty) => dashIfEmpty(toDate(row.landed)),
        width: '105px',
    },
    {
        label: t('globals.id'),
        name: 'id',
        isId: true,
        component: 'number',
        chip: {
            condition: () => true,
        },
    },
    {
        label: t('entry.list.tableVisibleColumns.supplierFk'),
        name: 'supplierFk',
        create: true,
        cardVisible: true,
        component: 'select',
        attrs: {
            url: 'suppliers',
            fields: ['id', 'name'],
        },
        format: (row, dashIfEmpty) => dashIfEmpty(row.supplierName),
    },
    {
        align: 'left',
        label: t('entry.list.tableVisibleColumns.invoiceNumber'),
        name: 'invoiceNumber',
        component: 'input',
    },
    {
        align: 'left',
        label: t('entry.list.tableVisibleColumns.reference'),
        name: 'reference',
        isTitle: true,
        component: 'input',
        columnField: {
            component: null,
        },
        cardVisible: true,
    },
    {
        align: 'left',
        label: 'AWB',
        name: 'awbCode',
        component: 'input',
    },
    {
        align: 'left',
        label: t('entry.list.tableVisibleColumns.agencyModeId'),
        name: 'agencyModeId',
        cardVisible: true,
        component: 'select',
        attrs: {
            url: 'agencyModes',
            fields: ['id', 'name'],
        },
        columnField: {
            component: null,
        },
        format: (row, dashIfEmpty) => dashIfEmpty(row.agencyModeName),
    },
    {
        align: 'left',
        label: t('entry.list.tableVisibleColumns.evaNotes'),
        name: 'evaNotes',
        component: 'input',
    },
    {
        align: 'left',
        label: t('entry.list.tableVisibleColumns.warehouseOutFk'),
        name: 'warehouseOutFk',
        cardVisible: true,
        component: 'select',
        attrs: {
            url: 'warehouses',
            fields: ['id', 'name'],
        },
        columnField: {
            component: null,
        },
        format: (row, dashIfEmpty) => dashIfEmpty(row.warehouseOutName),
    },
    {
        align: 'left',
        label: t('entry.list.tableVisibleColumns.warehouseInFk'),
        name: 'warehouseInFk',
        cardVisible: true,
        component: 'select',
        attrs: {
            url: 'warehouses',
            fields: ['id', 'name'],
        },
        columnField: {
            component: null,
        },
        format: (row, dashIfEmpty) => dashIfEmpty(row.warehouseInName),
    },
    {
        align: 'left',
        label: t('entry.list.tableVisibleColumns.entryTypeDescription'),
        name: 'entryTypeCode',
        cardVisible: true,
    },
    {
        name: 'dated',
        label: t('entry.list.tableVisibleColumns.dated'),
        component: 'date',
        cardVisible: false,
        visible: false,
        create: true,
    },
    {
        name: 'companyFk',
        label: t('entry.list.tableVisibleColumns.companyFk'),
        cardVisible: false,
        visible: false,
        create: true,
        component: 'select',
        attrs: {
            optionValue: 'id',
            optionLabel: 'code',
            url: 'Companies',
        },
    },
    {
        name: 'travelFk',
        label: t('entry.list.tableVisibleColumns.travelFk'),
        cardVisible: false,
        visible: false,
        create: true,
    },
]);
function getBadgeAttrs(row) {
    const date = row.landed;
    let today = Date.vnNew();
    today.setHours(0, 0, 0, 0);
    let timeTicket = new Date(date);
    timeTicket.setHours(0, 0, 0, 0);

    let timeDiff = today - timeTicket;

    if (timeDiff > 0) return { color: 'warning', 'text-color': 'black' };
    switch (row.entryTypeCode) {
        case 'regularization':
        case 'life':
        case 'internal':
        case 'inventory':
            if (!row.isOrdered || !row.isConfirmed)
                return { color: 'negative', 'text-color': 'black' };
            break;
        case 'product':
        case 'packaging':
        case 'devaluation':
        case 'payment':
        case 'transport':
            if (
                row.invoiceAmount === null ||
                (row.invoiceNumber === null && row.reference === null) ||
                !row.isOrdered ||
                !row.isConfirmed
            )
                return { color: 'negative', 'text-color': 'black' };
            break;
        default:
            break;
    }
    if (timeDiff < 0) return { color: 'info', 'text-color': 'black' };
    return { color: 'transparent' };
}

onBeforeMount(async () => {
    defaultEntry.value = (await axios.get('EntryConfigs/findOne')).data;
});
</script>

<template>
    <VnSection
        :data-key="dataKey"
        prefix="entry"
        url="Entries/filter"
        :array-data-props="{
            url: 'Entries/filter',
            order: 'landed DESC',
            userFilter: EntryFilter,
        }"
    >
        <template #advanced-menu>
            <EntryFilter :data-key="dataKey" />
        </template>
        <template #body>
            <VnTable
                v-if="defaultEntry.defaultSupplierFk"
                ref="tableRef"
                :data-key="dataKey"
                url="Entries/filter"
                :filter="entryQueryFilter"
                order="landed DESC"
                :create="{
                    urlCreate: 'Entries',
                    title: t('Create entry'),
                    onDataSaved: ({ id }) => tableRef.redirect(id),
                    formInitialData: {
                        supplierFk: defaultEntry.defaultSupplierFk,
                        dated: Date.vnNew(),
                        companyFk: user?.companyFk,
                    },
                }"
                :columns="columns"
                redirect="entry"
                :right-search="false"
            >
                <template #column-landed="{ row }">
                    <QBadge
                        v-if="row?.travelFk"
                        v-bind="getBadgeAttrs(row)"
                        class="q-pa-sm"
                        style="font-size: 14px"
                    >
                        {{ toDate(row.landed) }}
                    </QBadge>
                </template>
                <template #column-supplierFk="{ row }">
                    <span class="link" @click.stop>
                        {{ row.supplierName }}
                        <SupplierDescriptorProxy :id="row.supplierFk" />
                    </span>
                </template>
                <template #column-create-travelFk="{ data }">
                    <VnSelectTravelExtended
                        :data="data"
                        v-model="data.travelFk"
                        :onFilterTravelSelected="
                            (data, result) => (data.travelFk = result)
                        "
                        data-cy="entry-travel-select"
                    />
                </template>
            </VnTable>
        </template>
    </VnSection>
</template>

<i18n>
es:
    Inventory entry: Es inventario
    Virtual entry: Es una redada
    Search entries: Buscar entradas
    You can search by entry reference: Puedes buscar por referencia de la entrada
    Create entry: Crear entrada
</i18n>
