<template>
	<view>
		<nav-bar title="做题记录" class="nav"></nav-bar>
		
		<view class="record">
			<view class="item" v-for="(record,index) in recordList" :key="index">
				<view class="note">{{ record.paperName }}</view>
				<view class="title">{{ record.examName }}</view>
				<view class="desc">
					<view>
						<image src="/static/icon/wk_icon2.png"></image>
						<text>{{ $method.timestampToTime(record.updateTime, false) }}</text>
					</view>
					<view>
						<image src="/static/icon/wk_icon2.png"></image>
						<text>总共 {{ record.totalQuestionNum }} 题 做对 {{ record.rightQuestionNum }} 题</text>
					</view>
				</view>
				<view class="btns" v-if="record.doMode != 3">
					<view class="btn" v-if="record.doMode == 1 && record.status == 1" @click="doRepeat(record.goodsId, record)">重做</view>
					<view class="btn" @click="questionBankExplain(record)" v-if="record.status == 1">解析</view>
					<view class="btn" @click="questionReport(record)" v-if="record.status == 1">报告</view>
					<view class="btn continue" @click="doContinue(record,index)" v-if="courseType != 2 && record.status == 0 && record.historyExamJson">继续答题</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			index: 0,
			list: [],
			list1: [],
			recordList: [],
			goodsData: {},
			param: {
				moduleExamId:0,
				chapterExamId:0,
				examId:0,
				goodsId:0,
				pageNum: 1,
				pageSize: 10
			},
			isRepeat:false,
			total: 0,
			activeIndex: 0,
			typeIndex:0,
			itemIndex:'',
			courseType: 1, // 2视频试卷
			record: {
				examId: '',
				gradeId: '',
				chapterId: '',
				moduleId: '',
				courseId: '',
				sectionId: '',
			},
		};
	},
	onLoad(option) {
		this.courseType = option.courseType
		this.param.moduleExamId = option.moduleId,
		this.param.chapterExamId = option.chapterId,
		this.param.examId = option.examId,
		this.param.goodsId = option.goodsId
		if (this.courseType == 2) {
			const { examId, gradeId, chapterId, moduleId, courseId, sectionId} = option
			this.record = {
				examId: +examId,
				gradeId: +gradeId,
				chapterId: +chapterId,
				moduleId: +moduleId,
				courseId: +courseId,
				sectionId: +sectionId,
			}
			this.recordBankList()
		} else {
			this.getExamRecordList();
		}
		
	},
	onPullDownRefresh() {
		let that = this;
		this.param = {
			moduleExamId:this.param.moduleId,
			chapterExamId:this.param.chapterId,
			examId:this.param.examId,
			goodsId:this.param.goodsId,
			pageNum: 1,
			pageSize: 10
		};
		if (this.courseType == 2) {
			this.recordBankList()
		} else {
			this.getExamRecordList();
		}

		setTimeout(function() {
			uni.stopPullDownRefresh();
		}, 500);
	},
	onReachBottom() {
		if (this.recordList.length < this.total) {
			this.param.pageNum++;
			if (this.courseType == 2) {
				this.recordBankList()
			} else {
				this.getExamRecordList();
			}
		}
	},
	onShow() {
		if(this.isRepeat) {
			this.addRecord();
		} else {
			if(this.itemIndex !== '') {
				this.refreshByIndex();
			}
		}
		
	},
	methods: {
		questionBankExplain(record) {
			let moduleId = this.courseType == 2 ? record.moduleId : record.moduleExamId
			let chapterId = this.courseType == 2 ? record.chapterId : record.chapterExamId
			uni.navigateTo({
				url:'/pages2/bank/questionBankExplain?id='+record.examId +'&goodsid='+record.goodsId+'&moduleId='+moduleId+'&chapterId='+chapterId
				+'&orderGoodsId='+record.orderGoodsId + '&doMode=' + record.doMode
			})
		},
		questionReport(record) {
			if (this.courseType == 2) {
				uni.navigateTo({
                	url: "/pages2/class/question_report?examId=" + record.examId + '&recordId=' + record.recordId + '&type=' + record.type + '&orderGoodsId='+ record.orderGoodsId,
              });
			} else {
				let entryType = record.doMode == 2 ? 'random' : '' // random-随机练习
				uni.navigateTo({
					url:'/pages2/bank/question_report?goodsId='+record.goodsId+'&chapterId='+record.chapterExamId+'&moduleId='+record.moduleExamId
					+'&examId='+record.examId+'&id=' + record.recordId+'&orderGoodsId='+record.orderGoodsId + '&entryType=' + entryType
				})
			}
			
		},
		addRecord() {
			this.$api.examRecordList({
				moduleExamId:this.param.moduleId,
				chapterExamId:this.param.chapterId,
				examId:this.param.examId,
				goodsId:this.param.goodsId,
				pageNum: 1,
				pageSize: 1
			}).then(res => {
				this.recordList.unshift(res.data.rows[0])
			});
			this.isRepeat = false;
		},
		refreshByIndex() {
			this.$api.examRecordGroupList({
				moduleExamId:this.param.moduleId,
				chapterExamId:this.param.chapterId,
				examId:this.param.examId,
				goodsId:this.param.goodsId,
				pageNum: this.itemIndex+1,
				pageSize: 1
			}).then(res => {
				this.$set(this.recordList,this.itemIndex,res.data.rows[0])
				this.itemIndex = ''
			});
		},
		getExamRecordList() {
			if (this.param.pageNum == 1) {
				this.recordList = [];
			}
			this.$api.examRecordList(this.param).then(res => {
				this.recordList.push.apply(this.recordList, res.data.rows);
				this.total = res.data.total;
			});
		},
		recordBankList() {
			let params = {
				examId: this.record.examId,
				goodsId: this.param.goodsId,
				pageNum: this.param.pageNum,
				pageSize: this.param.pageSize
			}
			this.record.gradeId && (params['gradeId'] = this.record.gradeId)
			this.record.chapterId && (params['chapterId'] = this.record.chapterId)
			this.record.courseId && (params['courseId'] = this.record.courseId)
			this.record.moduleId && (params['moduleId'] = this.record.moduleId)
			this.record.sectionId && (params['sectionId'] = this.record.sectionId)
			console.log('参数params', params)
			if (this.param.pageNum == 1) {
				this.recordList = []
			}
			this.$http({
				url: '/bank/record/bankList',
				method: 'get',
				data: params
			}).then((res) => {
				this.recordList.push.apply(this.recordList, res.data.rows)
				this.total = res.data.total
			})
		},
		/**
		 * 继续做题
		 */
		doContinue(record,index) {
			this.itemIndex = index;
			this.isRepeat = false;
			uni.navigateTo({
				url:'/pages2/bank/questionBankContinue?recordId=' +
						record.recordId +
						'&id=' +
						record.examId +
						'&goodsid=' +
						record.goodsId +
						'&moduleId=' +
						record.moduleExamId +
						'&chapterId=' +
						record.chapterExamId + 
						'&orderGoodsId=' +
						record.orderGoodsId
			})
		},
		/**
		 * 去做题
		 */
		async doRepeat(goodsId, record) {
			// await this.getDetail(goodsId);
			this.itemIndex = '';
			this.isRepeat = true;
			let count = 0
			if (this.courseType == 2) {
				count = await this.bankRecordDoNum(record)
			} else {
				count = await this.examRecordCount(record.examId, goodsId);
			}
			let answerNum = await this.getExamDetail(record.examId);
			//超过答题次数
			if (answerNum > 0 && count >= answerNum) {
				this.$u.toast('该试卷只能答题' + answerNum + '次!');
				return;
			}
			if (this.courseType == 2) {
				const {courseId, gradeId, sectionId, examId, orderGoodsId = 0, type, reportStatus} = record
				let moduleId = record.moduleId || 0
				let chapterId = record.chapterId || 0
				uni.navigateTo({
					url:"/pages2/class/questionBank?courseId=" + courseId + "&gradeId=" + gradeId + "&isFromVideo=1&id=" + examId +
					"&goodsid=" + goodsId + "&moduleId=" + moduleId + "&chapterId=" + chapterId + "&sectionId=" + sectionId + "&orderGoodsId=" +
					orderGoodsId + "&type=" + type+ '&learning=' + reportStatus,
				})
			} else {
				let moduleId = record.moduleExamId || 0
				let chapterId = record.chapterExamId || 0
				uni.navigateTo({
					url: '/pages2/bank/questionBank?id=' + record.examId + '&goodsid=' + goodsId + '&moduleId=' + moduleId + '&chapterId=' + chapterId + '&orderGoodsId='+record.orderGoodsId
				});
			}
		},
		/**
		 * @param {Object} exam_id
		 * 获取试卷可以做的次数(题库的)
		 */
		getExamDetail(exam_id) {
			return new Promise(resolve => {
				this.$api.getExamDetail(exam_id).then(res => {
					resolve(res.data.data.answerNum);
				});
			});
		},
		/**
		 * 获取试卷已做的次数(视频课程的)
		 */
		bankRecordDoNum(record) {
			return new Promise((resolve) => {
				this.$api.bankRecordDoNum({
					goodsId: record.goodsId,
					gradeId: record.gradeId,
					chapterId: record.chapterId || 0,
					courseId: record.courseId,
					moduleId: record.moduleId || 0,
					examId: record.examId,
				})
				.then((res) => {
					resolve(res.data.data)
				});
			});
		},
		/**
		 * 查询试卷历史做题次数
		 */
		examRecordCount(examId,goodsId) {
			return new Promise(resolve => {
				this.$api
					.examRecordCount({
						examId: examId,
						goodsId: goodsId
					})
					.then(res => {
						resolve(res.data.data);
					});
			});
		},

		getDetail(id) {
			return new Promise(resolve => {
				this.$api.goodsDetail(id).then(res => {
					this.goodsData = res.data.data;
					resolve();
				});
			});
		}
	}
};
</script>
<style>
page {
	background: #eaeef1;
}
</style>
<style lang="scss" scope>
	.animals{
		transition: all 0.3s;
		transform: rotate(180deg);
	}

