<script setup>
import { reactive, ref } from 'vue';
import { useI18n } from 'vue-i18n';

import VnSelectFilter from 'src/components/common/VnSelectFilter.vue';
import FetchData from 'components/FetchData.vue';
import VnRow from 'components/ui/VnRow.vue';
import FormModelPopup from './FormModelPopup.vue';

const emit = defineEmits(['onDataSaved']);

const props = defineProps({
    itemFk: {
        type: Number,
        default: null,
    },
    warehouseFk: {
        type: Boolean,
        default: null,
    },
});

const { t } = useI18n();

const regularizeFormData = reactive({
    itemFk: props.itemFk,
    warehouseFk: props.warehouseFk,
    quantity: null,
});

const warehousesOptions = ref([]);

const onDataSaved = (data) => {
    emit('onDataSaved', data);
};
</script>

<template>
    <FetchData
        url="Warehouses"
        @on-fetch="(data) => (warehousesOptions = data)"
        auto-load
    />
    <FormModelPopup
        url-create="Items/regularize"
        model="Items"
        :title="t('Regularize stock')"
        :form-initial-data="regularizeFormData"
        @on-data-saved="onDataSaved($event)"
    >
        <template #form-inputs="{ data }">
            <VnRow class="row q-gutter-md q-mb-md">
                <div class="col">
                    <QInput
                        :label="t('Type the visible quantity')"
                        v-model.number="data.quantity"
                    />
                </div>
            </VnRow>
            <VnRow class="row q-gutter-md q-mb-md">
                <div class="col">
                    <VnSelectFilter
                        :label="t('Warehouse')"
                        v-model="data.warehouseFk"
                        :options="warehousesOptions"
                        option-value="id"
                        option-label="name"
                        hide-selected
                    />
                </div>
            </VnRow>
        </template>
    </FormModelPopup>
</template>

<i18n>
es:
    Warehouse: Almacén
    Type the visible quantity: Introduce la cantidad visible
    Regularize stock: Regularizar stock
</i18n>
