import * as httpApi from "@/api/chanpinZiRanPinDu.js"
import {
	reactive,
	watch
} from "vue"
import {
	onLoad
} from "@dcloudio/uni-app"
import {
	catchError,
	toast
} from "@/utils/common.js"
import cacheManager from "@/utils/cacheManager.js";


export function usePinduTest(handleSeeResult,handleSeeResultClose) {


	const data = reactive({
		list: [],
		total: 0,
		current: 0,
		jieId: null,
		rightAnswer: 0, // 答对
		wrongAnswer: 0, // 答错
	})

	watch(() => data.list, (val) => {
		const list = data.list.filter(item => {
			if (item.type == 3) {
				// 填空题 所有试题答完
				return !item.reply.some(citem => citem.trim() == '');
			} else {
				return item.reply !== null
			}
		});
		data.count = list.length;
	}, {
		deep: true
	})
	// 更新试题对错
	function updateRightWrong({
		rightAnswer,
		wrongAnswer,
	}) {
		data.rightAnswer = rightAnswer;
		data.wrongAnswer = wrongAnswer;
	}

	// 初始化页面数据
	async function initPage() {
		const [err, cdata] = await catchError(httpApi.getPinduChanpinShitiList({
			jieId: data.jieId
		}))

		if (err) {
			toast("单元测试数据获取异常");
			return;
		}

		refreshExam(cdata);
	}

	function formatListToUse(list) {
		list.forEach((item, index) => {
			item.mta_show = false;
			item.reply = null;
			if (item.type == 3) {
				item.result = JSON.parse(item.result);
				item.placeholders = item.result.map((item, cindex) => `[bank${cindex+1}]`)
				item.reply = item.reply ? JSON.parse(item.reply) : item.result.map(() => '');
			}
			if (item.type == 4) {
				// 特殊题型英语题
				const audioList = item.audios ? item.audios.split(',') : [];
				item.placeholders = audioList.map((item, cindex) => `[yingyu${cindex+1}]`)
				item.audioList = audioList;
			}
		})
	}


	// 数据赋值
	function refreshExam(list) {
		const cList = list;
		formatListToUse(cList)
		data.list = cList;
		data.total = cList.length;
	}

	// 交卷
	async function handleSubmit() {

		const result = [];
		data.list.forEach(item => {
			if (item.type == 1) {
				result.push({
					reply: item.reply,
					stId: item.stId
				})
			} else if (item.type == 2) {
				result.push({
					reply: item.reply,
					stId: item.stId
				})
			} else if (item.type == 3) {
				result.push({
					reply: item.reply ? JSON.stringify(item.reply) : '',
					stId: item.stId
				})
			} else if (item.type == 4) {
				result.push({
					reply: item.reply,
					stId: item.stId
				})
			}

		})
		uni.showLoading({
			title: '交卷中...'
		});
		const [error, cdata] = await catchError(httpApi.getPinduChanpinShitiSave({
			jieId: data.jieId,
			shitiList: result
		}));
		uni.hideLoading()
		if (error) {
			toast("单元测试数据提交异常");
			return;
		}
		// 更新单元测试状态
		cacheManager.updatePinduWanchengStatus(data.jieId)
		// 执行跳页
		uni.$emit('unitPinduTest-submit', {
			rightAnswer: cdata.dui,
			wrongAnswer: cdata.cuo,
		})

		handleSeeResult()
	}

	onLoad((options) => {
		data.jieId = options.jieId
		initPage()
	})

	function resetStart() {
		data.list = [];
		data.total = 0;
		data.current= 0;
		data.rightAnswer = 0; // 答对
		data.wrongAnswer = 0; // 答错
		handleSeeResultClose();
		initPage();
	}


	return {
		data,
		handleSubmit,
		updateRightWrong,
		resetStart
	}
}

