<template>
	<div class="testPaper">
		<div class="content">
			<div class="paper-left">
				<div class="paperName">{{this.paperName}}</div>
				<el-divider></el-divider>
				<div class="bigBox">
					<div class="operating" v-for="(item,index) in this.paperData.questionsInfoVos">
						<div class="title">
							<span>{{index+1}}、</span>
							<span>[{{item.typeName}}] </span>
							<span v-html="item.question"></span>
							<span>({{item.score}}分)</span>
						</div>
						<div class="operating_content">
							<el-radio-group v-model="item.result" v-if="item.type==1" @change="changeResult">
								<el-radio :label="x.optionKey" v-for="x in item.options">{{x.optionKey}}、<span
										v-html="x.optionContent"></span></el-radio>
							</el-radio-group>
							<el-checkbox-group v-model="item.result" v-if="item.type==2" @change="changeResult">
								<el-checkbox :label="x.optionKey" v-for="x in item.options">{{x.optionKey}}、<span
										v-html="x.optionContent"></span></el-checkbox>
							</el-checkbox-group>
							<el-radio-group v-model="item.result" v-if="item.type==3" @change="changeResult">
								<!-- <el-radio :label="x.optionKey" v-for="x in item.options" > <span v-html="x.optionContent"></span><i :class="el-icon-check"></i></el-radio> -->
								<el-radio label="1">对</el-radio>
								<el-radio label="0">错</i></el-radio>
							</el-radio-group>
							<el-input v-model="item.result" v-if="item.type==4" placeholder="请输入答案"
								@blur="changeResult(item.result)"></el-input>
							<el-input v-model="item.result" v-if="item.type==5" type="textarea" :rows="3"
								@blur="changeResult(item.result)" placeholder="请输入答案">
							</el-input>
							<el-input v-model="item.result" v-if="item.type==6||item.type==7" type="textarea" :rows="3"
								@blur="changeResult(item.result)" placeholder="请输入答案">
							</el-input>
						</div>
					</div>
				</div>
			</div>
			<div class="paper-right">
				<div class="block">
					<el-avatar :size="50" :src="circleUrl"></el-avatar>
				</div>
				<div class="information">
					<span>姓名：{{ $store.state.login.currentUser.username}}</span>
					<el-divider></el-divider>
					<div class="second">
						<div>{{ `${hr}:${min}: ${sec}` }}</div>
						<div>
							<i class="el-icon-alarm-clock" style="color: #2BA5FC;"></i>总剩余时间
						</div>
						<div>总题数:{{this.paperData.totalNumber}}</div>
						<div>总分数:{{this.paperData.totalScore}}</div>
					</div>
					<el-divider></el-divider>
					<div class="third">
						<div>题卡</div>
						<div style="maxHeight:40vh;overflowY:auto">
							<el-tag class="tagBox" :class="{'isTag':(t.result.length!==0&&t.result!=='')}" type="info"
								v-for="(t,ti) in this.paperData.questionsInfoVos">{{ti+1}}
							</el-tag>
						</div>
					</div>
					<el-divider></el-divider>
					<div class="fourth">
						<el-button type="primary" @click="submitPaper">提交试卷</el-button>
						<el-button type="info" @click="$router.push({
								name: 'mnsjMenu'
							})">返回</el-button>
					</div>
				</div>
			</div>
		</div>
	</div>
</template>

<script>
import {TableMixin} from '@/mixins/tableMixin'
import dictUtils from '@/components/dict/DictSelectUtils.js'
import mnsjMenu from '@/api/examination/mnsjMenu.js'

