<script setup>
import { ref, computed, onMounted } from 'vue';
import { useRoute } from 'vue-router';
import { useI18n } from 'vue-i18n';
import { toDateHourMinSec, toPercentage } from 'src/filters';
import TicketDescriptorProxy from 'pages/Ticket/Card/TicketDescriptorProxy.vue';
import ClaimDescriptorMenu from 'pages/Claim/Card/ClaimDescriptorMenu.vue';
import DepartmentDescriptorProxy from 'src/pages/Worker/Department/Card/DepartmentDescriptorProxy.vue';
import EntityDescriptor from 'components/ui/EntityDescriptor.vue';
import VnLv from 'src/components/ui/VnLv.vue';
import VnUserLink from 'src/components/ui/VnUserLink.vue';
import { getUrl } from 'src/composables/getUrl';
import ZoneDescriptorProxy from 'src/pages/Zone/Card/ZoneDescriptorProxy.vue';
import filter from './ClaimFilter.js';

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

const route = useRoute();
const { t } = useI18n();
const salixUrl = ref();
const entityId = computed(() => {
    return $props.id || route.params.id;
});

const STATE_COLOR = {
    pending: 'warning',
    incomplete: 'info',
    resolved: 'positive',
    canceled: 'negative',
};
function stateColor(code) {
    return STATE_COLOR[code];
}

onMounted(async () => {
    salixUrl.value = await getUrl('');
});
</script>

<template>
    <EntityDescriptor
        :url="`Claims/${entityId}`"
        :filter="filter"
        title="client.name"
        data-key="Claim"
    >
        <template #menu="{ entity }">
            <ClaimDescriptorMenu :claim="entity" />
        </template>
        <template #body="{ entity }">
            <VnLv v-if="entity.claimState" :label="t('claim.state')">
                <template #value>
                    <QBadge
                        :color="stateColor(entity.claimState.code)"
                        text-color="black"
                        dense
                    >
                        {{ entity.claimState.description }}
                    </QBadge>
                </template>
            </VnLv>
            <VnLv :label="t('claim.created')" :value="toDateHourMinSec(entity.created)" />
            <VnLv :label="t('globals.department')">
                <template #value>
                    <span class="link">
                        {{ entity?.client?.department?.name || '-' }}
                        <DepartmentDescriptorProxy :id="entity?.client?.departmentFk" />
                    </span>
                </template>
            </VnLv>
            <VnLv
                v-if="entity.worker"
                :label="t('claim.attendedBy')"
                :value="entity.worker.user.name"
            >
                <template #value>
                    <VnUserLink
                        :name="entity.worker.user.name"
                        :worker-id="entity.worker.id"
                    />
                </template>
            </VnLv>
            <VnLv v-if="entity.ticket?.zone?.id" :label="t('claim.zone')">
                <template #value>
                    <span class="link">
                        {{ entity.ticket?.zone?.name }}
                        <ZoneDescriptorProxy :id="entity.ticket?.zone?.id" />
                    </span>
                </template>
            </VnLv>
            <VnLv
                :label="t('claim.province')"
                :value="entity.ticket?.address?.province?.name"
            />
            <VnLv v-if="entity.ticketFk" :label="t('claim.ticketId')">
                <template #value>
                    <span class="link">
                        {{ entity.ticketFk }}
                        <TicketDescriptorProxy :id="entity.ticketFk" />
                    </span>
                </template>
            </VnLv>
            <VnLv
                :label="t('claimRate')"
                :value="toPercentage(entity.client?.claimsRatio?.claimingRate)"
            />
        </template>
        <template #actions="{ entity }">
            <QCardActions>
                <QBtn
                    size="md"
                    icon="vn:client"
                    color="primary"
                    :to="{ name: 'CustomerCard', params: { id: entity.clientFk } }"
                >
                    <QTooltip>{{ t('claim.customerSummary') }}</QTooltip>
                </QBtn>
                <QBtn
                    size="md"
                    icon="vn:ticket"
                    color="primary"
                    :to="{ name: 'TicketCard', params: { id: entity.ticketFk } }"
                >
                    <QTooltip>{{ t('claim.claimedTicket') }}</QTooltip>
                </QBtn>
                <QBtn
                    size="md"
                    icon="assignment"
                    color="primary"
                    :href="salixUrl + 'ticket/' + entity.ticketFk + '/sale-tracking'"
                >
                    <QTooltip>{{ t('claim.saleTracking') }}</QTooltip>
                </QBtn>
                <QBtn
                    size="md"
                    icon="visibility"
                    color="primary"
                    :href="salixUrl + 'ticket/' + entity.ticketFk + '/tracking/index'"
                >
                    <QTooltip>{{ t('claim.ticketTracking') }}</QTooltip>
                </QBtn>
            </QCardActions>
        </template>
    </EntityDescriptor>
</template>
<style scoped>
.q-item__label {
    margin-top: 0;
}
</style>
<i18n>
    en:
        claimRate: Claming rate
    es:
        claimRate: Ratio de reclamación
</i18n>
