<script setup>
import {dialogQueue, closeDialog, cleanupDialog} from '../Composables/useAppDialog'
import AppDialog from './AppDialog.vue'

const handleConfirm = async (dialog) => {
    dialog.loading = true
    try {
        // Ждём выполнения вашего запроса
        if (typeof dialog.onConfirm === 'function') {
            await dialog.onConfirm()
        }
        closeDialog(dialog.id, true)
    } catch (error) {
        console.error('Ошибка при подтверждении:', error)
        dialog.loading = false // Оставляем диалог открытым при ошибке
    }
}
</script>

<template>
    <Teleport to="body">
        <AppDialog
            v-for="dialog in dialogQueue"
            :key="dialog.id"
            :show="dialog.show"
            :loading="dialog.loading"
            :title="dialog.title"
            :content="dialog.content"
            :positive-text="dialog.positiveText"
            :negative-text="dialog.negativeText"
            :positive-props="dialog.positiveProps"
            :negative-props="dialog.negativeProps"
            :mask-closable="dialog.maskClosable"
            @confirm="handleConfirm(dialog)"
            @cancel="closeDialog(dialog.id, false)"
            @update:show="(val) => !val && dialog.show && !dialog.loading && closeDialog(dialog.id, false)"
            @after-leave="cleanupDialog(dialog.id)"
        />
    </Teleport>
</template>
