<template>
	<view class="study-detail">
		<view class="study-detail-section" v-if="info.question_info.title">
			<view class="li">
				<view class="tit" :style="{'fontSize': fontSize+'rpx', 'fontWeight': fontWeight?'normal':'bold'}">
					{{teamIndex}}、<view class="content" v-html="info.question_info.title"></view>
				</view>
				<view class="title_img" v-if="info.question_info.title_img">
					<image mode="widthFix" :src="info.question_info.title_img"></image>
				</view>
				<view class="title_video" v-if="info.question_info.title_video" >
					<video :src="info.question_info.title_video"></video>
				</view>
				<view class="con" v-for="(a,index) in info.question_info.choice" :key="index" 
				:class="a.isSelect?'right':a.isErrow?'wrong':''" :style="{'fontSize': fontSize+'rpx'}" @click="toSelect(index)">
					{{a.name}}.{{a.value}}
					<view class="mark">
						<image v-if="a.isSelect" :src="staticUrl('/static/icon-study-right.png')"></image>
						<template v-else>
							<image v-if="a.isErrow" :src="staticUrl('/static/icon-study-wrong.png')"></image>
							<image v-if="current_question_choice && info.question_info.correct == a.name" :src="staticUrl('/static/icon-study-right.png')"></image>
						</template>
					</view> 
				</view>
			</view>
			<view class="btns">
				<view class="btn" @click="updateFont(1)">放大</view>
				<view class="btn" @click="updateFont(2)" style="margin-left: 20rpx; margin-right: 20rpx;">缩小</view>
				<view class="btn" @click="updateFont(3)">{{fontWeight?'加粗':'恢复'}}</view>
			</view>
			<view class="tips" v-if="current_question_choice">
				<view class="title"><u-divider text="提示"></u-divider></view>
				<view class="content" :style="{'fontSize': fontSize1+'rpx'}" v-html="info.question_info.prompt"></view>
			</view>
		</view>
		<view class="study-detail-section" v-if="info.question_info.length == 0 && count >= 0">
			<view class="empty" style="margin-top: 200rpx;" v-if="count == 0">
				<u-empty mode="data" text="暂无相关信息">
				</u-empty>
			</view>
			<template v-else>
				<view class="empty" style="margin-top: 200rpx; margin-bottom: 100rpx;">
					<u-empty mode="data" text="当前题库全部练完">
					</u-empty>
				</view>
				<u-button color="#2080F9" @click="openPage(8)">返回上一页</u-button>
			</template>
		</view>
		<view class="study-detail-bottom" v-if="info.question_info.title && (teamIndex) <= count"
         :style="{ 'padding-bottom': safeBottom + 'px' }">
			<view class="check">
				<view class="a">
					<image :src="staticUrl('/static/icon-study-yes.png')"></image>{{successNum}}
				</view>
				<view class="b">
					<image :src="staticUrl('/static/icon-study-no.png')"></image>{{errorNum}}
				</view>
			</view>
			<view class="listen" v-if="info.question_info.speech" @click="toPlayAudio(info.question_info.speech)">
				<image v-if="isPlay == false" :src="staticUrl('/static/icon-listen.png')"></image>
				<image v-else :src="staticUrl('/static/icon-listen-on.png')"></image>
				<text :class="isPlay?'on':''">听题</text>
			</view>
			<view class="btn" @click="getInfo(1)">
				下一题
			</view>
		</view>
	</view>
</template>

