<template>
	<view>
		<view class="head">
			<image class="head-immg" src="https://autoiot.oss-cn-beijing.aliyuncs.com/static/login.png" mode=""></image>
		</view>
		<view class="input">
			<view class="">
				<view class="text-1">
					您好，
				</view>
				<view class="text-2">
					欢迎登录AI销管～
				</view>
			</view>
			<view class="cwjs-cells item-flex" style="margin-top: 80rpx;">
				<view class="cwjs-item center">
					<image src="https://qufang.oss-cn-beijing.aliyuncs.com/channelHelper/user.png" class="logo_input">
					</image>
					<input class="cwjs-item cwjs-input" v-model="username" placeholder="请输入账号名" type="number"
						maxlength="11" placeholder-style="color:#AAAAAA"></input>
				</view>
			</view>
			<view class="cwjs-cells item-flex" style="margin-top:13rpx;">
				<view class="cwjs-item center">
					<image src="https://qufang.oss-cn-beijing.aliyuncs.com/channelHelper/lock.png" class="logo_input">
					</image>
					<input class="cwjs-item cwjs-input" placeholder="请输入密码" placeholder-style="color:#AAAAAA"
						type="password" v-model="password" maxlength="16" v-if="passwordType"></input>
					<input class="cwjs-item cwjs-input" placeholder="请输入密码" placeholder-style="color:#AAAAAA"
						maxlength="16" v-model="password" v-else></input>
				</view>
				<view class="imagesBox" @tap="changeBindPassword">
					<image v-if="passwordType==false" src="../../static/images/zhengkai.png" class="images"
						mode="scaleToFill"></image>
					<image v-if="passwordType==true" src="../../static/images/bishang.png" class="images"
						mode="scaleToFill"></image>
				</view>
			</view>
			<view class="textbox">
				<view class="textbox-1" @tap="bindVerification">验证码登录</view>
				<view class="textbox-2"></view>
				<view class="textbox-11" @tap="bindpassword">忘记密码</view>
			</view>

			<view class="button" @tap="bindWxBLogin">立即登录</view>
		</view>

	</view>
</template>

<script>
	var util = require("../../utils/util.js");
	var config = require("../../config");
	var app = getApp();
	var WXB_SESSION_LOGIN_DATA = 'weapp_session_login_data';
	export default {
		data() {
			return {
				username: '',
				//获取到的用户名的值
				password: '',
				//获取到的密码栏中的值
				passwordType: true,
				agreeChecked: true
			};
		},
		onLoad: function(options) {
			uni.hideToast();
		},
		methods: {
			//验证码登录
			bindVerification() {
				uni.navigateTo({
					url: '/pages/login/Verification?role=1',
				})
			},
			//忘记密码
			bindpassword() {
				// uni.navigateTo({
				// 	url: '/pages/login/Verification?role=2',
				// })
				uni.navigateTo({
					url: '/pages/login/Setthepassword',
				})
			},
			//密码选择
			changeBindPassword: function(e) {
				this.passwordType=!this.passwordType;
			},
			//登录
			bindWxBLogin: function(e) {
				util.showBusy('正在登录...');
				var that = this;
				if (this.username == '') {
					util.showNone("请输入账号名");
					return false;
				}
				if (this.password == '') {
					util.showNone("请输入密码");
					return false;
				}
				var loginParams = {
					username: this.username,
					password: this.password,
					scope: 'server',
					grant_type: 'password'
				}; 
				const user = util.encryption({
				        data: loginParams,
				        key: 'pigxpigxpigxpigx',
				        param: ['password']
				      })
					  console.log(user)
					  // return
				// 请求服务器登录地址，获得会话信息
				uni.request({
					url: config.service.login,
					header: {
						'Authorization': 'Basic dGVzdDp0ZXN0',
						'content-type': 'application/x-www-form-urlencoded'
					},
					method: "POST",
					data: {
						username:user.username,
						password:user.password,
						scope: 'server',
						grant_type: 'password'
					},
					success: function(result) {
						var data = result.data; //console.log("登录信息", data);
						if (data) {
							var res = data;
							console.log(res.code)
							if (res.code!=1) {
								var data = {
									'token': res.access_token
								};
								uni.setStorageSync(WXB_SESSION_LOGIN_DATA, data); //写入缓存
								that.getMenu()
								that.getUser();
								util.showSuccess('登录成功');
							} else {
								util.showNone("账号名或密码错误，请重试");
								return false;
							} 
						} else {
							util.showNone(data.message);
							return false;
						}
					},
					// 响应错误
					fail: function(loginResponseError) {
						util.showNone("网络异常，请重试");
						return false;
					}
				});
			},
			getUser(){
				util.getRequestPromise(config.service.getUser, {}, false, "GET").then(data => {
					if (data.user.total==0) {
						uni.showToast({
						    title: '暂无绑定楼盘',
						    duration: 2000,
							icon:"none"
						});
						return
					} else {
							let lopan = {
								id: data.houseList[0].id,
								name: data.houseList[0].propertyName
							}
							let users=data.user
							users.zkProperties=data.houseList
							uni.setStorageSync("fendianindex", 0); //写入缓存
							
							uni.setStorageSync("weapp_session_userInfo_data",users); //写入缓存
							
							uni.setStorageSync("buildingID", lopan); //楼盘id写入缓存
							uni.switchTab({
								url: '/pages/index/index'
							});
					}
				
				});
			},
			//获取权限
			getMenu(){
				uni.request({
					url: config.service.getMenu,
					method: "POST",
					header: {
						'content-type': 'application/json',
						'Authorization': 'Bearer '+uni.getStorageSync('weapp_session_login_data').token
					},
					success: (data) => {
						let rescor=data.data;
						if (rescor.code == 0) {
							uni.setStorageSync("weapp_session_Menu_data", rescor.data)
						}else{
							uni.showToast({
							    title: rescor.msg,
							    duration: 2000,
								icon:"none"
							});
							return
						}
					}
				})
			},
			
			
		}
	};
