<script setup>
import { computed, ref } from 'vue';
import { useRoute } from 'vue-router';
import { useI18n } from 'vue-i18n';
import { useSession } from 'src/composables/useSession';
import CardDescriptor from 'src/components/ui/CardDescriptor.vue';
import VnLv from 'src/components/ui/VnLv.vue';
import VnLinkPhone from 'src/components/ui/VnLinkPhone.vue';
import useCardDescription from 'src/composables/useCardDescription';

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

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

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

const worker = ref();
const filter = {
    include: [
        {
            relation: 'user',
            scope: {
                fields: ['email', 'name', 'nickname'],
            },
        },
        {
            relation: 'department',
            scope: {
                include: [
                    {
                        relation: 'department',
                    },
                ],
            },
        },
        {
            relation: 'sip',
        },
    ],
};

const sip = computed(() => worker.value?.sip && worker.value.sip.extension);

function getWorkerAvatar() {
    const token = getTokenMultimedia();
    return `/api/Images/user/160x160/${entityId.value}/download?access_token=${token}`;
}
const data = ref(useCardDescription());
const setData = (entity) => {
    if (!entity) return;
    data.value = useCardDescription(entity.user.nickname, entity.id);
};
</script>
<template>
    <CardDescriptor
        module="Worker"
        data-key="workerData"
        :url="`Workers/${entityId}`"
        :filter="filter"
        :title="data.title"
        :subtitle="data.subtitle"
        :summary="$props.summary"
        @on-fetch="
            (data) => {
                worker = data;
                setData(data);
            }
        "
    >
        <template #before>
            <QImg :src="getWorkerAvatar()" class="photo">
                <template #error>
                    <div
                        class="absolute-full picture text-center q-pa-md flex flex-center"
                    >
                        <div>
                            <div class="text-grey-5" style="opacity: 0.4; font-size: 5vh">
                                <QIcon name="vn:claims" />
                            </div>
                            <div class="text-grey-5" style="opacity: 0.4">
                                {{ t('worker.imageNotFound') }}
                            </div>
                        </div>
                    </div>
                </template>
            </QImg>
        </template>
        <template #body="{ entity }">
            <VnLv :label="t('worker.card.name')" :value="entity.user?.nickname" />
            <VnLv :label="t('worker.card.email')" :value="entity.user?.email" copy />
            <VnLv
                :label="t('worker.list.department')"
                :value="entity.department ? entity.department.department.name : null"
            />
            <VnLv :value="entity.phone">
                <template #label>
                    {{ t('worker.card.phone') }}
                    <VnLinkPhone :phone-number="entity.phone" />
                </template>
            </VnLv>
            <VnLv :value="sip">
                <template #label>
                    {{ t('worker.summary.sipExtension') }}
                    <VnLinkPhone v-if="sip" :phone-number="sip" />
                </template>
            </VnLv>
        </template>
    </CardDescriptor>
</template>

<style lang="scss" scoped>
.photo {
    height: 256px;
}
</style>
