<template>
	<view class="container">
		<!-- 顶部导航 -->
		<!-- <topbar :title="paper_name" :scrollTop="scrollTop"></topbar> -->
		
		<!-- <tui-navigation-bar splitLine @init="initNavigation" @change="opacityChange" :scrollTop="scrollTop" title="NavBar自定义导航栏" backgroundColor="#fff" color="#333">
			<view class="tui-header-icon" :style="{ marginTop: top + 'px' }"><tui-icon name="arrowleft" :color="opacity > 0.85 ? '#333' : '#fff'" @click="back"></tui-icon></view>
		</tui-navigation-bar> -->
		<!-- <view class="tui-header-bg"><image src="/static/componentBg.png" class="tui-header-img"></image></view> -->
		<!-- <tui-navigation-bar  @init="initNavigation" :title="paper_name" @change="opacityChange" :scrollTop="scrollTop" :isOpacity="false" backgroundColor="#fff" color="#333">
			<view class="tui-header-icon" :style="{ marginTop: top + 'px' }"><tui-icon name="arrowleft" color="#333" @click="back"></tui-icon></view>
		</tui-navigation-bar> -->
		
		<!-- 倒计时 -->
		<!-- <tui-countdown :time="limit_time" borderColor="transparent" :isColon="false" @end="endOfTime"></tui-countdown> -->
		
		<!-- 答题组件 -->
		<kz-question v-show="result == null" mode="EXAM" :title="paper_name" :questions="questions" :configs="configs" :limit_time="limit_time" @submitQuestion="submitQuestion"></kz-question>
		
		<!-- 成绩组件 -->
		<kz-grade-result v-show="result != null" :score="score" :gradeResult="result" ref="gradeResult" :examMode="exam_mode"></kz-grade-result>
		
		<login ref="login" v-on:succ="getQuestion()"></login>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 顶部栏
				scrollTop: 0,
				// 试卷
				user: this.utils.getData('user'),
				paper_id: 0,
				paper: null,
				paper_name: '试卷考试中',
				questions: [],
				configs: {},
				start_time: 0,
				limit_time: 3600,
				total: 0,
				// 考试成绩
				result: null,
				score: 0,
				// 考场
				room_id: 0,
				room_grade_id: 0,
				exam_mode: 'PAPER',
				paper_mode: 'RANDOM',
				isSubmit: false,
			}
		},
		onLoad(options) {
			this.paper_id = options.id
			this.room_id = options.room_id ? options.room_id : 0
			
			this.getQuestion()
		},
		onBackPress(e) {
			console.log('onBackPress e', e)
			if (!this.isSubmit) {
				if (e.from == "backbutton") {
					let message = '您尚未交卷，离开本页面将没有成绩哦'
					if (this.room_id) {
						message = '您尚未交卷，离开本页面将没有成绩且失去本次考试资格哦'
					}
					
					uni.showModal({
						title: '提示',
						content: message,
						success: (res) => {
							if (res.confirm) {
								uni.navigateBack({
									delta: 1
								});
							}
						}
					});
					
					return true; //阻止默认返回行为
				}
			}
		},
		onPageScroll(e) {
			this.scrollTop = e.scrollTop;
		},
		methods: {
			// 获取试卷试题
			getQuestion() {
				this.http('paper/getExamQuestion', {
					paper_id: this.paper_id,
					room_id: this.room_id
				}, 'get').then(res => {
					if (res.code == 0) {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
						
						setTimeout(() => {
							const pages = getCurrentPages();
							if (pages && pages.length > 0) {
								const firstPage = pages[0];
								if (pages.length == 1 && (!firstPage.route || firstPage.route != "pages/index/index")) {
									uni.reLaunch({
										url: "/pages/index/index",
									});
								} else {
									uni.navigateBack({
										delta: 1,
									});
								}
							} else {
								uni.reLaunch({
									url: "/pages/index/index",
								});
							}
						}, 2000)
						
						return
					}
					
					this.paper = res.data.paper
					this.paper_name = this.paper.title
					this.limit_time = this.paper.limit_time
					this.start_time = res.data.start_time
					this.room_grade_id = res.data.room_grade_id
					this.paper_mode = this.paper.mode
					
					let questions = res.data.questions
					for (let i in questions) {
						questions[i]['code2'] = false
						questions[i]['score'] = this.getSingleScore(questions[i].kind, questions[i].difficulty, questions[i])
					}
					
					this.questions = questions
					this.total = questions.length
					this.configs = this.paper.configs
					
					console.log('total', this.total)
				})
			},
			// 交卷
			submitQuestion(paperData) {
				console.log('paperData', paperData)
				if (this.isSubmit) {
					return
				}
				
				uni.showLoading({
					title:'交卷中'
				})
				
				let data = {
					paper_id: this.paper.id,
					start_time: this.start_time,
					questions: paperData.questions,
					room_id: this.room_id,
					room_grade_id: this.room_grade_id,
				}
				
				this.http('paper/submit', data, 'post').then(res => {
					this.result = res
					this.score = res.score
					this.isSubmit = true
				})
			},
			// 计算试题分数
			getSingleScore(kind, difficulty, question) {
				if (this.paper_mode == 'FIX') {
					return question.score
				}
				
				const configs = this.paper.configs[kind.toLowerCase()]
				if (configs && configs['use_difficulty']) {
					return configs['difficulty'][difficulty.toLowerCase()]['score']
				}
				return configs['score']
			},
			
		}
	}
</script>

<style>
	page {
		height: 100%;
	}
	.container {
		padding-bottom: 120rpx;
		box-sizing: border-box;
	}
	.tui-header-icon {
		width: 100%;
		position: fixed;
		top: 0;
		padding: 0 12rpx;
		display: flex;
		align-items: center;
		height: 32px;
		transform: translateZ(0);
		z-index: 99999;
		box-sizing: border-box;
	}
</style>
