import { defineStore } from 'pinia'
import subjectApi from '@/api/exam/paper/subject.js'
import tool from '@/utils/tool'
const funPaperType = function (type) {
	return tool.dictList(type).map((item) => {
		return {
			key: Number(item.value),
			value: item.label
		}
	})
}
const format = function (array, key) {
	for (let item of array) {
		if (item.key === key) {
			return item.value
		}
	}
	return null
}

export const useExamStore = defineStore('exam', {
	state: () => ({
		// 学科相关
		subjects: [],
		// 枚举相关
		user: {
			bankTypeEnum: funPaperType('BANK_TYPE'),
			sexEnum: [
				{ key: 1, value: '男' },
				{ key: 2, value: '女' }
			],
			statusEnum: [
				{ key: 1, value: '启用' },
				{ key: 2, value: '禁用' }
			],
			levelEnum: [
				{ key: 1, value: '一年级' },
				{ key: 2, value: '二年级' },
				{ key: 3, value: '三年级' },
				{ key: 4, value: '四年级' },
				{ key: 5, value: '五年级' },
				{ key: 6, value: '六年级' },
				{ key: 7, value: '初一' },
				{ key: 8, value: '初二' },
				{ key: 9, value: '初三' },
				{ key: 10, value: '高一' },
				{ key: 11, value: '高二' },
				{ key: 12, value: '高三' }
			],
			roleEnum: [
				{ key: 1, value: '学生' },
				{ key: 2, value: '教师' },
				{ key: 3, value: '管理员' }
			],
			statusTag: [
				{ key: 1, value: 'success' },
				{ key: 2, value: 'danger' }
			],
			statusBtn: [
				{ key: 1, value: '禁用' },
				{ key: 2, value: '启用' }
			],
			message: {
				readTag: [
					{ key: true, value: 'success' },
					{ key: false, value: 'warning' }
				],
				readText: [
					{ key: true, value: '已读' },
					{ key: false, value: '未读' }
				]
			}
		},
		exam: {
			examPaper: {
				paperTypeEnum: funPaperType('PAPER_TYPE')
			},
			examPaperAnswer: {
				statusEnum: [
					{ key: 1, value: '待批改' },
					{ key: 2, value: '完成' }
				],
				statusTag: [
					{ key: 1, value: 'warning' },
					{ key: 2, value: 'success' }
				]
			},
			question: {
				typeEnum: [
					{ key: 1, value: '单选题' },
					{ key: 2, value: '多选题' },
					{ key: 3, value: '判断题' },
					{ key: 4, value: '填空题' },
					{ key: 5, value: '简答题' }
				],
				editUrlEnum: [
					{ key: 1, value: './edit/single-choice.vue', name: '单选题' },
					{ key: 2, value: './edit/multiple-choice.vue', name: '多选题' },
					{ key: 3, value: './edit/true-false.vue', name: '判断题' },
					{ key: 4, value: './edit/gap-filling.vue', name: '填空题' },
					{ key: 5, value: './edit/short-answer.vue', name: '简答题' }
				],
				answer: {
					doRightTag: [
						{ key: true, value: 'success' },
						{ key: false, value: 'danger' },
						{ key: null, value: 'warning' }
					],
					doRightEnum: [
						{ key: true, value: '正确' },
						{ key: false, value: '错误' },
						{ key: null, value: '待批改' }
					],
					doCompletedTag: [
						{ key: false, value: 'info' },
						{ key: true, value: 'success' }
					]
				}
			}
		}
	}),
	getters: {
		// 枚举格式化
		enumFormat: (state) => (array, key) => format(array, key),
		levelEnum: (state) => state.user.levelEnum,
		paperTypeEnum: (state) => state.exam.examPaper.paperTypeEnum,
		questionTypeEnum: (state) => state.exam.question.typeEnum,
		// 学科格式化
		subjectEnumFormat: (state) => (key) => {
			for (let item of state.subjects) {
				if (item.id === key) {
					return item.name + ' ( ' + item.levelName + ' )'
				}
			}
			return null
		},
		getLevelEnum: (state) => state.user.levelEnum,
		getBankTypeEnum: (state) => state.user.bankTypeEnum
	},
	actions: {
		async initSubject(action) {
			const re = await subjectApi.list()
			this.subjects = re
			if (action !== undefined) {
				action()
			}
		}
	}
})
