<template>
	<view class="bg">
		<view class="process">
			<view class="process_bar" :style="{'width': percentage}"></view>
		</view>
		<view class="tips">请选择你的答案进入下一题</view>
		<view class="question_box">
			<view class="question_num">
				<text class="num_front">出题{{currentIndex + 1}}</text><text class="num_behond">/{{maxLength}}</text>
			</view>
			<image src="https://test.jue-ming.com:8849/api/show?filePath=./webo/EQtest/prev_mark.png" mode="widthFix">
			</image>
			<text class="qs_txt">{{currentQuestion.answer}}</text>
			<image src="https://test.jue-ming.com:8849/api/show?filePath=./webo/EQtest/next_mark.png" mode="widthFix">
			</image>
		</view>
		<view class="answer_list">
			<view :class="['answer', `answer${index+1}`, checkActive(item)]" v-for="(item, index) in currentAnswerList"
				@click="nextHandle(item)">
				{{item}}
			</view>
		</view>
		<view class="contral_box">
			<view class="prev_btn" v-if="currentIndex > 0" @click="prevHandle">
				<image src="https://test.jue-ming.com:8849/api/show?filePath=./webo/EQtest/prev_btn.png"
					mode="widthFix"></image>
			</view>
			<view class="prev_btn" v-if="userAnswerList.length >= maxLength" @click="submitResult">
				<image src="https://test.jue-ming.com:8849/api/show?filePath=./webo/EQtest/submit_btn.png"
					mode="widthFix"></image>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getResult,
		queryPromotionBySubjectId,
	} from "@/api/index.js";
	export default {
		data() {
			return {
				isChecked: true,
				isShake: false,
				scaleDetail: {},
				questionList: [],
				currentQuestion: {},
				currentAnswerList: [],
				currentIndex: 0,
				userAnswerList: [],
				resultId: '',
				isLoading: false,
				isDisbale: false,
				canPrev: false,
				maxLength: 0,
				userInfo: {}
			}
		},
		computed: {
			percentage() {
				return ((this.userAnswerList.length / this.maxLength) * 652) + 'rpx'
			}
		},
		created() {
			this.loadData();
			this.userInfo = uni.getStorageSync('user');
		},
		methods: {
			loadData() {
				this.$request
					.get({
						url: `scaleInfo/20210820143117`,
						loadingTip: "加载中...",
						data: {},
					}).then((res) => {
						this.questionList = JSON.parse(JSON.stringify(res.data));
						this.maxLength = this.questionList.length;
						this.currentQuestion = this.questionList[this.currentIndex];
						this.currentAnswerList = this.getAnswerItem(this.currentQuestion.checkItems);
					})
			},

			nextHandle(str) {
				if (this.isDisbale) {
					return;
				}
				this.isDisbale = true;
				this.userAnswerList[this.currentIndex] = JSON.parse(JSON.stringify(this.currentQuestion));
				this.userAnswerList[this.currentIndex].checkItems = str;
				if (this.currentIndex >= this.questionList.length - 1) {
					this.isDisbale = false;
					// this.submitResult();
				} else {
					setTimeout(() => {
						this.currentIndex++;
						this.currentQuestion = this.questionList[this.currentIndex];
						this.currentAnswerList = this.getAnswerItem(this.currentQuestion.checkItems);
						this.isDisbale = false;
					}, 150)
				}
			},

			prevHandle() {
				if (this.canPrev || this.currentIndex == 0) {
					return;
				}
				this.canPrev = true;
				setTimeout(() => {
					this.canPrev = false;
					this.currentIndex--;
					this.currentQuestion = this.questionList[this.currentIndex];
					this.currentAnswerList = this.getAnswerItem(this.currentQuestion.checkItems);
				}, 300)
			},

			submitResult() {
				let _this = this;
				if (_this.isLoading) {
					return
				}
				_this.isLoading = true;
				let params = {
					testPlanId: "",
					scale_result: _this.userAnswerList,
					userId: uni.getStorageSync('user').id
				};

				uni.showLoading({
					title: "测试结果生成中",
				});
				_this.$request
					.post({
						url: `${getResult}/20210820143117`,
						loadingTip: "加载中...",
						data: params,
					})
					.then((res) => {
						_this.isLoading = false;
						_this.resultId = res.data;
						uni.hideLoading();
						// 跳转支付页面
						uni.navigateTo({
							url: `/newScale/paymentPage/index?tName=EQtest&resultId=${_this.resultId}`
						});
					})
					.catch(() => {
						_this.isLoading = false;
						uni.showToast({
							icon: "none",
							title: "提交失败",
						});
						uni.hideLoading();
					});
			},

			// 获取支付金额
			async getQueryPromotionBySubjectId() {
				let _this = this;
				let urls = queryPromotionBySubjectId + "/20210820143117";
				await _this.$request
					.get({
						url: urls,
						loadingTip: "加载中...",
						data: {},
					})
					.then((res) => {
						console.log('量表支付信息', res.data);
						let data = res.data;
						if (data.price == 0) {
							uni.navigateTo({
								url: `/newScale/EQtest/testResult?resultId=${_this.resultId}&messageShare=1`
							});
						} else {
							let params = {
								productId: '20210820143117',
								userId: _this.userInfo?.id,
								resultId: _this.resultId,
								description: data.name,
								total: data.price,
								sceneType: uni.getSystemInfoSync().platform == "android" ?
									"Android" : "iOS",
							};
							uni.setStorageSync('orderInfo', params);
							uni.navigateTo({
								url: "/newScale/paymentPage/index?tName=EQtest"
							});
						}
					});
			},

			checkActive(item) {
				return this.userAnswerList[this.currentIndex] && this.userAnswerList[this.currentIndex]
					.checkItems ==
					item ? 'active' : ''
			},

			getAnswerItem(arr) {
				return arr.split('；')
			}
		}
	}
