<template>
	<view class="normal-login-container">
		<view class="title-content">
			<text class="title">哈密市重点项目<br>建设及工作可视<br>化管理平台</text>
		</view>

		<view class="login-form-content">
			<view class="welcome-text" @click="goToLogin()">
				<u-icon name="arrow-left" color="#C6C6CD" size="26"></u-icon>
				<text>找回密码</text>
			</view>

			<view class="item-text">电子邮箱验证</view>
			<view class="input-item flex align-center">
				<input v-model="emailAddress" class="input" type="text" placeholder="请输入您的电子邮件地址" maxlength="30" />
			</view>

			<button class="login-btn cu-btn block bg-blue lg round" @click="sendCode()">发送验证码</button>
		</view>
	</view>
</template>

<script setup>
	import {
		getCodeImg,
		getCheckcode
	} from '@/api/login'
	import {
		ref
	} from "vue";
	import user from '@/store/modules/user'
	import config from '@/config.js'
	import store from '@/store'
	const codeUrl = ref("");
	const captchaEnabled = ref(false);
	const globalConfig = ref(config);
	const loginForm = ref({
		userName: "",
		password: ""
	});
	// 隐私协议
	function handlePrivacy() {
		let site = globalConfig.value.appInfo.agreements[0];
		uni.navigateTo({
			url: `/pages/common/webview/index?title=${site.title}&url=${site.url}`
		});
	};
	// 用户协议
	function handleUserAgrement() {
		let site = globalConfig.value.appInfo.agreements[1]
		uni.navigateTo({
			url: `/pages/common/webview/index?title=${site.title}&url=${site.url}`
		});
	};

	// 获取图形验证码
	function getCode() {
		getCodeImg().then(res => {
			captchaEnabled.value = res.captchaEnabled === undefined ? true : res.captchaEnabled
			if (captchaEnabled.value) {
				codeUrl.value = 'data:image/gif;base64,' + res.img
				loginForm.value.uuid = res.uuid
			}
		})
	};

	async function handleLogin() {
		if (loginForm.value.userName === "") {
			uni.showToast({
				title: '请输入您的账号',
				mask: false,
				duration: 1000
			});
		} else if (loginForm.value.password === "") {
			uni.showToast({
				title: '请输入您的密码',
				mask: false,
				duration: 1000
			});
		} else {
			uni.showToast({
				title: '登录中，请耐心等待...',
				mask: false,
				duration: 1000
			});
			pwdLogin()
		}
	};
	// 密码登录
	async function pwdLogin() {
		/* this.$modal.closeLoading() */
		store.dispatch('Login', loginForm.value).then(() => {
			loginSuccess()
		}).catch(() => {
			if (captchaEnabled.value) {
				getCode()
			}
		})
	};

	function loginSuccess(result) {
		// 设置用户信息
		store.dispatch('GetInfo').then(res => {
			uni.switchTab({
				url: '/pages/work/index'
			});
		})
	}

	function goToLogin() {
		uni.navigateBack({})
	}

	let emailAddress = ref(null)

	function sendCode() {
		let reg = /^([a-zA-Z\d][\w-]{2,})@(\w{2,})\.([a-z]{2,})(\.[a-z]{2,})?$/;
		let isValid = reg.test(emailAddress.value)
		if (!isValid) {
			return uni.showToast({
				title: "邮箱格式不正确",
				icon: "error",
				duration: 2000
			})
		}
		getCheckcode({
			email: emailAddress.value
		}).then(res => {
			if (res.code === 200) {
				uni.navigateTo({
					url: `/pages/login/changePass/index?login=none&email=${emailAddress.value}`
				})
			}
		})
	}
</script>

<style lang="scss">
	page {
		height: 100%;
		background-color: #EAF0FA;
	}

	.normal-login-container {
		position: relative;
		width: 100%;
		height: 100%;
		background: linear-gradient(108deg, #192DE8 0%, #1DA0FF 53%, #84D8FF 100%);

		.title-content {
			width: 100%;
			height: 46%;
			z-index: 1;
			background: url('@/static/login_bg.png');
			background-size: 100% 100%;

			.title {
				position: absolute;
				top: 8.5%;
				left: 7.4%;
				font-size: 52rpx;
				font-weight: 700;
				color: #FFFFFF;
			}
		}

		.login-form-content {
			position: absolute;
			top: 32%;
			left: 0;
			width: 100%;
			height: 68%;
			padding: 40rpx 7%;
			background-color: #fff;
			border-radius: 40rpx 40rpx 0rpx 0rpx;

			.welcome-text {
				display: flex;
				align-items: center;
				margin: 60rpx 0;
				font-size: 44rpx;
				font-weight: 700;
				color: #C6C6CD;

				text {
					margin-left: 20rpx;
				}
			}

			.item-text {
				font-size: 36rpx;
				font-weight: 400;
				font-family: Source Han Sans CN-Regular, Source Han Sans CN;
				color: #343437;
			}

			.input-item {
				height: 90rpx;
				margin: 20rpx auto 30rpx;
				font-size: 28rpx;
				background-color: #f5f6f7;
				border-radius: 16rpx;

				.input {
					width: 100%;
					font-size: 14px;
					line-height: 20px;
					text-align: left;
					padding-left: 15px;
				}
			}

			.forget-pass-item {
				text-align: right;
			}

			.forget-pass {
				color: #1869F6;
				border-bottom: 2rpx solid #1869F6;
			}

			.login-btn {
				margin-top: 50rpx;
				height: 45px;
				background: #1869F6;
				border-radius: 16rpx;
			}

			.xieyi {
				color: #333;
				margin-top: 20px;
			}

			.login-code {
				height: 38px;
				float: right;

				.login-code-img {
					height: 38px;
					position: absolute;
					margin-left: 10px;
					width: 200rpx;
				}
			}
		}
	}
</style>