<script setup>
import { ref } from 'vue';
import { useRoute } from 'vue-router';
import { useI18n } from 'vue-i18n';
import VnSelect from 'src/components/common/VnSelect.vue';
import FetchData from 'components/FetchData.vue';
import FormModel from 'components/FormModel.vue';
import VnRow from 'components/ui/VnRow.vue';
import VnInput from 'src/components/common/VnInput.vue';
import VnInputDate from 'components/common/VnInputDate.vue';

import axios from 'axios';
// import { useSession } from 'src/composables/useSession';
import VnImg from 'src/components/ui/VnImg.vue';

const route = useRoute();
const { t } = useI18n();
// const { getTokenMultimedia } = useSession();
// const token = getTokenMultimedia();

const claimStates = ref([]);
const claimStatesCopy = ref([]);
const optionsList = ref([]);

const workersOptions = ref([]);

function setClaimStates(data) {
    claimStates.value = data;
    claimStatesCopy.value = data;
}

async function getEnumValues() {
    optionsList.value = [{ id: null, description: t('claim.null') }];
    const { data } = await axios.get(`Applications/get-enum-values`, {
        params: {
            schema: 'vn',
            table: 'claim',
            column: 'pickup',
        },
    });
    for (let value of data)
        optionsList.value.push({ id: value, description: t(`claim.${value}`) });
}

getEnumValues();

const statesFilter = {
    options: claimStates,
    filterFn: (options, value) => {
        const search = value.toLowerCase();

        if (value === '') return claimStatesCopy.value;

        return options.value.filter((row) => {
            const description = row.description.toLowerCase();

            return description.indexOf(search) > -1;
        });
    },
};
</script>
<template>
    <FetchData
        url="Workers/activeWithInheritedRole"
        :filter="{ where: { role: 'salesPerson' } }"
        @on-fetch="(data) => (workersOptions = data)"
        auto-load
    />
    <FetchData url="ClaimStates" @on-fetch="setClaimStates" auto-load />
    <FormModel
        model="Claim"
        :url-update="`Claims/updateClaim/${route.params.id}`"
        auto-load
        :reload="true"
    >
        <template #form="{ data, validate, filter }">
            <VnRow class="row q-gutter-md q-mb-md">
                <VnInput
                    v-model="data.client.name"
                    :label="t('claim.customer')"
                    disable
                />
                <VnInputDate v-model="data.created" :label="t('claim.created')" />
            </VnRow>
            <VnRow class="row q-gutter-md q-mb-md">
                <VnSelect
                    :label="t('claim.assignedTo')"
                    v-model="data.workerFk"
                    :options="workersOptions"
                    option-value="id"
                    option-label="name"
                    emit-value
                    auto-load
                    :rules="validate('claim.claimStateFk')"
                >
                    <template #before>
                        <QAvatar color="orange">
                            <VnImg
                                v-if="data.workerFk"
                                :size="'160x160'"
                                :id="data.workerFk"
                                collection="user"
                                spinner-color="white"
                            />
                        </QAvatar>
                    </template>
                </VnSelect>
                <QSelect
                    v-model="data.claimStateFk"
                    :options="claimStates"
                    option-value="id"
                    option-label="description"
                    emit-value
                    :label="t('claim.state')"
                    map-options
                    use-input
                    @filter="(value, update) => filter(value, update, statesFilter)"
                    :rules="validate('claim.claimStateFk')"
                    :input-debounce="0"
                >
                </QSelect>
            </VnRow>
            <VnRow class="row q-gutter-md q-mb-md">
                <QInput
                    v-model.number="data.packages"
                    :label="t('globals.packages')"
                    :rules="validate('claim.packages')"
                    type="number"
                />
                <QSelect
                    v-model="data.pickup"
                    :options="optionsList"
                    option-value="id"
                    option-label="description"
                    emit-value
                    :label="t('claim.pickup')"
                    map-options
                    use-input
                    :input-debounce="0"
                />
            </VnRow>
        </template>
    </FormModel>
</template>
