<template>
	<view class="title-wrap" style="padding-top: 40rpx;">
		<view class="progress-block">
			<view class="row">
				<view class="title-index">
					第{{pageIndex+1}}题
				</view>
				<view class="all-title">
					共{{allNum}}道题
				</view>
				<view class="time">
					约20分钟
				</view>
			</view>
			<view class="bar">
				<view class="bar-inner" :style="{width: rate + '%'}"></view>
			</view>
		</view>
		<view class="content">
			<view class="item" v-show="pageIndex == index" v-for="(item, index) in list" :key="index">
				<view class="title">{{ item.Title }}</view>
				<view class="sel-label-block">
					<view class="label" :class="item.Value == 4 ? 'sel' : ''" @click="checkedTitle(4,index)">非常符合</view>
					<view class="label" :class="item.Value == 3 ? 'sel' : ''" @click="checkedTitle(3,index)">基本符合</view>
					<view class="label" :class="item.Value == 2 ? 'sel' : ''" @click="checkedTitle(2,index)">基本不符合</view>
					<view class="label" :class="item.Value == 1 ? 'sel' : ''" @click="checkedTitle(1,index)">完全不符合</view>
				</view>
			</view>
			<view class="btn-wrap">
				<view class="pre-btn"  @click="prePage">上一题</view>
				<view class="com-btn"   v-show="commit" @click="submitTitle">提交</view>
			</view>
		</view>
<!-- 		<u-modal :show="showBakcTip" @confirm="showBakcTip=false" @cancel="goback" confirmText="继续测评" cancelText="放弃测评" :showCancelButton="true">
			<view class="slot-content">
				<view class="tip-layer">
					<view class="title">提示</view>
					<view class="desc">确认要放弃测评吗？</view>
			  </view>
			</view>
		</u-modal> -->
	</view>
</template>


<script>
	import api from "@/packageCa/apiCa/testManage.js"
	import apiuser from "@/packageCa/apiCa/user.js"
	export default {
		data() {
			return {
				list: [],
				allNum: 0,//总题目数
				pageIndex: 0, //显示页
				showBakcTip: false,
				lastTapTime: 0 ,// 记录上次点击的时间
				historyTitle: [],// 历史题目
			}
		},
		computed: {
			rate: function() {
				let num = 0;
				this.list.forEach(item => {
					if (item.Value > 0) {
						num++
					}
				});
				return Math.round(num * 100 / this.allNum)
			},
			commit: function(){
				let num = 0;
				this.list.forEach(item => {
					if (item.Value > 0) {
						num++
					}
				});
				if(num == this.list.length){
					return true;
				}else {
					return false;
				}
			}
		},
		created() {
			this.getTitle();
		},
		methods: {
			// 返回
			goback(){
				uni.showModal({
					title: '提示',
					content: '确认要放弃测评吗？',
					cancelText: '取消',
					confirmText: '确认',
					confirmColor: '#1677ff', 
					cancelColor: '#999999',						
					success: function (res) {
						if (res.confirm) {
							uni.navigateBack(-1);
						} else if (res.cancel) {						
						}
					}
				});
				//uni.navigateBack(-1);
			},
			// 获取题目
			getTitle() {
				uni.showLoading({
					title: "加载中..."
				}) 
				api.queryTestContent(15).then(res => {
					uni.hideLoading();
					if (res.Result === 1) {
						let list = res.Data.List;
						list.forEach(item => {
							item.Value = 0
						})
						this.allNum = list.length;
						this.list = list;
					}
				})
			},
			// 上一页
			prePage() {
				if (this.pageIndex == 0) {
					return
				}
				this.pageIndex--
			},
			//选中题目
			checkedTitle(NUM,INDEX){
				 const now = Date.now();
				 if (now - this.lastTapTime < 500) {
					  return;
				 } else {
					 // 处理单击事件
					this.lastTapTime = now; 
					this.list[INDEX].Value = NUM;
					setTimeout(()=>{
						if(this.pageIndex != this.list.length - 1){
							this.pageIndex = INDEX + 1;
						}
					},400)
				}
			},
			// 添加日志
			postLog(operationType, remarks, beforeData = "", afterData = ""){
				try {
				  const data = {
				    OperationType: operationType,
				    Remarks: remarks,
				    BeforeOperationJson: beforeData,
				    AfterOperationJson: afterData
				  };
					console.log(data);
				  // return;
				  // 异步记录日志，不阻塞主流程
				  apiuser.saveUserOperationLog(data).then((res) => {
				    console.log('[Operation Log] 记录成功', res);
				  }).catch((err) => {
				    console.error('[Operation Log] 记录失败', err);
				  });
				} catch (e) {
				  console.error('[Operation Log] 记录异常', e);
				}
			},
			// 提交题目
			submitTitle() {
				let testStr = "";
				this.list.forEach(item => {
					switch (item.Value) {
						case 4: {
							testStr += `${item.TitleId}|A,`
							break;
						}
						case 3: {
							testStr += `${item.TitleId}|B,`
							break;
						}
						case 2: {
							testStr += `${item.TitleId}|C,`
							break;
						}
						case 1: {
							testStr += `${item.TitleId}|D,`
							break;
						}
					}
				});
				uni.showLoading({
					title: "提交中"
				})
				let data = {
					testStr,
					testType: 15
				}
				api.saveCharacterTestResult(data).then((res) => {
					uni.hideLoading();
					if (res.Result === 1) {
						uni.showToast({
							title: "提交成功",
							icon: "success"
						})
						let pages = getCurrentPages(); // 当前页面
						let beforePage = pages[pages.length - 2]; // 上一页
						beforePage.data.refreshIfNeeded = true;
						setTimeout(()=>{
							uni.redirectTo({
								url: `/packageCa/testReport/personalTestReport`
							})
						},1000)
						
					} else {
						uni.showToast({
							title: res.Message,
							icon: "none"
						})
					}
				})
				this.postLog(201,`【新增】保存人格测评`,"",JSON.stringify({
					TestType: 15,
					TestStr: testStr
				}))
			}
		}
	}
