<template>
	<view class="page">
		<z-nav-bar></z-nav-bar>
		<!-- 公共组件-每个页面必须引入 -->
		<public-module></public-module>
		<view class="title">手机号验证</view>
		<view class="input_box">
			<text class="input_tit">手机号</text>
			<input type="text" v-model="phone" placeholder="请输入手机号" />
		</view>
		<view class="input_box">
			<text class="input_tit">验证码</text>
			<input type="text" v-model="code" placeholder="请输入验证码" />
			<button @click="getCode">{{codeText}}</button>
		</view>
		<view class="btn_box"><button @click="onSubmit">提 交</button></view>
	<music-play :playData="playData"></music-play>
	</view>
</template>

<script>
	 import musicPlay from '@/components/music.vue'
	import md5 from '@/plugins/md5';
	var clear;
	import store from '@/store';
	import socket from '@/config/socket';
	import {
		mapState,
		mapMutations
	} from 'vuex';
	export default {
		data() {
			return {
				playData:{},
				//手机号账号
				phone: '',
				// 密码
				password: '',
				//验证码
				code: '',
				//验证码
				codeText: '获取验证码',
				//验证码已发
				readonly: false
			};
		},
		//第一次加载
		onLoad(e) {
			// 隐藏原生的tabbar
			uni.hideTabBar();

			this.showPhone();
		},
		computed: {
			...mapState(["userInfo"])
		},
		//页面显示
		onShow() {
			// 隐藏原生的tabbar
			uni.hideTabBar();
		},
		components:{
			musicPlay	
		},
		//方法
		methods: {
			// 显示手机号
			showPhone() {
				let HealthMes = store.state.HealthMes;
				let IsPhoneHealth = this.$base.phoneRegular.test(HealthMes.cellPhone)
				if (IsPhoneHealth) {
					this.phone = HealthMes.cellPhone
				}
			},
			//获取验证码
			getCode() {
				if (this.readonly) {
					uni.showToast({
						title: '验证码已发送',
						icon: 'none'
					});
					return;
				}
				if (!this.phone) {
					uni.showToast({
						title: '请输入手机号',
						icon: 'none'
					});
					return;
				}
				if (!this.$base.phoneRegular.test(this.phone)) {
					uni.showToast({
						title: '手机格式不正确',
						icon: 'none'
					});
					return;
				}

				this.$http
					.get('book/user/sms/sendcode', {
						phone: this.phone,
						type: 2000
					})
					.then(res => {
						this.getCodeState();
					});
			},
			//验证码按钮文字状态
			getCodeState() {
				const _this = this;
				this.readonly = true;
				this.codeText = '60S后重新获取';
				var s = 60;
				clear = setInterval(() => {
					s--;
					_this.codeText = s + 'S后重新获取';
					if (s <= 0) {
						clearInterval(clear);
						_this.codeText = '获取验证码';
						_this.readonly = false;
					}
				}, 1000);
			},

			onSubmit() {

				if (!this.phone) {
					uni.showToast({
						title: '请输入手机号',
						icon: 'none'
					});
					return;
				}
				if (!this.$base.phoneRegular.test(this.phone)) {
					uni.showToast({
						title: '请输入正确的手机号',
						icon: 'none'
					});
					return;
				}
				if (!this.code) {
					uni.showToast({
						title: '请输入验证码',
						icon: 'none'
					});
					return;
				}
				let HealthMes = store.state.HealthMes;

				let httpData = {};
				httpData.code = this.code;
				httpData.phone = this.phone;
				httpData.yljkOid = HealthMes.yljkOid;
				httpData.userName = HealthMes.nameCN;
				httpData.customerIcons = HealthMes.customerIcons;

				this.$http
					.get('book/user/registerHs', httpData)
					.then(res => {
						uni.showToast({
							title: '手机验证成功',
							duration: 1000,
						});
						console.log(res)
						
						res.userInfo.token = res.token.token;
							setTimeout(() => {
								uni.navigateBack({
									delta: 1
								});
												
							}, 1000);
						this.setUserInfo(res.userInfo);	
											
						// socket.init();
											

						// uni.showModal({
						// 	title: '提示',
						// 	content: '该手机号已经注册过花生，您是否要绑定登录？',
						// 	success: function(res) {
						// 		if (res.confirm) {
						// 			console.log('用户点击确定');
						//   } else if (res.cancel) {
						// 			console.log('用户点击取消');
						// 		}
						// 	}
						// });

						// 	uni.showModal({
						// 		title: '提示',
						// 		content: '验证成功，请问您要通过该账户登录花生吗？',
						// 		success: function(res) {
						// 			if (res.confirm) {
						// 				this.setUserInfo(res.userInfo);
						// 				// socket.init();
						// 				uni.switchTab({
						// 					url: "/pages/home/home"
						// 				});
						// 			} else if (res.cancel) {
						// 				console.log('用户点击取消');
						// 			}
						// 		}
						// 	});

					});


				// let bangDing = {};
				// bangDing.phone = this.code;
				// httpData.password = this.phone;
				// httpData.hsuserId = HealthMes.yljkOid;
				// this.$http
				// 	.get('book/user/getEverhealthInfo', bangDing)
				// 	.then(res => {
				// 		console.log(res)
				// 		uni.showToast({
				// 			title: '验证成功',
				// 			duration: 600,
				// 		});
				// 	});

			}
		},

	};
</script>
<style lang="scss" scoped>
	@import '@/style/mixin.scss';

	.page {
		background-color: #ffffff;
		padding: 0 65rpx;
		min-height: 100vh;

		.title {
			padding: 60rpx 0 40rpx 0;
			font-size: 60rpx;
			color: #333333;
		}

		.input_box {
			display: flex;
			justify-content: space-between;
			height: 100rpx;
			padding-top: 30rpx;
			border-bottom: 1rpx solid #eeeeee;
			align-items: center;

			text {
				font-size: 30rpx;
				width: 180rpx;
			}

			input {
				flex: 1;
				height: 70rpx;
				line-height: 70rpx;
				font-size: 30rpx;
			}

			button {
				height: 78rpx;
				line-height: 78rpx;
				font-size: 30rpx;
				color: $themeColor;

				&:active {
					background-color: transparent;
				}
			}
		}

		.btn_box {
			margin-top: 70rpx;

			button {
				font-size: 32rpx;
				@include theme('btn_bg') color: #fff;
				height: 80rpx;
				line-height: 80rpx;
				border-radius: 50rpx;
			}
		}

		.protocol {
			font-size: 24rpx;
			color: #999999;
			text-align: center;
			margin-top: 20rpx;

			text {
				color: $themeColor;
			}
		}
	}
</style>
