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

const { t } = useI18n();

const props = defineProps({
    icon: {
        type: String,
        default: null
    },
    title: {
        type: String,
        default: null
    },
    message: {
        type: String,
        default: null
    },
    data: {
        type: Object,
        required: false,
        default: null
    },
    promise: {
        type: Function,
        required: false,
        default: null
    }
});

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

const { dialogRef, onDialogOK } = useDialogPluginComponent();

const title = props.title || t('confirm');
const message = props.message || t('wantToContinue');
const isLoading = ref(false);

async function confirm() {
    isLoading.value = true;
    if (props.promise) {
        try {
            await props.promise(props.data);
        } finally {
            isLoading.value = false;
        }
    }
    onDialogOK(props.data);
}
</script>
<template>
    <QDialog ref="dialogRef">
        <QCard class="q-pa-sm">
            <QCardSection class="row items-center q-pb-none">
                <QAvatar
                    :icon="icon"
                    color="primary"
                    text-color="white"
                    size="xl"
                    v-if="icon"
                />
                <span class="text-h6 text-grey">{{ title }}</span>
                <QSpace />
                <QBtn
                    icon="close"
                    :disable="isLoading"
                    flat
                    round
                    dense
                    v-close-popup
                />
            </QCardSection>
            <QCardSection class="row items-center">
                <span v-html="message" />
                <slot name="customHTML" />
            </QCardSection>
            <QCardActions align="right">
                <QBtn
                    :label="t('cancel')"
                    color="primary"
                    :disable="isLoading"
                    flat
                    v-close-popup
                />
                <QBtn
                    :label="t('confirm')"
                    color="primary"
                    :loading="isLoading"
                    @click="confirm()"
                    unelevated
                    autofocus
                    data-cy="confirmDialogButton"
                />
            </QCardActions>
        </QCard>
    </QDialog>
</template>

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

<i18n lang="yaml">
en-US:
    wantToContinue: Are you sure you want to continue?
    cancel: Cancel
es-ES:
    wantToContinue: ¿Seguro que quieres continuar?
    cancel: Cancelar
ca-ES:
    wantToContinue: Segur que vols continuar?
    cancel: Cancel·lar
fr-FR:
    wantToContinue: Êtes-vous sûr de vouloir continuer?
    cancel: Annuler
pt-PT:
    wantToContinue: Tem a certeza de que deseja continuar?
    cancel: Cancelar
</i18n>
