<template>
	<view class="content">
		<view class="list-box" v-if="userInfo.user_type==2">
			<view class="list-item" v-for="(item,index) in list"
				@click="navTo('/pages/index/main?id='+item.id+'&pid='+project_id)">
				<view class="list-bg">
					<image :src="baseURL + item.backgroundimage" mode=""></image>
				</view>
				<image class="list-icon" :src="baseURL+item.iocnimage" mode=""></image>
				<view class="list-name">
					{{item.name}}
				</view>
			</view>
		</view>
		<view v-else>
			<view class="top">
				<view class="top-bg">
					<image src="../../static/img/top.png" mode=""></image>
				</view>
				<view class="top-box">
					<view class="top-item" v-for="(item,index) in list" @click="tabClick(index)">
						<view class="top-main">
							<image v-if="tabCurrentIndex == index" class="top-icon" :src="baseURL + item.iconimage"
								mode="">
							</image>
							<image v-if="tabCurrentIndex != index" class="top-icon" :src="baseURL + item.icon2image"
								mode="">
							</image>
							<view class="top-name" :class="{curret:tabCurrentIndex==index}">
								{{item.name}}
							</view>
						</view>
						<image class="jt" v-if="index != list.length -1" src="../../static/img/jt.png" mode=""></image>
					</view>
				</view>
				<swiper :current="tabCurrentIndex" :style="{ height: height }" class="swiper-box" duration="300"
					@change="changeTab">
					<swiper-item class="tab-content" v-for="(tabItem, tabIndex) in list" :key="tabIndex">
						<scroll-view scroll-y="true" style="height: 100%;">
							<view class="student-box flex">
								<view class="student-left">
									<view class="shu">
									</view>
									<view class="student-name">
										组: <text>{{dquser.group}}</text>
									</view>
								</view>
								<view class="student-right" @click="qh()">
									<view class="student-qh">
										切换
									</view>
									<image class="student-back" src="../../static/icon/back.png" mode=""></image>
								</view>
							</view>
							<view class="step" v-for="(item,index) in tabItem.item" :key="index">
								<view class="step-top">
									<view class="shu"></view>
									<view class="step-title">{{item.name}}</view>
								</view>
								<view class="step-main  flex">
									<view class="step-left">分数(共{{item.max_score}}分)</view>
									<view class="step-right">
										<input class="step-input" type="digit" v-model="item.score"
											:max="item.max_score*1" placeholder-class="place" placeholder="请输入分数">
										<view class="step-font">分</view>
									</view>
								</view>
								<view class="step-info" v-if="item.info">
									<view class="set-info-title">说明:</view>
									<view class="set-info-mian">{{item.info}}</view>
								</view>
							</view>
							<view class="btn" @click="tj(tabItem)">
								确认提交
							</view>
							<view style="height:50px;">

							</view>
						</scroll-view>
					</swiper-item>
				</swiper>
			</view>
			<uni-popup ref="student" type="bottom">
				<view class="popup-box">
					<view class="popup-title flex">
						<view class="qx" @click="cancel">取消</view>
						<view class="qd" @click="cancel">确定</view>
					</view>
					<view class="popup">
						<scroll-view class="popup-right" scroll-y="true">
							<view v-for="(item,index) in userlist" :key="index" class="popup-right-item"
								:class="{currRight:item.id==dquser.id}" @click="changepeople(item,index)">
								{{item.group}}
							</view>
							<view class="bottom" style="height: 50px"></view>
						</scroll-view>
					</view>
				</view>
			</uni-popup>
			<!-- 111 -->
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex';
	import * as mTabbar from '@/components/m-tabbar/m-tabbar.vue'
	import {
		getproject,
		new_project,
		misssion,
		user_list,
		score
	} from '@/api/project.js'
	export default {
		data() {
			return {
				id: '',
				list: [],
				userlist: [],
				height: '',
				dquser: '',
				dqdjg: '',
				tabCurrentIndex: 0,
				project_id: '',
			};
		},
		computed: {
			...mapState('user', ['userInfo', 'hasLogin', 'urlFile']),
			...mapState(['baseURL'])
		},
		onLoad() {},
		onShow() {
			if (this.userInfo.user_type != 2) {
				uni.setTabBarItem({
					index: 1,
					"pagePath": "pages/user/grade",
					"iconPath": "../../static/tabbar/tab-grade.png",
					"selectedIconPath": "../../static/tabbar/tab-grade-current.png",
					"text": "我的成绩",
					"visible": true
				});
			} else {
				uni.setTabBarItem({
					index: 1,
					"pagePath": "pages/user/grade",
					"iconPath": "../../static/tabbar/tab-grade.png",
					"selectedIconPath": "../../static/tabbar/tab-grade-current.png",
					"text": "我的成绩",
					"visible": false
				});
			}

			this.loadData()
		},
		onReachBottom() {},
		onReady(res) {
			var _this = this;
			uni.getSystemInfo({
				success: resu => {
					const query = uni.createSelectorQuery();
					query.select('.swiper-box').boundingClientRect();
					query.exec(function(res) {
						_this.height = resu.windowHeight - res[0].top + 'px';
						console.log('打印页面的剩余高度', _this.height);
					});
				},
				fail: res => {}
			});
		},
		methods: {
			loadData() {
				const obj = this
				new_project({}).then(er => {
					if (!er.data.new) {
						this.$api.msg('暂无考试')
					}
					getproject({}, er.data.new.id).then(({
						data
					}) => {
						this.project_id = data.id;
						console.log(data);
						if (this.userInfo.user_type == 2) {
							this.list = data.mission
						} else {
							data.mission.forEach(e => {
								misssion({}, e.id, obj.userInfo.group_id, obj.project_id).then(
									er => {
										if (er.data.step.length != 0) {
											obj.id = er.data.id
											console.log(obj.id);
											obj.studentpf()
										}
									})
								console.log(e);
							})
						}
					})
				})
			},
			qh() {
				this.$refs.student.open()
			},
			cancel() {
				this.$refs.student.close()
			},
			studentpf() {
				user_list({}).then(({
					data
				}) => {
					if (data.length == 0) {
						return this.$api.msg('暂无组员')
					}
					this.dquser = data[0]
					this.dqdjg = 0
					this.userlist = data
					this.getproject()
				})
			},
			changepeople(item, index) {
				this.dquser = item
				this.dqdjg = index
				this.cancel()
				this.getproject()
			},
			//swiper 切换
			changeTab(e) {
				this.tabCurrentIndex = e.detail.current;
			},
			//顶部tab点击
			tabClick(index) {
				this.tabCurrentIndex = index * 1;
				console.log(index == this.tabCurrentIndex);
			},
			getproject() {
				uni.showLoading({
					title: '加载中'
				})
				misssion({}, this.id, this.dquser.id, this.project_id).then(({
					data
				}) => {
					uni.hideLoading()
					this.list = data.step
				})
			},
			next() {
				const obj = this
				obj.dqdjg++
				if (obj.dqdjg >= this.userlist.length) {
					uni.showModal({
						title: '提示',
						content: '当前关的全部组已全部评完',
						success: function(res) {
							if (res.confirm) {
								uni.switchTab({
									url: '/pages/index/index'
								})
							} else if (res.cancel) {}
						}
					})
				} else {
					console.log(obj.dqdjg);
					obj.$api.msg('当前组已评价，切换到下一个')
					obj.dquser = obj.userlist[obj.dqdjg]
					this.tabCurrentIndex = 0
					this.getproject()
				}
			},
			tj(opt) {
				const obj = this
				let isok = 0;
				try {
					opt.item.forEach(e => {
						if (!e.score) {
							obj.$api.msg('请输入' + e.name + '的分数')
							isok = 1
							throw Error()
						}
						if (e.score * 1 > e.max_score) {
							obj.$api.msg(e.name + '的最大分数为' + e.max_score + '，请不要超过这个分数')
							isok = 1
							throw Error()
						}
						score({
							score: e.score
						}, e.id, obj.dquser.id, obj.project_id).then(e => {})

					})
					if (isok == 1) {
						throw Error()
					} else {
						obj.$api.msg('该组已评价完成')
						// if (obj.tabCurrentIndex != obj.list.length - 1 && isok == 0) {
						// 	obj.tabCurrentIndex++
						// } else {
						// 	obj.next()
						// }
					}
				} catch (e) {
					//TODO handle the exception
				}
			},
			navTo(url) {
				uni.navigateTo({
					url,
				})
			}
		}
	};
