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,
		});
		/** 隐藏评测功能 */
		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 () => {
			const workeData = props.workeData;
			if (workeData.id) {
				evaluatingWorkData.difficulty = workeData.heardLevel;
				state.setting.evaluationDifficulty = workeData.heardLevel;
				evaluatingWorkData.evaluateSpeed = state.originSpeed;
				evaluatingWorkData.start = Number(workeData.start);
				evaluatingWorkData.end = Number(workeData.end);
				state.userChooseEndIndex = evaluatingWorkData.end
				// 设置小节
				setSection(evaluatingWorkData.start, evaluatingWorkData.end, evaluatingWorkData.evaluateSpeed);
			}
		};
		/** 添加记录 */
		const addEvaluatingWorkRecored = async (data: any) => {
			try {
				const res = await api_lessonTrainingSubmitTraining({
					studentLessonExaminationDetailId: evaluatingWorkData.evaluatingRecord,
					score: 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);
				}
			}
		);
		onMounted(() => {
			handleHide();
			getWorkData();
			// verifyMembershipServices();
		});
		expose({
			getWorkData
		})		
		return () => <div></div>;
	},
});
