import { defineStore } from 'pinia'
import { ref, computed } from 'vue'

// 定义考试信息的类型接口
interface ExamInfo {
    id: string | number // 根据实际业务调整 id 类型
    examSubjectName: string,
    examSubjectCode: string,
    [key: string]: any // 允许其他未知属性
}

//考试相关的状态管理
export const useExamStore = defineStore('exam', () => {
    //初始化时尝试从localStorage中获取考试信息
    const storedExam = localStorage.getItem('current_exam_info')
    const initialExam = storedExam ? JSON.parse(storedExam) : null
    //显式指定泛型类型为 ExamInfo | null，解决类型推断为 never 的问题
    const currentExam = ref<ExamInfo | null>(initialExam)

    //计算属性 考试id
    const examId = computed(() => currentExam.value?.id)
    //计算属性 考试名称
    const examName = computed(() => currentExam.value?.examSubjectName)
    //计算属性 考试类型
    const examType = computed(() => currentExam.value?.examType || 1) //考试类型 1 选择题判分 2 填空题判分  3 作文题判分
    //设置考试信息
    const setExamInfo = (info: any) => {
        currentExam.value = info
        // 同步更新 localStorage，保证刷新后数据不丢失
        if (info) {
            localStorage.setItem('current_exam_info', JSON.stringify(info))
        } else {
            localStorage.removeItem('current_exam_info')
        }
    }
    const unScanned=ref(0);//未扫描
    const examMissNum=ref(0);//缺考人数
    const abnormalNum=ref(0);//异常人数
    const scannedNum=ref(0);//已上传人数

    const setExamInfoCount = (info: any) => {

        unScanned.value = info.unScanned;
        examMissNum.value = info.examMissNum;
        abnormalNum.value = info.abnormalNum;
        scannedNum.value = info.scannedNum;
        localStorage.setItem('current_exam_info_count', JSON.stringify(info));//当前考试异常数据信息
    }




    // 清空考试信息
    const clearExamInfo = () => {
        currentExam.value = null
        localStorage.removeItem('current_exam_info')
    }

  return {
    currentExam,
    examId,
    examName,
    examType,
    setExamInfo,
    clearExamInfo,
    setExamInfoCount,
    unScanned,
    examMissNum,
    abnormalNum,
    scannedNum
  }
})