<template>
	<view class="question_class">
		<view class="">
			<LineProgress class="pro-bg" :currentIndex="currentIndex" :total="total" />
			<swiper :current="currentIndex" style="height: 98vh" :disable-touch="true">
				<swiper-item style="overflow-y: scroll" v-for="(item, indexa) in QuestionListAll" :key="indexa" m
					catchtouchmove="catchTouchMove">
					<scroll-view scroll-y @scrolltolower="" style="height: 90vh">
						<exam-widght :total="total" :currentIndex="currentIndex" :QuestionList="QuestionList"
							@goPrevAnswer="goPrevAnswer" @goNextAnswer="goNextAnswer" @onItemAnswer="onItemAnswer"
							@subMitList="subMitList" @onListItemAnswer="onListItemAnswer" />
					</scroll-view>
				</swiper-item>
			</swiper>
		</view>
	</view>
</template>

<script>
	import examWidght from "@/examWidght/components/Li-ExamWidght/Li-ExamWidght.vue";
	import LineProgress from "@/examWidght/components/lineProgress";
	import {
		getScaleInfo,
		getUserInfo,
		getResult,
		queryPromotionBySubjectId,
	} from "@/api/index.js";
	var _this;
	export default {
		components: {
			examWidght,
			LineProgress,
		},
		data() {
			return {
				//当前索引
				currentIndex: 0,
				//总条数
				total: 0,
				QuestionListAll: [],
				//当前答案
				QuestionList: {
					answer: "",
				},
				//// 全部题目
				scale_infos: [{
					questionType: "",
					answer: "",
				}, ],
				//题目总数
				scale_all: 0,
				//备选答案数组
				scale_checkItems: [],
				//量表进度吧
				scale_percent: 0,
				// 当前索引
				scale_index: 0,
				//量表flag
				flag: "",
				//分享人的uid
				uid: "",
				//来源
				source: "",
				//是否重复点击标志
				loading: false,
				//支付金额
				price: "",
				//量表结果保存的唯一id
				resultId: "",
				userInfo: null,
			};
		},
		destroyed() {},
		onLoad(options) {
			_this = this;
			uni.pageScrollTo({
				scrollTop: 0,
				duration: 50
			})
			//查看是否传进来flag
			_this.uid = options.uid;
			_this.source = options.source;
			if (options.flag) {
				_this.flag = options.flag;

				// _this.selectUser();
				//根据flag查询出量表全部题目
				_this.getScaleInfoAnswerList(options.flag);
			}
			this.userInfo = uni.getStorageSync('user');
		},

		onHide() {
			// clearInterval(this.timer);
			// https://blog.csdn.net/weixin_42738504/article/details/125521004
		},
		methods: {
			//根据flag查询渲染的结果
			// queryTopic() {
			// 	this.$request.get({
			// 		url: 'https://www.example.com/request',
			// 		loadingTip: "加载中...",
			// 		data: {},
			// 	}).then((res) => {

			// 	})
			// },
			//查询用户信息
			// selectUser() {
			// 	this.$request
			// 		.get({
			// 			url: 'system/temporaryUser',
			// 			loadingTip: "加载中..."
			// 		})
			// 		.then((res) => {
			// 			this.userInfo = res.data.user;
			// 			sessionStorage.setItem("user", JSON.stringify(res.data.user));
			// 			sessionStorage.setItem("token", res.data.token);
			// 		});
			// },
			//上一题
			goPrevAnswer() {
				if (_this.currentIndex < 1) {
					uni.showToast({
						icon: "none",
						title: "已经是第一题",
					});
					return;
				}

				let newIndex = _this.currentIndex - 1;
				_this.currentIndex = newIndex;
				let QuestionListNew = Object.assign({}, _this.QuestionListAll[newIndex]);
				_this.QuestionList = QuestionListNew;
			},
			//下一题
			goNextAnswer(blankAnswer) {
				if (_this.scale_infos[_this.currentIndex].questionType == "5") {
					let paramsArr = JSON.parse(_this.scale_infos[_this.currentIndex].questionParam);
					blankAnswer = paramsArr[blankAnswer].label
				}

				if (blankAnswer) {
					_this.QuestionListAll[_this.currentIndex].checkItems = blankAnswer;
					_this.QuestionListAll[_this.currentIndex].fldAnswer = blankAnswer;
				}
				let newIndexs = _this.currentIndex + 1;
				if (newIndexs > _this.QuestionListAll.length - 1) {
					_this.currentIndex = newIndexs - 1;
					uni.showToast({
						icon: "none",
						title: "已经是最后一题",
					});
					return;
				}
				if (_this.QuestionListAll[_this.currentIndex].fldAnswer == "") {
					uni.showToast({
						icon: "none",
						title: "请先答题",
					});
					return;
				}
				_this.currentIndex = newIndexs;
				let QuestionListNew = Object.assign({}, _this.QuestionListAll[newIndexs]);
				_this.QuestionList = QuestionListNew;
			},
			//获取问题及答案全部列表
			getScaleInfoAnswerList(flag) {
				_this.$request
					.get({
						url: `${getScaleInfo}/${flag}`,
						loadingTip: "加载中...",
						data: {},
					})
					.then((res) => {
						if (res.code == 200 && res.data.length > 0) {
							_this.scale_infos = res.data;
							_this.scale_all = _this.scale_infos.length;
							_this.scale_checkItems =
								_this.scale_infos[_this.scale_index].checkItems.split("；");
							_this.scale_percent = parseInt(
								(_this.scale_index + 1) / _this.scale_all
							);
							_this.scale_percent =
								((_this.scale_index + 1) * 100) / _this.scale_all;
							let listAll = [];
							_this.scale_infos.forEach((item, index) => {
								item.questionID = item.id;
								// item.fldName = item.answer;
								item.fldAnswer = "";
								let QuestionOptionList = [];
								if (item.checkItems) {
									let arrList = item.checkItems.split("；");
									arrList.forEach((items, i) => {
										QuestionOptionList.push({
											fldOptionText: items,
											fldOptionIndex: i + 1,
										});
									});
									item.QuestionOptionList = QuestionOptionList;
								}
							});
							_this.total = _this.scale_infos.length;
							_this.QuestionListAll = _this.scale_infos;
							_this.QuestionList = _this.scale_infos[_this.currentIndex];
						}
					});
			},
			onListItemAnswer(item) {
				if (_this.currentIndex >= _this.total - 1) {
					_this.QuestionListAll[_this.currentIndex].checkItems =
						item.fldOptionText;
					_this.QuestionListAll[_this.currentIndex].fldAnswer =
						item.fldOptionText;
					let QuestionListNew = Object.assign({},
						_this.QuestionListAll[_this.currentIndex]
					);
					_this.QuestionList = QuestionListNew;
					// if (_this.handleNumber == 1) {
					// 	_this.subMitList();
					// }

					return;
				} else {
					_this.currentIndex = _this.currentIndex + 1;
					_this.QuestionListAll[_this.currentIndex - 1].checkItems =
						item.fldOptionText;
					_this.QuestionListAll[_this.currentIndex - 1].fldAnswer =
						item.fldOptionText;
					let QuestionListNew = Object.assign({},
						_this.QuestionListAll[_this.currentIndex]
					);
					_this.QuestionList = QuestionListNew;
				}
			},
			onItemAnswer(item) {
				_this.QuestionListAll[_this.currentIndex].checkItems = item.fldOptionText;
				_this.QuestionListAll[_this.currentIndex].fldAnswer = item.fldOptionText;
				let QuestionListNew = Object.assign({},
					_this.QuestionListAll[_this.currentIndex]
				);
				_this.QuestionList = QuestionListNew;
			},
			subMitList(flag) {
				// _this.handleNumber = 2
				// if (_this.handleNumber == 1) {
				// 	_this.subMitList();
				// }
				if (_this.QuestionListAll[_this.currentIndex].fldAnswer == "") {
					uni.showToast({
						icon: "none",
						title: "请先答题",
					});
					return;
				}

				//调用提交答案的接口

				this.createResult();

				// uni.navigateTo({
				// 	url:'/paymentPage/paymentPage/index'
				// })
				//提交时防抖函数
				// console.log(uni.$u)
				// uni.$u.throttle(this.createResult, 2500);
			},

			// 生成测试结果
			createResult() {
				let _this = this;
				if (this.loading) {
					return;
				}
				this.loading = true;
				let subMitListData = _this.QuestionListAll;
				subMitListData.forEach((item) => {
					delete item.QuestionOptionList;
					delete item.fldAnswer;
					delete item.questionID;
				});

				let params = {
					testPlanId: "",
					scale_result: subMitListData,
					userId: uni.getStorageSync("user").id
				};
				console.log("提交的参数", params);

				uni.showLoading({
					title: "测试结果生成中",
				});
				// _this.$dot.submit(0, _this.id);
				_this.$request
					.post({
						url: `${getResult}/${_this.flag}`,
						loadingTip: "加载中...",
						data: params,
					})
					.then((res) => {
						_this.resultId = res.data;
						_this.$request.post({
							url: `scaleExternalSource/save`,
							loadingTip: "加载中...",
							data: {
								currentUserId: _this.userInfo.id,
								resultId: _this.resultId,
								uid: _this.uid,
								source: _this.source,
								flag: _this.flag,
							},
						}).then(() => {})
						uni.hideLoading();
						// 答题结束获取支付
						_this.getQueryPromotionBySubjectId(_this.flag);
						_this.loading = false;
					})
					.catch((err) => {
						uni.showToast({
							icon: "none",
							title: "提交失败",
						});
						_this.loading = false;
					});
			},
			// 获取支付金额
			async getQueryPromotionBySubjectId(id) {
				let _this = this;
				let urls = queryPromotionBySubjectId + "/" + id;
				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: `/scaleTestResults/testResults/index?resultId=${_this.resultId}&messageShare=1`
							});
						} else {
							let params = {
								productId: id,
								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: "/paymentPage/paymentPage/index",
							});
						}
					});
			}
		},
	};
</script>
<style scoped>
	/deep/ uni-swiper {
		min-height: 80vh;
	}

	.question_class {
		background: linear-gradient(180deg, #d8fff2 0%, #46e2ed 42%, #beffe8 98%);
		border-radius: 0px 0px 0px 0px;
		padding-left: 10px;
		padding-right: 10px;
		padding-top: 20px;
	}

	.question_inner {
		padding-left: 10px;
		padding-right: 10px;
		padding-top: 20px;
		/* background: url('/static/images/question_main.png') no-repeat 100% 100%; */
		background-image: url("https://test.jue-ming.com:8849/api/show?filePath=./webo/question_main.png");
		/* background-size:contain; */
		background-size: 100% 100px;
		background-repeat: no-repeat;
		background-position: top;
	}

	.pro-bg {
		background-image: url("https://test.jue-ming.com:8849/api/show?filePath=./webo/question_main1.png");
		/* background-size: contain; */
		background-size: 100% 100%;
		background-position: top;
		background-repeat: no-repeat;
		padding-top: 80rpx;
		padding-left: 40rpx;
		padding-right: 40rpx;
	}
</style>