<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 VnImg from 'src/components/ui/VnImg.vue';

const route = useRoute();
const { t } = useI18n();

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
    />
    <fetch-data
        :filter="filter"
        @on-fetch="(data) => (clients = data)"
        auto-load
        url="Clients"
    />

    <FormModel :url="`Clients/${route.params.id}`" auto-load model="customer">
        <template #form="{ data, validate, filter }">
            <VnRow class="row q-gutter-md q-mb-md">
                <VnInput
                    :label="t('globals.name')"
                    :rules="validate('client.socialName')"
                    autofocus
                    clearable
                    v-model="data.name"
                />

                <QSelect
                    :input-debounce="0"
                    :label="t('customer.basicData.businessType')"
                    :options="businessTypes"
                    :rules="validate('client.businessTypeFk')"
                    emit-value
                    map-options
                    option-label="description"
                    option-value="code"
                    v-model="data.businessTypeFk"
                />
            </VnRow>
            <VnRow class="row q-gutter-md q-mb-md">
                <VnInput
                    :label="t('customer.basicData.contact')"
                    :rules="validate('client.contact')"
                    clearable
                    v-model="data.contact"
                />
                <VnInput
                    :label="t('customer.basicData.email')"
                    :rules="validate('client.email')"
                    clearable
                    type="email"
                    v-model="data.email"
                >
                    <template #append>
                        <QIcon name="info" class="cursor-info">
                            <QTooltip>{{
                                t('customer.basicData.youCanSaveMultipleEmails')
                            }}</QTooltip>
                        </QIcon>
                    </template>
                </VnInput>
            </VnRow>
            <VnRow class="row q-gutter-md q-mb-md">
                <VnInput
                    :label="t('customer.basicData.phone')"
                    :rules="validate('client.phone')"
                    clearable
                    v-model="data.phone"
                />
                <VnInput
                    :label="t('customer.basicData.mobile')"
                    :rules="validate('client.mobile')"
                    clearable
                    v-model="data.mobile"
                />
            </VnRow>
            <VnRow class="row q-gutter-md q-mb-md">
                <QSelect
                    :input-debounce="0"
                    :label="t('customer.basicData.salesPerson')"
                    :options="workers"
                    :rules="validate('client.salesPersonFk')"
                    @filter="(value, update) => filter(value, update, filterOptions)"
                    emit-value
                    map-options
                    option-label="name"
                    option-value="id"
                    use-input
                    v-model="data.salesPersonFk"
                >
                    <template #prepend>
                        <QAvatar color="orange">
                            <VnImg
                                v-if="data.salesPersonFk"
                                :id="user.id"
                                collection="user"
                                spinner-color="white"
                            />
                        </QAvatar>
                    </template>
                </QSelect>
                <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"
                />
            </VnRow>
            <VnRow class="row q-gutter-md q-mb-md">
                <QSelect
                    :input-debounce="0"
                    :label="t('customer.basicData.previousClient')"
                    :options="clients"
                    :rules="validate('client.transferorFk')"
                    emit-value
                    map-options
                    option-label="name"
                    option-value="id"
                    v-model="data.transferorFk"
                >
                    <template #append>
                        <QIcon name="info" class="cursor-pointer">
                            <QTooltip>{{
                                t(
                                    'In case of a company succession, specify the grantor company'
                                )
                            }}</QTooltip>
                        </QIcon>
                    </template>
                </QSelect>
            </VnRow>
        </template>
    </FormModel>
</template>

<i18n>
es:
    In case of a company succession, specify the grantor company: En el caso de que haya habido una sucesión de empresa, indicar la empresa cedente
    Comercial name: Nombre comercial
</i18n>