</script>
<style lang="scss">
	.cwjs-logo {
		display: block;
		width: 219rpx;
		height: 158rpx;
		margin: 54rpx auto 66rpx;
	}

	.cwjs-tips {
		font-size: 24rpx;
		padding: 80rpx 0;
		color: #8a8a8a;
	}

	.cwjs-form {
		position: relative;
		margin: 0;
		background-color: #fff;
		border-radius: 10px;
		padding: 20rpx 40rpx 113rpx;
	}

	.cwjs-cells {
		width: 510rpx;
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
		margin: 0 auto;
		height: 88rpx;
		overflow: hidden;
		border: 1rpx solid #BFC7D3;
		position: relative;
		border-radius: 12rpx;
	}

	.center {
		flex: 1;
	}

	.cwjs-input {
		height: 48rpx;
		line-height: 48rpx;
		padding: 23rpx 23rpx 23rpx 78rpx;
		font-size: 28rpx;
		font-family: PingFangSC-Regular, PingFang SC;
		color: #000;
	}

	.images {
		display: block;
		width: 40rpx;
		height: 21rpx;
		margin-right: 30rpx;
		margin-top: 33.5rpx;
	}

	.mod-btn {
		position: absolute;
		bottom: -80rpx;
		left: 50%;
		margin-left: -80rpx;
	}




	/* 头部 */

	.head {
		width: 750rpx;
		height: 500rpx;
		position: relative;

		.head-immg {
			// width: 118rpx;
			// height: 61rpx;
			// position: absolute;
			// top: 300rpx;
			// left: 100rpx;
			width: 750rpx;
			height: 778rpx;
			position: absolute;
		}
	}


	.logo {
		width: 248upx;
		height: 248upx;
		display: block;
		position: absolute;
		top: 134rpx;
		left: 240.5rpx;
	}


	/* 输入框 */
	.input {
		width: 630rpx;
		height: 824rpx;
		padding: 60rpx 60rpx 80rpx 60rpx;
		box-sizing: border-box;
		background: #FFFFFF;
		box-shadow: 0px 14px 20px -6px rgba(6, 16, 88, 0.1);
		border-radius: 20px;
		position: absolute;
		top: 107rpx;
		left: 60rpx;
	}

	.text-1 {
		font-size: 44rpx;
		line-height: 62rpx;
	}

	.text-2 {
		margin-top: 16rpx;
		font-size: 36rpx;
		line-height: 50rpx;
	}

	.logo_input {
		width: 34rpx;
		height: 38rpx;
		position: absolute;
		top: 25rpx;
		left: 30rpx;
	}

	/* 登录 */
	.button {
		width: 510rpx;
		height: 86rpx;
		background: #2671E2;
		box-shadow: 0px 2px 20px 0px rgba(38, 113, 226, 0.5);
		border-radius: 12rpx;
		text-align: center;
		line-height: 89rpx;
		color: #fff;
		margin: 0 auto;
		margin-top: 120rpx;
		font-size: 34rpx;

	}

	.footer {
		width: 100%;
		height: 157rpx;
		position: absolute;
		bottom: 0;
		left: 0;
	}

	.imagesBox {
		width: 80rpx;
		height: 88rpx;
	}



	.login {
		text-decoration: underline;
		text-align: center;
		margin-top: 40px;
		color: #88909E;
		font-size: 28rpx;
	}

	.textbox {
		width: 569rpx;
		display: flex;
		margin-top: 40rpx;
	}

	.textbox-1 {
		width: 30%;
		font-size: 30rpx;
		color: #999999;
	}

	.textbox-11 {
		width: 30%;
		text-align: right;
		font-size: 30rpx;
		color: #999999;
	}

	.textbox-2 {
		width: 30%;
	}
</style>
