<template>
	<view>
		<u-navbar title="修改手机号"></u-navbar>
		<view class="login-form">
			<view class="login-form-item">
				<view class="input">
					<u-input v-model="form.telephone" type="number" placeholder="请输入手机号" placeholder-style="font-size:16px;color:#ccc;"/>
				</view>
			</view>
			<view class="login-form-item">
				<view class="input">
					<u-input v-model="form.verifyCode" type="number" placeholder="请输入验证码" placeholder-style="font-size:16px;color:#ccc;"/>
					<view class="code" @click="getCode">{{codeTips}}</view>
				</view>
			</view>
		</view>
		
		<u-button class="login-btn" type="success" shape="circle" @click="sure">确定修改</u-button>
		
		<u-verification-code :seconds="sendMsgSecond" ref="uCode" @change="codeChange" @end="end" @start="start" change-text="已发送(Xs)">
		</u-verification-code>
	</view>
</template>

<script>
	import * as userApi from '@/apis/user.js'
	import * as loginApi from '@/apis/login.js'
	import {
		checkPhone
	} from '@/utils'
	
	export default {
		data() {
			return {
				form: {
					telephone: '',
					verifyCode: '',
				},
				isSendMsgIng: false,
				sendMsgSecond: 60,
				codeTips: '',
				jpcode: 'toLogin',
			}
		},
		methods: {
			codeChange(text) {
				this.codeTips = text;
			},
			start() {
				if (!this.isSendMsgIng) {
					uni.showLoading({
						title: "加载中",
						mask: true,
					})
					loginApi.getVerifyCode(this.form.telephone).then((response) => {
						uni.hideLoading();
						this.carhelp.set("getvcodetime", new Date().getTime());
			
						if (!"") {
							//倒计时
							uni.showToast({
								title: "发送成功"
							})
						} else {
							uni.showToast({
								title: "您的验证码已经发送[5分钟有效]，请勿重复点击"
							})
						}
					}).catch(error => {
						uni.showToast({
							title: error,
							icon: "none"
						})
					})
				}
			},
			//倒计时
			end() {
				this.sendMsgSecond = 60;
				this.isSendMsgIng = false;
			},
			// 获取验证码
			getCode() {
				if (this.$refs.uCode.canGetCode) {} else {
					uni.showToast({
						title: '倒计时结束后再发送',
						icon: "none"
					})
					return
				}
			
				var checkPhoneResult = checkPhone(this.form.telephone);
			
				if (checkPhoneResult !== true) {
					uni.showToast({
						title: checkPhoneResult,
					})
					return;
				}
				this.$refs.uCode.start();
			},
			sure() {
				var checkPhoneResult = checkPhone(this.form.telephone);
				
				if(!this.form.telephone || checkPhoneResult != true) {
					uni.showToast({
						title: checkPhoneResult,
						icon: "none"
					})
					return;
				}
				if(!this.form.verifyCode) {
					uni.showToast({
						title: "请输入验证码",
						icon: "none"
					})
					return
				}
				
				uni.showLoading({
					title: "加载中",
					mask: true,
				})		
				userApi.changePhone(this.form).then((response) => {
					uni.hideLoading();
					
					uni.reLaunch({
						url: '/pages/index/index?jpcode=' + this.jpcode
					})
				}).catch(error => {
					uni.showToast({
						title: error,
						icon: "none"
					})
				})
			}
		}
	}
</script>
<style>
	page{
		background-color: #fff;
	}
</style>
<style lang="scss" scoped>
	.login-form{
		background-color: #fff;
		padding: 12px 28px;
		
	}
	.login-form-item{
		
		.title{
			margin-bottom: 8px;
		}
		.input {
			padding: 4px 0;
			border-bottom: 1px solid #f7f7f7;
			position: relative;
		}
		.code{
			position: absolute;
			right: 0;
			top:12px;
			color:#1677FF;
		}
		.tips {
			color: $u-type-info;
			margin-top: 12px;
			font-size: 12px;
			span{
				color:#3fbd70;
			}
		}
	}
		
	.login-btn {
		margin: 28px ;
		background-color:#00B962!important;
		border-color: #00B962!important;
		color:#fff!important;
	}
</style>