</script>

<style lang="scss">
	page,
	.content {
		min-height: 100%;
		height: auto;
	}

	.list-item {
		width: 700rpx;
		margin: 30rpx auto;
		position: relative;
		padding: 40rpx 40rpx;
		display: flex;
		align-items: center;

		.list-bg {
			position: absolute;
			top: 0;
			left: 0;
			right: 0;
			width: 100%;
			height: 100%;

			image {
				width: 100%;
				height: 100%;
			}
		}

		.list-icon {
			width: 112rpx;
			height: 112rpx;
			border-radius: 24rpx;
		}

		.list-name {
			position: relative;
			z-index: 2;
			margin-left: 40rpx;
			font-size: 36rpx;
			font-family: Source Han Sans CN;
			font-weight: bold;
			color: #FFFFFF;
		}
	}

	.top {
		width: 750rpx;
		height: 300rpx;
		position: relative;

		.top-bg {
			width: 750rpx;
			height: 300rpx;
			position: absolute;
			top: 0;
			left: 0;
			right: 0;

			image {
				width: 100%;
				height: 100%;
			}
		}

		.top-box {
			padding-top: 44rpx;
			display: flex;
			justify-content: center;
			align-items: center;

			.top-item {
				display: flex;

				.top-main {
					margin: 0 10rpx;
					display: flex;
					flex-direction: column;
					align-items: center;

					.top-icon {
						width: 80rpx;
						height: 80rpx;
						background: #FFFFFF;
						border-radius: 50%;
					}

					.top-name {
						position: relative;
						z-index: 2;
						margin-top: 16rpx;
						font-size: 24rpx;
						font-family: PingFang SC;
						font-weight: 500;
						color: rgba(#ffffff, 0.7);
					}

					.curret {
						font-weight: bold;
						color: #2A9CEC;
						-webkit-text-stroke: 1rpx #FFFFFF;
						text-stroke: 1rpx #FFFFFF;
					}
				}

				.jt {
					margin-top: 40rpx;
					width: 38rpx;
					height: 22rpx;
				}
			}
		}
	}

	.student-box {
		margin: 34rpx auto 0;
		width: 698rpx;
		background: #FFFFFF;
		box-shadow: 0px 6rpx 12rpx 1rpx rgba(101, 101, 101, 0.06);
		border-radius: 28rpx;
		padding: 28rpx 22rpx;

		.student-left {
			display: flex;
			align-items: center;

			.shu {
				width: 6rpx;
				height: 30rpx;
				background: #F9A81B;
				border-radius: 3rpx;
			}

			.student-name {
				margin-left: 16rpx;
				font-size: 30rpx;
				font-family: PingFang SC;
				font-weight: bold;
				color: #000000;

				text {
					color: #40547E;
				}
			}
		}

		.student-right {
			display: flex;
			align-items: center;

			.student-qh {
				font-size: 26rpx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #666666;
				margin-right: 10rpx;
			}

			.student-back {
				width: 12rpx;
				height: 23rpx;
			}
		}
	}

	.step {
		margin: 34rpx auto 0;
		width: 698rpx;
		background: #FFFFFF;
		box-shadow: 0px 6rpx 12rpx 1rpx rgba(101, 101, 101, 0.06);
		border-radius: 20rpx;
		padding: 28rpx 22rpx;

		.step-top {
			display: flex;
			align-items: center;

			.shu {
				width: 6rpx;
				height: 30rpx;
				background: #F9A81B;
				border-radius: 3rpx;
			}

			.step-title {
				margin-left: 16rpx;
				font-size: 30rpx;
				font-family: PingFang SC;
				font-weight: bold;
				color: #000000;
			}
		}

		.step-main {
			padding: 50rpx 20rpx 32rpx;
			line-height: 1;

			.step-left {
				font-size: 28rpx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #666666;
			}

			.step-right {
				display: flex;
				align-items: center;

				.step-input {
					text-align: right;
					font-size: 32rpx;
					font-family: PingFang SC;
					font-weight: bold;
					color: #FF4C4C;
				}

				.place {
					line-height: 1;
					font-size: 28rpx;
					font-family: PingFang SC;
					font-weight: bold;
					color: #999999;
				}

				.step-font {
					margin-left: 10rpx;
					font-size: 28rpx;
					font-family: PingFang SC;
					font-weight: bold;
					color: #000000;
				}
			}
		}

		.step-info {
			margin-right: 20rpx;
			border-top: 1px solid #ECECEC;
			padding: 32rpx 20rpx 0;
			display: flex;
			align-items: flex-start;

			.set-info-title {
				width: 80rpx;
				flex-shrink: 0;
				margin-right: 10rpx;
				font-size: 24rpx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #666666;
				line-height: 41rpx;
			}

			.set-info-mian {
				font-size: 24rpx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #666666;
				line-height: 41rpx;
			}
		}
	}

	.btn {
		margin: 50rpx auto 0;
		width: 674rpx;
		height: 88rpx;
		background: #2A9CEC;
		border-radius: 20rpx;
		text-align: center;
		line-height: 88rpx;
		font-size: 36rpx;
		font-family: PingFang SC;
		font-weight: 500;
		color: #FFFFFF;
	}

	.popup-box {
		width: 750rpx;
		background: #FFFFFF;
		border-top-left-radius: 30rpx;
		border-top-right-radius: 30rpx;

		.popup-title {
			padding: 32rpx 38rpx;

			.qx {
				font-size: 28rpx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #666666;
			}

			.qd {
				font-size: 28rpx;
				font-family: PingFang SC;
				font-weight: bold;
				color: #2A9CEC;
			}
		}
	}

	.popup {
		display: flex;
		align-items: center;

		.popup-right {
			width: 100%;
			height: 500rpx;
			padding: 0 20rpx;

			.popup-right-item {
				text-align: center;
				padding: 30rpx 20rpx;
				border-bottom: 1px solid #f7f7f7;
			}

			.currRight {
				color: #2A9CEC;
			}
		}
	}
</style>