<template>
	<view class="ezy-exam-page">
		<view class="ezy-navBar-box">
			<view @click="handleBack" class="nav-bar-icon"></view>
			<text class="nav-bar-title">单元测试</text>
			<view class="nav-bar-other"><text class="key-note">{{count}}</text>/<text>{{total}}</text></view>
		</view>
		
		<view class="shiti-frame-box">
			<w-swiper :list="list" :current="current" class="ezy-exam-swiper" @change="onSwiperChange">
				<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>
			<view class="exam-submit-btn" v-if="current === list.length-1" 
				@click="handleSubmit(uniPointsRef)"></view>
		</view>

		<!--  左右滑动  -->
		<view class="tip-mask-box">
			<view class="exam-tip-box" @click="handleCloseTishi" v-if="showTishi">左右滑动查看更多题目</view>
		</view>
		

		<!-- 答卷 -->
		<chengji ref="chengjiRef" :list="list" :jieId="jieId" :zhangId="zhangId"
			: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 * as httpUnit from "@/api/unitTest.js"
	import {
		catchError,
		toast
	} from "@/utils/common.js"
	import {
		useStudyRouteParams
	} from "@/utils/emitEvents.js"
	import {
		useExam
	} from './useUnit';
	import {
		ref,
		onMounted
	} from "vue";

	const {setStudyStorage} = useStudyRouteParams();
	
	const showTishi = ref(true);

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

		handleSubmit,
		initPage
	} = useExam();

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

	// 查看答案
	function checkAnswer() {
		chengjiRef.value.showPopup();
	}
	// 继续学习
	async function goStudyContinue() {
		// 设置 从单元测试 到 岛 的路由参数
		
		const [err, data] = await catchError(httpUnit.getnextZhangInfo({
			nianji: nianji.value,
			xueqi: xueqi.value,
			zhangId:  zhangId.value
		}))
		
		uni.navigateTo({
			url: `/pages/study/index?nianji=${nianji.value}&xueqi=${xueqi.value}&zhangId=${data.zhangId}&jieId=${jieId.value}`
		})
	}
	
	function handleBack() {
		uni.navigateTo({
			url: `/pages/study/index?nianji=${nianji.value}&xueqi=${xueqi.value}&zhangId=${zhangId.value}&jieId=${jieId.value}`
		})
	}
	
	function onSwiperChange(index) {
		current.value = index;
	}
	
	// 大鹅关闭追加缓存 --- 单独针对当前手机的缓存提示
	function handleCloseTishi() {
		uni.setStorageSync('isShowTishi','has');
		showTishi.value = false;
	}
	// 大鹅显示追加缓存 --- 单独针对当前手机的缓存提示
	function handleShowTishi() {
		const isNotShow = Boolean(uni.getStorageSync('isShowTishi'));
		showTishi.value = !isNotShow;
	}
</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>