export default {
		mixins: [TableMixin],
		name: 'formalTestPaper',
		components: {},
		data() {
			return {
				circleUrl: "https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png", //头像
				hr: 1,
				min: 60,
				sec: 0,
				radio: '',
				checkList: [],
				radio3: '',
				input: '',
				textarea: '',
				end: null,
				paperData: {}, //试卷数据
				topicList: [], //题目类型
				formalType: false, //正式考试
				param: {
					status: 0,
					recordId: null,
					questionOptionList: []
				},
				userKey: null,
				paperName: null,
				newParams: {

				}
			}
		},
		created() {
			this.topicList = dictUtils.getDictItemsFromCache('sttx')
		},
		methods: {
			loadData() {
				if (this.$route.params.id !== {} && typeof(this.$route.params.id) === 'string') {
					this.userKey = this.$route.params.id
					this.paperName = this.$route.params.name
					sessionStorage.setItem('userkey', this.userKey) //初次收到userKey，存起来
					sessionStorage.setItem('paperName', this.paperName) //初次收到userKey，存起来
				} else {
					if (sessionStorage.getItem('userkey')) {
						this.userKey = sessionStorage.getItem('userkey') //不是第一次了，取出来
						this.paperName = sessionStorage.getItem('paperName') //不是第一次了，取出来
					} else {
						//如果实在是没有了，就直接去store里面取
						this.userKey = store.state.user.userInfo.userKey
				 	this.paperName = store.state.user.userInfo.paperName
					}
				}

				let params = {
					recordId: "",
					userId: '',
					paperId: this.userKey
				}
				mnsjMenu.startExam(params, res => {
					if (res) {
						let minutes = (res.timeLength / 1000) / 60;
						this.end = Date.parse(new Date()) + ((1 * minutes * 60)) * 1000
						this.countdown()
						console.log(res);

						// return
						res.questionsInfoVos.forEach(e => {
							if (e.result) {
								console.log(e.result);
								if (e.type == 2) {
									let arr = JSON.parse(JSON.stringify(e.result.split(",")))
									e.result = []
									arr.forEach(re => {
										e.result.push(re)
									})
									console.log(e.result);
								}
							} else {
								if (e.type == 2) {
									e.result = []
								} else {
									e.result = ''
								}
							}


							this.topicList.forEach(t => {
								if (e.type == t.value) {
									e.typeName = t.name
								}
							})
						})
						this.paperData = res
						console.log(this.paperData);
						this.param.recordId = res.recordId
					}
				})


			},
			countdown() {
				const now = Date.parse(new Date())
				let msec = this.end - now

				if (msec < 0) return;
				let day = parseInt(msec / 1000 / 60 / 60 / 24)
				let hr = parseInt(msec / 1000 / 60 / 60 % 24)
				let min = parseInt(msec / 1000 / 60 % 60)
				let sec = parseInt(msec / 1000 % 60)
				this.day = day
				this.hr = hr > 9 ? hr : '0' + hr
				this.min = min > 9 ? min : '0' + min
				this.sec = sec > 9 ? sec : '0' + sec
				const that = this
				if (hr >= 0 && min >= 0 && sec >= 0) {
					//倒计时结束关闭订单
					if (hr == 0 && min == 0 && sec == 0) {
							this.$message({
								type: 'warning',
								message: '考试时间结束'
							});
						return
					}
					setTimeout(function() {
						that.countdown()
					}, 1000)
				}
			},
			changeResult(val) {
				console.log(val);
				if (val.length > 0 || val != '') {
					this.param.status = 0
					this.newParams = {}
					this.param.questionOptionList = []
					this.newParams = JSON.parse(JSON.stringify(this.paperData))
					this.newParams.questionsInfoVos.forEach(e => {
						if (e.type == 2) {
							let ts = ""
							if (e.result.length > 0) {
								e.result.forEach((r, ri) => {
									if (ri == 0) {
										ts = r
									} else {
										ts += ',' + r
									}
								})
							}

							e.result = ts
						}
						this.param.questionOptionList.push({
							questionId: e.questionId,
							result: e.result

						})
					})
					mnsjMenu.submitAnswer(this.param, res => {
						if (res.state) {

						}
					})
				}
			},
			//提交试卷
			submitPaper() {
				this.param.questionOptionList=[]
				this.paperData.questionsInfoVos.forEach(e => {
					if (e.type==2) {
						let ts = ""
						e.result.forEach((r, ri) => {
							if (ri == 0) {
								ts = r
							} else {
								ts += ',' + r
							}
						})
						e.result = ts
					}
					this.param.questionOptionList.push({
						questionId: e.questionId,
						result: e.result

					})
				})
				this.$confirm('此操作将提交试卷, 是否继续?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					this.param.status = 1
					mnsjMenu.submitAnswer(this.param, res => {
						if (res.state) {
							this.$message({
								type: 'success',
								message: '提交成功'
							});
							this.$router.push({
								name: 'mnsjMenu'
							})
						}
					})

				}).catch(() => {
					this.$message({
						type: 'info',
						message: '取消提交'
					});
				});

				// this.$router.push({
				// 	name: 'mnsjMenu'
				// })
				// this.$router.go(-1)
			}
		},
		//自动保存
		autosave() {
			console.log(1);


		}

	}
</script>

<style>
	.testPaper p {
		display: inline !important;
	}
</style>
<style lang="scss" scoped>
	.testPaper {
		background-color: #EBEEF5;
		display: flex;
		justify-content: space-evenly;

		.content {
			width: 1200px;
			display: flex;

			.paper-left {
				padding: 10px 20px;
				box-sizing: border-box;
				background-color: #FFFFFF;
				width: 80%;
				margin-right: 10px;

				.paperName {
					margin-top: 20px;
					text-align: center;
					font-size: 24px;
				}

				.operating {
					width: 100%;
					margin: 10px 0;

					.title {
						padding: 20px 0;

						span:nth-child(1) {
							font-weight: bold;
						}

						span:nth-child(2) {
							color: #34A9FD;
						}
					}
				}
			}

			.paper-right {
				background-color: #FFFFFF;
				padding: 0 20px;
				width: 20%;

				.block {
					height: 120px;
					display: flex;
					align-items: center;
					justify-content: space-evenly;
				}

				.information {
					.second {
						text-align: center;

						div:nth-child(1) {
							font-size: 26px;
							color: #34A9FD;
						}

						div:nth-child(4) {
							font-weight: 700;
							line-height: 30px;
						}

						div:nth-child(3) {
							margin-top: 20px;
							font-weight: 700;
							line-height: 30px;
						}
					}

					.third {
						// text-align: center;

						div:nth-child(1) {
							height: 30px;
						}
					}

					.fourth {
						text-align: center;
					}
				}
			}
		}

	}

	.tagBox {
		width: 40px;
		height: 35px;
		line-height: 35px;
		margin-right: 5px;
		text-align: center;
		margin-bottom: 5px;
	}

	.isTag {
		background-color: #36AAFD;
		color: #FFFFFF;
	}

	.bigBox {
		height: calc(100% - 100px);
		overflow-y: auto;
	}
</style>
