<script setup>
import { ref } from 'vue';
import { useDialogPluginComponent } from 'quasar';
import { useI18n } from 'vue-i18n';

const props = defineProps({
    data: {
        type: Object,
        requied: true,
        default: null,
    },
    promise: {
        type: Function,
        required: true,
    },
});

defineEmits(['confirm', ...useDialogPluginComponent.emits]);

const { dialogRef, onDialogOK } = useDialogPluginComponent();
const { t } = useI18n();

const address = ref(props.data.address);
const isLoading = ref(false);

async function confirm() {
    const response = { address };

    if (props.promise) {
        isLoading.value = true;
        try {
            Object.assign(response, props.data);
            await props.promise(response);
        } finally {
            isLoading.value = false;
        }
    }

    onDialogOK(response);
}
</script>
<template>
    <q-dialog ref="dialogRef" persistent>
        <q-card class="q-pa-sm">
            <q-card-section class="row items-center q-pb-none">
                <span class="text-h6 text-grey">{{ t('Send email notification') }}</span>
                <q-space />
                <q-btn icon="close" flat round dense v-close-popup />
            </q-card-section>
            <q-card-section class="row items-center">
                {{ t('The notification will be sent to the following address') }}
            </q-card-section>
            <q-card-section class="q-pt-none">
                <q-input dense v-model="address" rounded outlined autofocus />
            </q-card-section>
            <q-card-actions align="right">
                <q-btn :label="t('globals.cancel')" color="primary" flat v-close-popup />
                <q-btn
                    :label="t('globals.confirm')"
                    color="primary"
                    :loading="isLoading"
                    @click="confirm"
                    unelevated
                />
            </q-card-actions>
        </q-card>
    </q-dialog>
</template>

<style lang="scss" scoped>
.q-card {
    min-width: 350px;
}
</style>

<i18n>
    es:
        Send email notification: Enviar notificación por correo
        The notification will be sent to the following address: La notificación se enviará a la siguiente dirección
</i18n>
