<script setup lang="js">
import QuizSheet from '@/view/components/QuizSheet.vue'
import { onMounted,ref } from 'vue'
import {registerAnwerDetailApi,wenJuanDetail,wenJuanAnswer} from '@/http/register.js'
import {getToken,setLogin} from '@/utils/local.js'
import {adaptQuestionsHome} from '@/utils/util.js'
import { useRoute,useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'

const questions = ref([])
const route = useRoute()
const router = useRouter()
const loading = ref(false)

const qid = route.query.qid || ''

// 提交回调
async function handleSubmit(payload) {
  console.log('提交结果Home：', payload)
  loading.value = true
  // 比如发送到后端：
  // await request.post('/api/submit', payload)
    const myToken = getToken()
    try{
    const res = await wenJuanAnswer(myToken,qid)
    loading.value = false
    if(res.code == 1){
        ElMessage.success('success')
        router.back()
    }
  }catch(err){
    loading.value = false
  }
}

async function getHomeAnswerList() {
    loading.value = true
    const myToken = getToken()
    try{
        const res = await wenJuanDetail(myToken,qid)
        loading.value = false
        if(res.code == 1){
            questions.value =  adaptQuestionsHome(res.data.data)
            // console.log('问题列表是:',questions.value)
        }
    }catch(err){
        loading.value = false
    }
}

onMounted(() => {
    getHomeAnswerList()
})

</script>

<template>
  <div class="w-full h-full" v-if="loading" v-loading="loading"></div>
  <QuizSheet v-else
    v-if="questions.length > 0"
    :questions="questions"
    storageKey="quiz_demo_v2"     
    @submit="handleSubmit"
  />
</template>
