<script setup>
import { ref, computed } from 'vue';
import { useI18n } from 'vue-i18n';
import EntryFilter from './EntryFilter.vue';
import VnTable from 'components/VnTable/VnTable.vue';
import { toCelsius, toDate } from 'src/filters';
import { useSummaryDialog } from 'src/composables/useSummaryDialog';
import EntrySummary from './Card/EntrySummary.vue';
import SupplierDescriptorProxy from 'src/pages/Supplier/Card/SupplierDescriptorProxy.vue';
import TravelDescriptorProxy from 'src/pages/Travel/Card/TravelDescriptorProxy.vue';
import VnSection from 'src/components/common/VnSection.vue';

const { t } = useI18n();
const tableRef = ref();
const dataKey = 'EntryList';

const { viewSummary } = useSummaryDialog();
const entryFilter = {
    include: [
        {
            relation: 'suppliers',
            scope: {
                fields: ['id', 'name'],
            },
        },
        {
            relation: 'travels',
            scope: {
                fields: ['id', 'ref'],
            },
        },
        {
            relation: 'companies',
            scope: {
                fields: ['id', 'code'],
            },
        },
    ],
};

const columns = computed(() => [
    {
        name: 'status',
        columnFilter: false,
    },
    {
        align: 'left',
        label: t('globals.id'),
        name: 'id',
        isId: true,
        chip: {
            condition: () => true,
        },
    },
    {
        align: 'left',
        label: t('globals.reference'),
        name: 'reference',
        isTitle: true,
        component: 'input',
        columnField: {
            component: null,
        },
        create: true,
        cardVisible: true,
    },
    {
        align: 'left',
        label: t('entry.list.tableVisibleColumns.created'),
        name: 'created',
        create: true,
        cardVisible: true,
        component: 'date',
        columnField: {
            component: null,
        },
        format: (row, dashIfEmpty) => dashIfEmpty(toDate(row.created)),
    },
    {
        align: 'left',
        label: t('entry.list.tableVisibleColumns.supplierFk'),
        name: 'supplierFk',
        create: true,
        cardVisible: true,
        component: 'select',
        attrs: {
            url: 'suppliers',
            fields: ['id', 'name'],
        },
        columnField: {
            component: null,
        },
        format: (row, dashIfEmpty) => dashIfEmpty(row.supplierName),
    },
    {
        align: 'left',
        label: t('entry.list.tableVisibleColumns.isBooked'),
        name: 'isBooked',
        cardVisible: true,
        create: true,
        component: 'checkbox',
    },
    {
        align: 'left',
        label: t('entry.list.tableVisibleColumns.isConfirmed'),
        name: 'isConfirmed',
        cardVisible: true,
        create: true,
        component: 'checkbox',
    },
    {
        align: 'left',
        label: t('entry.list.tableVisibleColumns.isOrdered'),
        name: 'isOrdered',
        cardVisible: true,
        create: true,
        component: 'checkbox',
    },
    {
        align: 'left',
        label: t('entry.list.tableVisibleColumns.companyFk'),
        name: 'companyFk',
        component: 'select',
        attrs: {
            url: 'companies',
            fields: ['id', 'code'],
            optionLabel: 'code',
            optionValue: 'id',
        },
        columnField: {
            component: null,
        },
        create: true,

        format: (row, dashIfEmpty) => dashIfEmpty(row.companyCode),
    },
    {
        align: 'left',
        label: t('entry.list.tableVisibleColumns.travelFk'),
        name: 'travelFk',
        component: 'select',
        attrs: {
            url: 'travels',
            fields: ['id', 'ref'],
            optionLabel: 'ref',
            optionValue: 'id',
        },
        columnField: {
            component: null,
        },
        create: true,
        format: (row, dashIfEmpty) => dashIfEmpty(row.travelRef),
    },
    {
        align: 'left',
        label: t('entry.list.tableVisibleColumns.invoiceAmount'),
        name: 'invoiceAmount',
        cardVisible: true,
    },
    {
        align: 'left',
        name: 'initialTemperature',
        label: t('entry.basicData.initialTemperature'),
        field: 'initialTemperature',
        format: (row) => toCelsius(row.initialTemperature),
    },
    {
        align: 'left',
        name: 'finalTemperature',
        label: t('entry.basicData.finalTemperature'),
        field: 'finalTemperature',
        format: (row) => toCelsius(row.finalTemperature),
    },
    {
        label: t('entry.list.tableVisibleColumns.isExcludedFromAvailable'),
        name: 'isExcludedFromAvailable',
        columnFilter: {
            inWhere: true,
        },
    },
    {
        align: 'right',
        name: 'tableActions',
        actions: [
            {
                title: t('components.smartCard.viewSummary'),
                icon: 'preview',
                action: (row) => viewSummary(row.id, EntrySummary),
                isPrimary: true,
            },
        ],
    },
]);
</script>

<template>
    <VnSection
        :data-key="dataKey"
        :columns="columns"
        prefix="entry"
        url="Entries/filter"
        :array-data-props="{
            url: 'Entries/filter',
            order: 'id DESC',
            userFilter: entryFilter,
        }"
    >
        <template #rightMenu>
            <EntryFilter data-key="EntryList" />
        </template>
        <template #body>
            <VnTable
                ref="tableRef"
                :data-key="dataKey"
                :create="{
                    urlCreate: 'Entries',
                    title: t('entry.list.newEntry'),
                    onDataSaved: ({ id }) => tableRef.redirect(id),
                    formInitialData: {},
                }"
                :columns="columns"
                redirect="entry"
                :right-search="false"
            >
                <template #column-status="{ row }">
                    <div class="row q-gutter-xs">
                        <QIcon
                            v-if="!!row.isExcludedFromAvailable"
                            name="vn:inventory"
                            color="primary"
                        >
                            <QTooltip>{{
                                t(
                                    'entry.list.tableVisibleColumns.isExcludedFromAvailable'
                                )
                            }}</QTooltip>
                        </QIcon>
                        <QIcon v-if="!!row.isRaid" name="vn:net" color="primary">
                            <QTooltip>
                                {{
                                    t('globals.raid', {
                                        daysInForward: row.daysInForward,
                                    })
                                }}</QTooltip
                            >
                        </QIcon>
                    </div>
                </template>
                <template #column-supplierFk="{ row }">
                    <span class="link" @click.stop>
                        {{ row.supplierName }}
                        <SupplierDescriptorProxy :id="row.supplierFk" />
                    </span>
                </template>
                <template #column-travelFk="{ row }">
                    <span class="link" @click.stop>
                        {{ row.travelRef }}
                        <TravelDescriptorProxy :id="row.travelFk" />
                    </span>
                </template>
            </VnTable>
        </template>
    </VnSection>
</template>
