<template>
	<view class="container register">
		<view class="register-main first clearfix">
			<view class="register-row clearfix">
				<view class="register-from">
					<view class="label">原手机号:</view>
					<view class="row-input">{{bindMobile}}</view>
				</view>
			</view>
			<view class="register-row clearfix">
				<view class="register-from code">
					<input class="row-input" type="number" v-model="mobileCode" placeholder="请输入原手机号的验证码" placeholder-class="place-holder" maxlength="6"/>
				</view>
				<view class="register-from btn" :class="[isMobileDisabled  ? 'disabled' : '']">
					<button class="row-input"  type="button" @click.stop="getMobileCodeFn" :disabled="isMobileDisabled">{{ mobileCodeText }}</button>
				</view>
			</view>
			<view class="register-row clearfix">
				<view class="register-from">
					<input class="row-input" type="number" v-model="contractMobile" placeholder="请输入新手机号" placeholder-class="place-holder" maxlength="11"/>
				</view>
			</view>
			<view class="register-row clearfix">
				<view class="register-from code">
					<input class="row-input" type="number" v-model="newMobileCode" placeholder="请输入新手机号的验证码" placeholder-class="place-holder" maxlength="6"/>
				</view>
				<view class="register-from btn" :class="[isNewMobileDisabled  ? 'disabled' : '']" >
					<button class="row-input"  type="button" @click.stop="getNewMobileCodeFn" :disabled="isNewMobileDisabled">{{ newMobileCodeText }}</button>
				</view>
			</view>
			<view class="register-row clearfix" style="border: 0;">
				<view class="register-btn"  @click="bindMobileCheck">确定</view>
			</view>
			<view class="register-text clearfix">
				<view >注意：本操作更换的是机构联系人的手机号，不是运营人员的手机号！</view>
			</view>
		</view>
	</view>
</template>

<script>
	import { mapMutations } from 'vuex';
	import { getClubMobileCode,getClubNewMobileCode } from "@/services/public.js" 
	import { changeMobile } from "@/services/use.js" 
	var self;
	export default{
		data() {
			return{
				userID:'',					//用户ID
			    count: '',					//倒计时
			    newCount: '',				//倒计时
			    codeTime: null,
			    codeTimeNew: null,
				bindMobile:'',				//联系人手机号
				contractMobile:'',			//新联系人手机号
				mobileCode:'',				//手机号验证码
				newMobileCode:'',			//新手机号手机验证码
				mobileCodeText: '获取验证码',
				newMobileCodeText: '获取验证码',
				isMobileDisabled: false,	//手机验证码按钮控制
				isNewMobileDisabled: false,	//手机验证码按钮控制
			}	
		},
		onLoad(option) {
			this.bindMobile = option.phone
		},
		methods:{
			bindMobileCheck(){
				let params = {
						contractMobile2:this.bindMobile,
						contractMobile:this.contractMobile,
						mobileCode:this.mobileCode,
						newMobileCode:this.newMobileCode,
						userID:this.userID
				}
				changeMobile(params).then(response =>{
					this.$util.msg('修改成功',1500,true,'success')
					setTimeout(()=>{
						uni.navigateBack({delta: 1})
					},2000)
				}).catch( error =>{
					this.$util.msg(error.msg,2000);
				})
			},
			getMobileCodeFn(){
				if( this.bindMobile == ''){
					this.$util.msg('请输入手机号',2000);
					return
				}
				if(!this.$reg.isMobile(this.bindMobile)){
					this.$util.msg('请输入正确的手机号',2000);
					return
				}
				let params = { mobile : this.bindMobile }
				this.isMobileDisabled = true;
				getClubMobileCode(params).then(response =>{
					this.$util.msg('验证短信已发送',2000);
					const TIME_COUNT = 60;
			     	if (!this.codeTime) {
			       		this.count = TIME_COUNT;
			       		this.isMobileDisabled = true;
			       		this.codeTime = setInterval(() => {
			       			if (this.count > 1 && this.count <= TIME_COUNT) {
			         			this.count--
			         			this.mobileCodeText = this.count +'s重新发送'
			        		} else {
				         		this.isMobileDisabled = false;
				         		clearInterval(this.codeTime)
				         		this.codeTime = null
								this.mobileCodeText = '获取验证码'
			        		}
			       		},1000)
			      	}		
				}).catch( error =>{
					this.$util.msg(error.msg,2000);
					this.isMobileDisabled = false;
				})
			},
			getNewMobileCodeFn(){
				if( this.contractMobile == ''){
					this.$util.msg('请输入手机号',2000);
					return
				}
				if(!this.$reg.isMobile(this.contractMobile)){
					this.$util.msg('请输入正确的手机号',2000);
					return
				}
				let params = { newMobile : this.contractMobile }
				this.isNewMobileDisabled = true;
				getClubNewMobileCode(params).then(response =>{
					this.$util.msg('验证短信已发送',2000);
					const TIME_COUNT = 60;
			     	if (!this.codeTimeNew) {
			       		this.newCount = TIME_COUNT;
			       		this.isNewMobileDisabled = true;
			       		this.codeTimeNew = setInterval(() => {
			       			if (this.newCount > 1 && this.newCount <= TIME_COUNT) {
			         			this.newCount--
			         			this.newMobileCodeText = this.newCount +'s重新发送'
			        		} else {
				         		this.isNewMobileDisabled = false;
				         		clearInterval(this.codeTimeNew)
				         		this.codeTimeNew = null
								this.newMobileCodeText = '获取验证码'
			        		}
			       		},1000)
			      	}
				}).catch( error =>{
					this.$util.msg(error.msg,2000);
					this.isNewMobileDisabled = false;
				})
			}
		},
		onShow() {
			this.$api.getStorage().then((resolve) =>{
				this.userID = resolve.userID
			})
		}
	}
