<template>
	<view class="page-box">


<!-- 
			<uni-segmented-control :current="current" :values="items" @clickItem="onClickItem" styleType="button"
				activeColor="#007aff">
			</uni-segmented-control> -->


			<view style="margin-top: 30px;">

				<uni-forms v-if="current==0" :modelValue="postForm" ref="postForm" :rules="rules" :label-width="100">
					<uni-forms-item name="username" label="登录账号" required>
						<uni-easyinput type="text" v-model="postForm.username" placeholder="用户名/手机号" />
					</uni-forms-item>
					<uni-forms-item name="password" label="登录密码" required>
						<uni-easyinput type="password" v-model="postForm.password" placeholder="请输入密码" />
					</uni-forms-item>

					<uni-forms-item name="captchaValue" label="图形验证" required>

						<view class="ca-box">
							<uni-easyinput class="input" type="text" v-model="postForm.captchaValue"
								placeholder="输入验证码" />
							<image class="img" @click="refreshCaptcha" :src="captchaUrl"></image>
						</view>

					</uni-forms-item>
				</uni-forms>


				<uni-forms v-if="current==1" :modelValue="postForm" ref="postForm" :rules="rules">
					<sms-box v-model="postForm" ref="smsBox" :type="2"></sms-box>
				</uni-forms>


				<view>
					<button type="primary" @tap="handleLogin">登录</button>
				</view>

				<view class="login-opt">
					<a @tap="$navs.toReg">立即注册</a>
					<a @tap="$navs.toForgot">忘记密码？</a>
				</view>


		</view>




		<view class="other-login">

			<!-- <view class="tt">其它登录方式</view> -->


			<!-- <view style="display: flex; align-items: center;"> -->



				<!-- #ifdef MP-WEIXIN -->
	<!-- 			<view class="types">
					<image src="../../static/images/we-chat.png" style="width: 35px; height: 35px;" @click="appLoginWx">
					</image>
					<view>微信登录</view>
				</view> -->
				<!-- #endif -->


		<!-- 		<view class="types">
					<image src="../../static/images/face-id.png" style="width: 35px; height: 35px;"
						@click="appLoginFace"></image>
					<view>人脸登录</view>
				</view> -->

			<!-- </view> -->



		</view>








	</view>
</template>

<script>
	import SmsBox from './components/SmsBox.vue'
	import {
		login,
		mobileLogin
	} from '@/api/user.js'
	import {
		wxLogin,
		apiGetWechatUrl
	} from '@/api/ability/login/wx.js'

	export default {
		components: {
			SmsBox
		},
		data() {
			return {
				captchaUrl: '',
				current: 0,
				items: ['账号登录', '手机登录'],
				rules: {
					username: {
						rules: [{
							required: true,
							errorMessage: '用户名不能为空！'
						}]
					},
					password: {
						rules: [{
							required: true,
							errorMessage: '密码不能为空！'
						}]

					},
					captchaValue: {
						rules: [{
							required: true,
							errorMessage: '图形验证码不能为空！'
						}]

					}
				},
				postForm: {
					captchaKey: '',
					captchaValue: '',
					smsCode: '',
					smsType: 2,
					username: '',
					password: ''
				}
			}
		},
		
		onBackPress(e) {
			this.backIndex()
			return true;
		},

		created() {
			this.refreshCaptcha()
		},

		methods: {
			
			backIndex(){
				uni.switchTab({
					url: '/pages/index/index'
				})
			},

			onClickItem(e) {
				if (this.current != e.currentIndex) {
					this.current = e.currentIndex;
				}
			},

			handleWxLogin(code, nickName, avatar) {

				uni.showLoading({
					title: '登录中...'
				});

				wxLogin({
					code: code,
					nickName: nickName,
					avatar: avatar
				}).then(data => {

					// 保存本地token
					this.loginBack(data)

					uni.hideLoading();

				})
			},



			appLoginWx() {


				let that = this

				// #ifdef MP-WEIXIN

				uni.showLoading({
					title: '请稍后...'
				});


				let code = '';


				// 获取微信code
				uni.login({
					provider: 'weixin',
					success: (res) => {
						code = res.code
					}
				})

				// 获取用户信息-新版
				if (uni.getUserProfile) {
					uni.getUserProfile({
						desc: "获取你的昵称,头像,地区以及姓名",
						success: (profile) => {
							if (profile.errMsg === 'getUserProfile:ok') {
								const data = JSON.parse(profile.rawData);
								that.handleWxLogin(code, data.nickName, data.avatarUrl);
							}
						}
					})
				}

				// #endif

				// #ifdef H5
				apiGetWechatUrl({
					state: 'h5'
				}).then(data => {
					window.location = data.url
				})
				//#endif
			},




			thirdLogin() {

				uni.login({
					success: function(loginRes) {
						console.log(loginRes.authResult);
					}
				});
			},

			refreshCaptcha() {
				this.postForm.captchaKey = new Date().getTime()
				this.captchaUrl = `${this.$urls.api}/api/common/captcha/gen?key=${this.postForm.captchaKey}`
			},

			quickLogin() {
				this.postForm.username = 'student'
				this.postForm.password = 'student'
				this.doLogin()
			},

			loginBack(data) {

				// 关闭加载
				uni.hideLoading()
				// 保存本地token
				uni.setStorageSync('token', data.token);
				
				if(!data.idCard){
					//打印请求返回的数据
					uni.redirectTo({
						url: '/pages/user/info/info'
					});
				}else{
					uni.setStorageSync('idCard', data.idCard);
					//打印请求返回的数据
					uni.switchTab({
						url: '/pages/index/index'
					});
				}
			},

			async mobileLogin() {


				// 消息框
				const smsCheck = await this.$refs.smsBox.validate().then(res => {
					return res
				});

				console.log('smsCheck', smsCheck)

				if (!smsCheck) {
					return;
				}


				// 对整个表单进行校验，返回一个 Promise
				this.$refs.postForm.validate().then((res) => {

					uni.showLoading({
						title: '登录中...'
					});

					// 手机登录
					mobileLogin(this.postForm).then(data => {
						this.loginBack(data)
					})

				}).catch(() => {

				})

			},

			accountLogin() {


				this.$refs.postForm.validate().then((res) => {

					uni.showLoading({
						title: '登录中...'
					});

					// 快速注册后开始考试
					login(this.postForm).then(data => {
						this.loginBack(data)
					});
				}).catch(() => {

				})

			},

			// 登录入口
			handleLogin() {

				if (this.current === 0) {
					this.accountLogin()
				} else {
					this.mobileLogin()
				}
			},

			// 人脸登录
			appLoginFace() {
				uni.redirectTo({
					url: '/pages/login/face'
				})
			}
		}
	}
</script>

<style>
	.other-login {

		flex-grow: 1;
		display: flex;
		align-items: center;
		justify-content: center;
		flex-direction: column;
	}

	.other-login .tt {

		text-align: center;
		border-bottom: #eee 1px solid;
		padding-bottom: 10px;
		width: 100%;
		color: #666;
	}

	.other-login .types {
		display: flex;
		align-items: center;
		flex-direction: column;
		justify-content: center;
		margin-top: 20px;
		font-size: 12px;
		color: #333;
		padding: 0px 10px 0px 10px;
	}

	.other-login .types uni-view,
	view {

		font-size: 12px;
		color: #666;
	}
</style>