.record {
	padding: 16rpx 8rpx;
	display: flex;
	flex-wrap: wrap;
	.item {
		margin-bottom: 16rpx;
		width:359rpx;
		background: #ffffff;
		border-radius: 16rpx;
		padding: 65rpx 20rpx 22rpx;
		position: relative;
		overflow: hidden;
		
		&:nth-of-type(2n) {
			margin-left:16rpx;
			
		}

		.note {
			color: #fff;
			position: absolute;
			left: 0;
			top: 0;
			// width: 112rpx;
			padding: 0rpx 10rpx;
			height: 40rpx;
			text-align: center;
			line-height: 40rpx;
			background: linear-gradient(180deg, #4facfe, #007aff);
			border-radius: 16rpx 0px 16rpx 0rpx;
		}

		.title {
			font-size: 32rpx;
			color: #333333;
			font-weight: bold;
		}

		.desc {
			margin-top: 26rpx;
			view {
				margin: 16rpx 0;

				image {
					width: 23rpx;
					height: 24rpx;
				}

				text {
					margin-left: 15rpx;
					font-size: 24rpx;
					color: #999999;
					line-height: 36rpx;
				}
			}
		}

		.btns {
			margin-top: 26rpx;
			display: flex;
			justify-content: space-around;
			.btn {
				flex:1;
				margin:0 5rpx;
				height: 48rpx;
				line-height: 48rpx;
				text-align: center;
				color: #007aff;
				background: #ffffff;
				border: 1rpx solid #007aff;
				border-radius: 16rpx;
				
				&.continue {
					width:317rpx;
				}
			}
		}
	}
}

.modal {
	bottom:0;
	z-index: 199999999;
	position: fixed;
	left: 0;
	width: 100%;

	.content {
		position: relative;
		z-index: 10;
		background: #fff;
		padding: 8rpx 12rpx 20rpx;
		display: flex;
		flex-wrap: wrap;

		.top {
			margin: 0 auto;
			width: 726rpx;
			height: 80rpx;
			background: #f5f5f5;
			color: #666666;
			border-radius: 16rpx;
			text-align: center;
			line-height: 80rpx;
			font-size: 32rpx;
		}

		.list {
			width:100%;
			margin-top: 16rpx;
			display: flex;
			flex-wrap: wrap;
			justify-content: space-between;

			.item {
				padding: 25rpx 20rpx;
				width: 49%;
				background: #f5f5f5;
				border-radius: 16rpx;
				font-size: 32rpx;
				color: #666666;
				margin: 8rpx 0;
			}
		}
	}

	.modal_wrap {
		position: absolute;
		left: 0;
		width: 100%;
		top: 0;
		height: 100%;
		background: rgba(0, 0, 0, 0.3);
	}
}
.activesty {
	background: #007aff !important;
	color: #fff !important;
}
</style>
