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

import VnSelect from 'src/components/common/VnSelect.vue';
import FetchData from 'components/FetchData.vue';
import VnRow from 'components/ui/VnRow.vue';
import FormPopup from 'components/FormPopup.vue';

const emit = defineEmits(['onSubmitCreateAlias']);

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

const aliasFormData = reactive({
    mailAlias: null,
    account: route.params.id,
});

const aliasOptions = ref([]);
</script>

<template>
    <FetchData
        url="MailAliases"
        :filter="{ fields: ['id', 'alias'], order: 'alias ASC' }"
        auto-load
        @on-fetch="(data) => (aliasOptions = data)"
    />
    <FormPopup
        model="ZoneWarehouse"
        @on-submit="emit('onSubmitCreateAlias', aliasFormData)"
    >
        <template #form-inputs>
            <VnRow>
                <div class="col">
                    <VnSelect
                        :label="t('account.card.alias')"
                        v-model="aliasFormData.mailAlias"
                        :options="aliasOptions"
                        option-value="id"
                        option-label="alias"
                        hide-selected
                        :required="true"
                    />
                </div>
            </VnRow>
        </template>
    </FormPopup>
</template>
