<template>
	<view class="examPaper">
		<view class="examPaper_bg">
			<img src="../../../static/img/bg/exam.png">
			<view class="examPaper_header">
				<view class="examPaper_header_name">
					{{examPaper.name || ''}}
				</view>
				<view class="examPaper_header_score">
					得分:{{score || 0}}分
				</view>
			</view>
		</view>
		<view class="examPaper_content" v-for="(examItem, index) in examItemList" :key="index">
			<view class="examPaper_content_topic">
				<view class="examPaper_content_topic_label"
					:style="examItem['examItem.questionTypeLabel']==='多选'?'background-color:#F7A64A;':''">
					{{examItem['examItem.questionTypeLabel'] || ''}}题
				</view>
				<view class="examPaper_content_topic_content">
					{{index+1}}.{{examItem['examItem.stem']}}
				</view>
			</view>
			<view class="examPaper_content_checkItem" v-for="(item,ind) in examItem.result" :key="ind"
				@click="choiceItem(item.id)"
				>
				<span class="examPaper_content_checkItemAnswer" v-if="item.correct">
					<image src="../../../static/img/icon/success.png" mode=""></image>
				</span>
				<span class="examPaper_content_checkItemLabel" v-else>{{String.fromCharCode(65+ind)}}</span>
				{{item.option}}
			</view>

			<view class="examPaper_content_correctCheckItem" >
				正确答案：{{getCorrectAnswer(examItem.result)}}
			</view>
		</view>
		<view class="detailFoot">
			<button @click="back">返回</button>
			<button @click="joinExam">重新考试</button>
		</view>
	</view>
</template>

<script>
	import {
		GetDriverExamItem,
		GetDriverExamItemInfo,
		ReportExamPaperResult,
	} from '../../../api/apiList.js'
	export default {
		data() {
			return {
				id: '',
				examItemList: [],
				examPaper: {},
				result: '',
				score: 0,
			};
		},
		methods: {
			back() {
				uni.navigateBack()
			},
			getDriverExamItem() {
				GetDriverExamItem({
					"aux": {
						"id": this.id
					}
				}).then(async res => {
					this.examPaper = res.data.data.header
					const examItem = res.data.data.items.rows
					const examItemList = []
					for (const exam of examItem){
						examItemList.push(this.getDriverExamItemInfo(exam['examItem.id'], exam))
					}
					this.examItemList = await Promise.all(examItemList)
				})
			},
			getDriverExamItemInfo(id, val) {
				return GetDriverExamItemInfo({
					"aux": {
						"id": id ? id : this.examItem[this.currentIndex]['examItem.id']
					}
				}).then(res => {
					return Object.assign(val, {
						result: res.data.data.items.rows,
						choice: new Set()					})

				})
			},
			// 判断是否已进行选择
			choiceItem(itemId) {
				const result = this.examItem[this.currentIndex].result
				if (this.examItem[this.currentIndex]['choice'].has(itemId)) {
					this.examItem[this.currentIndex]['choice'].delete(itemId)

				} else {
					// 判断是否根据单选复选进行逻辑判断
					if (this.examItem[this.currentIndex]['examItem.questionTypeLabel'] === '单选') {
						this.examItem[this.currentIndex]['choice'].clear()
						this.examItem[this.currentIndex]['choice'].add(itemId)
					} else {
						this.examItem[this.currentIndex]['choice'].add(itemId)
					}
				}
				this.examItem[this.currentIndex].result = []
				this.$nextTick(() => {
					this.examItem[this.currentIndex].result = result
				})

			},
			canClickNext() {
				if (this.examItem[this.currentIndex]) {
					return this.currentIndex !== (this.examItem.length - 1) && Array.from(this.examItem[this.currentIndex]
						.choice.values()).length !== 0
				}
				return false
			},

			joinExam() {
				uni.redirectTo({
					url: `/subpkg/exam/examPaper/examPaper?id=${this.id}`
				})
			},
			getCorrectAnswer(resultList){
				let newResult = []
				for(let i = 0; i<resultList.length;i++){
					if(resultList[i].correct){
						newResult.push(String.fromCharCode(65+i))
					}
				}
				return newResult.join("、")

			},
		},
		onLoad(option) {
			const {
				id,
				result,
				score
			} = option
			this.id = id
			this.result = result
			this.score = score
			this.getDriverExamItem()
		}
	}