</script>

<style lang="scss">
	.register{
		width: 100%;
		height: auto;
		border-top: 1px solid #F7F7F7;
		.model-warp.none{
			display: none;			
		}
		.model-warp.show{
			display: block;
		}
		.register-main{
			width: 100%;
			height: auto;
			&.detailed{
				padding-bottom: 300rpx;
			}
			&.first{
				padding-top: 40rpx;
			}
			.register-tips{
				display: flex;
				flex-direction: column;
				align-items: center;
				line-height: 44rpx;
				font-size: $font-size-24;
				color: #FF0000;
				margin-bottom: 40rpx;
				.iconfont{
					font-size: $font-size-24;
				}
			}
			.register-row{
				width: 702rpx;
				height: auto;
				margin: auto;
				// margin-bottom: 20rpx;
				border-bottom: 2rpx solid #e1e1e1;
				.register-title{
					line-height: 60rpx;
					font-size: $font-size-32;
					color: $text-color;
					text-align: left;
					padding-left: 20rpx;
					.txt{
						font-size: $font-size-26;
						font-weight: normal;
					}
				}
				.row-btn{
					position: absolute;
					right: 24rpx;
					top: 0;
					line-height: 88rpx;
					text-align: center;
					font-size: $font-size-28;
					color: $color-system;
				}
				.register-from{
					width: 702rpx;
					height: 64rpx;
					padding: 24rpx 0;
					position: relative;
					.label{
						text-align: left;
						font-size: $font-size-28;
						color: #666666;
						line-height: 64rpx;
						float: left;
					}
					.row-input{
						width: 440rpx;
						padding-left:10rpx;
						font-size: $font-size-28;
						color: $text-color;
						line-height: 64rpx;
						float: left;
						height: 64rpx;
						&.none{
							color: #999999;
						}
					}
					&.code{
						width: 410rpx;
						float: left;
						margin-right: 20rpx;
						.row-input{
							width: 390rpx;
						}
					}
					&.btn{
						width: 180rpx;
						height:64rpx;
						float: right;
						padding: 0;
						border: 2rpx solid #e15616;
						border-radius: 45rpx;
						.row-input{
							width: 180rpx;
							height: 64rpx;
							line-height: 64rpx;
							padding: 0;
							color:#E15616;
							text-align: center;
							border-radius: 45rpx;
							font-size: $font-size-24;
							&.none{
								background: #F7F7F7;
							}
						}
						&.disabled{
							background: #F7F7F7;
							border: 2rpx solid #F7F7F7;
							.row-input{
								background: #F7F7F7;
								color: #999999;
							}
						}
					}
					&.btn{
						margin-top: 20rpx;
					}
				}
				
			}
			.register-btn{
				width: 600rpx;
				height: 90rpx;
				border-radius: 45rpx;
				font-size: $font-size-28;
				line-height: 88rpx;
				color: #FFFFFF;
				margin: 0 auto;
				text-align: center;
				background: $btn-confirm;
				margin-top: 96rpx;
				&.sub{
					margin-top: 0;
				}
			}
		}
	}
	.register-text{
		width: 506rpx;
		color: #fea785;
		font-size: 22rpx;
		line-height: 32rpx;
		margin: 30rpx auto;
		text-align: center
	}
	.place-holder{
		color: #b2b2b2;
	}
</style>

