<script setup lang="ts">
import type { PaperQuestionType } from '@/types'
import CourseExamCard from '../components/CourseExamCard.vue'
import { getPaper } from '../api'

const route = useRoute()
const courseId = $ref(route.query.course_id as string)
const semesterId = $ref(route.query.semester_id as string)
const paperId = $ref(route.query.paper_id as string)
const paperTitle = $ref(route.query.paper_title as string)
const type = $ref(parseInt(route.query.type as string))
const detail = reactive({ status: 1 })
let questionList = $ref<PaperQuestionType[]>([])
provide('questionList', $$(questionList))

let loading = $ref<boolean>(false)
// 获取试卷详情
function fetchInfo() {
  loading = true
  getPaper({ course_id: courseId, semester_id: semesterId, paper_id: paperId, type }).then(res => {
    const { question, status, score_details } = res.data
    Object.assign(detail, res.data)
    questionList = status === 3 ? score_details : question
    loading = false
  })
}
onMounted(() => {
  fetchInfo()
})
const title = computed(() => {
  if (paperTitle) return paperTitle
  return type === 1 ? '课程考试' : '课后作业'
})
</script>

<template>
  <CourseExamCard :status="detail.status" :title="title" @update="fetchInfo" v-loading="loading"></CourseExamCard>
</template>
