<script setup>
import { ref, onBeforeMount, computed } from 'vue';
import { useRoute } from 'vue-router';
import { useI18n } from 'vue-i18n';
import axios from 'axios';
import { dashIfEmpty, toDate } from 'src/filters';
import VnLv from 'src/components/ui/VnLv.vue';
import VnLinkPhone from 'src/components/ui/VnLinkPhone.vue';
import CardSummary from 'components/ui/CardSummary.vue';
import VnUserLink from 'src/components/ui/VnUserLink.vue';
import VnTitle from 'src/components/common/VnTitle.vue';
import RoleDescriptorProxy from 'src/pages/Account/Role/Card/RoleDescriptorProxy.vue';
import DepartmentDescriptorProxy from 'src/pages/Department/Card/DepartmentDescriptorProxy.vue';
import { useRole } from 'src/composables/useRole';

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

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

const entityId = computed(() => $props.id || route.params.id);
const basicDataUrl = ref(null);
const isHr = computed(() => useRole().hasAny(['hr']));
const advancedSummary = ref();

onBeforeMount(async () => {
    if (isHr.value) {
        advancedSummary.value = (
            await axios.get('Workers/advancedSummary', {
                params: { filter: { where: { id: entityId.value } } },
            })
        ).data[0];
        basicDataUrl.value = `#/worker/${entityId.value}/basic-data`;
    }
});
</script>

<template>
    <CardSummary
        ref="summary"
        :url="`Workers/summary`"
        :filter="{ where: { id: entityId } }"
        data-key="WorkerSummary"
    >
        <template #header="{ entity }">
            <div>{{ entity.id }} - {{ entity.firstName }} {{ entity.lastName }}</div>
        </template>
        <template #body="{ entity: worker }">
            <QCard class="vn-one">
                <VnTitle :url="basicDataUrl" :text="t('worker.summary.basicData')" />
                <VnLv :label="t('worker.card.name')" :value="worker.user?.nickname" />
                <VnLv :label="t('worker.list.department')">
                    <template #value>
                        <span class="link" v-text="worker.department?.department?.name" />
                        <DepartmentDescriptorProxy
                            :id="worker.department?.department?.id"
                        />
                    </template>
                </VnLv>
                <VnLv :label="t('worker.summary.boss')" link>
                    <template #value>
                        <VnUserLink
                            v-if="worker.boss"
                            :name="dashIfEmpty(worker.boss?.name)"
                            :worker-id="worker.bossFk"
                        />
                    </template>
                </VnLv>
                <VnLv :value="worker.mobileExtension">
                    <template #label>
                        {{ t('worker.summary.phoneExtension') }}
                        <VnLinkPhone :phone-number="worker.mobileExtension" />
                    </template>
                </VnLv>
                <VnLv :value="worker.phone">
                    <template #label>
                        {{ t('worker.summary.entPhone') }}
                        <VnLinkPhone :phone-number="worker.phone" />
                    </template>
                </VnLv>
                <VnLv :value="worker.client?.phone">
                    <template #label>
                        {{ t('worker.summary.personalPhone') }}
                        <VnLinkPhone :phone-number="worker.client?.phone" />
                    </template>
                </VnLv>
            </QCard>
            <QCard class="vn-one" v-if="advancedSummary">
                <VnTitle :url="basicDataUrl" :text="t('worker.summary.basicData')" />
                <VnLv
                    :label="t('worker.summary.fiDueDate')"
                    :value="toDate(worker.fiDueDate)"
                />
                <VnLv :label="t('worker.summary.sex')" :value="worker.sex" />
                <VnLv
                    :label="t('worker.summary.seniority')"
                    :value="toDate(worker.seniority)"
                />
                <VnLv :label="t('worker.summary.fi')" :value="worker.fi" />
                <VnLv :label="t('worker.summary.birth')" :value="toDate(worker.birth)" />
                <VnLv
                    :label="t('worker.summary.isFreelance')"
                    :value="worker.isFreelance"
                />
                <VnLv
                    :label="t('worker.summary.isSsDiscounted')"
                    :value="worker.isSsDiscounted"
                />
                <VnLv
                    :label="t('worker.summary.hasMachineryAuthorized')"
                    :value="worker.hasMachineryAuthorized"
                />
                <VnLv :label="t('worker.summary.isDisable')" :value="worker.isDisable" />
            </QCard>
            <QCard class="vn-one">
                <VnTitle :text="t('worker.summary.userData')" />
                <VnLv :label="t('worker.card.name')" :value="worker?.user?.nickname" />
                <VnLv
                    :label="t('worker.list.email')"
                    :value="worker.user?.emailUser?.email"
                    copy
                />
                <VnLv :label="t('worker.summary.role')">
                    <template #value>
                        <span class="link">
                            {{ worker?.user?.role?.name }}
                            <RoleDescriptorProxy :id="worker?.user?.role?.id" />
                        </span>
                    </template>
                </VnLv>
                <VnLv :value="worker?.sip?.extension">
                    <template #label>
                        {{ t('worker.summary.sipExtension') }}
                        <VnLinkPhone :phone-number="worker?.sip?.extension" />
                    </template>
                </VnLv>
                <VnLv :label="t('queue')" :value="worker.sip?.queueMember?.queue" />
            </QCard>
        </template>
    </CardSummary>
</template>
