<template>
	<view class="container">
		<view class="container_text">
			<view>
				<image class="banner-img" src="../../static/image/logo.png" mode="scaleToFill"></image>
			</view>
			<view class="logName">
				卡羚能服智能电动车
			</view>
		</view>
		<view class="login_text">
			<view class="login_input flex">
				<view class="login_img">
					+86
				</view>
				<view class="login_name"><input class="uni-input" v-model="phone" focus placeholder="请输入手机号" />
				</view>
			</view>
			<view class="login_input flex">
				<view class="login_img">
					验证码
				</view>

				<view class="login_name flex">
					<input class="uni-input width" v-model="code" focus placeholder="请输入验证码" />
					<view class="code" @click="verification">{{ countDown == 0 ? '验证码' : countDown }}</view>
				</view>
			</view>
			<view><button type="green" class="uni-button uni-button-green" @click="toLogin">立即登录</button></view>
			<view><button type="green" class="uni-button uni-button-green-plain" plain="true" hover-class="none"
					@click="$refs.popup.open()">更多</button></view>
		</view>
		<!-- #ifdef APP-PLUS -->
		<!-- <loginMethods  @onLogin='loginGl'></loginMethods> -->
		<!-- #endif -->
		<agreement @checkedChange='changeChecked'></agreement>
		<uni-popup ref="popup" type="bottom">
			<view class="buttomBox">
				<navigator url="/pages/public/login">
					<view class="item borde-b">
						账号登录
					</view>
				</navigator>
				<navigator url="/pages/public/forget">
					<view class="item borde-b">
						忘记密码
					</view>
				</navigator>
				<navigator url="/pages/public/register">
					<view class="item">
						注册
					</view>
				</navigator>
			</view>
			<view class="buttomBox" @click="close">
				<view class="item qx">
					取消
				</view>
			</view>
		</uni-popup>

	</view>
</template>

<script>
	import {
		mapMutations
	} from 'vuex';
	import {
		verify,
		loginMobile,
		getUserInfo
	} from '@/api/login.js';
	// #ifdef APP-PLUS
	// applelogin接口需要开发编写，基础项目中可能没有
	import {
		applelogin
	} from '@/api/set.js';
	// loginWx接口需要开发编写，基础项目中可能没有
	import {
		loginWx
	} from '@/api/login.js';
	// #endif
	// #ifdef H5
	import {
		loginWinxin
	} from '@/utils/wxAuthorized';
	// #endif
	import loginMethods from "./loginMethods.vue"
	import agreement from "./agreement.vue"

	export default {
		data() {
			return {
				checked: false, //是否同意条款
				phone: '',
				code: '',
				time: '', //保存倒计时对象
				countDown: 0 //倒计时
			};
		},
		components: {
			loginMethods,
			agreement
		},
		watch: {
			// 监听倒计时
			countDown(i) {
				if (i == 0) {
					clearInterval(this.time);
				}
			}
		},
		onLoad() {

		},
		methods: {
			...mapMutations('user', ['setUserInfo', 'login']),
			changeChecked(check) {
				this.checked = check;
			},
			//登录
			async toLogin() {
				let obj = this;

				if (obj.phone == '') {
					obj.$api.msg('请输入电话号码');
					return;
				}
				if (obj.phone.length!=11) {
					obj.$api.msg('请输入正确的手机号');
					return;
				}
				if (obj.code == '') {
					obj.$api.msg('请输入验证码');
					return;
				}
				if (!obj.checked) {
					uni.showModal({
						title: '提示',
						content: '请先阅读同意《卡羚能服服务》《隐私条例》',
						showCancel: false,
					});
					return;
				}
				loginMobile({
					phone: obj.phone, //账号
					captcha: obj.code
				}).then(function(e) {
					uni.setStorageSync('token', e.data.token);
					getUserInfo({}).then(e => {
						obj.login();
						// 保存返回用户数据
						obj.setUserInfo(e.data);
						//成功跳转首页
						uni.switchTab({
							url: '/pages/index/index'
						});
					});
				}).catch((e) => {
					console.log(e);
				});
			},
			//发送验证码
			verification() {
				let obj = this;
				if (this.phone == '') {
					this.$api.msg('请输入电话号码');
					return;
				}
				if (this.phone.length < 11) {
					this.$api.msg('请输入正确的手机号');
					return;
				}
				// 判断是否在倒计时
				if (obj.countDown > 0) {
					return false;
				} else {
					obj.countDown = 60;
					obj.time = setInterval(() => {
						obj.countDown--;
					}, 1000);
					//调用验证码接口
					verify({
							phone: obj.phone,
							type: 'login'
						})
						.then(({
							data
						}) => {})
						.catch(err => {
							console.log(err);
						});
				}
			},
			// 后退
			navBack() {
				uni.navigateBack();
			},
			// 关闭更多弹窗
			close() {
				this.$refs.popup.close();
			}
		}
	};
</script>

<style lang="scss">
	page {
		height: 100%;
		background-color: #FFFFFF;
		padding-top: 44px;
	}

	.container {
		width: 100%;
		height: 100%;
		background-size: 100%;
		padding-top: 50rpx;
	}

	.container_text {
		text-align: center;

		.logName {
			font-weight: bold;
			font-size: 36rpx;
			margin: 30rpx 0 80rpx;
		}

		.banner-img {
			width: 170rpx;
			height: 170rpx;
			margin: 0 auto;
			border-radius: 20rpx;
		}
	}

	.login_text {
		margin: auto 50rpx;
		position: relative;
		background-color: #ffffff;
		border-radius: 20rpx;

		.login_input {
			border-bottom: 1px solid #f0f0f0;
			margin-bottom: 30rpx;
			padding: 30rpx 0;

			.login_img {
				height: 35rpx;
				width: 100rpx;
				flex-shrink: 0;
				font-size: 30rpx;
				text-align: center;
				margin-right: 20rpx;
				color: $font-base;
			}

			.uni-input {
				text-align: left;
				width: 100%;
				font-size: 28rpx !important;
			}

			.login_name {
				color: #333333;
				flex-grow: 1;

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

		.forget {
			font-size: 28rpx;
			width: 100%;
			text-align: right;
			color: #999999;
		}

		.uni-button-green {
			color: #ffffff;
			background-color: #5dbc7c;
			margin: 40rpx 10rpx;
			border-radius: 50rpx;
			box-shadow: 0px 8px 10px 0px rgba(166, 203, 184, 0.75);
		}

		.uni-button-green-plain {
			margin: 40rpx 10rpx;
			border-radius: 50rpx;
			color: $font-base;
			background-color: $page-color-base;
			border: none;
		}

		.uni-button {
			height: 80rpx;
			line-height: 80rpx;
			font-size: 28rpx;
			width: 100%;
		}
	}


	.buttomBox {
		border-radius: 20rpx;
		margin: 0 $page-row-spacing;
		margin-bottom: 30rpx;
		color: $color-green;
		background-color: #FFFFFF;
		overflow: hidden;

		.item {
			line-height: 100rpx;
			height: 100rpx;
			text-align: center;
			font-size: 32rpx;

			&.qx {
				font-weight: bold;
			}
		}

		.border_b {
			border-bottom: 1px solid $page-color-light;
		}
	}
</style>
