<script setup>
import { computed, ref } from 'vue';
import { useRoute } from 'vue-router';
import { useI18n } from 'vue-i18n';

import CardDescriptor from 'components/ui/CardDescriptor.vue';
import VnLv from 'src/components/ui/VnLv.vue';
import useCardDescription from 'src/composables/useCardDescription';
import TravelDescriptorMenuItems from './TravelDescriptorMenuItems.vue';

import { toDate } from 'src/filters';

const $props = defineProps({
    id: {
        type: Number,
        required: false,
        default: null,
    },
});

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

const filter = {
    fields: [
        'id',
        'ref',
        'shipped',
        'landed',
        'totalEntries',
        'warehouseInFk',
        'warehouseOutFk',
        'cargoSupplierFk',
        'agencyModeFk',
    ],
    include: [
        {
            relation: 'warehouseIn',
            scope: {
                fields: ['name'],
            },
        },
        {
            relation: 'warehouseOut',
            scope: {
                fields: ['name'],
            },
        },
    ],
};

const entityId = computed(() => {
    return $props.id || route.params.id;
});

const data = ref(useCardDescription());
const setData = (entity) => (data.value = useCardDescription(entity.ref, entity.id));
</script>

<template>
    <CardDescriptor
        module="Travel"
        :url="`Travels/${entityId}`"
        :title="data.title"
        :subtitle="data.subtitle"
        :filter="filter"
        data-key="travelData"
        @on-fetch="setData"
    >
        <template #menu="{ entity }">
            <TravelDescriptorMenuItems :travel="entity" />
        </template>
        <template #body="{ entity }">
            <VnLv :label="t('globals.wareHouseIn')" :value="entity.warehouseIn.name" />
            <VnLv :label="t('globals.wareHouseOut')" :value="entity.warehouseOut.name" />
            <VnLv :label="t('globals.shipped')" :value="toDate(entity.shipped)" />
            <VnLv :label="t('globals.landed')" :value="toDate(entity.landed)" />
            <VnLv :label="t('globals.totalEntries')" :value="entity.totalEntries" />
        </template>
        <template #actions="{ entity }">
            <QCardActions>
                <QBtn
                    :to="{
                        name: 'TravelList',
                        query: {
                            params: JSON.stringify({
                                agencyModeFk: entity.agencyModeFk,
                            }),
                        },
                    }"
                    size="md"
                    icon="local_airport"
                    color="primary"
                >
                    <QTooltip>{{ t('All travels with current agency') }}</QTooltip>
                </QBtn>
            </QCardActions>
        </template>
    </CardDescriptor>
</template>

<i18n>
es:
    Go to module index: Ir al índice del módulo
    The travel will be deleted: El envío será eliminado
    Do you want to delete this travel?: ¿Quieres eliminar este envío?
    All travels with current agency: Todos los envíos con la agencia actual
</i18n>
