import { defineComponent, onMounted, reactive, watch } from "vue";
import { useRoute } from "vue-router";
// import { verifyMembershipServices } from "../vip-verify";
import { api_lessonTrainingSubmitTraining } from "../../api";
import state, { IDifficulty, handleSetSpeed, hanldeDirectSelection, setSection } from "/src/state";
import { getQuery } from "/src/utils/queryString";
import { evaluatingData } from "/src/view/evaluating";

export default defineComponent({
	name: "EvaluatingWork",
	props: {
		workeData: {
			type: Object,
			default: () => ({}),
		},
	},
	setup(props, { expose }) {
		const query = getQuery();
		const evaluatingWorkData = reactive({
			difficulty: "" as IDifficulty,
			evaluatingRecord: props.workeData?.id,
			start: "" as any,
			end: "" as any,
			evaluateSpeed: 0,
			score: 0, // 评测作业及格分数
		});
		/** 隐藏评测功能 */
		const handleHide = () => {
			const ids = ["studnetT-0"];
			for (let i = 0; i < ids.length; i++) {
				const speedBtn = document.getElementById(ids[i]);
				if (speedBtn) {
					speedBtn.style.pointerEvents = "none";
					speedBtn.style.opacity = ".5";
				}
			}
		};
		/** 获取作业详情 */
		const getWorkData = async () => {
			let trainingContent: any = {};
			try {
				trainingContent = JSON.parse(props.workeData.trainingContent);
			} catch (error) {
				console.log("🚀 ~ error:", error);
			}
			if (["BEGINNER", "ADVANCED", "PERFORMER"].includes(trainingContent.evaluateDifficult)) {
				evaluatingWorkData.difficulty = trainingContent.evaluateDifficult;
				state.setting.evaluationDifficulty = trainingContent.evaluateDifficult;

				evaluatingWorkData.evaluateSpeed = trainingContent.evaluateSpeed;
				evaluatingWorkData.start = Number(trainingContent.practiceChapterBegin);
				evaluatingWorkData.end = Number(trainingContent.practiceChapterEnd);
				evaluatingWorkData.score = Number(trainingContent.trainingTimes);
				state.userChooseEndIndex = evaluatingWorkData.end
				state.isFullEvaluatWork = (evaluatingWorkData.end - evaluatingWorkData.start) + 1 === state.totalMeasureNumber;
				// 设置小节
				setSection(evaluatingWorkData.start, evaluatingWorkData.end, evaluatingWorkData.evaluateSpeed);
			}
		};
		/** 添加记录 */
		const addEvaluatingWorkRecored = async (data: any) => {
			try {
				const res = await api_lessonTrainingSubmitTraining({
					id: evaluatingWorkData.evaluatingRecord,
					trainingTimes: data?.score || 0,
					recordId: evaluatingData.resultData.recordId
				});
			} catch (error) {
				console.log(error);
			}
		};
		watch(
			() => evaluatingData.resultData.recordId,
			() => {
				// 评测作业，完整评测才调用保存作业接口
				if (evaluatingData.resulstMode && evaluatingData.resultData.recordId && evaluatingData.isComplete) {
					addEvaluatingWorkRecored(evaluatingData.resultData);
					// 完整评测，判断当前的评测分数是否已达标，没有达标，点击返回按钮需要给出弹窗提示
					if (!state.isWorkDone) {
						state.isWorkDone = Number(evaluatingData.resultData?.score) >= evaluatingWorkData.score;
					}
				}
			}
		);
		onMounted(() => {
			handleHide();
			getWorkData();
			// verifyMembershipServices();
		});
		expose({
			getWorkData
		})		
		return () => <div></div>;
	},
});
