<template>
	<view class="login_body">
		<view class="status_bar"></view>
		<uni-segmented-control :current="current" :values="items" @clickItem="onClickItem" styleType="text" activeColor="#010101" class="select"></uni-segmented-control>
		<view class="body">
			<view class="login" v-show="current==0">
				<input type="text" class="login_input" placeholder="eamil address" v-model="loginDate.username" @input="verificationLogin">
				<view class="login_input1">
					<input :password="!tag" class="login_input1_text"placeholder="password"  v-model="loginDate.password" @input="verificationLogin">
				    <image :src="eye" mode ="" class="see login_see" @click="seePwd"></image>
				</view>
				
				<view class="tips" v-show="loginErr">{{errMsg}}</view>
				<view class="forgot" @click="forgotPwd">Forgot your Password?</view>
				<button class="login_submit"  :disabled="isLogin" @click="login()">SIGN IN</button>
				<view class="agreement">
					<image src="../../static/img/select2.png" mode="" class="agreement_image" @click="loginSelect=!loginSelect,loginErr=false" v-show="!loginSelect"></image>
					<image src="../../static/img/select1.png" mode="" class="agreement_image" @click="loginSelect=!loginSelect" v-show="loginSelect"></image>
					<view class="agreement_right">
						by continuong, you agree to be <text class="text" @click="gotonewpage(1)"> terms of use </text> and <text class="text"@click="gotonewpage"> privacy policy</text>.
					</view>
				</view>
				
			</view>
			<view class="login" v-show="current==1">
				<input type="text" class="login_input" placeholder="name" @input="verificationReg" v-model="regDate.username">
				<input type="text" class="login_input" placeholder="eamil address" @input="verificationReg" v-model="regDate.email">
				<!-- <input type="text" class="login_input" placeholder="password" @input="verificationReg" v-model="regDate.password1"> -->
				<view class="login_input1">
					<input :password="!tag" class="login_input1_text"placeholder="password"  v-model="regDate.password1" @input="verificationReg">
				    <image :src="eye" mode ="" class="see login_see" @click="seePwd"></image>
				</view>
				<view class="tips" v-show="regErr">{{errMsg}}</view>
				<button class="login_submit" :disabled="isReg" @click="Reg()">REGISTER</button>
				<view class="agreement">
					<image src="../../static/img/select2.png" mode="" class="agreement_image" @click="regSelect=!regSelect" v-show="!regSelect"></image>
					<image src="../../static/img/select1.png" mode="" class="agreement_image" @click="regSelect=!regSelect" v-show="regSelect"></image>
					<view class="agreement_right">
						by continuong, you agree to be <text class="text"@click="gotonewpage(1)" > terms of use </text> and <text class="text" @click="gotonewpage(2)"> privacy policy</text>.
					</view>
				</view>
			</view>
			
			<view class="mode">
				<view class="mode_title">or continue with</view>
				<view class="mode_content">
					<image src="/static/img/facebook.png" class="mode_content_image"></image>
					<image src="/static/img/gugei.png" class="mode_content_image"></image>
					<image src="/static/img/apple.png" class="mode_content_image"></image>
				</view>
			</view>
		</view>
		<uni-popup ref="alertDialog" type="dialog">
			<uni-popup-dialog type="error" cancelText="Close" confirmText="Activation" title="Tips" content="Please activate the mailbox first" 
			@confirm="dialogConfirm" ></uni-popup-dialog>
		</uni-popup>
	</view>
</template>

