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

import VnSelectFilter from 'src/components/common/VnSelectFilter.vue';
import VnInput from 'src/components/common/VnInput.vue';

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

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

const $props = defineProps({
    rows: {
        type: Array,
        default: () => [],
    },
});

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

const fieldsOptions = [
    { field: 'packing', label: t('entry.latestBuys.packing') },
    { field: 'grouping', label: t('entry.latestBuys.grouping') },
    { field: 'packageValue', label: t('entry.latestBuys.packageValue') },
    { field: 'weight', label: t('entry.latestBuys.weight') },
    { field: 'description', label: t('entry.latestBuys.description') },
    { field: 'size', label: t('entry.latestBuys.size') },
    { field: 'weightByPiece', label: t('entry.latestBuys.weightByPiece') },
    { field: 'packingOut', label: t('entry.latestBuys.packingOut') },
    { field: 'landing', label: t('entry.latestBuys.landing') },
];

const formData = reactive({
    field: null,
    newValue: null,
});

const closeButton = ref(null);
const isLoading = ref(false);

const onDataSaved = () => {
    notify('globals.dataSaved', 'positive');
    emit('onDataSaved');
    closeForm();
};

const submitData = async () => {
    try {
        isLoading.value = true;
        const rowsToEdit = $props.rows.map((row) => ({ id: row.id, itemFk: row.itemFk }));
        const payload = {
            field: formData.field,
            newValue: formData.newValue,
            lines: rowsToEdit,
        };

        await axios.post('Buys/editLatestBuys', payload);
        onDataSaved();
        isLoading.value = false;
    } catch (err) {
        console.error('Error submitting table cell edit');
    }
};

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

<template>
    <QForm @submit="submitData()" class="all-pointer-events">
        <QCard class="q-pa-lg">
            <span ref="closeButton" class="close-icon" v-close-popup>
                <QIcon name="close" size="sm" />
            </span>
            <h1 class="title">
                {{
                    t('editBuyTitle', {
                        buysAmount: rows.length,
                    })
                }}
            </h1>
            <VnRow class="row q-gutter-md q-mb-md">
                <div class="col">
                    <VnSelectFilter
                        :label="t('Field to edit')"
                        :options="fieldsOptions"
                        hide-selected
                        option-label="label"
                        option-value="field"
                        v-model="formData.field"
                    />
                </div>
                <div class="col">
                    <VnInput :label="t('Value')" v-model="formData.newValue" />
                </div>
            </VnRow>
            <div class="q-mt-lg row justify-end">
                <QBtn
                    :label="t('globals.save')"
                    type="submit"
                    color="primary"
                    :disabled="isLoading"
                    :loading="isLoading"
                />
                <QBtn
                    :label="t('globals.cancel')"
                    type="reset"
                    color="primary"
                    flat
                    class="q-ml-sm"
                    :disabled="isLoading"
                    :loading="isLoading"
                    v-close-popup
                />
            </div>
        </QCard>
    </QForm>
</template>

<style lang="scss" scoped>
.title {
    font-size: 17px;
    font-weight: bold;
    line-height: 20px;
}

.close-icon {
    position: absolute;
    top: 20px;
    right: 20px;
    cursor: pointer;
}
</style>

<i18n>
    en:
        editBuyTitle: Edit {buysAmount} buy(s)
    es:
        editBuyTitle: Editar {buysAmount} compra(s)
        Field to edit: Campo a editar
        Value: Valor
    </i18n>
