<template>
	<view class="ezy-exam-page">
		<view class="ezy-navBar-box">
			{{haveFlag}}
			<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'" :placeholders="item.placeholders"></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"  @click="handleCloseTishi" v-if="showTishi">
			<view class="exam-tip-box">左右滑动查看更多题目</view>
		</view>
		

		<!-- 答卷 -->
		<chengji ref="chengjiRef" :list="list"  @back="handleBack"></chengji>
		<!-- 分数弹窗 -->
		<uniPointsVue ref="uniPointsRef" @checkAnswer="checkAnswer" @goStudy="goStudyContinue" :isLastZhang="!!haveFlag"></uniPointsVue>
	</view>

</template>

<script setup>
	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,
	} from "@/utils/common.js"
	import {
		useExam
	} from './useUnit';
	import {
		ref,
	} from "vue";
    import cacheManager from "@/utils/cacheManager";
  
	const {
		count,
		total,
		current,
		list,
		rightAnswer,
		wrongAnswer,
		jifen,
		zhangId,
		jieId,
		nianji,
		xueke,
		showTishi,
		haveFlag,
		handleSubmit,
		initPage,
		handleCloseTishi
	} = useExam();

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

	// 查看答案
	function checkAnswer() {
		chengjiRef.value.showPopup();
	}
	// 继续学习
	async function goStudyContinue() {
		// 设置 从单元测试 到 岛 的路由参数
		
		const [err, data] = await catchError(httpUnit.getnextZhangInfo({
			nianji: nianji.value,
			cardId: xueke.value,
			zhangId:  zhangId.value
		}))
    cacheManager.updateObject('auth', {
      zhangId: data.zhangId,
    })
		uni.redirectTo({
			url: `/pages/study/index`
		})
	}
	
	
	
	function handleBack() {
		// 数学
		uni.redirectTo({
			url: `/pages/study/index`
		})
	}
	
	function onSwiperChange(index) {
		current.value = index;
		uni.$emit('swiper-change', 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>