<template>
  <view class="regbox pt80 " :style="froms=='login'?'':'background-color: #f5f5f5'">
		
		<block v-if="froms=='login'">
			<image :src="sucimg" class="titimg"></image>
			<view class="search">
				<view class="f15 co16 fw mb10">手机号码</view>
				<input placeholder="请输手机号码" v-model="phonenumber"/>
			</view>
			<view class="apllytxt"><text>*</text>请输入您注册时填写的手机号码进行查询</view>
			<view class="rbtn mt40" @click="getQueryLogin">查询</view>
		</block>
		<pop-up :type='type' :regdat="regdat" @getregfn="getregfn" @getClose="getClose"></pop-up>
		<view v-if="showflag||froms=='mine'" class="mt15">
			<sh-list :datalist='list' :zhtype="zhtype"  @getUpdate="getUpdate" :froms="froms"></sh-list>
		</view>
		
  </view>
</template>

<script>
	import popUp from "@/components/popup/popup.vue"
	import shList from "@/components/shlist/list.vue"
	import {getqueryFn} from "@/api/mine/register.js"
  export default {
	data(){
		return{
			sucimg:require("@/mine/static/mine/success.png"),
			type:0,
			list:[],
			zhtype:7,
			froms:'login',
			regdat:{},
			phonenumber:'',
			showflag:false
		}
	},
	components:{popUp,shList},
	onUnload(){
		uni.$off('refreshstulist')
	},
	onLoad: function(e) {
		uni.$on('refreshstulist', (e) => {
			if(this.froms='mine'){
				this.reachflag = true;
				this.pageNum = 1;
				this.list = [];
				this.getQuery();
			}
		})
		if(e.from&&e.from=='mine'){
			this.froms=e.from;
			this.getQuery();
		}
	},
	methods:{
		getUpdate(e){
			// 个人中心才能去修改
			var id=e.id;
			var from=this.froms;
			if(e.zctype=='school'){
				this.$tab.navigateTo(`/mine/pages/mine/applyregister?type=update&id=${id}&from=${from}`)
			}else if(e.zctype=='teacher'){
				this.$tab.navigateTo(`/mine/pages/mine/applyteacher?type=update&id=${id}&from=${from}`)
			}else if(e.zctype=='parents'){
				this.$tab.navigateTo(`/mine/pages/mine/applyparent?type=update&id=${id}&from=${from}`)
			}	
		},
		getQueryLogin(){
			if(!this.phonenumber){
				this.$toast('请输入手机号')
				return
			}
			let regphone = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
			if(!regphone.test(this.phonenumber)){
				this.$toast('请输入正确的手机号')
				return
			}
			var params={
				phonenumber:this.phonenumber
			}
			var that=this;
			that.list=[];
			 getqueryFn(params).then(res=>{
				 if(res.code==200){
					 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
								}
							 }
						})
						if(b){
							//只要有一个拒绝  就展示列表
							// 展示列表
							var newArr=[];
							 Object.keys(dat).some((key) => {
								 if(key=='parents'){
									 var nArr=dat[key].registerParentsStudentList;
									 if(nArr&&nArr.length>0){
										nArr.forEach(ite=>{
											var newObja=ite;
											newObja.zctype=key;
											newArr.push(newObja)
										}) 
									 } 
								 }else if(key=='school'){
									 var newObjb=dat[key];
									 if(newObjb.isPass){
										newObjb.zctype=key;
										newArr.push(newObjb) 
									 } 
								 }else if(key=='teacher'){
									 var newObjc=dat[key];
									 if(newObjc.isPass){
										newObjc.zctype=key;
										newArr.push(newObjc) 
									 }   
								 }
							 })
							 that.showflag=true;
							 that.list=JSON.parse(JSON.stringify(newArr))	
						}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("您还未申请，请先去申请")
						}
					 }else{
						this.$toast("您还未申请，请先去申请")
					 }
				 }
				
			 })
		},
		getQuery(){
			var phonenumber=this.$store.state.user.phonenumber;
			 var params={
				phonenumber:phonenumber
			 }
			 var that=this;
			 getqueryFn(params).then(res=>{
				 if(res.code==200){
					 that.typeflag=false;
					 var a,b,c,isPass;
					 var nobj=res.data;
					 var newArr=[];
					Object.keys(nobj).some((key) => {
					 if(key=='parents'){
						 if(nobj[key].isPass){
							var nArr=nobj[key].registerParentsStudentList;
							nArr.forEach(ite=>{
								var newObja=ite;
								newObja.zctype=key;
								newArr.push(newObja)
							}) 
						 }
						 
					 }else if(key=='school'){
						 if(nobj[key].isPass){
							 var newObjb=nobj[key];
							   newObjb.zctype=key;
							 newArr.push(newObjb)
						 }
						 
					 }else if(key=='teacher'){
						 if(nobj[key].isPass){
							var newObjc=nobj[key];
							  newObjc.zctype=key;
							newArr.push(newObjc) 
						 } 
					 }
					 this.list=JSON.parse(JSON.stringify(newArr))
				 })
				 }
				
			 })
			 
		 },
		 getLogin(){
			  this.$tab.reLaunch(`/pages/login`)
		 },
		 getregfn(data){
			if(data==2){
		 		this.type = 0;
		 		this.$tab.reLaunch(`/pages/login`)
		 	}else{
		 		this.type = 0
		 	}
		 },
		 getClose(){
			 this.type=0
		 },
		 getConfirm(){
			 
		 }
	},
  }
</script>

<style lang="scss" scoped>
// .regbox{padding: 100rpx 86rpx;background-color: #f5f5f5;
.regbox{padding:24rpx 36rpx;background-color: #ffffff;
	.titimg{width: 210rpx;height: 210rpx;margin: 0 auto 28rpx;}
	.search{
		input{width: 100%;height: 80rpx;background: #FFFFFF;border: 2rpx solid #FFB132;border-radius: 36rpx;box-sizing: border-box;padding: 0rpx 40rpx;font-size: 30rpx;color: #161616;display: flex;align-items: center;}
	}
	.apllytxt{text-align: left;font-size: 26rpx;}
}
</style>
