<script setup>
import { onMounted, ref } from 'vue';
import { useI18n } from 'vue-i18n';
import { useRouter } from 'vue-router';
import { useRoute } from 'vue-router';
import { useStateStore } from 'stores/useStateStore';
import VnTable from 'components/VnTable/VnTable.vue';
import { useSummaryDialog } from 'src/composables/useSummaryDialog';
import { computed } from 'vue';
import TravelSummary from './Card/TravelSummary.vue';
import VnSearchbar from 'components/ui/VnSearchbar.vue';
import { dashIfEmpty, toDate } from 'src/filters';
const { viewSummary } = useSummaryDialog();
const router = useRouter();
const { t } = useI18n();
const stateStore = useStateStore();
const route = useRoute();
const tableRef = ref();
const $props = defineProps({
    id: {
        type: Number,
        default: 0,
    },
});
const entityId = computed(() => $props.id || route.params.id);
const cloneTravel = (travelData) => {
    const stringifiedTravelData = JSON.stringify(travelData);
    redirectToCreateView(stringifiedTravelData);
};

const redirectToCreateView = (queryParams) => {
    router.push({ name: 'TravelCreate', query: { travelData: queryParams } });
};

const redirectCreateEntryView = (travelData) => {
    router.push({ name: 'EntryCreate', query: { travelFk: travelData.id } });
};

onMounted(async () => {
    stateStore.rightDrawer = true;
});

const columns = computed(() => [
    {
        align: 'left',
        name: 'id',
        label: t('travel.travelList.tableVisibleColumns.id'),
        isId: true,
        field: 'id',
        cardVisible: true,
    },
    {
        align: 'left',
        name: 'ref',
        label: t('travel.travelList.tableVisibleColumns.ref'),
        field: 'ref',
        component: 'input',
        columnField: {
            component: null,
        },
        cardVisible: true,
        create: true,
    },
    {
        align: 'left',
        name: 'agencyModeFk',
        label: t('travel.travelList.tableVisibleColumns.agency'),
        field: 'agencyModeFk',
        component: 'select',
        attrs: {
            url: 'agencyModes',
            fields: ['id', 'name'],
        },
        format: (row) => row.agencyModeName,
        columnField: {
            component: null,
        },
        cardVisible: true,
        create: true,
    },
    {
        align: 'left',
        name: 'shipped',
        label: t('travel.travelList.tableVisibleColumns.shipped'),
        field: 'shipped',
        component: 'date',
        columnField: {
            component: null,
        },
        cardVisible: true,
        create: true,
        format: (row, dashIfEmpty) => dashIfEmpty(toDate(row.shipped)),
    },
    {
        align: 'left',
        name: 'landed',
        label: t('travel.travelList.tableVisibleColumns.landed'),
        field: 'landed',
        component: 'date',
        columnField: {
            component: null,
        },
        cardVisible: true,
        create: true,
        format: (row, dashIfEmpty) => dashIfEmpty(toDate(row.landed)),
    },
    {
        align: 'left',
        name: 'warehouseInFk',
        label: t('travel.travelList.tableVisibleColumns.warehouseIn'),
        field: 'warehouseInFk',
        component: 'select',
        attrs: {
            url: 'warehouses',
            fields: ['id', 'name'],
            optionLabel: 'name',
            optionValue: 'id',
        },
        format: (row) => row.warehouseInName,
        columnField: {
            component: null,
        },
        cardVisible: true,
        create: true,
    },
    {
        align: 'left',
        name: 'warehouseOutFk',
        label: t('travel.travelList.tableVisibleColumns.warehouseOut'),
        field: 'warehouseOutFk',
        component: 'select',
        attrs: {
            url: 'warehouses',
            fields: ['id', 'name'],
        },
        format: (row) => row.warehouseOutName,
        columnField: {
            component: null,
        },
        cardVisible: true,
        create: true,
    },
    {
        align: 'left',
        name: 'totalEntries',
        label: t('travel.travelList.tableVisibleColumns.totalEntries'),
        field: 'totalEntries',
        component: 'input',
        columnField: {
            component: null,
        },
        cardVisible: true,
        create: true,
    },
    {
        align: 'right',
        label: '',
        name: 'tableActions',
        actions: [
            {
                title: t('Clone'),
                icon: 'vn:clone',
                action: cloneTravel,
                isPrimary: true,
            },
            {
                title: t('Add entry'),
                icon: 'contact_support',
                action: redirectCreateEntryView,
            },
            {
                title: t('View Summary'),
                icon: 'preview',
                action: (row) => viewSummary(row.id, TravelSummary),
            },
        ],
    },
]);
</script>

<template>
    <VnSearchbar
        :info="t('You can search by travel id or name')"
        :label="t('Search travel')"
        data-key="TravelList"
    />
    <VnTable
        ref="tableRef"
        data-key="TravelList"
        url="Travels/filter"
        :create="{
            urlCreate: 'Travels',
            title: t('Create Travels'),
            onDataSaved: ({ id }) => tableRef.redirect(id),
            formInitialData: {
                editorFk: entityId,
            },
        }"
        order="landed DESC"
        :columns="columns"
        auto-load
        redirect="travel"
        :is-editable="false"
        :use-model="true"
    />
</template>

<i18n>
en:
    addEntry: Add entry
    searchByIdOrReference: Search by ID or reference

es:
    addEntry: Añadir entrada
    searchByIdOrReference: Buscar por ID o por referencia
    You can search by travel id or name: Buscar por envio por id o nombre
    Search travel: Buscar envio
</i18n>
