<script setup>
import { computed, ref } from 'vue';
import { useRoute } from 'vue-router';
import { useI18n } from 'vue-i18n';
import VnLv from 'src/components/ui/VnLv.vue';
import VnLinkPhone from 'src/components/ui/VnLinkPhone.vue';
import VnChangePassword from 'src/components/common/VnChangePassword.vue';
import axios from 'axios';
import VnImg from 'src/components/ui/VnImg.vue';
import EditPictureForm from 'components/EditPictureForm.vue';
import WorkerDescriptorMenu from './WorkerDescriptorMenu.vue';
import DepartmentDescriptorProxy from 'src/pages/Worker/Department/Card/DepartmentDescriptorProxy.vue';
import CardDescriptor from 'src/components/ui/CardDescriptor.vue';
import WorkerCard from './WorkerCard.vue';

const $props = defineProps({
    id: {
        type: Number,
        required: false,
        default: null,
    },
    dataKey: {
        type: String,
        required: false,
        default: 'Worker',
    },
    summary: {
        type: Object,
        default: null,
    },
});
const image = ref(null);

const route = useRoute();
const { t } = useI18n();
const showEditPhotoForm = ref(false);
const toggleEditPictureForm = () => {
    showEditPhotoForm.value = !showEditPhotoForm.value;
};
const entityId = computed(() => {
    return $props.id || route.params.id;
});
const workerExcluded = ref(false);

const getIsExcluded = async () => {
    const { data } = await axios.get(`WorkerDisableExcludeds/${entityId.value}/exists`);
    if (!data) return;
    workerExcluded.value = data.exists;
};

const handlePhotoUpdated = (evt = false) => {
    image.value.reload(evt);
};
</script>
<template>
    <CardDescriptor
        v-bind="$attrs"
        ref="cardDescriptorRef"
        :data-key="dataKey"
        :summary="$props.summary"
        :card="WorkerCard"
        :id="entityId"
        :filter="{ where: { id: entityId } }"
        title="user.nickname"
        @on-fetch="getIsExcluded"
        module="Worker"
    >
        <template #menu="{ entity }">
            <WorkerDescriptorMenu
                :worker="entity"
                :is-excluded="workerExcluded"
                @show-dialog="$refs.changePassRef.show"
            />
        </template>
        <template #before>
            <div class="relative-position">
                <VnImg
                    ref="image"
                    :id="parseInt(entityId)"
                    collection="user"
                    resolution="520x520"
                    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> </VnImg
                ><QBtn
                    color="primary"
                    size="lg"
                    round
                    class="edit-photo-btn"
                    @click="toggleEditPictureForm()"
                >
                    <QIcon name="edit" size="sm" />
                    <QDialog ref="editPhotoFormDialog" v-model="showEditPhotoForm">
                        <EditPictureForm
                            collection="user"
                            :id="entityId"
                            @close-form="toggleEditPictureForm()"
                            @on-photo-uploaded="handlePhotoUpdated"
                        />
                    </QDialog>
                </QBtn>
            </div>
        </template>
        <template #body="{ entity }">
            <VnLv :label="t('globals.user')" :value="entity.user?.name" />
            <VnLv
                class="ellipsis"
                :label="t('globals.params.email')"
                :value="entity.user?.emailUser?.email"
                copy
            />
            <VnLv :label="t('globals.department')">
                <template #value>
                    <span class="link" v-text="entity.department?.department?.name" />
                    <DepartmentDescriptorProxy :id="entity.department?.department?.id" />
                </template>
            </VnLv>

            <VnLv :label="t('globals.phone')">
                <template #value>
                    <VnLinkPhone :phone-number="entity.phone" />
                </template>
            </VnLv>
            <VnLv :label="t('worker.summary.sipExtension')">
                <template #value>
                    <VnLinkPhone :phone-number="entity?.sip?.extension" />
                </template>
            </VnLv>
        </template>
        <template #actions="{ entity }">
            <QCardActions class="flex justify-center">
                <QBtn
                    :to="{
                        name: 'CustomerCard',
                        params: { id: entity.id },
                    }"
                    size="md"
                    icon="vn:Person"
                    color="primary"
                >
                    <QTooltip>{{ t('Go to client') }}</QTooltip>
                </QBtn>
                <QBtn
                    :to="{
                        name: 'AccountCard',
                        params: { id: entity.user?.id },
                    }"
                    size="md"
                    icon="face"
                    color="primary"
                >
                    <QTooltip>{{ t('Go to user') }}</QTooltip>
                </QBtn>
            </QCardActions>
        </template>
    </CardDescriptor>
    <VnChangePassword
        ref="changePassRef"
        :submit-fn="
            async (newPass) => {
                await axios.patch(`Workers/${$route.params.id}/setPassword`, { newPass });
            }
        "
        @on-submit="$refs.cardDescriptorRef?.getData"
    />
</template>

<style lang="scss" scoped>
.photo {
    height: 256px;
}
.ellipsis-text {
    width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
</style>
