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

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

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

const workers = ref([]);
const workersCopy = ref([]);
const businessTypes = ref([]);
const contactChannels = ref([]);

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

const filterOptions = {
    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;
        });
    },
};
</script>
<template>
    <fetch-data
        url="Workers/activeWithInheritedRole"
        :filter="{ where: { role: 'salesPerson' } }"
        @on-fetch="setWorkers"
        auto-load
    />
    <fetch-data
        url="ContactChannels"
        @on-fetch="(data) => (contactChannels = data)"
        auto-load
    />
    <fetch-data
        url="BusinessTypes"
        @on-fetch="(data) => (businessTypes = data)"
        auto-load
    />

    <FormModel :url="`Clients/${route.params.id}`" model="customer" auto-load>
        <template #form="{ data, validate, filter }">
            <VnRow class="row q-gutter-md q-mb-md">
                <div class="col">
                    <VnInput
                        v-model="data.socialName"
                        :label="t('customer.basicData.socialName')"
                        :rules="validate('client.socialName')"
                        autofocus
                    />
                </div>
                <div class="col">
                    <QSelect
                        v-model="data.businessTypeFk"
                        :options="businessTypes"
                        option-value="code"
                        option-label="description"
                        emit-value
                        :label="t('customer.basicData.businessType')"
                        map-options
                        :rules="validate('client.businessTypeFk')"
                        :input-debounce="0"
                    />
                </div>
            </VnRow>
            <VnRow class="row q-gutter-md q-mb-md">
                <div class="col">
                    <VnInput
                        v-model="data.contact"
                        :label="t('customer.basicData.contact')"
                        :rules="validate('client.contact')"
                        clearable
                    />
                </div>
                <div class="col">
                    <VnInput
                        v-model="data.email"
                        type="email"
                        :label="t('customer.basicData.email')"
                        :rules="validate('client.email')"
                        clearable
                    />
                </div>
            </VnRow>
            <VnRow class="row q-gutter-md q-mb-md">
                <div class="col">
                    <VnInput
                        v-model="data.phone"
                        :label="t('customer.basicData.phone')"
                        :rules="validate('client.phone')"
                        clearable
                    />
                </div>
                <div class="col">
                    <VnInput
                        v-model="data.mobile"
                        :label="t('customer.basicData.mobile')"
                        :rules="validate('client.mobile')"
                        clearable
                    />
                </div>
            </VnRow>
            <VnRow class="row q-gutter-md q-mb-md">
                <div class="col">
                    <QSelect
                        v-model="data.salesPersonFk"
                        :options="workers"
                        option-value="id"
                        option-label="name"
                        emit-value
                        :label="t('customer.basicData.salesPerson')"
                        map-options
                        use-input
                        @filter="(value, update) => filter(value, update, filterOptions)"
                        :rules="validate('client.salesPersonFk')"
                        :input-debounce="0"
                    >
                        <template #prepend>
                            <QAvatar color="orange">
                                <QImg
                                    v-if="data.salesPersonFk"
                                    :src="`/api/Images/user/160x160/${data.salesPersonFk}/download?access_token=${token}`"
                                    spinner-color="white"
                                />
                            </QAvatar>
                        </template>
                    </QSelect>
                </div>
                <div class="col">
                    <QSelect
                        v-model="data.contactChannelFk"
                        :options="contactChannels"
                        option-value="id"
                        option-label="name"
                        emit-value
                        :label="t('customer.basicData.contactChannel')"
                        map-options
                        :rules="validate('client.contactChannelFk')"
                        :input-debounce="0"
                    />
                </div>
            </VnRow>
        </template>
    </FormModel>
</template>
