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

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

import axios from 'axios';
import useNotify from 'src/composables/useNotify.js';

const $props = defineProps({
    invoiceOutData: {
        type: Object,
        default: () => {},
    },
});

const { t } = useI18n();
const router = useRouter();
const { notify } = useNotify();

const transferInvoiceParams = reactive({
    id: $props.invoiceOutData?.id,
    refFk: $props.invoiceOutData?.ref,
});
const closeButton = ref(null);
const clientsOptions = ref([]);
const rectificativeTypeOptions = ref([]);
const siiTypeInvoiceOutsOptions = ref([]);
const invoiceCorrectionTypesOptions = ref([]);

const closeForm = () => {
    if (closeButton.value) closeButton.value.click();
};

const transferInvoice = async () => {
    try {
        const { data } = await axios.post(
            'InvoiceOuts/transferInvoice',
            transferInvoiceParams
        );
        notify(t('Transferred invoice'), 'positive');
        closeForm();
        router.push('InvoiceOutSummary', { id: data.id });
    } catch (err) {
        console.error('Error transfering invoice', err);
    }
};
</script>

<template>
    <FetchData
        url="Clients"
        @on-fetch="(data) => (clientsOptions = data)"
        :filter="{ fields: ['id', 'name'], order: 'id', limit: 30 }"
        auto-load
    />
    <FetchData
        url="CplusRectificationTypes"
        :filter="{ order: 'description' }"
        @on-fetch="(data) => (rectificativeTypeOptions = data)"
        auto-load
    />
    <FetchData
        url="SiiTypeInvoiceOuts"
        :filter="{ where: { code: { like: 'R%' } } }"
        @on-fetch="(data) => (siiTypeInvoiceOutsOptions = data)"
        auto-load
    />
    <FetchData
        url="InvoiceCorrectionTypes"
        @on-fetch="(data) => (invoiceCorrectionTypesOptions = data)"
        auto-load
    />
    <FormPopup
        @on-submit="transferInvoice()"
        :title="t('Transfer invoice')"
        :custom-submit-button-label="t('Transfer client')"
        :default-cancel-button="false"
    >
        <template #form-inputs>
            <VnRow class="row q-gutter-md q-mb-md">
                <div class="col">
                    <VnSelectFilter
                        :label="t('Client')"
                        :options="clientsOptions"
                        hide-selected
                        option-label="name"
                        option-value="id"
                        v-model="transferInvoiceParams.newClientFk"
                        :required="true"
                    >
                        <template #option="scope">
                            <QItem v-bind="scope.itemProps">
                                <QItemSection>
                                    <QItemLabel>
                                        #{{ scope.opt?.id }} -
                                        {{ scope.opt?.name }}
                                    </QItemLabel>
                                </QItemSection>
                            </QItem>
                        </template>
                    </VnSelectFilter>
                </div>
                <div class="col">
                    <VnSelectFilter
                        :label="t('Rectificative type')"
                        :options="rectificativeTypeOptions"
                        hide-selected
                        option-label="description"
                        option-value="id"
                        v-model="transferInvoiceParams.cplusRectificationTypeFk"
                        :required="true"
                    />
                </div>
            </VnRow>
            <VnRow class="row q-gutter-md q-mb-md">
                <div class="col">
                    <VnSelectFilter
                        :label="t('Class')"
                        :options="siiTypeInvoiceOutsOptions"
                        hide-selected
                        option-label="description"
                        option-value="id"
                        v-model="transferInvoiceParams.siiTypeInvoiceOutFk"
                        :required="true"
                    >
                        <template #option="scope">
                            <QItem v-bind="scope.itemProps">
                                <QItemSection>
                                    <QItemLabel>
                                        {{ scope.opt?.code }} -
                                        {{ scope.opt?.description }}
                                    </QItemLabel>
                                </QItemSection>
                            </QItem>
                        </template>
                    </VnSelectFilter>
                </div>
                <div class="col">
                    <VnSelectFilter
                        :label="t('Type')"
                        :options="invoiceCorrectionTypesOptions"
                        hide-selected
                        option-label="description"
                        option-value="id"
                        v-model="transferInvoiceParams.invoiceCorrectionTypeFk"
                        :required="true"
                    />
                </div>
            </VnRow>
        </template>
    </FormPopup>
</template>

<i18n>
es:
    Transfer invoice: Transferir factura
    Transfer client: Transferir cliente
    Client: Cliente
    Rectificative type: Tipo rectificativa
    Class: Clase
    Type: Tipo
    Transferred invoice: Factura transferida
</i18n>
