<template>
	<view class="content">
		<view class="title">
			<text class="thing" style="width: 100rpx;"></text>
			<text class="name">用户登录</text>
			<text class="thing" style="width: 300rpx;"></text>
		</view>
		<view class="form-box">
			<view class="row-input">

				<u-icon name="account" color="#2979ff" size="50" style="padding: 0 20rpx;"></u-icon>
				<input v-model="account" placeholder="请输入用户账号" maxlength="18" clearable />
			</view>
			<view class="row-input">
				<u-icon name="lock" color="#2979ff" size="50" style="padding: 0 20rpx;"></u-icon>
				<input v-model="password" placeholder="登陆密码" clearable :password="!isShowPassword" />
				<u-icon name="eye-fill" color="#2979ff" size="50" style="padding-right:20rpx" v-if="isShowPassword"
					@click="showOrHide"></u-icon>
				<u-icon name="eye-off" color="#2979ff" size="50" style="padding-right:0rpx" v-else @click="showOrHide">
				</u-icon>
			</view>

			<!-- <view class="rowinputcaptcha">
				<u-icon name="hourglass" color="#2979ff" size="50" style="padding: 0 20rpx;"></u-icon>
				<input class="captchainput" v-model="captcha"  placeholder="验证码"/>
				<u--image :showLoading="true" :src="captchaPath" width="150px" height="35px" @click="getCaptcha"></u--image>

			</view> -->

			<view class="menu-link">
				<text @click="forget">找回密码</text>
			</view>
			<view class="login-btn" @click="loginbtn">登录</view>
			<view class="tip-link">
				还没有账号?<text @click="register">注册</text>
			</view>
		</view>

		<u-toast ref="uToast" />
	</view>
</template>

