<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 VnSelectFilter from 'src/components/common/VnSelectFilter.vue';

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

const paymethodsOptions = ref([]);
const payDemsOptions = ref([]);

const formatPayDems = (data) => {
    payDemsOptions.value = data.map(({ id, payDem }) => ({
        id: id,
        payDem: payDem || '0',
    }));
};
</script>
<template>
    <FetchData
        url="Paymethods"
        @on-fetch="(data) => (paymethodsOptions = data)"
        auto-load
    />
    <FetchData url="PayDems" @on-fetch="(data) => formatPayDems(data)" auto-load />
    <FormModel
        :url="`Suppliers/${route.params.id}`"
        :url-update="`Suppliers/${route.params.id}`"
        model="supplier"
        auto-load
        :clear-store-on-unmount="false"
    >
        <template #form="{ data, validate }">
            <VnRow class="row q-gutter-md q-mb-md">
                <div class="col">
                    <VnSelectFilter
                        :label="t('supplier.billingData.payMethodFk')"
                        v-model="data.payMethodFk"
                        :options="paymethodsOptions"
                        option-value="id"
                        option-label="name"
                        hide-selected
                        :rules="validate('supplier.payMethodFk')"
                    />
                </div>
                <div class="col">
                    <VnSelectFilter
                        :label="t('supplier.billingData.payDemFk')"
                        v-model="data.payDemFk"
                        :options="payDemsOptions"
                        option-value="id"
                        option-label="payDem"
                        hide-selected
                        :rules="validate('supplier.payDemFk')"
                    />
                </div>
            </VnRow>
            <VnRow class="row q-gutter-md q-mb-md">
                <div class="col">
                    <QInput
                        :label="t('supplier.billingData.payDay')"
                        type="number"
                        v-model="data.payDay"
                    />
                </div>
            </VnRow>
        </template>
    </FormModel>
</template>
