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

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 { useSession } from 'src/composables/useSession';

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

const claimFilter = {
    fields: [
        'id',
        'clientFk',
        'created',
        'workerFk',
        'claimStateFk',
        'packages',
        'hasToPickUp',
    ],
    include: [
        {
            relation: 'client',
            scope: {
                fields: ['name'],
            },
        },
    ],
};

const workers = ref([]);
const workersCopy = ref([]);
const claimStates = ref([]);
const claimStatesCopy = ref([]);

function setWorkers(data) {
    workers.value = data;
    workersCopy.value = data;
}

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

const workerFilter = {
    options: workers,
    filterFn: (options, value) => {
        const search = value.toLowerCase();

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

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

            const idMatches = id == search;
            const nameMatches = name.indexOf(search) > -1;

            return idMatches || nameMatches;
        });
    },
};

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="setWorkers"
        auto-load
    />
    <FetchData url="ClaimStates" @on-fetch="setClaimStates" auto-load />
    <FormModel
        :url="`Claims/${route.params.id}`"
        :url-update="`Claims/updateClaim/${route.params.id}`"
        :filter="claimFilter"
        model="claim"
        auto-load
    >
        <template #form="{ data, validate, filter }">
            <VnRow class="row q-gutter-md q-mb-md">
                <div class="col">
                    <VnInput
                        v-model="data.client.name"
                        :label="t('claim.basicData.customer')"
                        disable
                    />
                </div>
                <div class="col">
                    <VnInputDate
                        v-model="data.created"
                        :label="t('claim.basicData.created')"
                    />
                </div>
            </VnRow>
            <VnRow class="row q-gutter-md q-mb-md">
                <div class="col">
                    <QSelect
                        v-model="data.workerFk"
                        :options="workers"
                        option-value="id"
                        option-label="name"
                        emit-value
                        :label="t('claim.basicData.assignedTo')"
                        map-options
                        use-input
                        @filter="(value, update) => filter(value, update, workerFilter)"
                        :rules="validate('claim.claimStateFk')"
                        :input-debounce="0"
                    >
                        <template #before>
                            <QAvatar color="orange">
                                <QImg
                                    v-if="data.workerFk"
                                    :src="`/api/Images/user/160x160/${data.workerFk}/download?access_token=${token}`"
                                    spinner-color="white"
                                />
                            </QAvatar>
                        </template>
                    </QSelect>
                </div>
                <div class="col">
                    <QSelect
                        v-model="data.claimStateFk"
                        :options="claimStates"
                        option-value="id"
                        option-label="description"
                        emit-value
                        :label="t('claim.basicData.state')"
                        map-options
                        use-input
                        @filter="(value, update) => filter(value, update, statesFilter)"
                        :rules="validate('claim.claimStateFk')"
                        :input-debounce="0"
                    >
                    </QSelect>
                </div>
            </VnRow>
            <VnRow class="row q-gutter-md q-mb-md">
                <div class="col">
                    <QInput
                        v-model.number="data.packages"
                        :label="t('globals.packages')"
                        :rules="validate('claim.packages')"
                        type="number"
                    />
                </div>
            </VnRow>
            <VnRow class="row q-gutter-md q-mb-md">
                <div class="col">
                    <QCheckbox
                        v-model="data.hasToPickUp"
                        :label="t('claim.basicData.picked')"
                    />
                </div>
            </VnRow>
        </template>
    </FormModel>
</template>
