<template>
    <mx-paper :readonly="!!prevData.readonly" :continue-name="prevData.continueName"/>
</template>

<script setup>
import {onMounted, ref} from 'vue'
import {useTransfer} from "@/hooks/useTransfer";
import {getExamRecordsDetail} from "@/api/webApi/paper";
import {openExamineePaper} from "@/api/webApi/studentEvaluating";
import {useProvidePaperService} from "@/components/mx-paper/usePaperInjection";
import {useProvideQuestionService} from "@/components/mx-question/useQuestionInjection";
import {alertAsync} from "@/utils/uni-helper";
import {useProvidePaperNavigationService} from "@/components/mx-paper/usePaperNavigationServiceInjection";

const {prevData, callbackEventData} = useTransfer()
const paper = ref({})
// paper service, question service解耦了视图与数据的关系，使两方都可以独立重写
const paperService = useProvidePaperService(paper)
const questionService = useProvideQuestionService(paperService)
useProvidePaperNavigationService(paperService, questionService)

onMounted(() => loadPaper())
paperService.onAnswerComplete(async () => {
    const msg = paperService.answerCompletedTips.value
    await alertAsync(msg)
    questionService.cleanUserData()
    paperService.reset()
    await loadPaper()
})

paperService.onScoreComplete(async () => {
    // 因为这里并没有合适的API同步试卷的属性，
    // 暂时手改allowScore为false，可以呈现做题统计和继续练习按钮
    paper.value.allowScore = false
    paperService.index.value = 0
    callbackEventData.value = true // 只需要标记有变更即可
})

paperService.onContinueNext(async () => {
    questionService.cleanUserData()
    paperService.reset()
    await loadPaper()
})

const loadPaper = async () => {
    uni.showLoading()
    try {
        const loadFun = prevData.value.readonly ? getExamRecordsDetail : openExamineePaper
        const res = await loadFun(prevData.value)
        paper.value = res.data
    } finally {
        uni.hideLoading()
    }
}
</script>

<style lang="scss">

</style>
