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

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

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

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

const filter = {
    include: [
        {
            relation: 'worker',
            scope: {
                fields: ['id'],
                include: {
                    relation: 'user',
                    scope: { fields: ['nickname'] },
                },
            },
        },
        { relation: 'parking' },
    ],
};
const data = ref(useCardDescription());
const setData = (entity) => (data.value = useCardDescription(entity.code, entity.id));
</script>

<template>
    <CardDescriptor
        module="Shelving"
        :url="`Shelvings/${entityId}`"
        :filter="filter"
        :title="data.title"
        :subtitle="data.subtitle"
        data-key="Shelvings"
        @on-fetch="setData"
    >
        <template #body="{ entity }">
            <VnLv :label="t('shelving.summary.code')" :value="entity.code" />
            <VnLv :label="t('shelving.summary.parking')" :value="entity.parking?.code" />
            <VnLv v-if="entity.worker" :label="t('shelving.summary.worker')">
                <template #value>
                    <VnUserLink
                        :name="entity.worker?.user?.nickname"
                        :worker-id="entity.worker?.id"
                    />
                </template>
            </VnLv>
        </template>
        <template #menu="{ entity }">
            <ShelvingDescriptorMenu :shelving="entity" />
        </template>
    </CardDescriptor>
</template>
