<template>
	<view class="container">
		<view class="container_text">
			<image class="banner-img" src="/static/img/logo.jpg" mode="widthFix"></image>
		</view>
		<view class="login_text">
			<view class="login_input flex">
				<view class="login_img">
					<image src="/static/icon/ze4.png"></image>
				</view>
				<view class="login_name"><input class="uni-input"  v-model="store_name" focus
						placeholder="请输入商户名称" /></view>
			</view>
			<view class="login_input flex">
				<view class="login_img">
					<image src="/static/icon/ze.png"></image>
				</view>
				<view class="login_name"><input class="uni-input" @input="checkAccount" v-model="account" focus
						placeholder="请输入手机号" /></view>
			</view>
			<view class="login_input flex">
				<view class="login_img">
					<image src="/static/icon/ze2.png"></image>
				</view>
				<view class="login_name"><input class="uni-input" type="password" v-model="password" focus
						:placeholder="$t('login.a3')" /></view>
			</view>
			<view class="login_input flex">
				<view class="login_img">
					<image src="/static/icon/ze2.png"></image>
				</view>
				<view class="login_name"><input class="uni-input" type="password" v-model="trade_password" focus
						:placeholder="$t('login.b3')" /></view>
			</view>
			<view class="login_input flex">
				<view class="login_img">
					<image src="/static/icon/ze2.png"></image>
				</view>
				<view class="login_name"><input class="uni-input" type="text" v-model="spread" focus
						:placeholder="$t('login.b4')" /></view>
			</view>
			<view class="login_input flex" style="margin-bottom: 25rpx;">
				<view class="login_img">
					<image src="/static/icon/ze3.png"></image>
				</view>
				<view class="login_name flex">
					<input class="uni-input width" v-model="captcha" focus :placeholder="$t('login.b6')" />
					<view class="code" @click="verification">{{ countDown == 0 ? $t('login.b5') : countDown }}</view>
				</view>
			</view>
		    <!-- #ifdef H5 -->
		    <view class="upload"><a :href="baseURL+'/register/yzf.apk'">下载APP</a></view>
		    <!-- #endif -->
			<view><button type="green" @click="register" class="uni-button uni-button-green">{{$t('login.a7')}}</button>
			</view>
			<view><button class="uni-button uni-button-green uni-button-green-plain" type="green" plain="true"
					hover-class="none" @click="login">{{$t('login.a4')}}</button></view>
		</view>
	</view>
</template>
<script>
	import {
		register,
		verify
	} from '@/api/login.js';
	// #ifdef H5
	import {
		getAppVersion
	} from '@/api/index.js'
	// #endif
	import { mapState } from "vuex";
	export default {
		data() {
			return {
				// phone: '', //用户
				account: '', //用户
				password: '', //密码
				repassword: '',
				// invitation: '', //邀请码
				spread: '', //邀请码
				captcha: '', //验证码
				time: '', //保存倒计时对象
				countDown: 0, //倒计时
				trade_password: '', // 交易密码
				isPhone: true, //是否为手机号，默认为true
				store_name:''
			};
		},
		computed: {
			...mapState(['baseURL']),
		},
		onLoad(option) {
			uni.setNavigationBarTitle({
				title: this.$t("login.a7"),
			});
			// #ifndef MP
			if (option.spread) {
				// 存储其他邀请人
				uni.setStorageSync('spread', option.spread);
			}
			// #endif
			// #ifdef MP
			if (option.scene) {
				// 存储小程序邀请人
				uni.setStorage({
					key: 'spread_code',
					data: option.scene
				});
			}
			// #endif
			// 获取扫码邀请人id
			this.spread = option.spread || uni.getStorageSync('spread') || '';
		},
		watch: {
			// 监听倒计时
			countDown(i) {
				if (i == 0) {
					clearInterval(this.time);
				}
			}
		},
		methods: {
			// #ifdef H5
			domApp(type) {
				const bool = navigator.userAgent.toLowerCase().match(/MicroMessenger/i) == 'micromessenger';
				if (bool) {
					uni.showModal({
						// title: '提示',
						title: this.$t("enter.a7"),
						// content: '无法在微信中下载，请用浏览器打开下载',
						content: this.$t("login.c2"),
						showCancel: false,
					});
				} else {
					if (type == 'apk') {
						getAppVersion().then((res) => {
							console.log(res, 'res');
							window.open(res.data.apk);
						}).catch((err) => {
							console.log(err, 'err');
						})
						return
					}
					if(type=="ios"){
						window.open("/index/dom/iosdom.mobileconfig");
					}
				}
			},
			// #endif
			checkAccount() {
				const regPhone = /^1[3|4|5|7|8][0-9]{9}$/;
				const regEmail = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
				this.isPhone = regPhone.test(this.account);
			},
			// 注册
			register() {
				let obj = this;
				if (obj.account == '') {
					obj.$api.msg(obj.$t("login.a1"));
					return;
				}
				if (this.isPhone) {
					if (!/^1[3|4|5|7|8][0-9]{9}$/.test(this.account)) {
						this.$api.msg(obj.$t("safe.b8"));
						return;
					}
				} else {
					if (!/^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/.test(this.account)) {
						this.$api.msg(obj.$t("login.b9"));
						return;
					}
				}
				if(!this.store_name){
					this.$api.msg("请输入商户名称");
				}
				if (obj.password == '') {
					obj.$api.msg(obj.$t("reg.a9"));
					return;
				}
				if (obj.trade_password == '') {
					obj.$api.msg(obj.$t("login.b3"));
					return;
				}

				register({
					account: obj.account, //账号
					captcha: obj.captcha, //验证码
					password: obj.password, //密码
					trade_password: obj.trade_password, // 交易密码
					spread: obj.spread ,//上级推广人
					store_name:obj.store_name
				}).then(function(e) {
					// uni.showToast({
					// 	title: obj.$t("reg.c5"),
					// 	duration: 2000,
					// 	position: 'top'
					// });
					uni.showModal({
						title: obj.$t("enter.a7"),
						content: obj.$t("login.c3"),
						cancelText: obj.$t("zy.b4"),
						confirmText: obj.$t("zy.b3"),
						success: res => {
							if(res.confirm){
								uni.navigateTo({
									url: '/pages/public/login'
								});
							}
						},
						fail: () => {},
						complete: () => {}
					});
				});
				//调用注册接口，成功跳转登录页
			},
			//发送验证码
			verification() {
				let obj = this;
				if (this.account == '') {
					this.$api.msg(obj.$t("login.a1"));
					return;
				}
				if (this.account.length < 11) {
					this.$api.msg(obj.$t("safe.b8"));
					return;
				}
				// 判断是否在倒计时
				if (obj.countDown > 0) {
					return false;
				} else {
					obj.countDown = 60;
					obj.time = setInterval(() => {
						obj.countDown--;
					}, 1000);
					//调用验证码接口
					verify({
							phone: obj.account,
							type: 'register'
						})
						.then(({
							data
						}) => {})
						.catch(err => {
							console.log(err);
						});
				}
			},
			login() {
				//返回登录
				uni.navigateTo({
					url: '/pages/public/login'
				});
			}
		}
	};
