<template>
	<view class="register">
		<tm-nav-bar left-icon="arrow-left" title="注册账号" @clickLeft="back" />
		<view class="content">
			<!-- 头部logo -->
			<view class="header">
				<image :src="logoImage"></image>
			</view>
			<!-- 主体 -->
			<view class="main">
				<wInput
					v-model="phoneData"
					type="text"
					maxlength="11"
					placeholder="手机号"
				></wInput>
				<wInput
					v-model="passData"
					type="password"
					maxlength="11"
					placeholder="登录密码"
					isShowPass
				></wInput>
				<view class="main-list oBorder">
					<view class="" style="color: red;">
					</view>
					<input class="main-input" v-model="imgCode" placeholder="请输入图形验证码"/>
					<image @click="reset()" style="width: 200rpx; height: 40rpx" :src="img64" mode=""></image>
				</view>
				<wInput
					v-model="verCode"
					type="number"
					maxlength="4"
					placeholder="短信验证码"
					isShowCode
					ref="runCode"
					@setCode="getVerCode()"
				></wInput>
			</view>
				
			<wButton 
				class="wbutton"
				text="注 册"
				:rotate="isRotate" 
				@click.native="startReg()"
			></wButton>
			
			<!-- 底部信息 -->
			<view class="footer">
				<text 
					@tap="isShowAgree" 
					class="cuIcon"
					:class="showAgree?'cuIcon-radiobox':'cuIcon-round'"
				> 同意</text>
				<!-- 协议地址 -->
				<navigator url="./explain?desc=1" open-type="navigate">《用户协议》</navigator>
				<navigator url="./explain?desc=2" open-type="navigate">《隐私政策》</navigator>
			</view>
		</view>
	</view>
</template>

<script>
	let _this;
	import store from "@/store/index.js";
	import wInput from '../../components/watch-login/watch-input.vue' //input
	import wButton from '../../components/watch-login/watch-button.vue' //button
	
	export default {
		data() {
			return {
				logoImage: 'https://img.agrimedia.cn/watch-app/logo.jpeg',
				phoneData:'', // 用户/电话
				passData:'', //密码
				imgCode: '',
				captcha_id: '',
				img64:'',
				verCode:"", //验证码
				showAgree:true, //协议是否选择
				isRotate: false, //是否加载旋转
			}
		},
		components:{
			wInput,
			wButton,
		},
		mounted() {
			_this= this;
			
			this.getImgcode();
		},
		methods: {
			getImgcode() {
				this.$store.dispatch('api/code').then(res => {
					this.img64 = res.captcha_src;
					this.captcha_id = res.captcha_id
				})
			},
			
			isShowAgree() {
				//是否选择协议
				_this.showAgree = !_this.showAgree;
			},
			
			// 刷新二维码
			reset() {
				this.getImgcode()
			},
			
			// 获取验证码
			getVerCode(){
				if (!this.imgCode) {
					uni.showToast({
					    icon: 'none',
						position: 'bottom',
					    title: '请输入图形验证码'
					});
					return false;
				}
				
				//获取验证码
				if (_this.phoneData == '') {
				     uni.showToast({
				        icon: 'none',
						position: 'bottom',
				        title: '手机号不能为空'
				    });
				    return false;
				}
				
				this.$refs.runCode.$emit('runCode'); //触发倒计时（一般用于请求成功验证码后调用）
				uni.showToast({
				    icon: 'none',
					position: 'bottom',
				    title: '验证码发送中'
				});
				
				this.$store.dispatch('api/sendSms', {
					type: 0, 
					mobile: this.phoneData, 
					captcha_id:  this.captcha_id,
					captcha_code: this.imgCode
				}).then(res => {
					
				})
				setTimeout(function(){
					_this.$refs.runCode.$emit('runCode',0); //假装模拟下需要 终止倒计时
				}, 60000)
			},
			back() {
				uni.navigateBack({
					delta: 1
				})
			},
		    startReg() {
				//注册
				console.log(this.phoneData, this.verCode, this.passData, this.showAgree, this.imgCode)
				if(this.isRotate){
					//判断是否加载中，避免重复点击请求
					return false;
				}
				if (this.showAgree == false) {
				    uni.showToast({
				        icon: 'none',
						position: 'bottom',
				        title: '请先同意《协议及隐私》'
				    });
				    return false;
				}
				if (this.phoneData == '') {
					console.log(this.phoneData)
				    uni.showToast({
				        icon: 'none',
						position: 'bottom',
				        title: '手机号不能为空'
				    });
				    return false;
				}
		        if (this.passData == '') {
		            uni.showToast({
		                icon: 'none',
						position: 'bottom',
		                title: '密码不能为空'
		            });
		            return false;
		        }
				if (this.verCode == '') {
				    uni.showToast({
				        icon: 'none',
						position: 'bottom',
				        title: '验证码不能为空'
				    });
				    return false;
				}
				
				this.$store.dispatch('api/register', {
					type: 0, 
					mobile: this.phoneData, 
					password: this.passData,
					code: this.verCode
				}).then(res => {
					console.log(res)
					uni.showToast({
					    icon: 'none',
						position: 'bottom',
					    title: '注册成功！'
					});
					
					setTimeout(function(){
						uni.reLaunch({
							url:'/pages/login/login'
						})
					},3000)
				})
				
				setTimeout(function(){
					_this.isRotate=false
				},3000)
		    }
		}
	}
</script>

<style>
	@import url("../../components/watch-login/css/icon.css");
	@import url("./css/main.css");
	
	.main-list{
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
		/* height: 36rpx; */   /* Input 高度 */
		color: #333333;
		padding: 40rpx 32rpx;
		margin:32rpx 0;
	}
	.img{
		width: 32rpx;
		height: 32rpx;
		font-size: 32rpx;
	}
	.main-input{
		flex: 1;
		text-align: left;
		font-size: 28rpx;
		/* line-height: 100rpx; */
		padding-right: 10rpx;
		margin-left: 20rpx;
	}
	.vercode {
		color: rgba(0,0,0,0.7);
		font-size: 24rpx;
		/* line-height: 100rpx; */
	}
	.vercode-run {
		color: rgba(0,0,0,0.4) !important;
	}
	.oBorder{
	    border: none;
	    border-radius: 2.5rem ;
	    -webkit-box-shadow: 0 0 60rpx 0 rgba(43,86,112,.1) ;
	    box-shadow: 0 0 60rpx 0 rgba(43,86,112,.1) ;
	}
</style>