<template>
	<view class="content">
		<view class="input-group">
			<view class="input-row border">
				<text class="title">账号：</text>
				<m-input class="m-input" type="text" clearable focus v-model="account" placeholder="请输入账号"></m-input>
			</view>
			<view class="input-row">
				<text class="title">密码：</text>
				<m-input type="password" displayable v-model="password" placeholder="请输入密码"></m-input>
			</view>
		</view>
		<view class="btn-row">
			<button type="primary" class="primary" @tap="bindLogin">登录</button>
		</view>
		<view class="action-row">
			<navigator url="../reg/reg">注册账号</navigator>
			<text>|</text>
			<navigator url="../pwd/pwd">忘记密码</navigator>
		</view>
		<view class="oauth-row" v-if="hasProvider" v-bind:style="{top: positionTop + 'px'}">
			<view class="oauth-image" v-for="provider in providerList" :key="provider.value">
				<image :src="provider.image" @tap="oauth(provider.value)"></image>
				<!-- #ifdef MP-WEIXIN -->
				<button v-if="!isDevtools" open-type="getUserInfo" @getuserinfo="getUserInfo"></button>
				<!-- #endif -->
			</view>
		</view>
	</view>
</template>

<script>
	// import service from '../../service.js';
	// import {
	// 	mapState,
	// 	mapMutations
	// } from 'vuex'
	import mInput from '../../components/m-input.vue'
	import {sha256, sha224 } from './sha256.js'
	
	export default {
		components: {
			mInput
		},
		data() {
			return {
				providerList: [],
				hasProvider: false,
				account: '20040998',
				// account:"20190071",
				password: 'password',
				positionTop: 0,
				isDevtools: false,
			}
		},
		// computed: mapState(['forcedLogin']),
		methods: {
			onLoad() {
			  const sysInfo = uni.getSystemInfoSync();
			  console.log(sysInfo.windowWidth)
			  this.GLOBALUTIL.sysInfo = sysInfo
			},
			initPosition() {
				/**
				 * 使用 absolute 定位，并且设置 bottom 值进行定位。软键盘弹出时，底部会因为窗口变化而被顶上来。
				 * 反向使用 top 进行定位，可以避免此问题。
				 */
				this.positionTop = uni.getSystemInfoSync().windowHeight - 100;
			},
			invokeServer(body) {
				console.log(this.GLOBALUTIL.user)
				var form = [];
				for(let i in body){
					if(body.hasOwnProperty(i)){
						form.push(i+'='+encodeURI(body[i]));
					}
				}
				var bodyStr = form.join("&");
				uni.request({
				    url: this.GLOBALUTIL.url+'/client/api', //仅为示例，并非真实接口地址。
					method: 'POST',
				    data: body,
				    header: {
				        'Accept': 'application/json',
				        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
				    },
				    success: (res) => {
						console.log(JSON.stringify(res))
						this.GLOBALUTIL.user = res.data.loginresponse
				        this.text = 'request success';
						console.log(res.data.loginresponse)
						uni.reLaunch({
							url: '../main/main',
						});
				    }
				})
			},
			bindLogin() {
				var type = 0;
				var emailReg = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;  //邮箱
				var phoneReg = /^1[34578]\d{9}$/;  //手机
				var idcnReg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;  //身份证
				var sidnReg = /^[A-Za-z0-9]{4,20}$/;  //学号/工号(由字母、数字组成，4-20位)
				if (emailReg.test(this.account)) {
					type = 0;
				} else if (phoneReg.test(this.account)) {
					type = 1;
				} else if (idcnReg.test(this.account)) {
				  type = 2;
				} else if (sidnReg.test(this.account)) {
					type = 3;
				} else {
					type = 4;
				}
				this.invokeServer({
					username : this.account,
					password : sha256(this.password),
					type : type,
					command : 'cppLogin',
					response : 'json'
				});
				return
				/**
				 * 客户端对账号信息进行一些必要的校验。
				 * 实际开发中，根据业务需要进行处理，这里仅做示例。
				 */
				if (this.account.length < 5) {
					uni.showToast({
						icon: 'none',
						title: '账号最短为 5 个字符'
					});
					return;
				}
				if (this.password.length < 6) {
					uni.showToast({
						icon: 'none',
						title: '密码最短为 6 个字符'
					});
					return;
				}
				/**
				 * 下面简单模拟下服务端的处理
				 * 检测用户账号密码是否在已注册的用户列表中
				 * 实际开发中，使用 uni.request 将账号信息发送至服务端，客户端在回调函数中获取结果信息。
				 */
				const data = {
					account: this.account,
					password: this.password
				};
				const validUser = service.getUsers().some(function(user) {
					return data.account === user.account && data.password === user.password;
				});
				if (validUser) {
					this.toMain(this.account);
				} else {
					uni.showToast({
						icon: 'none',
						title: '用户账号或密码不正确',
					});
				}
			},
			toMain(userName) {
				this.login(userName);
				/**
				 * 强制登录时使用reLaunch方式跳转过来
				 * 返回首页也使用reLaunch方式
				 */
				if (this.forcedLogin) {
					uni.reLaunch({
						url: '../Security-Check/security-task-list',
					});
				} else {
					uni.navigateBack();
				}

			}
		},
		onReady() {
			this.initPosition();
			// #ifdef MP-WEIXIN
			this.isDevtools = uni.getSystemInfoSync().platform === 'devtools';
			// #endif
		}
	}
</script>

<style>
	.action-row {
		display: flex;
		flex-direction: row;
		justify-content: center;
	}

	.action-row navigator {
		color: #007aff;
		padding: 0 10px;
	}

	.oauth-row {
		display: flex;
		flex-direction: row;
		justify-content: center;
		position: absolute;
		top: 0;
		left: 0;
		width: 100%;
	}

	.oauth-image {
		position: relative;
		width: 50px;
		height: 50px;
		border: 1px solid #dddddd;
		border-radius: 50px;
		margin: 0 20px;
		background-color: #ffffff;
	}

	.oauth-image image {
		width: 30px;
		height: 30px;
		margin: 10px;
	}

	.oauth-image button {
		position: absolute;
		left: 0;
		top: 0;
		width: 100%;
		height: 100%;
		opacity: 0;
	}
</style>
