<script setup>
import { ref } from 'vue';
import { useRoute } from 'vue-router';
import { useI18n } from 'vue-i18n';

import FetchData from 'components/FetchData.vue';
import FormModel from 'components/FormModel.vue';
import VnRow from 'components/ui/VnRow.vue';
import VnInput from 'src/components/common/VnInput.vue';
import VnSelect from 'src/components/common/VnSelect.vue';
import VnAvatar from 'src/components/ui/VnAvatar.vue';

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

const categoriesOptions = ref([]);
const temperaturesOptions = ref([]);
const itemPackingTypesOptions = ref([]);
</script>
<template>
    <FetchData
        url="ItemCategories"
        @on-fetch="(data) => (categoriesOptions = data)"
        :filter="{ order: 'name ASC', fields: ['id', 'name'] }"
        auto-load
    />
    <FetchData
        url="Temperatures"
        @on-fetch="(data) => (temperaturesOptions = data)"
        :filter="{ order: 'name ASC', fields: ['code', 'name'] }"
        auto-load
    />
    <FetchData
        url="ItemPackingTypes"
        @on-fetch="(data) => (itemPackingTypesOptions = data)"
        :filter="{
            where: { isActive: true },
            order: 'description ASC',
            fields: ['code', 'description'],
        }"
        auto-load
    />
    <FormModel
        :url="`ItemTypes/${route.params.id}`"
        :url-update="`ItemTypes/${route.params.id}`"
        model="itemTypeBasicData"
        auto-load
    >
        <template #form="{ data }">
            <VnRow>
                <VnInput v-model="data.code" :label="t('shared.code')" />
                <VnInput v-model="data.name" :label="t('shared.name')" />
            </VnRow>
            <VnRow>
                <VnSelect
                    url="Workers/search"
                    v-model="data.workerFk"
                    :label="t('shared.worker')"
                    sort-by="nickname ASC"
                    :fields="['id', 'nickname']"
                    option-label="nickname"
                    option-value="id"
                    hide-selected
                >
                    <template #prepend>
                        <VnAvatar
                            :worker-id="data.workerFk"
                            color="primary"
                            :title="title"
                        />
                    </template>
                    <template #option="scope">
                        <QItem v-bind="scope.itemProps">
                            <QItemSection>
                                <QItemLabel>{{ scope.opt?.name }}</QItemLabel>
                                <QItemLabel caption
                                    >{{ scope.opt?.nickname }},
                                    {{ scope.opt?.code }}</QItemLabel
                                >
                            </QItemSection>
                        </QItem>
                    </template></VnSelect
                >
                <VnSelect
                    v-model="data.categoryFk"
                    :label="t('shared.category')"
                    :options="categoriesOptions"
                    option-value="id"
                    option-label="name"
                    hide-selected
                />
            </VnRow>
            <VnRow>
                <VnSelect
                    v-model="data.temperatureFk"
                    :label="t('shared.temperature')"
                    :options="temperaturesOptions"
                    option-value="code"
                    option-label="name"
                    hide-selected
                />
                <VnInput v-model="data.life" :label="t('shared.life')" />
            </VnRow>
            <VnRow>
                <VnSelect
                    v-model="data.itemPackingTypeFk"
                    :label="t('shared.itemPackingType')"
                    :options="itemPackingTypesOptions"
                    option-value="code"
                    option-label="description"
                    hide-selected
                />
                <VnInput v-model="data.maxRefs" :label="t('shared.maxRefs')" />
            </VnRow>
            <VnRow>
                <QCheckbox v-model="data.isFragile" :label="t('shared.fragile')" />
            </VnRow>
        </template>
    </FormModel>
</template>