</script>

<style lang="scss">
	page {
		height: 100%;
	}

	.container {
		width: 100%;
		height: 100%;
		background-size: 100% 100%;
		background-image: url("../../static/img/loginBg.png");
		background-repeat: no-repeat;
	}

	.container_text {
		width: 100%;
		text-align: center;
		.banner-img {
			width: 200rpx;
			margin-top: 100rpx;
			border-radius: 100%;
		}
	}

	.login_text {
		margin: auto 10rpx;
		position: relative;
		padding: 100rpx 72rpx;
		border-radius: 20rpx;

		.login_input {
			border-bottom: 1px solid #f0f0f0;
			margin-bottom: 65rpx;

			.login_img image {
				height: 35rpx;
				width: 29rpx;
				// padding-right: 20rpx;
			}

			.uni-input {
				text-align: left;
				width: 100%;
				font-size: 28rpx !important;
			}
			.input-placeholder{
				color: #FFF;
			}
			
			.login_name {
				color: #FFFFFF;
				flex-grow: 1;
				margin-left: 20rpx;
			}
		}

		.other {
			margin-top: 60rpx;

			.fenge {
				width: 30%;
				height: 2rpx;
				background-color: #eeeeee;
			}

			.qita {
				font-size: 28rpx;
				color: #999999;
			}
		}
		.upload{
			color: #FFF;
			text-align: right;
			a{
				color: #FFF;
				font-size: 28rpx;
			}
		}

		// .weixin {
		// 	width: 75rpx;
		// 	height: 75rpx;
		// 	margin: 25rpx auto;
		// }

		// .weixin image {
		// 	width: 100%;
		// 	height: 100%;
		// }

		// .weixin_text {
		// 	text-align: center;
		// 	font-size: 28rpx;
		// 	color: #999999;
		// }

		.forget {
			font-size: 28rpx;
			width: 100%;
			text-align: right;
			color: #999999;
		}
		/* #ifdef H5 */
		.loadapp {
			border: 1px solid #feb041;
			background-color: transparent;
			color: #feb041;
			width: 45%;
		}
		/* #endif */

		.uni-button-green {
			color: #1257FE;
			background-color: #FFF;
			margin: 40rpx 0rpx;
			border-radius: 10rpx;
		}
		
		.uni-button-green-plain {
			border: 1px solid #FFF;
			margin: 40rpx 0rpx;
			border-radius: 10rpx;
			color: #FFF;
			background-color: transparent;
		}

		.uni-button {
			height: 85rpx;
			line-height: 85rpx;
		}
	}

	.loginTitle {
		position: absolute;
		top: 250rpx;
		width: 100%;
		text-align: center;
		color: #c6a674;
		font-size: 40rpx;
	}

	.forget {
		width: 100rpx;
		font-size: 24rpx;
		color: #ffffff;
		margin: 0px auto;
		border-bottom: 1px solid #ffffff;
	}

	.width {
		width: 325rpx !important;
	}

	.code {
		color: #FFF;
		font-size: 23rpx;
		border-left: 1px solid #eeeeee;
		width: 150rpx;
		flex-shrink: 0;
		text-align: center;
	}

	uni-button {
		height: 80rpx !important;
		line-height: 80rpx !important;
	}
</style>