<template>
	<view class="page">
		<!-- 公共组件-每个页面必须引入 -->
		<public-module></public-module>
		
		<view class="form">
			<view class="phone d-flex">
				<view class="title">手机号码</view>
				<input placeholder="请输入手机号" v-model="phone"  maxlength="11"/>
			</view>
		</view>
		<view class="main-text-color mx-3 my-4 mt-1">注:更改手机号码后，请用新的手机号码登录。</view>
		
		<view class="px-3">
			<button class="my-3 mt-4 d-flex a-center j-center main-bg-color" :loading="loading" :class="{'main-bf-hover-color':disabled}" type="primary" @tap="submit" :disabled="disabled">修改手机绑定</button>
		</view>
		
	</view>
</template>

<script>
	import {mapState,mapMutations} from "vuex"
	export default {
		data() {
			return {
				phone:"",
				disabled:true,
				loading:false
				
			}
		},
		watch:{
			phone(val){
				this.OnBtnChange();
			}
		},
		computed:{
			...mapState(['userInfo'])
		},
		methods: {
			...mapMutations(['emptyUserInfo']),
			// 改变按钮状态
			OnBtnChange(){
				if(this.phone){
					this.disabled=false; return;
				}
				this.disabled=true;
			},
			
			// 提交注册
			async submit(){
				// 验证手机号合法性
				if (!this.$base.phoneRegular.test(this.phone)) {
					return uni.showToast({title: '请输入正确的手机号码',icon: 'none'});
				}
				// 请求服务器，发送验证码
				let res = await this.$http.post('/user/updatePartnerMobile?id='+this.userInfo.sysUser.userId+'&mobile='+this.phone);
				this.phone="";
				uni.showToast({ title: '修改绑定手机成功，请重新登录', icon:"none",duration:2000 });
				setTimeout(() => {
					this.emptyUserInfo();
						uni.reLaunch({
							url:"/pages/login/login"
						})
						return true;
				}, 2000);
				
			}
		}
	}
</script>

<style>
.page{
	background: #F5F5F5;
	height: 100vh;
	width: 100%;
	position: absolute;
}

.form{
	margin-top: 30upx;
}

.phone{
	position: relative;
	height: 90upx;
	display: flex;
	align-items: center;
	background-color: #FFFFFF;
	padding: 0 30upx;
	margin-bottom: 26upx;
}
.phone .title{
	width: 160upx;
	flex-shrink: 0;
	color: #666;
}

.phone input{
	width: 350upx;
	font-size: 28upx;
}
</style>
