<script setup>
import { useRoute } from 'vue-router';
import { reactive, ref } from 'vue';
import { useI18n } from 'vue-i18n';
import axios from 'axios';
import { useState } from 'composables/useState';
import FormModel from 'components/FormModel.vue';
import FetchData from 'components/FetchData.vue';
import VnRow from 'components/ui/VnRow.vue';
import VnSelectFilter from 'components/common/VnSelectFilter.vue';
import VnInputDate from 'components/common/VnInputDate.vue';
import VnSubToolbar from 'src/components/ui/VnSubToolbar.vue';

const { t } = useI18n();
const route = useRoute();
const state = useState();
const ORDER_MODEL = 'order';

const isNew = Boolean(!route.params.id);
const initialFormState = reactive({
    clientFk: null,
    addressFk: null,
    agencyModeFk: null,
    landed: null,
});
const clientList = ref([]);
const agencyList = ref([]);
const addressList = ref([]);

const fetchAddressList = async (addressId) => {
    try {
        const { data } = await axios.get('addresses', {
            params: {
                filter: JSON.stringify({
                    fields: ['id', 'nickname', 'street', 'city'],
                    where: { id: addressId },
                }),
            },
        });
        addressList.value = data;
        //  Set address by default
        if (addressList.value?.length === 1) {
            state.get(ORDER_MODEL).addressFk = addressList.value[0].id;
        }
    } catch (err) {
        console.error(`Error fetching addresses`, err);
        return err.response;
    }
};
const fetchAgencyList = async (landed, addressFk) => {
    if (!landed || !addressFk) {
        return;
    }
    try {
        const { data } = await axios.get('Agencies/landsThatDay', {
            params: {
                addressFk,
                landed: new Date(landed).toISOString(),
            },
        });
        agencyList.value = data;
    } catch (err) {
        console.error(`Error fetching agencies`, err);
        return err.response;
    }
};

const fetchOrderDetails = (order) => {
    fetchAddressList(order?.addressFk);
    fetchAgencyList(order?.landed, order?.addressFk);
};

const orderMapper = (order) => {
    return {
        addressId: order.addressFk,
        agencyModeId: order.agencyModeFk,
        landed: new Date(order.landed).toISOString(),
    };
};
const orderFilter = {
    include: [
        { relation: 'agencyMode', scope: { fields: ['name'] } },
        {
            relation: 'address',
            scope: { fields: ['nickname'] },
        },
        { relation: 'rows', scope: { fields: ['id'] } },
        {
            relation: 'client',
            scope: {
                fields: [
                    'salesPersonFk',
                    'name',
                    'isActive',
                    'isFreezed',
                    'isTaxDataChecked',
                ],
                include: {
                    relation: 'salesPersonUser',
                    scope: { fields: ['id', 'name'] },
                },
            },
        },
    ],
};
</script>

<template>
    <FetchData
        url="Clients"
        @on-fetch="(data) => (clientList = data)"
        :filter="{ fields: ['id', 'name', 'defaultAddressFk'] }"
        auto-load
    />
    <VnSubToolbar v-if="isNew" />
    <div class="q-pa-md">
        <FormModel
            :url="!isNew ? `Orders/${route.params.id}` : null"
            :url-create="isNew ? 'Orders/new' : null"
            :model="ORDER_MODEL"
            :form-initial-data="isNew ? initialFormState : null"
            :observe-form-changes="!isNew"
            :mapper="isNew ? orderMapper : null"
            :filter="orderFilter"
            @on-fetch="fetchOrderDetails"
        >
            <template #form="{ data }">
                <VnRow class="row q-gutter-md q-mb-md">
                    <div class="col">
                        <VnSelectFilter
                            :label="t('order.form.clientFk')"
                            v-model="data.clientFk"
                            :options="clientList"
                            option-value="id"
                            option-label="name"
                            hide-selected
                            @update:model-value="
                                (client) => fetchAddressList(client.defaultAddressFk)
                            "
                        >
                            <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('order.form.addressFk')"
                            v-model="data.addressFk"
                            :options="addressList"
                            option-value="id"
                            option-label="nickname"
                            hide-selected
                            :disable="!addressList?.length"
                            @update:model-value="
                                () => fetchAgencyList(data.landed, data.addressFk)
                            "
                        >
                            <template #option="scope">
                                <QItem v-bind="scope.itemProps">
                                    <QItemSection>
                                        <QItemLabel>
                                            {{
                                                `${scope.opt.nickname}: ${scope.opt.street},${scope.opt.city}`
                                            }}
                                        </QItemLabel>
                                    </QItemSection>
                                </QItem>
                            </template>
                        </VnSelectFilter>
                    </div>
                </VnRow>
                <VnRow class="row q-gutter-md q-mb-md">
                    <div class="col">
                        <VnInputDate
                            placeholder="dd-mm-aaa"
                            :label="t('order.form.landed')"
                            v-model="data.landed"
                            @update:model-value="
                                () => fetchAgencyList(data.landed, data.addressFk)
                            "
                        />
                    </div>
                </VnRow>
                <VnRow class="row q-gutter-md q-mb-md">
                    <div class="col">
                        <VnSelectFilter
                            :label="t('order.form.agencyModeFk')"
                            v-model="data.agencyModeFk"
                            :options="agencyList"
                            option-value="agencyModeFk"
                            option-label="agencyMode"
                            hide-selected
                            :disable="!agencyList?.length"
                        >
                        </VnSelectFilter>
                    </div>
                </VnRow>
            </template>
        </FormModel>
    </div>
</template>
