<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([]);
</script>
<template>
    <FetchData
        url="Workers/search"
        :filter="{ fields: ['id', 'nickname'], order: 'nickname ASC', limit: 30 }"
        @on-fetch="(data) => (workersOptions = data)"
        auto-load
    />
    <FormModel
        :url="`Suppliers/${route.params.id}`"
        :url-update="`Suppliers/${route.params.id}`"
        model="supplier"
        auto-load
        :clear-store-on-unmount="false"
    >
        <template #form="{ data, validate }">
            <VnRow class="row q-gutter-md q-mb-md">
                <div class="col">
                    <VnInput
                        v-model="data.nickname"
                        :label="t('supplier.basicData.alias')"
                        :rules="validate('supplier.nickname')"
                        clearable
                    />
                </div>
                <div class="col">
                    <VnSelectFilter
                        :label="t('supplier.basicData.workerFk')"
                        v-model="data.workerFk"
                        :options="workersOptions"
                        option-value="id"
                        option-label="name"
                        hide-selected
                        map-options
                        :rules="validate('supplier.workerFk')"
                    >
                        <template #append>
                            <QIcon name="info" class="cursor-pointer">
                                <QTooltip>{{
                                    t('Responsible for approving invoices')
                                }}</QTooltip>
                            </QIcon>
                        </template>
                        <template #option="scope">
                            <QItem v-bind="scope.itemProps">
                                <QItemSection>
                                    <QItemLabel>{{ scope.opt?.name }}</QItemLabel>
                                    <QItemLabel caption>
                                        {{ scope.opt?.nickname }}, {{ scope.opt?.id }}
                                    </QItemLabel>
                                </QItemSection>
                            </QItem>
                        </template>
                    </VnSelectFilter>
                </div>
            </VnRow>
            <VnRow class="row q-gutter-md q-mb-md">
                <div class="col">
                    <QCheckbox
                        v-model="data.isSerious"
                        :label="t('supplier.basicData.isSerious')"
                    />
                </div>
                <div class="col">
                    <QCheckbox
                        v-model="data.isActive"
                        :label="t('supplier.basicData.isActive')"
                    />
                </div>
                <div class="col">
                    <QCheckbox
                        v-model="data.isPayMethodChecked"
                        :label="t('supplier.basicData.isPayMethodChecked')"
                    />
                </div>
            </VnRow>
            <VnRow class="row q-gutter-md q-mb-md">
                <div class="col">
                    <QInput
                        :label="t('supplier.basicData.note')"
                        type="textarea"
                        v-model="data.note"
                        fill-input
                    />
                </div>
            </VnRow>
        </template>
    </FormModel>
</template>

<i18n>
es:
    Responsible for approving invoices: Responsable de aprobar las facturas
</i18n>
