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

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

const workersOptions = ref([]);
const clientsOptions = ref([]);
</script>
<template>
    <fetch-data
        url="Workers/search"
        @on-fetch="(data) => (workersOptions = data)"
        auto-load
    />
    <fetch-data url="Clients" @on-fetch="(data) => (clientsOptions = data)" auto-load />
    <FormModel
        :url="`Departments/${route.params.id}`"
        model="department"
        auto-load
        class="full-width"
    >
        <template #form="{ data, validate }">
            <VnRow class="row q-gutter-md q-mb-md">
                <div class="col">
                    <VnInput
                        :label="t('department.name')"
                        v-model="data.name"
                        :rules="validate('department.name')"
                        clearable
                        autofocus
                    />
                </div>
                <div class="col">
                    <VnInput
                        v-model="data.code"
                        :label="t('department.code')"
                        :rules="validate('department.code')"
                        clearable
                    />
                </div>
            </VnRow>
            <VnRow class="row q-gutter-md q-mb-md">
                <div class="col">
                    <VnInput
                        :label="t('department.chat')"
                        v-model="data.chatName"
                        :rules="validate('department.chat')"
                        clearable
                    />
                </div>
                <div class="col">
                    <VnInput
                        v-model="data.notificationEmail"
                        :label="t('department.email')"
                        :rules="validate('department.email')"
                        clearable
                    />
                </div>
            </VnRow>
            <VnRow class="row q-gutter-md q-mb-md">
                <div class="col">
                    <VnSelectFilter
                        :label="t('department.bossDepartment')"
                        v-model="data.workerFk"
                        :options="workersOptions"
                        option-value="id"
                        option-label="name"
                        hide-selected
                        map-options
                        :rules="validate('department.workerFk')"
                    />
                </div>
                <div class="col">
                    <VnSelectFilter
                        :label="t('department.selfConsumptionCustomer')"
                        v-model="data.clientFk"
                        :options="clientsOptions"
                        option-value="id"
                        option-label="name"
                        hide-selected
                        map-options
                        :rules="validate('department.clientFk')"
                    />
                </div>
            </VnRow>
            <VnRow class="row q-gutter-md q-mb-md">
                <div class="col">
                    <QCheckbox
                        :label="t('department.telework')"
                        v-model="data.isTeleworking"
                    />
                </div>
                <div class="col">
                    <QCheckbox
                        :label="t('department.notifyOnErrors')"
                        v-model="data.hasToMistake"
                        :false-value="0"
                        :true-value="1"
                    />
                </div>
            </VnRow>
            <VnRow class="row q-gutter-md q-mb-md">
                <div class="col">
                    <QCheckbox
                        :label="t('department.worksInProduction')"
                        v-model="data.isProduction"
                    />
                </div>
                <div class="col">
                    <QCheckbox
                        :label="t('department.hasToRefill')"
                        v-model="data.hasToRefill"
                    />
                </div>
            </VnRow>
            <VnRow class="row q-gutter-md q-mb-md">
                <div class="col">
                    <QCheckbox
                        :label="t('department.hasToSendMail')"
                        v-model="data.hasToSendMail"
                    />
                </div>
            </VnRow>
        </template>
    </FormModel>
</template>