<script>
	
	export default {
		data() {
			return {
				playType: 1,
				page: 1,
				chapter_id: "",
				teamIndex: 0,
				successNum: 0,
				errorNum: 0,
				type: 0,
				count: 0,
				list: [],
				fontSize: 30,
				cid: "",
				fontSize1: 26,
				fontWeight: true,
				info: {
					question_info: []
				},
                safeBottom: '',
				isPlay: false,
				innerAudioContext: null,
				current_question_id: "",//当前题目ID
				next_question_id: "",//下一题ID
				current_question_choice: "",//current_question_choice
			};
		},
		onLoad(o) {
			this.type = o.type;
			this.chapter_id = o.id;
			this.cid = o.cid;
            const {
                safeAreaInsets
            } = uni.getSystemInfoSync()
            this.safeBottom = safeAreaInsets?.bottom
			uni.setNavigationBarTitle({
				title: o.type != 1?"章节练习":"章节错误题"
			})
			if(o.type == 0 || o.type == 2){
				this.getZhangJieList();
			}else{
				this.getCuowuList();
			}
			this.innerAudioContext = uni.createInnerAudioContext({
				useWebAudioImplement: false // 默认关闭。对于短音频、播放频繁的音频建议开启此选项
			});
		},
		methods: {
			updateFont(type){
                if (type == 1) {
                    this.fontSize < 70 ? this.fontSize++ : this.fontSize
                } else if (type == 2) {
                    this.fontSize > 20 ? this.fontSize-- : this.fontSize
                } else if (type == 3) {
                    this.fontWeight = !this.fontWeight
                }
			},
			openPage(){
				uni.navigateBack({
					delta: 1
				})
			},
			//播发试题
			toPlayAudio(src) {
				if(this.isPlay == false){
					if(this.playType == 1){
						console.log(111)
						this.innerAudioContext.src = src;
						this.innerAudioContext.play();
						this.playType = 2
					}else{
						console.log(22)
						this.innerAudioContext.play();
					}
					this.isPlay = true;
				}else{
					this.isPlay = false;
					this.innerAudioContext.pause();
				}
				this.innerAudioContext.onPlay(() => {
				  console.log('开始播放');
				});
				this.innerAudioContext.onEnded(() => {
				   this.isPlay = false
				});
			},
			//选择答案
			toSelect(index) {
				if(this.info.question_info.isDisable == true){
					return ;
				}
				this.info.question_info.isDisable = true;
				this.info.question_info.choice.map(a=>{
					a.isSelect = false
				})
				if(this.info.question_info.choice[index].name == this.info.question_info.correct){
					this.info.question_info.choice[index].isSelect = true
					this.info.question_info.choice[index].isErrow = false
					this.successNum ++
				}else{
					this.info.question_info.choice[index].isSelect = false
					this.info.question_info.choice[index].isErrow = true
					this.errorNum ++
				}
				this.current_question_choice = this.info.question_info.choice[index].name
			},
			//获取章节题目列表
			async getZhangJieList() {
				const { code, data, msg } = await this.$api.chapterQuestionsList({
					is_free: this.type == 0?0:1,
					chapter_id: this.type == 0?this.chapter_id:"",
					category_id: this.type == 0?this.cid: this.chapter_id ,
					page: this.page
				})
				if (code == 200) {
					this.list = data.list;
					this.count = data.count;
					if(this.count>0){
						this.next_question_id = data.list[0]
						this.getInfo();
					}
					
				}
			},
			//获取错误题目列表
			async getCuowuList() {
				const { code, data, msg } = await this.$api.chapterErrorQuestionsList({
					category_id: this.chapter_id,
					page: this.page
				})
				if (code == 200) {
					this.list = data.list;
					this.count = data.count;
					if(this.count>0){
						this.next_question_id = data.list[0]
						this.getInfo();
					}
				}else{
					uni.showToast({
						icon: "none",
						title: msg
					})
				}
			},
			//获取详情
			async getInfo(type) {
				if(type == 1){
					if(!this.current_question_choice){
						uni.showToast({
							icon: "none",
							title: "请选择答案！"
						})
						return ;
					}
					this.current_question_id = this.teamIndex>this.count?'':this.list[this.teamIndex-1];
					this.next_question_id = this.teamIndex>this.count?'':this.list[this.teamIndex];
					this.isPlay = false;
					this.playType = 1;
					this.innerAudioContext.pause();
				}
				uni.showLoading({
					title: "加载中"
				})
				const { code, data, msg } = await this.$api.chapterQuestionsInfo({
					next_question_id: this.next_question_id,
					current_question_id: this.current_question_id,
					current_question_choice: this.current_question_choice,
					is_error: this.type
				})
				if (code == 200) {
					uni.hideLoading()
					let arr = data
					arr.question_info.isDisable = false
					if(arr.question_info && arr.question_info.choice){
						arr.question_info.choice.map(a=>{
							a.isSelect = false
							a.isErrow = false
						})
					}
					this.teamIndex ++;
					this.current_question_choice =""
					this.info = arr
				}else{
					uni.showToast({
						icon: "none",
						title: msg
					})
				}
			},
		},
		onUnload() {
			this.innerAudioContext.src=""
			this.innerAudioContext.stop()
			this.innerAudioContext.destroy();
		},
		destroyed() {
			this.innerAudioContext.src=""
			this.innerAudioContext.stop()
			this.innerAudioContext.destroy();
		}
	}