</script>;

<style lang="scss">
	.examPaper {
		background-color: #FAFAFA;
		min-height: 100vh;

		&_bg {
			position: relative;
			height: 357rpx;
			image {
				width: 100%;
				height: 650rpx;
			}
		}

		&_header {
			position: absolute;
			top: 0;
			left: 0;
			width: 100%;
			color: #FFFFFF;
			padding: 23rpx;

			&_name {
				margin-top: 73rpx;
				font-size: 40rpx;
				font-weight: bolder;
			}

			&_score {
				margin: 16rpx 0 97rpx 0;
				font-size: 28rpx;
			}
		}

		&_content {
			background-color: #FFFFFF;
			border-radius: 32rpx;
			transform: translateY(-32rpx);
			padding: 0 25rpx;
			padding-top: 48rpx;
			padding-bottom: 24rpx;
			margin: 24rpx;

			&_topic {
				margin-bottom: 51rpx;

				&_label {
					border-radius: 8rpx;
					padding: 4rpx 24rpx;
					color: #FFFFFF;
					font-size: 28rpx;
					margin-bottom: 24rpx;
					background-color: #66CCCC;
					display: inline-block;
				}

				&_content {
					color: #434343;
					font-size: 32rpx;
					font-weight: bolder;
				}
			}

			&_checkItem {
				margin-bottom: 32rpx;
				font-size: 28rpx;
				padding: 0 24rpx;
				line-height: 40rpx;
			}
			&_correctCheckItem {
				height: 93rpx;
				background-color: #FAFAFA;
				margin-bottom: 24rpx;
				border-radius: 8rpx;
				font-size: 32rpx;
				font-weight: bolder;
				padding: 24rpx;
				margin-top: 48rpx;
			}
			&_checkItemLabel{
				margin-right: 24rpx;
				border: 1rpx solid #BFBFBF;
				width: 40rpx;
				height: 40rpx;
				display: inline-block;
				line-height: 40rpx;
				text-align: center;
				border-radius: 20rpx;
			}
			&_checkItemAnswer{
				color: #FFFFFF;
				background-color: #27C488;
				width: 40rpx;
				height: 40rpx;
				text-align: center;
				line-height: 40rpx;
				font-size: 32rpx;
				display: inline-block;
				margin-right: 24rpx;
				border-radius: 20rpx;
				image{
					width: 38rpx;
					height: 38rpx;
				}
			}

		}

		.detailFoot {
			position: sticky;
			display: flex;
			justify-content: space-between;
			bottom: 0;
			width: 100%;
			padding-bottom: 28rpx;
			background-color: #FAFAFA;
			padding-top: 24rpx;
			z-index: 100;

			button {
				width: 339rpx;
				height: 88rpx;
				line-height: 88rpx;
				border-radius: 44rpx;
				color: #2E75E6;
				padding: 0 24rpx;
				border: 3rpx solid transparent;
				position: relative;
				background-clip: padding-box, border-box;
				background-origin: padding-box, border-box;
				background-image: linear-gradient(90deg, #FAFAFA, #FAFAFA), linear-gradient(90deg, #2E75E6, #5E58EE);

				&:after {
					content: "";
				}

				&:last-child {
					background-image: linear-gradient(90deg, #2E75E6, #5E58EE), linear-gradient(90deg, #2E75E6, #5E58EE);
					color: #FFFFFF;
				}
			}

		}

	}
</style>