</script>
<style lang="scss" scoped>
	.tip-layer {
		.title {
			font-size: 36rpx;
			color: #000000;
			text-align: center;
			font-weight: 600;
			margin-bottom: 30rpx;
		}
		.desc {
			font-size: 32rpx;
			color: #666666;
			margin-bottom: 20rpx;
			text-align: center;
			&:last-child {
				margin-bottom: 0;
			}
		}
	}
	.head-bar {
		position: relative;
		text-align: center;
		font-size: 36rpx;
		font-weight: 600;
		height: 60rpx;
		line-height: 60rpx;
		margin-bottom: 30rpx;
		.go-back {
			position: absolute;
			left: 10rpx;
			top: 0;
			width: 60rpx;
			height: 60rpx;
			background: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAABUklEQVRoQ+3ZOwrCQBCA4UlyCVsrQauQdPYewcNYWXoHK1s9gYeYdIKlracYWYggkgiTnccuaB3D/+1sYEMKyPxXZN4Pf0DMBOu6XpZluQOANRGduq7bc+/nNoE+/gwAq3c0IrJ72H/grtDQ9UPxAPBAxDn3/uaAkfjQvUXES9IA6fiANZuARrwZQCveBKAZrw7QjlcFWMSrAaziVQCW8eIA63hRgEe8GMArXgTgGR8N8I6PAqQQPxmQSvxkQNM0VwDYfJ3dJ53nuef/7+vZx+l+9W8pxE+aQNu2CyK6ZwsI4VlvoQDI/iFOCcF+iD/3fgqTiAKkMIlogDdCBOCJEAN4IUQBHghxgDVCBWCJUANYIVQBFgh1gDbCBKCJMANoIUwBGghzwA/EExFn3HdkF8AYIpvPrO9VDu8TVVUdiKghomNWH7q5W2Xserct9Af0K/AChQ/cMY9OGScAAAAASUVORK5CYII=") center no-repeat;
			background-size: 38rpx 38rpx;
		}
	}
	.title-wrap {
		background: #fff;
		.progress-block {
			padding: 0 40rpx;
			.row {
				position: relative;
				display: flex;
				align-items: flex-end;
				height: 40rpx;
				margin-bottom: 20rpx;
				.title-index {
					font-size: 28rpx;
					color: #1677ff;
				}
				.all-title {
					font-size: 24rpx;
					color: #999999;
					margin-left: 10rpx;
				}
				.time {
					position: absolute;
					right: 0;
					top: 0;
					font-size: 24rpx;
					color: #20B664;
				}
			}
			.bar {
				position: relative;
				width: 670rpx;
				height: 8rpx;
				background: #EBEBEB;
				border-radius: 8rpx 8rpx 8rpx 8rpx;
				.bar-inner {
					position: absolute;
					left: 0;
					top: 0;
					height: 8rpx;
					background: #1677ff;
					border-radius: 8rpx 8rpx 8rpx 8rpx;
				}
			}
		}
		.content {
			padding: 0 40rpx;
			.item {
				padding-top: 40rpx;
				.title {
					font-size: 32rpx;
					color: #000000;
					font-weight: 600;
					margin-bottom: 40rpx;
				}
				.sel-label-block {
					.label {
						display: flex;
						align-items: center;
						width: 630rpx;
						height: 96rpx;
						padding-left: 40rpx;
						background: #f5f5f5;
						margin-bottom: 24rpx;
						border-radius: 12rpx 12rpx 12rpx 12rpx;
						font-size: 28rpx;
						color: #000000;
						&.sel {
							background: #EDF6FF;
							color: #1677ff;
						}
					}
				}
			}
			.btn-wrap {
				display: flex;
				justify-content: center;
				padding-top: 50rpx;
				.pre-btn {
					width: 316rpx;
					height: 76rpx;
					line-height: 76rpx;
					text-align: center;
					background: #F8F8F8;
					border-radius: 60rpx 60rpx 60rpx 60rpx;
					border: 2rpx solid #EAEAEA;
					font-size: 28rpx;
					color: #666666;
				}
				.com-btn {
					width: 320rpx;
					height: 80rpx;
					line-height: 80rpx;
					text-align: center;
					background: #1677ff;
					margin-left: 20rpx;
					border-radius: 60rpx 60rpx 60rpx 60rpx;
					font-size: 28rpx;
					color: #fff;
				}
			}
		}
	}
</style>
