<script setup>
import { ref, computed, onMounted, watch } from 'vue';
import { useI18n } from 'vue-i18n';
import TicketDescriptorProxy from 'src/pages/Ticket/Card/TicketDescriptorProxy.vue';
import WorkerDescriptorProxy from 'src/pages/Worker/Card/WorkerDescriptorProxy.vue';
import { useStateStore } from 'stores/useStateStore';
import { useArrayData } from 'composables/useArrayData';
import { toCurrency } from 'filters/index';
import useNotify from 'src/composables/useNotify.js';
import axios from 'axios';
import ItemRequestDenyForm from './ItemRequestDenyForm.vue';
import { toDate } from 'src/filters';
import VnTable from 'components/VnTable/VnTable.vue';
import VnInput from 'src/components/common/VnInput.vue';
const { t } = useI18n();
const { notify } = useNotify();
const stateStore = useStateStore();
let filterParams = ref({});
const denyFormRef = ref(null);
const denyRequestId = ref(null);
const denyRequestIndex = ref(null);
const itemRequestsOptions = ref([]);
const arrayData = useArrayData('ItemRequests', {
    url: 'TicketRequests/filter',
    userParams: filterParams,
    order: ['shippedDate ASC', 'isOk ASC'],
});
const store = arrayData.store;

const userParams = {
    state: 'pending',
};

const tableRef = ref();
watch(
    () => store.data,
    (value) => (itemRequestsOptions.value = value)
);

const columns = computed(() => [
    {
        label: t('item.buyRequest.ticketId'),
        name: 'ticketFk',
        align: 'left',
        isId: true,
        chip: {
            condition: () => true,
        },
        cardVisible: true,
    },
    {
        label: t('item.buyRequest.shipped'),
        name: 'shipped',
        align: 'left',
        component: 'date',
        columnField: {
            component: null,
        },
        format: (row, dashIfEmpty) => dashIfEmpty(toDate(row.shipped)),
        columnClass: 'shrink',
    },
    {
        label: t('globals.description'),
        field: 'description',
        name: 'description',
        align: 'left',
        columnClass: 'expand',
    },
    {
        label: t('item.buyRequest.requester'),
        name: 'requesterName',
        columnClass: 'shrink',
    },
    {
        label: t('item.buyRequest.requested'),
        name: 'quantity',
        columnClass: 'shrink',
    },
    {
        label: t('item.buyRequest.price'),
        name: 'price',
        align: 'left',
        format: (row) => toCurrency(row.price),
        columnClass: 'shrink',
    },
    {
        label: t('item.buyRequest.attender'),
        name: 'attenderName',
        align: 'left',
        columnClass: 'shrink',
    },
    {
        label: t('item.buyRequest.item'),
        name: 'item',
        align: 'left',
        component: 'input',
        columnClass: 'expand',
    },
    {
        label: t('item.buyRequest.achieved'),
        name: 'achieved',
        align: 'left',
        component: 'input',
        columnClass: 'shrink',
    },
    {
        label: t('item.buyRequest.concept'),
        name: 'concept',
        align: 'left',
        sortable: true,
        component: 'input',
        columnClass: 'expand',
    },
    {
        label: t('item.buyRequest.state'),
        name: 'state',
        format: (row) => getState(row.isOk),
        align: 'left',
    },
    {
        align: 'left',
        name: 'daysOnward',
        label: t('item.buyRequest.daysOnward'),
        visible: false,
        columnFilter: {
            inWhere: false,
        },
    },
    {
        align: 'right',
        label: '',
        name: 'denyOptions',
    },
]);

const changeQuantity = async (request) => {
    try {
        if (request.saleFk) {
            const params = {
                quantity: request.saleQuantity,
            };

            await axios.patch(`Sales/${request.saleFk}`, params);
            notify(t('globals.dataSaved'), 'positive');
            confirmRequest(request);
        } else confirmRequest(request);
    } catch (error) {
        console.error('Error changing quantity:: ', error);
    }
};

