<template>
	<view class="wordTest">
		<van-skeleton title row="10" v-if="loading" />
		<van-empty description="暂无数据" v-if="nodata"/>
		<swiper class="swiper" @change="changeSwiper" :current="curTest" v-if="!loading&&!nodata">
			<swiper-item v-for="(item, index) in reviewGroupList" :key="index" class="swiper-item" v-if="item">
				<Choice v-if="item.QuestionTypeId == 1" :item="item" :num="index+1" :total="reviewGroupList.length+prepGroupList.length" @next="next($event, item)"></Choice>
				<FillIn v-if="item.QuestionTypeId == 3" :item="item" :num="index+1" :total="reviewGroupList.length+prepGroupList.length" @next="next($event, item)"></FillIn>
			</swiper-item>
			<swiper-item v-for="(item, index) in prepGroupList" :key="index" class="swiper-item" v-if="item">
				<Choice v-if="item.QuestionTypeId == 1" :item="item" :num="index+1+reviewGroupList.length" :total="reviewGroupList.length+prepGroupList.length" @next="next($event, item)"></Choice>
				<FillIn v-if="item.QuestionTypeId == 3" :item="item" :num="index+1+reviewGroupList.length" :total="reviewGroupList.length+prepGroupList.length" @next="next($event, item)"></FillIn>
			</swiper-item>
			<swiper-item>
				<van-popup :show="true" position="center" custom-style="background-color: transparent;">
					<van-loading size="50rpx" type="spinner" color="#ffffff"> <text style="color:#ffffff">交卷中...</text>
					</van-loading>
				</van-popup>
			</swiper-item>
		</swiper>
	</view>
</template>

<script>
	import {
		ref,
		reactive,
		toRefs,
		getCurrentInstance,
		onMounted
	} from "vue";
	import {
		getCurrentTime
	} from '../../utils/date.js'
	import Choice from "../../components/word/choiceQuestion.vue";
	import FillIn from "../../components/word/fillInTheBlanks.vue";
	export default {
		components: {
			Choice,
			FillIn,
		},
		setup(props) {
			let {
				proxy
			} = getCurrentInstance();
			let data = reactive({
				msg: {
					CourseId: 0,
					ChapterId: 0,
					NextChapterId: 0,
				},
				startTime: "",
				curTest: 0,
				reviewGroupList: [],
				prepGroupList: [],
				loading: false,
				nodata: false,
			});
			let methods = {
				getList() {
					data.loading = true
					proxy
						.$request("/AppletWords/CreateCourseExam", data.msg)
						.then((res) => {
							data.loading = false
							data.reviewGroupList = res.Data.reviewGroupList;
							data.prepGroupList = res.Data.prepGroupList;
							if (data.reviewGroupList.length === 0 && data.reviewGroupList.length == 0) {
								data.nodata = true
							}
						});
				},
				changeSwiper(val) {
					data.curTest = val.detail.current;
					const total = data.reviewGroupList.length + data.prepGroupList.length
					let obj = {
						CourseId:data.msg.CourseId,
						ChapterId:data.msg.ChapterId,
						NextChapterId:data.msg.NextChapterId,
						index:data.curTest
					}
					uni.setStorageSync("testData", obj);
					if (data.curTest == total) {
						uni.removeStorageSync("testData");
						methods.submitWordsTest()
					}
				},
				next(val, item) {
					if (val) {
						item.IsTrue = val;
					}
					data.curTest += 1;
				},
				//提交单词测试
				submitWordsTest() {
					const indexData = uni.getStorageSync("indexData");
					let postMsg = {
						ClassId: indexData.ClassId,
						CourseId: data.msg.CourseId,
						ChapterId: data.msg.ChapterId,
						ExamStartTime: data.startTime,
						Details: data.reviewGroupList,
					};
					proxy.$request("/AppletWords/SubmitWordsExam", postMsg).then((res) => {
						const obj = {
							score: res.Data.Score,
							time: res.Data.ExamMinutes
						}
						uni.navigateTo({
							url: "/pages/word/studyComplete?data=" + encodeURIComponent(JSON.stringify(
								obj))
						})
					});
				},
			};
			onMounted(() => {
				data.startTime = getCurrentTime();
				let testObj = uni.getStorageSync('testData');
				if(testObj){
					if((data.msg.CourseId==testObj.CourseId)&&(data.msg.ChapterId==testObj.ChapterId)&&(data.msg.NextChapterId==testObj.NextChapterId)){
						uni.showModal({
							title: '提示',
							content: `是否继续上次测试`,
							success: function(res) {
								if (res.confirm) {
									data.curTest = testObj.index;
								} else if (res.cancel) {
									data.curTest = 0;
								}
							}
						});
					}
				}
			});
			let that = methods;
			return {
				...toRefs(data),
				...methods,
			};
		},
		onLoad(options) {
			uni.setNavigationBarTitle({
				title: "单词测试",
			});
			const userInfo = uni.getStorageSync("userinfo");
			this.msg.CourseId = options.CourseId;
			this.msg.ChapterId = options.ChapterId;
			this.msg.NextChapterId = options.NextChapterId;
			this.getList();
		},
		onShow() {
			const total = this.reviewGroupList.length + this.prepGroupList.length
			if(this.curTest==total&&this.curTest!==0){
				this.curTest = total - 1
			}
		}
	};
</script>

<style scoped>
	.wordTest {
		min-height: 100vh;
		background-color: #f5f5f5;
	}

	.wordTest .swiper {
		min-height: 100vh;
		background-color: #f5f5f5;
	}
</style>