</script>

<style lang="scss" scoped>
	.study-detail {
		padding-bottom: 140rpx;
		overflow: hidden;
		&-section {
			width: 100%;
			padding: 0 25rpx 20rpx;
			overflow: hidden;
			box-sizing: border-box;
			.btns{
				padding: 25rpx;
				box-sizing: border-box;
				margin-top: 20rpx;
				display: flex;
				justify-content: center;
				.btn{
					flex: 1;
					line-height: 80rpx;
					border-radius: 10rpx;
					text-align: center;
					background-color: #2080F9;
					font-size: 28rpx;
					color: #fff;
				}
			}
			.tips{
				background-color: #fff;
				border-radius: 20rpx;
				padding: 25rpx;
				box-sizing: border-box;
				margin-top: 20rpx;
				line-height: 1.5;
				letter-spacing: 0.5px;
				color: #6b6b6b;
				.title{
					padding: 0 30rpx;
				}
			}
			.li {
				width: 90%;
				margin: 20rpx auto 0;
				padding: 30rpx;
				background: #FFFFFF;
				border-radius: 20rpx;
				box-shadow: 0rpx 5rpx 10rpx 0rpx rgba(32, 79, 156, 0.06);
				border-radius: 20rpx;

				.tit {
					overflow: hidden;
					padding-bottom: 10rpx;
					color: #333333;
					display: flex;
					align-items: flex-start;
				}
				.title_img{
					width: 100%;
					image{
						width: 100%;
					}
				}
				.title_video{
					width: 100%;
					height: 300rpx;
					video{
						width: 100%;
						height: 100%;
					}
				}
				.con {
					overflow: hidden;
					display: flex;
					align-items: center;
					justify-content: space-between;
					margin-top: 20rpx;
					padding: 30rpx;
					background: #F6F6F6;
					color: #333333;
					line-height: 50rpx;
					position: relative;
					&.right{
						color: #49c99c;
						background-color: #f3f8f6;
					}
					&.wrong{
						color: #fa6e6e;
						background-color: #fff3f3;
					}
					.mark{
						width: 60rpx;
						height: 60rpx;
						position: absolute;
						right: 10rpx;
						top: 50%;
						margin-top: -30rpx;
						z-index: 2;
						image{
							width: 100%;
							height: 100%;
						}
					}
				}
			}

		}

		&-bottom {
			position: fixed;
			bottom: 0;
			width: 100%;
			height: 100rpx;
			background: #FFFFFF;
			display: flex;
			align-items: center;
			justify-content: space-between;
			.check{
				margin-left: 20rpx;
				line-height: 30rpx;
				.a{
					float: left;
					display: flex;
					align-items: center;
					margin-right: 30rpx;
					font-size: 30rpx;
					color: #37C594;
				}
				.b{
					float: left;
					display: flex;
					align-items: center;
					font-size: 30rpx;
					color: #F01B1B;
				}
				image{
					width: 44rpx;
					height: 44rpx;
					margin-right: 10rpx;
				}
			}
			.listen{
				display: flex;
				align-items: center;
				font-size: 26rpx;
				line-height: 30rpx;
				color: #666666;
				text.on{
					color: #2080F9;
				}
				image{
					width: 44rpx;
					height: 44rpx;
					margin-right: 12rpx;
				}
			}
			
			.btn {
				width: 141rpx;
				margin-right: 20rpx;
				font-size: 28rpx;
				line-height: 57rpx;
				color: #2080F9;
				text-align: center;
				background: #FFFFFF;
				border: 1px solid #2080F9;
				border-radius: 29rpx;
			}
		}
	}
</style>