<script>
	import {defaultRequest1,defaultRequest} from '../../api/index.js'
	export default {
		data() {
			return {
				items: ['SIGN IN', 'REGISTER'],
				current: 0,
				tag:false,
				eye:'../../static/icon/eye_close.png',
				loginDate:{
					username:'',
					password:'',
					_action:'loginbyun'
				},
				regDate:{
					username:'',
					email:'',
					password1:'',
					password2:'',
					_action:'registerun'
				},
				// 是否填写完成信息
				isLogin:true,
				isReg:true,
				// 是否勾选
				regSelect:false,
				loginSelect:false,
				// 是否显示错误信息
				backButtonPress:0,
				loginErr:false,
				regErr:false,
				errMsg:'The email address or password you enterde is incorrect.',
				query:{
					_action:'sendmessagesforvalidation',
					username:'',
				}
			}
		},
		// onBackPress(options) {
		// 					this.backButtonPress++;
		// 					if (this.backButtonPress > 1) { 
		// 						plus.runtime.quit();
		// 					} else {
		// 						plus.nativeUI.toast('Click again to exit');
		// 					} 
		// 					setTimeout(function() {
		// 						this.backButtonPress = 0;
		// 					}, 1000);
		// 					return true;
		// 			},
		methods:{
			seePwd(){
				
				this.tag = !this.tag;
				if(this.eye.includes('open')){
					this.eye = '../../static/icon/eye_close.png'
				}else{
					this.eye ='../../static/icon/eye_open.png'
				}
			},
			forgotPwd(){
				if(this.loginDate.username==''){
					uni.showToast({
						title:'Please fill in the user email first',
						icon:'none',
					})
					return
				}
				uni.navigateTo({
					url:'./verifyEmail?email='+this.loginDate.username
				})
			},
			onClickItem(e) {
				if (this.current != e.currentIndex) {
					this.current = e.currentIndex;
				}
			},
			//获取页面配置
			configuration(){
				let data={_action:'getpagedata',pagecode:'PAGE-LOGIN'}
				defaultRequest1(data).then(res=>{
					console.info(res)
				})
			},
			// 登录
			login(){
				if(!this.loginSelect){
					this.errMsg='Please read and tick the terms of service and privacy policy first.'
					this.loginErr=true
					return
				}
				console.log(this.loginDate)
				defaultRequest1(this.loginDate).then(res=>{
					console.info(res)
					if(res.error==0){
						this.loginErr=false
						res.data.isLogin=true
						uni.setStorageSync('isLogin',1)
						uni.setStorageSync('user_info',res.data)
						uni.switchTab({
							url:'../index/index'
						})
					}else{
						if(res.data){
							this.$refs.alertDialog.open()
							this.query.username=this.loginDate.username
						}else{
							this.loginErr=true
							this.errMsg=res.message
						}
					}
				})
			},
			// 验证邮件
			dialogConfirm(){
				console.log(this.query,'////')
				defaultRequest(this.query).then(res=>{
					console.info(res)
					if(res.error==0){
						uni.showToast({
							title:res.message,
							icon:'none',
						})
					}
				})
			},
			
			// 注册
			Reg(){
				if(!this.regSelect){
					uni.showToast({
						title:'please agree to and tick the User Agreement first',
						icon:'none',
					})
					return
				}
				this.regDate.password2=this.regDate.password1
				defaultRequest1(this.regDate).then(res=>{
					console.info(res)
					if(res.error==0){
						this.regErr=false
						uni.showToast({
							title:'Successful registration, please verify your email address!',
							icon:'success',
						})
					}else{
						this.regErr=true
						this.errMsg=res.message
					}
				})
			},
			// 验证是否填写登录信息
			verificationLogin(){
				if(this.loginDate.username && this.loginDate.password){
					this.isLogin=false
				}else{
					this.isLogin=true
				}
			},
			// 验证是否填写注册信息
			verificationReg(){
				if(this.regDate.username && this.regDate.password1 && this.regDate.email){
					this.isReg=false
				}else{
					this.isReg=true
				}
			},
			// 跳到页面
			gotonewpage(e){
				
				if(e==1){
					var urlStr = encodeURI("https://dopeplus.com/help/Terms.html")
					 // plus.runtime.openURL(urlStr);
					uni.navigateTo({
						url:'../order/paypalWebview?src='+urlStr
					})
				}
				else{
					var urlStr = encodeURI("https://en.meixx.com/help/privacy.html")
					
					 // plus.runtime.openURL(urlStr);
					uni.navigateTo({
						url:'../order/paypalWebview?src='+urlStr
					})
				}
				
				
			}

		},
		onLoad() {
			this.configuration()
			
		}
	}
</script>

<style lang="scss" scoped>
	.login_body{
		padding-top: 145.33rpx;
	}
	.see{
		height: 30rpx;
		width: 30rpx;
		margin-top: 36rpx;
		float: right;
		
	}
	.select{
		margin-bottom: 80rpx;
		font-size: 33.33rpx;
		font-weight: bold;
		
	}
	.mode{
		position: fixed;
		bottom: 138.67rpx;
		width: 100%;
		left: 0;
		
		.mode_title{
			font-size: 26.67rpx;
			color: #666666;
			position: relative;
			text-align: center;
			width: 100%;
			
			&:before{
				content: "";
				height: 1px;
				width: 89.33rpx;
				background-color: #BCBCBC;
				position: absolute;
				left: 20%;
				top: 50%;
			}
			&:after{
				content: "";
				height: 1px;
				width: 89.33rpx;
				background-color: #BCBCBC;
				position: absolute;
				right: 20%;
				top: 50%;
			}
		}
		.mode_content{
			display: flex;
			align-items: center;
			justify-content: center;
			margin-top: 50.67rpx;
			
			.mode_content_image{
				width: 54.67rpx;
				height: 54.67rpx;
				margin-right: 49.33rpx;
				&:last-child{
					margin-right: 0;
				}
			}
		}
	}
	.agreement_right{
		width: 582rpx;
	}
</style>