<template>
	<view class="ezy-exam-page" :style="{backgroundImage: 'url(' + courseBjFun() + ')'}">
		<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">{{current}}</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" :cardId="cardId"></chengji>
		<!-- 分数弹窗 -->
		<uniPointsVue ref="uniPointsRef" @checkAnswer="checkAnswer" @goStudy="goStudyContinue" :isLastZhang="!!haveFlag" :studyFlag="studyFlag"></uniPointsVue>
		<!-- 填空 -->
		<FillItem :value="result" ref="popupRef" @blur="onBlur"></FillItem>


	</view>

</template>

<script setup>
	import FillItem from "@/components/question/FillItem.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,
	} from "@/utils/common.js"
	import {
		useExam
	} from './useUnit';
	import {
		ref,
	} from "vue";
    import cacheManager from "@/utils/cacheManager";
	import {onLoad} from "@dcloudio/uni-app"
	


	const {
		count,
		total,
		current,
		list,
		rightAnswer,
		wrongAnswer,
		jifen,
		zhangId,
		jieId,
		nianji,
		studyFlag,
		xueke,
		showTishi,
		haveFlag,
		activeZhang,
		handleSubmit,
		initPage,
		handleCloseTishi
	} = useExam();
	const cardId = Number(cacheManager.get('auth').cardId);

	const uniPointsRef = ref(null);
	const chengjiRef = ref(null);
	const popupRef= ref(null);
	const result = ref('');
	const curTiankong = ref(null);
	
	function getPopupRef() {
		return popupRef.value;
	}
	onLoad(() => {
		uni.$on('tiankong-fillItem',(val) => {
			const {index,question} = val;
			curTiankong.value = val;
			result.value = question.reply[index];
			const dom = getPopupRef();
			dom && dom.showPopup();
		})
		
	})
	function onBlur({result}) {
		if (curTiankong.value) {
			uni.$emit('tiankong-setResult', {index: curTiankong.value.index,stId:curTiankong.value.question.stId,result});
		}
		const dom = getPopupRef();
		dom && dom.handleClear();
	}
	
	// 查看答案
	function checkAnswer() {
		chengjiRef.value.showPopup();
	}
	// 继续学习
	async function goStudyContinue() {
		// 设置 从单元测试 到 岛 的路由参数
    activeZhang.value.nextZhang ? activeZhang.value.nextZhang.zhangId : null;
    cacheManager.updateObject('auth', {
      zhangId: activeZhang.value.nextZhang.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)
	}
	
	function courseBjFun() {
		switch (cardId) {
			case 1:
				return 'static/images/course/couse-shuxue-bj.png'
				break;
			case 2:
				return 'static/images/course/course-yingyu-bj.png'
				break;
			default:
				break;
		}
	}
	

</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>