<script>
	// function getUUID() {
	//   return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
	//     return (c === 'x' ? (Math.random() * 16 | 0) : ('r&0x3' | '0x8')).toString(16)
	//   })
	// }
	//const BaseApi= require("@/http/baseApi.js");

	export default {
		data() {
			return {
				isShowPassword: false,
				account: 'admin',
				password: 'cx868688',
				captcha: '',
				uuid: '',
				captchaPath: '',


			}
		},

		onLoad() {
			//	 this.getCaptcha();
		},

		methods: {
			// 获取验证码
			//  getCaptcha() {
			//    this.uuid = getUUID();
			// this.captchaPath =BaseApi.BaseApi+ `/captcha?uuid=${this.uuid}`;

			//  },

			loginbtn() {

				if (this.account == '') {
					this.$refs.uToast.show({
						type: 'error',
						message: "请输入账号",
					})
					// uni.showToast({
					// 	title: "请输入账号",
					// 	icon:'error',
					// 	duration: 1000,
					// })
				} else if (this.password == '') {
					this.$refs.uToast.show({
						type: 'error',
						message: "请输入密码",
					})
				}

				let formData = {
					username: this.account,
					password: this.password,
					// uuid:this.uuid,
					// captcha:this.captcha,
				}
				//登录功能
				this.$api.post('/login', formData)
					.then(res => {

						if (res.data.code == 0) {

							uni.setStorageSync('tokendata', res.data.data.token); //token
							uni.setStorageSync('Userinformation', formData); //用户信息

							//获取用户的信息，如组织架构，个人信息，权限等
							Promise.all([this.getpermissions(), this.getDictList(), this.getuserInfo(), ]).then(() => {

								// uni.switchTab({
								// 	url: '/pages/index/index',
								// 	success: res => {},
								// 	fail: () => {},
								// 	complete: () => {},
								// })
								uni.navigateTo({
									url: '/pages/index/index'
								})
							});


						} else {
							this.$refs.uToast.show({
								type: 'error',
								message: "账号或者密码错误，请重新输入",
							})
						}
					})


			},

			getpermissions() {
				console.log('权限功能', )
				//获取用户权限功能
				return new Promise((resolve, reject) => {
					this.$api.get('/menu/permissions', {}).then((res) => {
							uni.setStorageSync('ButtonPermissions', res.data.data);
						resolve(res);
					//	console.log('111111111111111111', res)

					}).catch(e => {
						reject(e)
					})
				});


				// this.$api.get('/menu/permissions', {})
				// 	.then(res1 => {
				// 		uni.setStorageSync('ButtonPermissions', res1.data.data); //权限列表
				// 	})
			},
			getDictList() {
				//获取字典列表, 添加并全局变量保存
				return new Promise((resolve, reject) => {
					this.$api.get('/all', {}).then((res) => {
						uni.setStorageSync('getDictDataList', res.data.data);
						resolve(res);
					//	console.log('222222', res)

					}).catch(e => {
						reject(e)
					})
				});


				// this.$api.get('/all', {})
				// 	.then(res => {
				// 		//  console.log('88888', res.data.data)
				// 		uni.setStorageSync('getDictDataList', res.data.data);
				// 	})
			},
			getuserInfo() {
				//获取用户信息
				return new Promise((resolve, reject) => {
					this.$api.get('/user/userInfo', {}).then((res) => {
						uni.setStorageSync('getuserInfo', res.data.data);
						resolve(res);
						//console.log('333333', res)

					}).catch(e => {
						reject(e)
					})
				});

				// this.$api.get('/user/userInfo', {})
				// 	.then(res => {
				// 		//  console.log('88888', res.data.data)
				// 		uni.setStorageSync('getuserInfo', res.data.data);
				// 	})
			},

			// 密码显示/密码隐藏
			showOrHide() {
				this.isShowPassword = !this.isShowPassword
			},

			// 找回密码
			forget() {
				uni.navigateTo({
					url: '/pages/login/forget',
					success: res => {},
					fail: () => {},
					complete: () => {},
				})
			},

			// 注册
			register() {
				uni.navigateTo({
					url: '/pages/login/register',
					success: res => {},
					fail: () => {},
					complete: () => {},
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #0b0c14;
	}

	.content {
		padding: 100rpx 40rpx;
	}

	.title {
		display: flex;
		flex-direction: column;
		height: 200rpx;

		.thing {
			height: 8rpx;
			background-image: linear-gradient(to right, #3feaea, #0168e6);
		}

		.name {
			font-weight: bold;
			font-size: 50rpx;
			line-height: 120rpx;
			background: linear-gradient(to right, #3feaea, blue);
			background-clip: text;
			color: transparent;
			letter-spacing: 5rpx;
		}
	}

	.form-box {
		.rowinputcaptcha {
			display: flex;
			justify-content: flex-start;
			align-items: center;
			height: 110rpx;
			//background-color: #2b2f41;
			border-radius: 8rpx;
			margin-bottom: 50rpx;

			input {
				width: 300rpx;
				font-size: 30rpx;
				color: #a1a2a3;
			}
		}


		.row-input {
			display: flex;
			justify-content: flex-start;
			align-items: center;
			height: 110rpx;
			background-color: #2b2f41;
			border-radius: 8rpx;
			margin-bottom: 50rpx;

			image {
				margin: 0 30rpx;
				flex-shrink: 0;
				width: 38rpx;
				height: 38rpx;
			}

			input {
				width: 460rpx;
				font-size: 30rpx;
				color: #a1a2a3;
			}
		}

		.menu-link {
			display: flex;
			justify-content: flex-end;
			align-items: center;
			font-size: 28rpx;
			color: #676c7f;
		}

		.login-btn {
			margin-top: 60rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			font-size: 35rpx;
			letter-spacing: 15rpx;
			color: #FFFFFF;
			height: 110rpx;
			border-radius: 8rpx;
			background-image: linear-gradient(to right, #3feaea, #0168e6);
		}

		.tip-link {
			position: fixed;
			letter-spacing: 4rpx;
			bottom: 50rpx;
			left: 40rpx;
			font-size: 28rpx;
			color: #a1a2a3;

			text {
				padding-left: 10rpx;
				color: #226ef0;
			}
		}
	}

	.captchainput {
		width: 300rpx;
	}
</style>
