<template>
    <van-dialog v-model:show="show" :showConfirmButton="false">
        <div class="py-4">
            <Vue3Lottie ref="loadingLottieRef" :animationData="AnimationLoadingJSON" :height="200" :width="200"
                @onLoopComplete="onComplete" :speed="speed" :loop="true" />
        </div>
    </van-dialog>
    <Dialog ref="dialogRef" @confirm="onConfirm" />
    <Dialog ref="dialogRef2"  />
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { useRouter } from 'vue-router'
import Dialog from '@/components/Dialog.vue'
import { Vue3Lottie } from 'vue3-lottie'
import { onMounted } from 'vue';
import AnimationLoadingJSON from '@/assets/lottie/animation_loading.json'
import { retrieveTask } from '@/api/start'
import { useI18n } from 'vue-i18n';
const { t } = useI18n()

const dialogRef = ref()
const dialogRef2 = ref()
const grailedDialogRef = ref()
const router = useRouter()
const loadingLottieRef = ref();
const show = ref(false)
const speed = ref(1.3)
const toPath = ref('/order')
const emits = defineEmits(['success','retrieveTaskSuccess'])

const grabOrder = async (): Promise<boolean> => {
    let res: any = {}
    try {
        res = await retrieveTask() as any
    }finally{
        emits('retrieveTaskSuccess')
    }
    const code = res.code
    if (code === 1004008001) {
        // 有未完成的任务
        dialogRef.value.open({
            type: 'warning',
            content: t('start.grabOrder.not_completed'),
            showConfirmButton: true,
            showCancelButton: false,
            confirmText:  t('start.grabOrder.not_completed_text'),
        })
        return false
    }
    if (code === 1004008002) {
        // 已经达到今日订单数量上线
        dialogRef2.value.open({
            type: 'warning',
            content:  t('start.grabOrder.order_limit'),
            showConfirmButton: true,
            showCancelButton: false,
            confirmText:  t('start.grabOrder.order_limit_text'),
        })
        return false
    }
    if (code === 1004008003) {
        toPath.value = '/channel-select/1'
        dialogRef.value.open({
            type: 'warning',
            content:  t('start.grabOrder.balance_not_enough'),
            showConfirmButton: true,
            showCancelButton: false,
            confirmText: t('start.grabOrder.balance_not_enough_text'),
        })
    }
    if (code === 1004060001) {
        dialogRef2.value.open({
            type: 'warning',
            content: t('start.grabOrder.no_order_available'),
            showConfirmButton: true,
            showCancelButton: false,
            confirmText:  t('start.grabOrder.no_order_available_text'),
        })
    }
    if (code === 1004180004) {
        dialogRef2.value.open({
            type: 'warning',
            content: t('start.grabOrder.not_meet_start'),
            showConfirmButton: true,
            showCancelButton: false,
            confirmText: t('start.grabOrder.no_order_available_text'),
        })
        return false
    }
    if (code === 0) {
        emits('success', res.data)
    }
    return true
}


const onComplete = async () => {
    loadingLottieRef.value.stop()
    show.value = false
    await grabOrder()
}

const onConfirm = () => {
    router.push(toPath.value)
}

const open = () => {
    if (loadingLottieRef.value) {
        loadingLottieRef.value.stop()
        loadingLottieRef.value.play()
    }
    show.value = true
}

defineExpose({
    open,
    grabOrder
})

onMounted(() => {

})

</script>