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

import VnSelect from 'src/components/common/VnSelect.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: Number,
        default: null,
    },
});

const { t } = useI18n();

const regularizeFormData = reactive({
    itemFk: Number(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>
                <QInput
                    :label="t('Type the visible quantity')"
                    v-model.number="data.quantity"
                    type="number"
                    autofocus
                    data-cy="regularizeStockInput"
                />
            </VnRow>
            <VnRow>
                <div class="col">
                    <VnSelect
                        :label="t('Warehouse')"
                        v-model.number="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>
