<template>
	<view>
		<u-navbar title="修改手机号" title-color="#101010"></u-navbar>
		<view class="main">
			<view class="original-password">
				<text>新手机号</text><u-input  v-model="form.telephone" style="padding: 0 0 0 20px"  maxlength="11" type="number"  placeholder="请填写新手机号"  />
			</view>
			<view class="new-password">
				<text>验证码</text><u-input  v-model="form.verifyCode" style="padding: 0 0 0 20px" maxlength="6" type="number"  placeholder="6位验证码"  />
				<text class="verification-code" :style="isCodeTipsColor ? 'color: #999999;' : ''" @click="getCode" >{{codeTips}}</text>
			</view>
			<u-verification-code :seconds="sendMsgSecond" ref="uCode" @change="codeChange" @end="end" @start="start"
				change-text="(Xs)">
			</u-verification-code>
		</view>
		<!-- 确认修改 -->
		<button class="confirm" @click="changePhone">确认修改</button>
	</view>
</template>

<script>
	import * as API from '@/apis/pagejs/user.js'
	
	import {
		checkPhone
	} from '@/apis/utils'
	
	export default {
		data() {
			return {
				form: {
					telephone: '',
					verifyCode: '',
				},
				isSendMsgIng: false,
				isCodeTipsColor: false,
				sendMsgSecond: 60,
				codeTips: '',
			}
		},
		onReady() {
			var time = this.carhelp.get("getvcodetime");
			if (time) {
				//this.$refs.uCode.start();
				var nowtime = new Date().getTime()
				var differ = (nowtime - time) / 1000
				if (differ < 2 * 60) {
					this.sendMsgSecond = 2 * 60 - parseInt(differ)
					this.isSendMsgIng = true;
					this.$refs.uCode.start();
					this.isCodeTipsColor = true;
				}
			}
		},
		methods: {
			
			changePhone() {
				
				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,
				})
			
				
			
				API.changePhone(this.form).then((response) => {
					uni.hideLoading();
					
					this.carhelp.logoff()
					uni.redirectTo({
						url: '/pages/login/login'
					})
				}).catch(error => {
					uni.showToast({
						title: error,
						icon: "none"
					})
				})
			
			},
			codeChange(text) {
				this.codeTips = text;
			},
			//倒计时
			end() {
				this.sendMsgSecond = 60;
				this.isSendMsgIng = false;
				this.isCodeTipsColor = 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({
						icon: "none",
						title: checkPhoneResult,
					})
					return;
				}
				this.$refs.uCode.start();
				this.isCodeTipsColor = true;
			},
			start() {
				if (!this.isSendMsgIng) {
					uni.showLoading({
						title: "加载中",
						mask: true,
					})
					API.getVerifyCode(this.form).then((response) => {
						uni.hideLoading();
						this.carhelp.set("getvcodetime", new Date().getTime());
			
						if (!"") {
							//倒计时
							uni.showToast({
								icon: "none",
								title: "发送成功"
							})
						} else {
							uni.showToast({
								icon: "none",
								title: "您的验证码已经发送[5分钟有效]，请勿重复点击"
							})
						}
					}).catch(error => {
						uni.showToast({
							title: error,
							icon: "none"
						})
					})
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	.main{
		background-color: #fff;
		margin-top: 24rpx;
		.original-password,.new-password{
			line-height: 96rpx;
			padding:0 32rpx;
			display: flex;
			align-items: center;
			border-bottom: 1px solid rgba(221,221,221,1);
			/deep/.u-input__input{
				height: 96rpx;
			}
			
		}
		text{
			display: inline-block;
			width: 128rpx;
			color: rgba(51,51,51,1);
			font-size: 32rpx;
		}
		.verification-code{
			width: 160rpx;
			color: rgba(22,119,255,1);
			font-size: 32rpx;
		}
	}
  // 确认修改
  .confirm{
	  border-radius: 8px;
	  background-color: rgba(22,119,255,1);
	  color: rgba(255,255,255,1);
	  font-size: 32rpx;
	  line-height: 88rpx;
	  margin: 24rpx 32rpx;
  }
</style>
