<template>
  <view>
	  <view class="regbox">
	  	  <view class="flex1">
	  		  <image :src="headimg" class="headimg"></image>
	  		  <image :src="mzimg" class="mzimg"></image>
	  		  <view class="mt50">
	  			  <!-- 注册学校账号 -->
				  <view class="rbtn mb24" @click="getRegister">申请注册</view>
				  <!-- <button  type="primary" class="rbtn mb24 mt50" open-type="getUserInfo" @getuserinfo="getuserinfo">
				  	 <view class="rbtn mb24" >申请注册</view>
				  </button> -->
	  			 
	  			<view class="rtxt" @click="getlogin">已有账号，去登录</view>
	  		  </view>
	  	  </view>
		  
	  	 <view class="apllytxt flex0" @click="getQuery" >已注册，去查询</view>
	  </view>
	  <phone-btn :type="typeflag" @getPhoneNumber='getPhoneNumber' @getClose="getClose"></phone-btn>
	  <pop-up :type='type' :regdat="regdat" @getregfn="getregfn" @getClose="getClose"></pop-up>
  </view>
</template>

<script>
	import popUp from "@/components/popup/popup.vue"
	import phoneBtn from "@/components/toptab/phonebtn.vue"
	import {getweChatOpenid} from "@/api/login.js"
	import {getqueryFn} from "@/api/mine/register.js"
  export default {
	  components:{
		  phoneBtn,popUp
	  },
	data(){
		return{
			headimg:require('@/static/images/lricon.png'),
			mzimg:require('@/mine/static/mine/mzicon.png'),
			typeflag:false,
			type:0,
			wxInfo:{},
			regdat:{},
			typereg:'register'
		}
	},
	onLoad: function() {
		if(uni.getStorageSync('wxInfo')){
			this.wxInfo=JSON.parse(JSON.stringify(uni.getStorageSync('wxInfo')))
		}
		uni.removeStorageSync('nopasslist')
	},
	methods:{
		 getlogin(){
			  this.$tab.reLaunch(`/pages/login`)
		 },
		 getClose() {
		 	this.typeflag = false;
			this.type=0;
		 },
		 getregfn(data){
			 if(data==2){
				this.type = 0;
				this.$tab.reLaunch(`/pages/login`)
			 }else{
				this.type = 0
			 }
		 },
		 getRegister(){
			 this.$tab.navigateTo(`/mine/pages/mine/auth`)
		 },
		getPhoneNumber(data){
			var that=this;
			getweChatOpenid(data).then(res=>{
				if(res.code==200){
					that.typeflag=false;
					that.wxInfo =res.data
					uni.setStorageSync('wxInfo', JSON.parse(JSON.stringify(res.data)));
					// 判断是从哪来的
					if(that.typereg=='query'){
						that.getQuery()
					}else{
						that.$tab.navigateTo(`/mine/pages/mine/auth`)
					}
					
				}
				
			})
		},
		 async getuserinfo(e) {
		 	const {iv,encryptedData} = e.detail
		 	const {nickName, avatarUrl, ...userInfo} = e.detail.userInfo
		 	
		 	try{
		 			uni.login({
		            provider: 'weixin',
		            success: (res) => {
		                // 获取用户信息
		 				if(res.code){
		 					if(this.wxInfo.openId){
								this.$tab.navigateTo(`/mine/pages/mine/auth`)
							}else{
								this.typeflag=true;
								this.typereg="register"
							}
		 				}
		            }
		        })
		 	} catch(e){
		 		//TODO handle the exception
		 	}
		 },
		 getQuery(){
			 this.$tab.navigateTo('/mine/pages/login/query')
			 return
			 // 原来的获取授权
			 if(!this.wxInfo.openId){
				 // 授权
				uni.getUserProfile({
					desc: "获取你的昵称、头像、地区及性别",
					success: (res) => {
						if(!res.encryptedData || !res.iv){
							return false;
						}else{
							this.typeflag=true;
							this.typereg="query"
							return
						}
					},
					fail() {
						
					}
				})
				return
			 }
			 var params={
				 phonenumber:''
			 }
			 var that=this;
			 getqueryFn(params).then(res=>{
				 if(res.code==200){
					 that.typeflag=false;
					 var a,b,c,isPass;
					 var dat=res.data;
					 if(dat.parents||dat.school||dat.teacher){
						Object.keys(dat).some((key) => {
							 if(dat[key]&&dat[key].isPass){
								if (dat[key].isPass == 2) {
									a=1
								}
								if (dat[key].isPass == 3) {
									b=1
								}
								if (dat[key].isPass == 1) {
									c=1
								}
							 }
						})
						// uni.setStorageSync("nopasslist",JSON.parse(JSON.stringify(dat)))
						// that.$tab.navigateTo('/mine/pages/login/query')
						// return
						if(b){
							//只要有一个拒绝  就展示列表
							// var regdat={
							// 	tit:'注册失败',txt:'您提交的注册申请未通过 请重新注册',btn:'去注册',type:3,
							// }
							// that.regdat=regdat;
							// that.type=4;
							// 走列表页面
							uni.setStorageSync("nopasslist",JSON.parse(JSON.stringify(dat)))
							that.$tab.navigateTo('/mine/pages/login/query')
							
						}else if(a){
							// 通过
							var regdat={
								tit:'注册成功',txt:'您提交的注册申请已通过 可前去登录',btn:'去登录',type:2
							}
							that.regdat=regdat;
							that.type=4;	
						}else if(c){
							var regdat={
								tit:'注册审核',txt:'您提交的注册申请待审核 请耐心等待',btn:'取消',type:1
							}
							that.regdat=regdat;
							that.type=4;
						}else{
							this.$toast("您还未申请，请先去申请")
						}
						// this.$tab.navigateTo(`/mine/pages/login/query?isPass=${isPass}`)
						// console.log(a,b,c,isPass)
					 }else{
						this.$toast("您还未申请，请先去申请")
					 }
				 }
				
			 })
			 
		 }
	},
	
  }
</script>

<style lang="scss" scoped>
.regbox{padding: 160rpx 56rpx 100rpx;display: flex;flex-direction: column;}
.headimg{width: 136rpx;height: 136rpx;margin:0 auto 36rpx;}
.mzimg{width: 100rpx;height: 44rpx;margin: 0 auto ;}

.rtxt{font-size: 32rpx;font-weight: 500;
color: #4775EA;text-align: center;}
</style>
