<template>
	<view>
		<view>
			<uni-icons type="left" size="30" @click="handleBack"></uni-icons>
			<text>单元测试</text>
			<view><text>{{count}}</text>/<text>{{total}}</text></view>
		</view>
		<w-swiper :list="list" :swiperHeight="300" :positionIndex="current">
			<template v-slot:default="{item}">
				<view class="body" v-if="item.mta_show">
					<danxuan :question="item" v-if="item.type == '1'"></danxuan>
					<panduan :question="item" v-if="item.type == '2'"></panduan>
					<tiankong :question="item" v-if="item.type == '3'"></tiankong>
				</view>
			</template>
		</w-swiper>
		<button class="transition-button" v-if="isLast" type="primary"
			@click="handleSubmit(uniPointsRef)">submit</button>

		<!-- 答卷 -->
		<chengji ref="chengjiRef" :list="list" :jieId="jieId" :zhangId="zhangId" :nextZhangId="nextZhangId"
			:nianji="nianji" :xueqi="xueqi"></chengji>

		<!-- 分数弹窗 -->
		<uniPointsVue ref="uniPointsRef" @checkAnswer="checkAnswer" @goStudy="goStudyContinue"></uniPointsVue>
	</view>

</template>

<script setup>
	import questionJiexi from '@/components/questionJiexi/questionJiexi.vue';
	import wSwiper from '@/components/wSwiper/wSwiper.vue';
	import danxuan from "@/components/question/danxuan.vue";
	import panduan from "@/components/question/panduan.vue";
	import tiankong from "@/components/question/tiankong.vue";
	import chengji from "@/components/chengji/chengji.vue";
	import uniPointsVue from '@/components/points/uni-points.vue';
	import {
		useStudyRouteParams
	} from "@/utils/emitEvents.js"
	import {
		useExam
	} from './useUnit';
	import {
		ref,
		onMounted
	} from "vue";

	const {setStudyStorage} = useStudyRouteParams();

	const {
		isFirst,
		isLast,
		count,
		total,
		current,
		list,
		rightAnswer,
		wrongAnswer,
		jifen,
		zhangId,
		jieId,
		nextZhangId,
		nianji,
		xueqi,

		nextQuestion,
		prevQuestion,
		handleSubmit,
		initPage
	} = useExam();

	const uniPointsRef = ref(null);
	const chengjiRef = ref(null);

	// 查看答案
	function checkAnswer() {
		chengjiRef.value.showPopup();
	}
	// 继续学习
	function goStudyContinue() {
		// 设置 从单元测试 到 岛 的路由参数
		setStudyStorage({
			nianji: nianji.value,
			xueqi: xueqi.value,
			zhangId: nextZhangId.value,
			jieId: jieId.value,
			nextZhangId: null,
		});
		uni.switchTab({
			url: `/pages/study/index`
		})
	}
	
	function handleBack() {
		// 设置 从单元测试 到 岛 的路由参数
		setStudyStorage({
			nianji: nianji.value,
			xueqi: xueqi.value,
			zhangId: zhangId.value,
			jieId: jieId.value,
			nextZhangId: nextZhangId.value,
		});
		uni.switchTab({
			url: `/pages/study/index`
		})
	}
</script>

<style lang="scss" scoped>
	.swiper-box {
		height: 200px;
	}

	.swiper-item {
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		height: 200px;
	}
</style>