<template>
	<view :class="[AppTheme]" class="all_box">
		<view class="loginstatus">
			<view class="sign_in_title">手机号注册</view>
			<!-- 登录信息 -->
			<view class="login_infon">
				<view class="inputline">
					<u-input placeholder="请输入手机号" v-model="loginphone" border="bottom" fontSize="22"></u-input>
				</view>
				<view>
					<view class="inputline">
						<u-input placeholder="请输入验证码" v-model="verificationcode" border="bottom" fontSize="22">
							<template slot="suffix">
								<u-button  :color="primary" @tap="getCode" :disabled="!ifsend" :text="codemess"
									:type="ifsend?'success':'warning'" shape="circle"></u-button>
							</template>
						</u-input>
					</view> 
				</view>
				<view class="inputline">
					<u-input placeholder="请输入密码" v-model="loginpassword" :password="true" border="bottom" fontSize="22"></u-input>
				</view>
			</view>
			<navigator url="../login/login" class="gologin">已有账号,点击登录</navigator>
			<!-- 登录按钮 -->
			<view class="login_button bg-linear-gradient" @click="register()">
				注册
			</view>
		</view>
	</view>
</template>
<script>
	import loginApi from "../../../api/login/login.js"
	export default {
		data() {
			return {
				primary:this.$theme.primary,
				// 登录手机号
				loginphone: null,
				// 验证码
				verificationcode: null,
				//密码
				loginpassword: null,
				//验证码是否可以发送
				ifsend: true,
				// 验证码状态
				codemess: '获取验证码'
			}
		},
		//下拉刷新
		onPullDownRefresh() {
			uni.startPullDownRefresh({
				success() {
					uni.stopPullDownRefresh()
				}
			})
		},
		onLoad() {
			console.warn(this.$u.config.v);
		},
		onShow() {
			this.$init_config();
		},
		methods: {
			// 获取验证码
			getCode() {
				let that = this
				if (that.loginphone !== null && that.loginphone !== undefined && that.loginphone.length === 11) {
					var myreg = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
					if (myreg.test(that.loginphone)) {
						uni.showLoading({
							title: '正在获取验证码'
						})
						setTimeout(() => {
							uni.hideLoading()
							//获取验证码
							let params = {
								mobile: that.loginphone
							}
							loginApi.sendvecode(params).then(res => {
								if (parseInt(res.status) === 1) {
									uni.showToast({
										icon: 'none',
										title: '验证码已发送'
									})
									that.ifsend = false
								} else {
									uni.showToast({
										icon: 'none',
										title: res.msg
									})
								}
							})
						}, 1000)
						let num = 60
						that.timeer = setInterval(function() {
							if (num > 0) {
								that.codemess = num + 's后重新获取'
								num = num - 1
							} else {
								clearInterval(that.timeer)
								that.ifsend = true
								that.codemess = '重新获取验证码'
							}
						}, 1000)
					} else {
						uni.showToast({
							icon: 'none',
							title: '请输入有效的手机号'
						})
						return;
					}
				} else {
					uni.showToast({
						icon: 'none',
						title: '请输入有效的手机号'
					})
				}
			},
			// 注册接口
			register() {
				let that = this
				if (that.loginphone !== null && that.loginphone !== undefined && that.loginphone.length === 11) {
					var myreg = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
					if (myreg.test(that.loginphone)) {
						if (that.verificationcode !== null && that.verificationcode !== undefined && that.verificationcode
							.length > 0) {
							if (that.loginpassword !== null && that.loginpassword !== undefined && that.loginpassword
								.length > 0) {
								let params = {
									mobile: that.loginphone,
									code: that.verificationcode,
									password: that.loginpassword,
									pid: that.bindid
								}
								loginApi.reg(params).then(res => {
									if (parseInt(res.status) === 200) {
										uni.showToast({
											icon: 'none',
											title: '注册成功'
										})
										setTimeout(function() {
											uni.redirectTo({
												url: '/pagesC/pages/login/login'
											})
										}, 1000)
									} else {
										uni.showToast({
											icon: 'none',
											title: '注册失败' + res.msg
										})
										setTimeout(function() {
											uni.redirectTo({
												url: '/pagesC/pages/login/login'
											})
										}, 1000)
									}
								})
							} else {
								uni.showToast({
									icon: 'none',
									title: '请输入正确的密码'
								})
							}
						} else {
							uni.showToast({
								icon: 'none',
								title: '请输入正确的验证码'
							})
						}
					} else {
						uni.showToast({
							icon: 'none',
							title: '请输入正确的手机号'
						})
					}
				} else {
					uni.showToast({
						icon: 'none',
						title: '请输入正确的手机号'
					})
				}
			}
		}
	}
</script>
<style lang="scss" scoped>
	@import 'register.scss';
</style>