const confirmRequest = async (request) => {
    try {
        if (request.itemFk && request.saleQuantity) {
            const params = {
                itemFk: request.itemFk,
                quantity: request.saleQuantity,
                attenderFk: request.attenderFk,
            };

            const { data } = await axios.post(
                `TicketRequests/${request.id}/confirm`,
                params
            );
            request.itemDescription = data.concept;
            request.isOk = true;
            notify(t('globals.dataSaved'), 'positive');
        }
    } catch (error) {
        console.error('Error confirming request:: ', error);
    }
};

const getState = (isOk) => {
    if (isOk === null) return t('Pending');
    else if (isOk) return t('Accepted');
    else return t('Denied');
};

const showDenyRequestForm = (requestId, rowIndex) => {
    denyRequestId.value = requestId;
    denyRequestIndex.value = rowIndex;
    denyFormRef.value.show();
};

const onDenyAccept = (_, responseData) => {
    itemRequestsOptions.value[denyRequestIndex.value].isOk = responseData.isOk;
    itemRequestsOptions.value[denyRequestIndex.value].attenderFk =
        responseData.attenderFk;
    itemRequestsOptions.value[denyRequestIndex.value].response = responseData.response;
    denyRequestId.value = null;
    denyRequestIndex.value = null;
    tableRef.value.reload();
};

onMounted(async () => {
    await arrayData.fetch({ append: false });
    stateStore.rightDrawer = true;
});
</script>

<template>
    <VnTable
        ref="tableRef"
        data-key="itemRequest"
        url="ticketRequests/filter"
        order="shipped ASC, isOk ASC"
        :columns="columns"
        :user-params="userParams"
        :is-editable="true"
        auto-load
        :disable-option="{ card: true }"
    >
        <template #column-attenderName="{ row }">
            <span class="link" @click.stop>
                {{ row.attenderName }}
                <WorkerDescriptorProxy :id="row.attenderFk" />
            </span>
        </template>

        <template #column-requesterName="{ row }">
            <span class="link" @click.stop>
                {{ row.requesterName }}
                <WorkerDescriptorProxy :id="row.requesterFk" />
            </span>
        </template>

        <template #column-item="{ row }">
            <span>
                <VnInput v-model.number="row.itemFk" dense />
            </span>
        </template>
        <template #column-achieved="{ row }">
            <span>
                <VnInput
                    type="number"
                    v-model.number="row.saleQuantity"
                    :disable="!row.itemFk || row.isOk != null"
                    @blur="changeQuantity(row)"
                    @keyup.enter="changeQuantity(row)"
                    dense
                />
            </span>
        </template>
        <template #column-concept="{ row }">
            <span @click.stop disabled="row.isOk != null">
                {{ row.itemDescription }}
            </span>
        </template>
        <template #moreFilterPanel="{ params }">
            <VnInputNumber
                :label="t('params.scopeDays')"
                v-model.number="params.scopeDays"
                @keyup.enter="(evt) => handleScopeDays(evt.target.value)"
                @remove="handleScopeDays()"
                class="q-px-xs q-pr-lg"
                filled
                dense
            />
        </template>
        <template #column-denyOptions="{ row, rowIndex }">
            <QTd class="sticky no-padding">
                <QIcon
                    v-if="row.response?.length"
                    name="insert_drive_file"
                    color="primary"
                    size="sm"
                >
                    <QTooltip>
                        {{ row.response }}
                    </QTooltip>
                </QIcon>
                <QIcon
                    v-if="row.isOk == null"
                    name="thumb_down"
                    color="primary"
                    size="sm"
                    class="fill-icon"
                    @click="showDenyRequestForm(row.id, rowIndex)"
                >
                    <QTooltip>
                        {{ t('Discard') }}
                    </QTooltip>
                </QIcon>
            </QTd>
        </template>
    </VnTable>
    <QDialog ref="denyFormRef" transition-show="scale" transition-hide="scale">
        <ItemRequestDenyForm :request-id="denyRequestId" @on-data-saved="onDenyAccept" />
    </QDialog>
</template>

<i18n>
es:
    Discard: Descartar
    You can search by Id or alias: Buscar peticiones por identificador o alias
    Denied: Denegada
    Accepted: Aceptada
    Pending: Pendiente
</i18n>
