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

import { dashIfEmpty, 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';
import { useState } from 'src/composables/useState';
const state = useState();

const customer = computed(() => state.get('customer'));

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="customer"
    >
        <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)"
                :info="t('customer.summary.riskInfo')"
            />
            <VnLv :label="t('customer.card.salesPerson')">
                <template #value>
                    <VnUserLink
                        v-if="entity.salesPersonUser"
                        :name="entity.salesPersonUser.name"
                        :worker-id="entity.salesPersonFk"
                    />
                    <span v-else>{{ dashIfEmpty(entity.salesPersonUser) }}</span>
                </template>
            </VnLv>
            <VnLv
                :label="t('customer.card.businessTypeFk')"
                :value="entity.businessType.description"
            />
        </template>
        <template #icons>
            <QCardActions v-if="customer" class="q-gutter-x-md">
                <QIcon
                    v-if="!customer.isActive"
                    name="vn:disabled"
                    size="xs"
                    color="primary"
                >
                    <QTooltip>{{ t('customer.card.isDisabled') }}</QTooltip>
                </QIcon>
                <QIcon
                    v-if="customer.isFreezed"
                    name="vn:frozen"
                    size="xs"
                    color="primary"
                >
                    <QTooltip>{{ t('customer.card.isFrozen') }}</QTooltip>
                </QIcon>
                <QIcon
                    v-if="!customer.account?.active"
                    color="primary"
                    name="vn:noweb"
                    size="xs"
                >
                    <QTooltip>{{ t('customer.card.webAccountInactive') }}</QTooltip>
                </QIcon>
                <QIcon
                    v-if="customer.debt > customer.credit"
                    name="vn:risk"
                    size="xs"
                    color="primary"
                >
                    <QTooltip>{{ t('customer.card.hasDebt') }}</QTooltip>
                </QIcon>
                <QIcon
                    v-if="!customer.isTaxDataChecked"
                    name="vn:no036"
                    size="xs"
                    color="primary"
                >
                    <QTooltip>{{ t('customer.card.notChecked') }}</QTooltip>
                </QIcon>
                <QBtn
                    v-if="customer.unpaid"
                    flat
                    size="sm"
                    icon="vn:Client_unpaid"
                    color="primary"
                    :to="{ name: 'CustomerUnpaid' }"
                >
                    <QTooltip>
                        {{ t('Unpaid') }}
                        <br />
                        {{
                            t('unpaidDated', {
                                dated: toDate(customer.unpaid.dated),
                            })
                        }}
                        <br />
                        {{
                            t('unpaidAmount', {
                                amount: toCurrency(customer.unpaid.amount),
                            })
                        }}
                    </QTooltip>
                </QBtn>
            </QCardActions>
        </template>
        <template #actions="{ entity }">
            <QCardActions class="flex justify-center">
                <QBtn
                    :to="{
                        name: 'TicketList',
                        query: {
                            from: undefined,
                            to: undefined,
                            table: JSON.stringify({
                                clientFk: entity.id,
                            }),
                        },
                    }"
                    size="md"
                    icon="vn:ticket"
                    color="primary"
                >
                    <QTooltip>{{ t('Customer ticket list') }}</QTooltip>
                </QBtn>
                <QBtn
                    :to="{
                        name: 'InvoiceOutList',
                        query: { table: JSON.stringify({ clientFk: entity.id }) },
                    }"
                    size="md"
                    icon="vn:invoice-out"
                    color="primary"
                >
                    <QTooltip>{{ t('Customer invoice out list') }}</QTooltip>
                </QBtn>
                <QBtn
                    :to="{
                        name: 'AccountSummary',
                        params: { id: entity.id },
                    }"
                    size="md"
                    icon="face"
                    color="primary"
                >
                    <QTooltip>{{ t('Go to user') }}</QTooltip>
                </QBtn>
                <QBtn
                    v-if="entity.supplier"
                    :to="{
                        name: 'SupplierSummary',
                        params: { id: entity.supplier.id },
                    }"
                    size="md"
                    icon="vn:supplier"
                    color="primary"
                >
                    <QTooltip>{{ t('Go to supplier') }}</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
    Go to user: Ir al usuario
    Go to supplier: Ir al proveedor
    Customer unpaid: Cliente impago
    Unpaid: Impagado
    unpaidDated: 'Fecha {dated}'
    unpaidAmount: 'Importe {amount}'
</i18n>
