<script setup>
import { onMounted, ref } from 'vue';
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 VnInputDate from 'components/common/VnInputDate.vue';
import VnSelect from 'src/components/common/VnSelect.vue';
import VnSelectDialog from 'src/components/common/VnSelectDialog.vue';
import CreateBankEntityForm from 'src/components/CreateBankEntityForm.vue';
import VnLocation from 'src/components/common/VnLocation.vue';
import VnInput from 'src/components/common/VnInput.vue';
import VnSubToolbar from 'src/components/ui/VnSubToolbar.vue';

import { useUserConfig } from 'src/composables/useUserConfig';

const { t } = useI18n();

const workerConfigFilter = {
    field: ['payMethodFk'],
};

const newWorkerForm = ref({
    companyFk: null,
    payMethodFk: null,
    firstName: null,
    lastNames: null,
    birth: null,
    fi: null,
    code: null,
    phone: null,
    postcode: null,
    provinceFk: null,
    city: null,
    street: null,
    name: null,
    email: null,
    bossFk: null,
    iban: null,
    bankEntityFk: null,
});

const companiesOptions = ref([]);
const workersOptions = ref([]);
const payMethodsOptions = ref([]);
const bankEntitiesOptions = ref([]);
const postcodesOptions = ref([]);

const onFetchWorkerConfig = (workerConfig) => {
    newWorkerForm.value.payMethodFk = workerConfig.payMethodFk;
};

const onBankEntityCreated = (data) => {
    bankEntitiesOptions.value.push(data);
};

function handleLocation(data, location) {
    const { town, code, provinceFk, countryFk } = location ?? {};
    data.postcode = code;
    data.city = town;
    data.provinceFk = provinceFk;
    data.countryFk = countryFk;
}

onMounted(async () => {
    const userInfo = await useUserConfig().fetch();
    newWorkerForm.value = { companyFk: userInfo.companyFk };
});
</script>

<template>
    <FetchData
        url="WorkerConfigs/findOne"
        @on-fetch="(data) => onFetchWorkerConfig(data)"
        :filter="workerConfigFilter"
        auto-load
    />

    <FetchData
        url="Companies"
        @on-fetch="(data) => (companiesOptions = data)"
        auto-load
    />
    <FetchData
        url="Workers/search"
        @on-fetch="(data) => (workersOptions = data)"
        auto-load
    />
    <FetchData
        url="Paymethods"
        @on-fetch="(data) => (payMethodsOptions = data)"
        auto-load
    />
    <FetchData
        url="BankEntities"
        @on-fetch="(data) => (bankEntitiesOptions = data)"
        auto-load
    />

    <QPage>
        <VnSubToolbar />
        <FormModel
            url-create="Workers/new"
            model="worker"
            :form-initial-data="newWorkerForm"
        >
            <template #form="{ data, validate }">
                <VnRow class="row q-gutter-md q-mb-md">
                    <VnInput
                        v-model="data.firstName"
                        :label="t('worker.create.name')"
                        :rules="validate('Worker.firstName')"
                    />
                    <VnInput
                        v-model="data.lastNames"
                        :label="t('worker.create.lastName')"
                        :rules="validate('Worker.lastNames')"
                    />
                    <VnInputDate
                        v-model="data.birth"
                        :label="t('worker.create.birth')"
                        :rules="validate('Worker.birth')"
                    />
                </VnRow>
                <VnRow class="row q-gutter-md q-mb-md">
                    <VnInput
                        v-model="data.fi"
                        :label="t('worker.create.fi')"
                        :rules="validate('Worker.fi')"
                    />
                    <VnInput
                        v-model="data.code"
                        :label="t('worker.create.code')"
                        :rules="validate('Worker.code')"
                    />
                    <VnInput
                        v-model="data.phone"
                        :label="t('worker.create.phone')"
                        :rules="validate('Worker.phone')"
                    />
                </VnRow>
                <VnRow class="row q-gutter-md q-mb-md">
                    <VnLocation
                        :rules="validate('Worker.postcode')"
                        :roles-allowed-to-create="['deliveryAssistant']"
                        :options="postcodesOptions"
                        v-model="data.location"
                        @update:model-value="(location) => handleLocation(data, location)"
                    >
                    </VnLocation>
                </VnRow>
                <VnRow class="row q-gutter-md q-mb-md">
                    <VnInput
                        :label="t('worker.create.street')"
                        v-model="data.street"
                        :rules="validate('Worker.street')"
                    />
                </VnRow>
                <VnRow class="row q-gutter-md q-mb-md">
                    <VnInput
                        v-model="data.name"
                        :label="t('worker.create.webUser')"
                        :rules="validate('Worker.name')"
                    />
                    <VnInput
                        v-model="data.email"
                        :label="t('worker.create.personalEmail')"
                        :rules="validate('Worker.email')"
                    />
                </VnRow>
                <VnRow class="row q-gutter-md q-mb-md">
                    <VnSelect
                        :label="t('worker.create.company')"
                        v-model="data.companyFk"
                        :options="companiesOptions"
                        option-value="id"
                        option-label="code"
                        hide-selected
                        :rules="validate('Worker.company')"
                    />
                    <VnSelect
                        :label="t('worker.create.boss')"
                        v-model="data.bossFk"
                        :options="workersOptions"
                        option-value="id"
                        option-label="name"
                        hide-selected
                        :rules="validate('Worker.boss')"
                    >
                        <template #option="scope">
                            <QItem v-bind="scope.itemProps">
                                <QItemSection>
                                    <QItemLabel>{{ scope.opt.name }}</QItemLabel>
                                    <QItemLabel caption
                                        >{{ scope.opt.nickname }},
                                        {{ scope.opt.code }}
                                    </QItemLabel>
                                </QItemSection>
                            </QItem>
                        </template>
                    </VnSelect>
                </VnRow>
                <VnRow class="row q-gutter-md q-mb-md">
                    <VnSelect
                        :label="t('worker.create.payMethods')"
                        v-model="data.payMethodFk"
                        :options="payMethodsOptions"
                        option-value="id"
                        option-label="name"
                        map-options
                        hide-selected
                        :rules="validate('Worker.payMethodFk')"
                    />
                    <VnInput
                        v-model="data.iban"
                        :label="t('worker.create.iban')"
                        :rules="validate('Worker.iban')"
                    >
                        <template #append>
                            <QIcon name="info" class="cursor-info">
                                <QTooltip>{{ t('components.iban_tooltip') }}</QTooltip>
                            </QIcon>
                        </template>
                    </VnInput>
                    <VnSelectDialog
                        :label="t('worker.create.bankEntity')"
                        v-model="data.bankEntityFk"
                        :options="bankEntitiesOptions"
                        option-label="name"
                        option-value="id"
                        hide-selected
                        :roles-allowed-to-create="['salesAssistant', 'hr']"
                        :rules="validate('Worker.bankEntity')"
                    >
                        <template #form>
                            <CreateBankEntityForm
                                @on-data-saved="onBankEntityCreated($event)"
                            />
                        </template>
                        <template #option="scope">
                            <QItem v-bind="scope.itemProps">
                                <QItemSection v-if="scope.opt">
                                    <QItemLabel
                                        >{{ scope.opt.bic }}
                                        {{ scope.opt.name }}</QItemLabel
                                    >
                                </QItemSection>
                            </QItem>
                        </template>
                    </VnSelectDialog>
                </VnRow>
            </template>
        </FormModel>
    </QPage>
</template>
