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

import { toCurrency, toDate } from 'src/filters';

import useCardDescription from 'src/composables/useCardDescription';

import CardDescriptor from 'components/ui/CardDescriptor.vue';
import VnLv from 'src/components/ui/VnLv.vue';
import VnUserLink from 'src/components/ui/VnUserLink.vue';
import CustomerDescriptorMenu from './CustomerDescriptorMenu.vue';

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

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

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

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

<template>
    <CardDescriptor
        module="Customer"
        :url="`Clients/${entityId}/getCard`"
        :title="data.title"
        :subtitle="data.subtitle"
        @on-fetch="setData"
        :summary="$props.summary"
        data-key="customerData"
    >
        <template #header-extra-action>
            <QBtn
                round
                flat
                size="sm"
                icon="vn:Person"
                color="white"
                :to="{ name: 'CustomerList' }"
            >
                <QTooltip>
                    {{ t('Go to module index') }}
                </QTooltip>
            </QBtn>
        </template>
        <template #menu="{ entity }">
            <CustomerDescriptorMenu :customer="entity" />
        </template>
        <template #body="{ entity }">
            <VnLv :label="t('customer.card.payMethod')" :value="entity.payMethod.name" />

            <VnLv :label="t('customer.card.credit')" :value="toCurrency(entity.credit)" />
            <VnLv
                :label="t('customer.card.securedCredit')"
                :value="toCurrency(entity.creditInsurance)"
            />

            <VnLv :label="t('customer.card.debt')" :value="toCurrency(entity.debt)" />
            <VnLv v-if="entity.salesPersonUser" :label="t('customer.card.salesPerson')">
                <template #value>
                    <VnUserLink
                        :name="entity.salesPersonUser?.name"
                        :worker-id="entity.salesPersonFk"
                    />
                </template>
            </VnLv>
            <VnLv
                :label="t('customer.card.businessTypeFk')"
                :value="entity.businessTypeFk"
            />
        </template>
        <template #icons="{ entity }">
            <QCardActions class="q-gutter-x-md">
                <QIcon
                    v-if="!entity.isActive"
                    name="vn:disabled"
                    size="xs"
                    color="primary"
                >
                    <QTooltip>{{ t('customer.card.isDisabled') }}</QTooltip>
                </QIcon>
                <QIcon v-if="entity.isFreezed" name="vn:frozen" size="xs" color="primary">
                    <QTooltip>{{ t('customer.card.isFrozen') }}</QTooltip>
                </QIcon>
                <QIcon
                    v-if="!entity.account.active"
                    color="primary"
                    name="vn:noweb"
                    size="xs"
                >
                    <QTooltip>{{ t('customer.card.webAccountInactive') }}</QTooltip>
                </QIcon>
                <QIcon
                    v-if="entity.debt > entity.credit"
                    name="vn:risk"
                    size="xs"
                    color="primary"
                >
                    <QTooltip>{{ t('customer.card.hasDebt') }}</QTooltip>
                </QIcon>
                <QIcon
                    v-if="!entity.isTaxDataChecked"
                    name="vn:no036"
                    size="xs"
                    color="primary"
                >
                    <QTooltip>{{ t('customer.card.notChecked') }}</QTooltip>
                </QIcon>
                <QBtn
                    v-if="entity.unpaid"
                    flat
                    size="sm"
                    icon="vn:Client_unpaid"
                    color="primary"
                    :to="{ name: 'CustomerUnpaid' }"
                >
                    <QTooltip>
                        {{ t('Unpaid') }}
                        <br />
                        {{
                            t('unpaidDated', {
                                dated: toDate(entity.unpaid.dated),
                            })
                        }}
                        <br />
                        {{
                            t('unpaidAmount', {
                                amount: toCurrency(entity.unpaid.amount),
                            })
                        }}
                    </QTooltip>
                </QBtn>
            </QCardActions>
        </template>
        <template #actions="{ entity }">
            <QCardActions class="flex justify-center">
                <QBtn
                    :to="{
                        name: 'TicketList',
                        query: { params: JSON.stringify({ clientFk: entity.id }) },
                    }"
                    size="md"
                    icon="vn:ticket"
                    color="primary"
                >
                    <QTooltip>{{ t('Customer ticket list') }}</QTooltip>
                </QBtn>
                <QBtn
                    :to="{
                        name: 'InvoiceOutList',
                        query: { params: JSON.stringify({ clientFk: entity.id }) },
                    }"
                    size="md"
                    icon="vn:invoice-out"
                    color="primary"
                >
                    <QTooltip>{{ t('Customer invoice out list') }}</QTooltip>
                </QBtn>
                <QBtn
                    :to="{
                        name: 'OrderCreate',
                        query: { clientFk: entity.id },
                    }"
                    size="md"
                    icon="vn:basketadd"
                    color="primary"
                >
                    <QTooltip>{{ t('New order') }}</QTooltip>
                </QBtn>
                <QBtn size="md" icon="face" color="primary">
                    <!-- TODO:: Redirigir a la vista de usuario cuando exista -->
                    <QTooltip>{{ t('Go to user') }}</QTooltip>
                </QBtn>
            </QCardActions>
        </template>
    </CardDescriptor>
</template>

<i18n>
en:
    unpaidDated: 'Date {dated}'
    unpaidAmount: 'Amount {amount}'
es:
    Go to module index: Ir al índice del módulo
    Customer ticket list: Listado de tickets del cliente
    Customer invoice out list: Listado de facturas del cliente
    New order: Nuevo pedido
    Go to user: Ir al usuario
    Customer unpaid: Cliente impago
    Unpaid: Impagado
    unpaidDated: 'Fecha {dated}'
    unpaidAmount: 'Importe {amount}'
</i18n>
