<template>
	<view class="page" :style="{'min-height':h+'px', 'padding-top':mt+'px'}">
		<cus-header title='创衡汇教练AI智能体' :showback="false"></cus-header>
		<div class="content">
			<div class="c_top">
				<image src="@/static/logo2.png" mode="heightFix"></image>
				<div class="title">Hi，我是企业教练～</div>
				<div class="tip">很高兴遇见你！作为你的AI教练，我随时可以回答您的问题</div>
				<div class="item adf">
					<image src="@/static/qa.png"></image>
					<div class="qa">
						<p>智能问答</p>
						<p>正念修己 教练达人，知识推理整合，要点清晰呈现，助力企业打造高价值团队</p>
					</div>
				</div>
				<div class="item adf">
					<image src="@/static/qs.png"></image>
					<div class="qa">
						<p>PERILL模型问题库</p>
						<p>作为团队成员，您可以对所在团队相关描述的体验进行评分，帮助团队有效评估团队的现状</p>
					</div>
				</div>
			</div>
			<div class="c_bottom">
				<button class="zt_btn" open-type="getPhoneNumber" @getphonenumber="decryptPhoneNumber">手机号一键登录</button>
				<button class="qx_btn" style="margin-top: 20rpx;" @tap="codeLogin">授权码登录</button>
			</div>
		</div>
		<u-popup :show="show" @close="cancel" mode="center" :round="20">
			<div class="code">
				<p>授权码登录</p>
				<u-input v-model="phone" placeholder="请输入手机号"></u-input>
				<div style="margin-top: 20rpx;"></div>
				<u-input v-model="code" placeholder="请输入授权码"></u-input>
				<div class="cbtns adfacjb">
					<div class="zt_btn" @tap="toCodeLogin">登录</div>
					<div class="qx_btn" @tap="cancel">取消</div>
				</div>
			</div>
		</u-popup>
	</view>
</template>

<script>
	export default {
		data(){
			return {
				show:false,
				phone:'',
				code:''
			}
		},
		methods:{
			decryptPhoneNumber(e){
				if(uni.getStorageSync('token')){
					uni.reLaunch({
						url:'/pages/home'
					})
					return
				}
				if(e.detail.code) this.wxLogin(e.detail.code);
			},
			codeLogin(){
				this.show = true;
			},
			toCodeLogin(){
				if(!this.$reg.mobile(this.phone)) return this.$showToast('请输入正确的手机号！')
				if(!this.code) return this.$showToast('请输入授权码！')
				this.$api.get('/wx/login-code',{
					checkCode:this.code,
					phone:this.phone
				}).then(res=>{
					if(res.data.code!==0) return this.$showToast(res.data.msg)
					this.loginSuccessFn(res);
				})
			},
			cancel(){
				this.code = '';
				this.show = false;
			},
			wxLogin(code){
				uni.showLoading({ title:'登录中' });
				let that = this;
				wx.login({
					success(res){
						that.$api.get('/wx/login',{
							code:res.code,
							phoneCode:code
						},false).then(res=>{
							if(res.data.code===0){
								that.loginSuccessFn(res);
							}else that.$showToast(res.data.msg)
						})
					}
				})
			},
			loginSuccessFn(res){
				uni.setStorageSync('token',res.data.data.token);
				uni.setStorageSync('userInfo',JSON.stringify({
					id:res.data.data.id,
					gender:res.data.data.gender,
					realName:res.data.data.realName,
					mobile:res.data.data.mobile,
					headUrl:res.data.data.headUrl,
					teamId:res.data.data.teamId,
					enterpriseId:res.data.data.enterpriseId,
					enterpriseName:res.data.data.enterpriseName,
					teamName:res.data.data.teamName
				}));
				
				uni.hideLoading();
				this.$showToast('登录成功');
				setTimeout(()=>{
					uni.reLaunch({ url:'/pages/home' })
				},1500)
			},
			cancelLogin(){
				// uni.exitMiniProgram();
				uni.reLaunch({ url:'/pages/home' })
			}
		}
	}
</script>

<style scoped lang="scss">
	.page{
		background: #FFFFFF;
		display: flex;
		flex-direction: column;
		box-sizing: border-box;
		.content{
			padding: 0 45rpx;
			flex: 1;
			overflow-y: auto;
			display: flex;
			flex-direction: column;
			justify-content: space-between;
			
			.c_top{
				&>image{
					height: 88rpx;
					margin-top: 100rpx;
				}
				.title{
					font-family: PingFang-SC, PingFang-SC;
					font-weight: bold;
					font-size: 48rpx;
					color: #252525;
					line-height: 67rpx;
					margin-top: 36rpx;
				}
				.tip{
					font-family: PingFangSC, PingFang SC;
					font-weight: 400;
					font-size: 30rpx;
					color: #646464;
					line-height: 47rpx;
					margin-top: 20rpx;
				}
				.item{
					margin-top: 64rpx;
					&>image{
						width: 64rpx;
						height: 64rpx;
					}
					.qa{
						width: calc(100% - 64rpx);
						padding-left: 24rpx;
						box-sizing: border-box;
						p{
							font-family: PingFang-SC, PingFang-SC;
							font-weight: bold;
							font-size: 32rpx;
							color: #252525;
							line-height: 45rpx;
							&:last-child{
								font-weight: 400;
								font-size: 28rpx;
								color: #646464;
								line-height: 47rpx;
							}
						}
					}
				}
			}
			
			.c_bottom{
				.qx_btn{
					margin-top: 36rpx;
				}
			}
		}
	}
	
	.code{
		width: 700rpx;
		padding: 40rpx 60rpx 40rpx;
		border-radius: 32rpx 32rpx 0 0;
		box-sizing: border-box;
		p{
			font-size: 40rpx;
			font-weight: bold;
			margin-bottom: 50rpx;
		}
		.cbtns{
			margin-top: 50rpx;
			&>div{
				width: calc(50% - 20rpx);
			}
			.qx_btn{
				background: #f1f1f1;
			}
		}
	}
</style>