</script>

<style scoped>
	.bg {
		width: 100%;
		min-height: 100vh;
		background: url(https://test.jue-ming.com:8849/api/show?filePath=./webo/EQtest/testPage_bg.png) no-repeat top;
		background-size: 100% auto;
		overflow: hidden;
	}

	.tips {
		font-family: 'Alibaba PuHuiTi 2.0';
		font-weight: normal;
		font-size: 24rpx;
		color: #FFFFFF;
		line-height: 55rpx;
		margin: 11rpx 0 0 44rpx;
	}

	.question_box {
		box-sizing: border-box;
		width: 726.9rpx;
		height: 332rpx;
		background: url(https://test.jue-ming.com:8849/api/show?filePath=./webo/EQtest/question_bg.png);
		background-size: cover;
		margin: -28rpx 11rpx 54rpx 11rpx;
		font-family: 'Alibaba PuHuiTi 2.0';
		font-weight: normal;
		font-size: 40rpx;
		color: #333333;
		padding: 96rpx 30rpx 0 54rpx;
		position: relative;
	}

	.question_box text.qs_txt {
		padding-bottom: 4rpx;
		border-bottom: 4rpx solid #2E9AFF;
		line-height: 1.6;
	}

	.question_box image {
		width: 43rpx;
		margin: 0 10rpx;
	}

	.answer {
		width: 577rpx;
		padding: 0 50rpx 0 70rpx;
		line-height: 126rpx;
		font-family: 'Alibaba PuHuiTi 2.0';
		font-weight: normal;
		font-size: 36rpx;
		color: #333333;
		text-align: center;
		margin: 0 auto 28rpx;
		cursor: pointer;
		transition: background 200ms linear;
	}

	.answer1 {
		background: url(https://test.jue-ming.com:8849/api/show?filePath=./webo/EQtest/answer_A.png);
		background-size: cover;
	}

	.answer1.active {
		background: url(https://test.jue-ming.com:8849/api/show?filePath=./webo/EQtest/answer_A_active.png);
		background-size: cover;
		color: #FFFFFF;
	}

	.answer2 {
		background: url(https://test.jue-ming.com:8849/api/show?filePath=./webo/EQtest/answer_B.png);
		background-size: cover;
	}

	.answer2.active {
		background: url(https://test.jue-ming.com:8849/api/show?filePath=./webo/EQtest/answer_B_active.png);
		background-size: cover;
		color: #FFFFFF;
	}

	.answer3 {
		background: url(https://test.jue-ming.com:8849/api/show?filePath=./webo/EQtest/answer_C.png);
		background-size: cover;
	}

	.answer3.active {
		background: url(https://test.jue-ming.com:8849/api/show?filePath=./webo/EQtest/answer_C_active.png);
		background-size: cover;
		color: #FFFFFF;
	}

	.answer4 {
		background: url(https://test.jue-ming.com:8849/api/show?filePath=./webo/EQtest/answer_D.png);
		background-size: cover;
	}

	.answer4.active {
		background: url(https://test.jue-ming.com:8849/api/show?filePath=./webo/EQtest/answer_D_active.png);
		background-size: cover;
		color: #FFFFFF;
	}

	.answer5 {
		background: url(https://test.jue-ming.com:8849/api/show?filePath=./webo/EQtest/answer_E.png);
		background-size: cover;
	}

	.answer5.active {
		background: url(https://test.jue-ming.com:8849/api/show?filePath=./webo/EQtest/answer_E_active.png);
		background-size: cover;
		color: #FFFFFF;
	}

	.prev_btn {
		text-align: center;
	}

	.process {
		box-sizing: border-box;
		width: 656rpx;
		height: 25rpx;
		padding: 2rpx;
		background: #FFFFFF;
		border-radius: 12rpx;
		border: 1px solid #2E9AFF;
		overflow: hidden;
		margin: 190rpx auto 0;
	}

	.process_bar {
		height: 19rpx;
		background: #2E9AFF;
		border-radius: 10rpx;
		transition: width 200ms linear;
	}

	.question_num {
		width: 238rpx;
		line-height: 70rpx;
		background: url(https://test.jue-ming.com:8849/api/show?filePath=./webo/EQtest/question_num_bg.png) no-repeat;
		background-size: cover;
		text-align: center;
		position: absolute;
		top: 30rpx;
		left: 20rpx;
	}

	.num_front {
		font-family: Alibaba PuHuiTi 2.0;
		font-weight: bold;
		font-size: 48rpx;
		color: #FEFEFE;
		line-height: 55rpx;
		text-shadow: 2rpx 2rpx 5rpx rgba(146, 9, 0, 0.45);
	}

	.num_behond {
		font-family: Alibaba PuHuiTi 2.0;
		font-weight: bold;
		font-size: 32rpx;
		color: #FEFEFE;
		line-height: 55rpx;
		text-shadow: 2rpx 2rpx 5rpx rgba(146, 9, 0, 0.45);
	}

	.contral_box {
		display: flex;
		justify-content: center;
		align-items: center;
		margin: 54rpx 0 100rpx 0;
	}

	.prev_btn {
		margin: 0 20rpx;
	}

	.prev_btn image {
		width: 304rpx;
	}
</style>