<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';

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

const categoriesOptions = ref([]);
const temperaturesOptions = 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
    />
    <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
                    v-model="data.workerFk"
                    :label="t('shared.worker')"
                    url="Workers"
                    sort-by="firstName ASC"
                    :fields="['id', 'firstName']"
                    option-value="id"
                    option-label="firstName"
                    hide-selected
                />
                <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
                />
            </VnRow>
        </template>
    </FormModel>
</template>
