<script setup>
import { ref, computed } from 'vue';
import { useRoute } from 'vue-router';
import { useI18n } from 'vue-i18n';
import { toDate } from 'src/filters';
import CustomerDescriptorProxy from 'pages/Customer/Card/CustomerDescriptorProxy.vue';
import CardDescriptor from 'components/ui/CardDescriptor.vue';
import TicketDescriptorMenu from './TicketDescriptorMenu.vue';
import VnLv from 'src/components/ui/VnLv.vue';
import useCardDescription from 'src/composables/useCardDescription';
import VnUserLink from 'src/components/ui/VnUserLink.vue';

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

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

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

const filter = {
    include: [
        {
            relation: 'address',
            scope: {
                fields: ['id', 'name', 'mobile', 'phone'],
            },
        },
        {
            relation: 'client',
            scope: {
                fields: ['id', 'name', 'salesPersonFk', 'phone', 'mobile', 'email'],
                include: [
                    {
                        relation: 'user',
                        scope: {
                            fields: ['id', 'lang'],
                        },
                    },
                    { relation: 'salesPersonUser' },
                ],
            },
        },
        {
            relation: 'ticketState',
            scope: {
                include: { relation: 'state' },
            },
        },
        {
            relation: 'warehouse',
            scope: {
                fields: ['id', 'name'],
            },
        },
        {
            relation: 'agencyMode',
            scope: {
                fields: ['id', 'name'],
            },
        },
    ],
};

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

<template>
    <CardDescriptor
        module="Ticket"
        :url="`Tickets/${entityId}`"
        :filter="filter"
        :title="data.title"
        :subtitle="data.subtitle"
        data-key="ticketData"
        @on-fetch="setData"
    >
        <template #menu="{ entity }">
            <TicketDescriptorMenu :ticket="entity" />
        </template>
        <template #body="{ entity }">
            <VnLv :label="t('ticket.card.customerId')">
                <template #value>
                    <span class="link">
                        {{ entity.clientFk }}
                        <CustomerDescriptorProxy :id="entity.client.id" />
                    </span>
                </template>
            </VnLv>
            <VnLv v-if="entity.ticketState" :label="t('ticket.card.state')">
                <template #value>
                    <QBadge :color="entity.ticketState.state.classColor">
                        {{ entity.ticketState.state.name }}
                    </QBadge>
                </template>
            </VnLv>
            <VnLv :label="t('ticket.summary.salesPerson')">
                <template #value>
                    <VnUserLink
                        :name="entity.client.salesPersonUser?.name"
                        :worker-id="entity.client.salesPersonFk"
                    />
                </template>
            </VnLv>
            <VnLv :label="t('ticket.card.shipped')" :value="toDate(entity.shipped)" />
            <VnLv
                v-if="entity.agencyMode"
                :label="t('ticket.card.agency')"
                :value="entity.agencyMode.name"
            />
            <VnLv :label="t('ticket.card.warehouse')" :value="entity.warehouse?.name" />
            <VnLv :label="t('ticket.card.alias')" :value="entity.nickname" />
        </template>
        <template #icons="{ entity }">
            <QCardActions>
                <QIcon
                    v-if="entity.isDeleted == true"
                    name="vn:deletedTicket"
                    size="xs"
                    color="primary"
                >
                    <QTooltip>{{ t('This ticket is deleted') }}</QTooltip>
                </QIcon>
            </QCardActions>
        </template>
        <template #actions="{ entity }">
            <QCardActions>
                <QBtn
                    size="md"
                    icon="vn:client"
                    color="primary"
                    :to="{ name: 'CustomerCard', params: { id: entity.clientFk } }"
                >
                    <QTooltip>{{ t('ticket.card.customerCard') }}</QTooltip>
                </QBtn>
            </QCardActions>
        </template>
    </CardDescriptor>
</template>

<i18n>
es:
    This ticket is deleted: Este ticket está eliminado
</i18n>
