<template>
	<view class="title-wrap">
		<view class="head-bar" :style="{'margin-top': barHeight + 5 + 'px'}">
			<view class="go-back" @click="goback"></view>
			<text>职业兴趣测评</text>
		</view>
		<view class="progress-block">
			<view class="row">
				<view class="title-index">
					第{{pageIndex+1}}题
				</view>
				<view class="all-title">
					共{{allNum}}道题
				</view>
				<view class="time">
					约25分钟
				</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="item" v-show="pageIndex == 180">
				<view class="title">只能选一种[自我介绍]代表你自己，你最想选哪一种？</view>
				<view class="sel-label-block">
					<view class="long-label" :class="item.Value === firstDescVal ? 'sel' : ''" v-for="(item,index) in checkTitleList" :key="index"  @click="checkItem(1,item,index)">{{item.Title}}</view>
				</view>
			</view>
			<view class="item" v-show="pageIndex == 181">
				<view class="title">让您再选一种[自我介绍]代表你自己，你会选哪一种？</view>
				<view class="sel-label-block">
					<view class="long-label" v-show="item.Value != firstDescVal" :class="item.Value === secondDescVal ? 'sel' : ''" v-for="(item,index) in checkTitleList" :key="index"  @click="checkItem(2,item,index)">{{item.Title}}</view>
				</view>
			</view>
			<view class="item" v-show="pageIndex == 182">
				<view class="title">如果能有选择第三种，你会选哪一种？</view>
				<view class="sel-label-block">
					<view class="long-label" v-show="item.Value !== firstDescVal && item.Value !== secondDescVal" :class="item.Value === thirdDescVal? 'sel' : ''" v-for="(item,index) in checkTitleList" :key="index"  @click="checkItem(3,item,index)">{{item.Title}}</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 "@/apiB/testManage.js"
	export default {
		data() {
			return {
				barHeight: wx.getWindowInfo().statusBarHeight,
				list: [],
				allNum: 0,//总题目数
				pageIndex: 0, //显示页
				showBakcTip: false,
				lastTapTime: 0 ,// 记录上次点击的时间
				
				checkTitleList: [{
						Title: "我有操作机械的能力，倾向自己独立完成工作，避免做些常常与人沟通的事。爱动手操弄，喜欢做和机械、工具、电器、户外有关的工作。我认为自己是勤奋的技术家，期望从事机械、电子、土木建筑、工程技术等工作。",
						Checked: false,
						Value: 0
					},
					{
						Title: "我有分析推理的能力，倾向自己思考解决问题，胜过在商场或政界发展。喜欢观察、分析、探究和研发的工作，我认为自己是客观的科学家，期望从事物理、生物、化学、医药、数学、地科等相关工作。",
						Checked: false,
						Value: 1
					},
					{
						Title: "我乐于助人、善解人意并具洞察力，倾向与人接触胜过操作机械仪器，喜欢协助他人增进知识与技能、健全身心健康与信念。我认为自己是教育或心理学家，期望从事教师、咨商辅导、社会救助、健康照护等相关工作。",
						Checked: false,
						Value: 2
					},
					{
						Title: "我有敏捷文书整理或计算能力，倾向在有清楚规范的环境下工作，胜过创作或研究工作，喜欢有条理的将文书、数字与物品归类管理。我认为自己是谨慎的事务家。期望从事财务、金融、会计、物流管理等工作。",
						Checked: false,
						Value: 3
					},
					{
						Title: "我有艺术创作的能力，倾向运用想象力，胜过快速计算或物品归类。喜欢藉文字、声音、色彩或形式来表达美感创作。我认为自己是表现美的艺术家，期望从事音乐、写作、戏剧、绘画、设计、舞蹈等工作。",
						Checked: false,
						Value: 4
					},
					{
						Title: "我有领导、说服他人的能力，倾向在法政与商界工作，胜过基础科学研究。喜欢影响他人，获取相当的报酬。我认为自己是有自信的事业家，期望未来从事管理、销售、司法、从政等工作。",
						Checked: false,
						Value: 5
					}
				],
				firstDescVal: "", //第一种
				secondDescVal: "",//第二种
				thirdDescVal: "", //第三种
				
				historyTitle: [],// 历史题目
				
			}
		},
		computed: {
			rate: function() {
				let num = 0;
				this.list.forEach(item => {
					if (item.Value > 0) {
						num++
					}
				});
				if(this.firstDescVal !== ''){
					num++
				}
				if(this.secondDescVal !== ''){
					num++
				}
				if(this.thirdDescVal !== ''){
					num++
				}
				return Math.round(num * 100 / this.allNum)
			},
			commit: function(){
				let num = 0;
				this.list.forEach(item => {
					if (item.Value > 0) {
						num++
					}
				});
				if(this.firstDescVal !== ''){
					num++
				}
				if(this.secondDescVal !== ''){
					num++
				}
				if(this.thirdDescVal !== ''){
					num++
				}
				console.log(num);
				if(num == this.allNum){
					return true;
				}else {
					return false;
				}
			}
		},
		onLoad(e) {
			this.getHistoryTitle();
		},
		methods: {
			// 返回
			goback(){
				uni.showModal({
					title: '提示',
					content: '确认要放弃测评吗？',
					success: function (res) {
						if (res.confirm) {
							uni.navigateBack(-1);
						} else if (res.cancel) {						
						}
					}
				});
				//uni.navigateBack(-1);
			},
			//获取答题记录
			getHistoryTitle(){
				api.getTestRecordProcessList(11).then(res => {
					if (res.Result === 1) {
						let data = res.Data;
						this.historyTitle = data;
						this.getTitle();
					}
				})
			},
			// 获取题目
			getTitle() {
				uni.showLoading({
					title: "加载中..."
				}) 
				api.queryTestContent(11).then(res => {
					uni.hideLoading();
					if (res.Result === 1) {
						let list = res.Data;
						list.forEach(item => {
							item.Value = 0
							this.historyTitle.forEach(ritem=>{
								if(item.Id == ritem.TestTitleId){
									item.Value = Number(ritem.TestResult);
								}
							})
						})
						if(this.historyTitle.length > 180){
							let len = this.historyTitle.length - 180;
							for(let i = 0; i < len; i++){
								this.checkTitleList[i].Value = this.historyTitle[i + 180];
								this.checkTitleList[i].Checked = true;
							}
						}
						if(this.historyTitle.length == 0){
							this.pageIndex = 0;
						}else if(this.historyTitle.length == list.length){
							this.pageIndex = list.length - 1;
						}else {
							this.pageIndex = this.historyTitle.length;
						}
						this.allNum = list.length + 3;
						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;
					this.saveTestRecordProcess(this.list[INDEX].Id,NUM);
					setTimeout(()=>{
						if(this.pageIndex != this.allNum - 1){
							this.pageIndex = INDEX + 1;
						}
					},400)
				}
			},
			// 选中题目
			checkItem(TYPE,ITEM,INDEX) {
				switch(TYPE){
					case 1: {
						this.checkTitleList.forEach(item=>{
							item.Checked = false;
						})
						this.checkTitleList[INDEX].Checked = true;
						this.firstDescVal = ITEM.Value;
						this.secondDescVal = "";
						this.thirdDescVal = "";
						this.saveTestRecordProcess(181,ITEM.Value);
						break;
					}
					case 2: {
						this.checkTitleList.forEach(item=>{
							if(item.Value != this.firstDescVal){
								item.Checked = false;
							}
						})
						this.checkTitleList[INDEX].Checked = true;
						this.secondDescVal = ITEM.Value;
						this.thirdDescVal = "";
						this.saveTestRecordProcess(182,ITEM.Value);
						break;
					}
					case 3: {
						this.checkTitleList[INDEX].Checked = true;
						this.thirdDescVal = ITEM.Value;
						this.saveTestRecordProcess(183,ITEM.Value);
						break;
					}
				}
				setTimeout(()=>{
					if(this.pageIndex != this.allNum - 1){
						this.pageIndex = 180 + TYPE;
					}
				},400)

			},
			//存储答题记录
			saveTestRecordProcess(ID,VALUE){
				let data = {
					testType: 11,
					testTitleId: ID,
					testResult: VALUE
				}
				api.saveTestRecordProcess(data).then(res => {
					if (res.Result === 1) {
					}
				})
			},
			// 提交题目
			submitTitle() {
				
				// uni.showToast({
				// 	title: "请选择三个特质描述",
				// 	icon: "none"
				// })
				// return;

				let testStr = "";
				this.list.forEach(item => {
					testStr += `${item.Id}|${item.Value - 1},`
				});
				testStr += `181|${this.firstDescVal},182|${this.secondDescVal},183|${this.thirdDescVal}`
				uni.showLoading({
					title: "提交中"
				})
				let data = {testStr}
				api.removeTestRecordProcess(11).then((res) => {
					 return api.saveInterestTestResult(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: `/packageB/pages/testReport/interestTestReport`
							})
						},1000)
					} else {
						uni.showToast({
							title: res.Message,
							icon: "none"
						})
					}
				})
			}
		}
	}
</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 120rpx;
			.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: #F6F7F9;
						margin-bottom: 24rpx;
						border-radius: 12rpx 12rpx 12rpx 12rpx;
						font-size: 28rpx;
						color: #000000;
						&.sel {
							background: #EDF6FF;
							color: #1677ff;
						}
					}
					.long-label {
						align-items: center;
						width: 610rpx;
						padding: 20rpx 30rpx;
						background: #F6F7F9;
						margin-bottom: 24rpx;
						line-height: 40rpx;
						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>