<script setup>
import { ref, computed } from 'vue';
import { useRoute } from 'vue-router';
import { useI18n } from 'vue-i18n';
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';
import { toDateTimeFormat } from 'src/filters/date';

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', 'incotermsFk'],
            },
        },
        {
            relation: 'client',
            scope: {
                fields: [
                    'id',
                    'name',
                    'salesPersonFk',
                    'phone',
                    'mobile',
                    'email',
                    'isActive',
                    'isFreezed',
                    'isTaxDataChecked',
                    'hasElectronicInvoice',
                ],
                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'],
            },
        },
        {
            relation: 'zone',
            scope: {
                fields: [
                    'agencyModeFk',
                    'bonus',
                    'hour',
                    'id',
                    'isVolumetric',
                    'itemMaxSize',
                    'm3Max',
                    'name',
                    'price',
                    'travelingDays',
                ],
            },
        },
    ],
};

const data = ref(useCardDescription());

function ticketFilter(ticket) {
    return JSON.stringify({ clientFk: ticket.clientFk });
}
</script>

<template>
    <CardDescriptor
        module="Ticket"
        :url="`Tickets/${entityId}`"
        :filter="filter"
        :title="data.title"
        :subtitle="data.subtitle"
        data-key="ticketData"
    >
        <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('globals.state')">
                <template #value>
                    <QBadge
                        text-color="black"
                        :color="entity.ticketState.state.classColor"
                        data-cy="ticketDescriptorStateBadge"
                    >
                        {{ entity.ticketState.state.name }}
                    </QBadge>
                </template>
            </VnLv>
            <VnLv :label="t('globals.salesPerson')">
                <template #value>
                    <VnUserLink
                        :name="entity.client?.salesPersonUser?.name"
                        :worker-id="entity.client?.salesPersonFk"
                    />
                </template>
            </VnLv>
            <VnLv
                :label="t('globals.shipped')"
                :value="toDateTimeFormat(entity.shipped)"
            />
            <VnLv
                v-if="entity.agencyMode"
                :label="t('globals.agency')"
                :value="entity.agencyMode.name"
            />
            <VnLv :label="t('globals.warehouse')" :value="entity.warehouse?.name" />
            <VnLv :label="t('globals.alias')" :value="entity.nickname" />
        </template>
        <template #icons="{ entity }">
            <QCardActions class="q-gutter-x-xs">
                <QIcon
                    v-if="entity.client.isActive == false"
                    name="vn:disabled"
                    size="xs"
                    color="primary"
                >
                    <QTooltip>{{ t('Client inactive') }}</QTooltip>
                </QIcon>
                <QIcon
                    v-if="entity.client.isFreezed == true"
                    name="vn:frozen"
                    size="xs"
                    color="primary"
                >
                    <QTooltip>{{ t('Client Frozen') }}</QTooltip>
                </QIcon>
                <QIcon
                    v-if="entity?.problem?.includes('hasRisk')"
                    name="vn:risk"
                    size="xs"
                    color="primary"
                >
                    <QTooltip>{{ t('Client has debt') }}</QTooltip>
                </QIcon>
                <QIcon
                    v-if="entity.client.isTaxDataChecked == false"
                    name="vn:no036"
                    size="xs"
                    color="primary"
                >
                    <QTooltip>{{ t('Client not checked') }}</QTooltip>
                </QIcon>
                <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>
                <QBtn
                    size="md"
                    icon="vn:ticket"
                    color="primary"
                    :to="{ name: 'TicketList', query: { table: ticketFilter(entity) } }"
                >
                    <QTooltip>{{ t('ticket.card.ticketList') }}</QTooltip>
                </QBtn>
                <QBtn
                    size="md"
                    icon="vn:basketadd"
                    color="primary"
                    :to="{
                        name: 'OrderList',
                        query: {
                            createForm: JSON.stringify({ clientFk: entity.clientFk }),
                        },
                    }"
                >
                    <QTooltip>{{ t('ticket.card.newOrder') }}</QTooltip>
                </QBtn>
            </QCardActions>
        </template>
    </CardDescriptor>
</template>

<i18n>
es:
    This ticket is deleted: Este ticket está eliminado
    Go to module index: Ir al índice del modulo
    Client inactive: Cliente inactivo
    Client not checked: Cliente no verificado
    Client has debt: Cliente con deuda
    Client Frozen: Cliente congelado
</i18n>
