import { ref, onBeforeMount, computed } from "vue";
import { useRoute, useRouter } from "vue-router";
import testModel from "@/model/test";
import { CollectionModel } from "@/model/collection";
/**获取题目列表 */
export const useSubjectList = (type: ExerciseType.ListType) => {
	type TestModelListResType = Awaited<ReturnType<typeof testModel.getList | typeof testModel.getFreeList>>;

	const subjectList = ref<TestModelListResType["list"]>([]); //题目列表
	const subjectTotal = ref(0); //题目总数
	const query = useRoute().query; //路由query参数
	const currentSubjectIndex = ref(0); //当前题目下标
	const vehicle = useRouter().currentRoute.value.query.vehicle as string; //路由qvehicle参数
	const subject = useRouter().currentRoute.value.query.subject as string; //路由subject参数

	onBeforeMount(async () => {
		let res: TestModelListResType;
		if (type == "normal" || type == "test") {
			res = await testModel.getList({
				...query,
			});
		}
		else if (type == "localWrong") {
			let wrongSet: [any] = JSON.parse(window.localStorage.getItem(vehicle + subject) || "[]")
			wrongSet.forEach(item => {
				item.userAnswer = item.questionType !== 3 ? "" : []
				item.isTrue = null
			})
			res = {
				total: wrongSet.length,
				list: wrongSet
			} as TestModelListResType
		}
		else if (type == "wrong") {
			const collectionModel = new CollectionModel(type);
			const result = await collectionModel.getList({
				carType: query.vehicle as CollectionAndWrongType.CarType,
				km: query.name as CollectionAndWrongType.Km,
				pageNum: 1,
				pageSize: 50
			});
			if (result.rows.length == 0) {
				return
			}
			result.rows.forEach(item => {
				item.isTrue = null
				if (typeof item.opts == "string") {
					item.opts = item.opts.split("-")
				}
			})
			let questionItem: Test.QuestionInfo = result.rows[0]
			let questionIndex = 0
			//排序
			if (query.questionId) {
				result.rows.forEach((item, index) => {
					if (item.id == Number(query.questionId)) {
						questionIndex = index
						questionItem = item
					}
				})
				result.rows.splice(questionIndex, 1)
				result.rows.unshift(questionItem)

			}
			res = {
				total: result.total,
				list: result.rows
			}


			// const collectionList.value.push(...result.collectionList)
			// const questionList.value.push(...result.rows)
		}
		else if (type == "collection") {
			const collectionModel = new CollectionModel(type);
			const result = await collectionModel.getList({
				carType: query.vehicle as CollectionAndWrongType.CarType,
				km: query.name as CollectionAndWrongType.Km,
				pageNum: 1,
				pageSize: 50
			});
			console.log(result.collectionList)
			if (result.rows.length == 0) {
				return
			}
			result.rows.forEach(item => {
				item.isTrue = null
				if (typeof item.opts == "string") {
					item.opts = item.opts.split("-")
				}
			})
			let questionItem: Test.QuestionInfo = result.rows[0]
			let questionIndex = 0
			//排序
			if (query.questionId) {
				result.rows.forEach((item, index) => {
					if (item.id == Number(query.questionId)) {
						questionIndex = index
						questionItem = item
					}
				})
				result.rows.splice(questionIndex, 1)
				result.rows.unshift(questionItem)

			}
			res = {
				total: result.total,
				list: result.rows
			}

		}
		else {
			res = await testModel.getFreeList({
				...query,
			});
		}

		subjectList.value = res.list;
		subjectTotal.value = res.total;
		
	});

	
	//当前题目内容
	const currentSubject = computed(() => {
		console.log(subjectList.value[currentSubjectIndex.value])
		return subjectList.value[currentSubjectIndex.value];
	});
	return {
		subjectList,
		subjectTotal,
		currentSubject,
		currentSubjectIndex,
	};
};
