<template>
    <van-dialog v-model:show="show" :show-confirm-button="false">
        <template #title>
            <button type="button"
                class="absolute top-3 right-2.5 text-gray-400 bg-transparent hover:bg-gray-200 hover:text-gray-900 rounded-lg text-sm w-8 h-8 ml-auto inline-flex justify-center items-center dark:hover:bg-gray-600 dark:hover:text-white"
                data-modal-hide="popup-modal" @click="close">
                <svg class="w-3 h-3" aria-hidden="true" fill="none" viewBox="0 0 14 14" @click="show=false">
                    <path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                        d="m1 1 6 6m0 0 6 6M7 7l6-6M7 7l-6 6" />
                </svg>
                <span class="sr-only">Close modal</span>
            </button>
        </template>
        <template #default>
            <div class="p-6 text-center">
                <div v-if="optional.type == 'warning'" >
                    <svg class="mx-auto mb-4 text-gray-400 w-12 h-12 dark:text-gray-200" aria-hidden="true"
                        xmlns="http://www.w3.org/2000/svg" fill="none"
                        viewBox="0 0 20 20">
                        <path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                            d="M10 11V6m0 8h.01M19 10a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z" />
                    </svg>
                </div>
                <div class="mx-auto flex h-12 w-12 items-center justify-center rounded-full bg-green-100 mb-4"
                    v-if="optional.type == 'success'">
                    <svg class="h-6 w-6 text-green-600" fill="none" viewBox="0 0 24 24" stroke-width="1.5"
                        stroke="currentColor" aria-hidden="true">
                        <path stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5" />
                    </svg>
                </div>
                <div v-if="optional.type == 'error'"
                    class="mx-auto flex h-12 w-12 flex-shrink-0 items-center justify-center rounded-full bg-red-100 sm:mx-0 sm:h-10 sm:w-10 mb-4">
                    <svg class="h-6 w-6 text-red-600" fill="none" viewBox="0 0 24 24" stroke-width="1.5"
                        stroke="currentColor" aria-hidden="true">
                        <path stroke-linecap="round" stroke-linejoin="round"
                            d="M12 9v3.75m-9.303 3.376c-.866 1.5.217 3.374 1.948 3.374h14.71c1.73 0 2.813-1.874 1.948-3.374L13.949 3.378c-.866-1.5-3.032-1.5-3.898 0L2.697 16.126zM12 15.75h.007v.008H12v-.008z" />
                    </svg>
                </div>
                <h3 class="mb-5 text-lg font-normal text-gray-500 dark:text-gray-400">{{ optional.content }}</h3>
                <div class="flex flex-row items-center justify-center">
                    <van-button block color="linear-gradient(90deg, #F70 0%, #FF4900 100%)" class="font-semibold btn"
                    @click="confirm">{{ optional.confirmText }}</van-button>
         
                <button data-modal-hide="popup-modal" type="button" v-if="optional.showCancelButton" @click="close"
                    class="w-full flex-row justify-center
                    text-gray-500 bg-white hover:bg-gray-100 focus:ring-4 focus:outline-none focus:ring-gray-200 rounded-lg border border-gray-200 text-sm font-medium px-5 py-2.5 hover:text-gray-900 focus:z-10 dark:bg-gray-700 dark:text-gray-300 dark:border-gray-500 dark:hover:text-white dark:hover:bg-gray-600 dark:focus:ring-gray-600">No,
                   <span> {{ optional.cancelText }}</span></button>
                </div>
            </div>
        </template>
    </van-dialog>
</template>


<script setup lang="ts" >
import { ref } from 'vue';

const optional = ref({
    type: 'success',
    title: '',
    content: '',
    showConfirmButton: true,
    showCancelButton: false,
    confirmText: 'Confirm',
    cancelText: 'Cancel'
})

const show = ref(false)

const open = (options: any) => {
    options.type && (optional.value.type = options.type)
    options.title && (optional.value.title = options.title)
    options.content && (optional.value.content = options.content)
    options.showConfirmButton && (optional.value.showConfirmButton = options.showConfirmButton)
    optional.value.showCancelButton = options.showCancelButton
    options.confirmText && (optional.value.confirmText = options.confirmText)
    options.cancelText && (optional.value.cancelText = options.cancelText)
    show.value = true
}

const confirm = async ():Promise<Boolean> => {
    show.value = false
    return Promise.resolve(true)
}

const close = async () => {
    show.value = false
    return Promise.reject(false)
}

defineExpose({
    open,
    close
})
</script>