<template>
	<view class="normal-login-container">
		<view class="logo-content align-center justify-center flex">
			<image style="width: 100%;" :src="config.logoSrc" mode="widthFix">
			</image>
			<!-- <text class="title">OA移动端登录</text> -->
		</view>
		<view class="login-form-content">
			<view class="input-item flex align-center">
				<view class="iconfont icon-user icon"></view>
				<input v-model="loginForm.username" class="input" type="text" placeholder="请输入账号" maxlength="30" />
			</view>
			<view class="input-item flex align-center">
				<view class="iconfont icon-password icon"></view>
				<input v-model="loginForm.password" type="password" class="input" placeholder="请输入密码" maxlength="20" />
			</view>
			<view class="action-btn">
				<button @click="handleLogin" class="login-btn cu-btn block bg-blue lg round">登录</button>
			</view>
		</view>
	</view>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { checkAttendance } from '@/api/mine.js'
import config from '@/config.js'
import $modal from '@/plugins/modal.js'
import $tab from '@/plugins/tab.js'
import { useUserStore } from '@/store/user.js'
import { getLoginInfo } from '@/utils/auth'
const userStore = useUserStore()

const loginForm = ref({
	username: getLoginInfo().username || '',
	password: getLoginInfo().password || ''
})
function handleLogin() {
	const username = loginForm.value.username
	const password = loginForm.value.password
	if (username == '') {
		$modal.msgError("请输入您的账号")
	} else if (password == '') {
		$modal.msgError("请输入您的密码")
	} else {
		$modal.loading("登录中，请耐心等待...")
		// TEST: 测试登录接口
		userStore.Login(loginForm.value).then(() => {
			$modal.closeLoading()
			loginSuccess()
		})
	}
}
function loginSuccess() {
	userStore.GetInfo()
	// 是否在上班打卡时间段
	if (isInTimeRange(...config.signInTimeRange)) {
		const now = new Date()
		const params = {
			universalid: userStore.useId,
			rizi: (now.getFullYear()) + '-' + (now.getMonth() + 1) + '-' + (now.getDate())
		}
		// 获取当天考勤信息
		checkAttendance(params).then(({ returnParams }) => {
			if (returnParams.list.length) {
				// 已签到 跳转消息页
				$tab.reLaunch('/pages/message/index')
			} else {
				// 未签到 跳转考勤页面
				$tab.reLaunch('/pages/message/index?to=clockIn')
			}
		})
	} else {
		$tab.reLaunch('/pages/message/index')
	}
}
//判断时间是否在区间内
function isInTimeRange(start, end) {
	// 获取当前时间戳
	const now = Date.now();

	// 获取当前日期
	const currentDate = new Date(now);
	const year = currentDate.getFullYear();
	const month = currentDate.getMonth();
	const day = currentDate.getDate();

	// 构建当天的起始时间和结束时间的时间戳
	const startTime = new Date(year, month, day, ...start.split(':').map(Number)).getTime();
	const endTime = new Date(year, month, day, ...end.split(':').map(Number)).getTime();

	// 判断当前时间是否在范围内
	return now >= startTime && now <= endTime;
}
</script>

<style lang="scss">
@import '@/static/font/iconfont.css';

page {
	background-color: #ffffff;
}

.justify-center {
	justify-content: center;
}

.align-center {
	align-items: center;
}

.flex {
	display: flex;
}

view,
scroll-view,
swiper,
button,
input,
textarea,
label,
navigator,
image {
	box-sizing: border-box;
}

.normal-login-container {
	width: 100%;

	.logo-content {
		width: 100%;
		font-size: 21px;
		text-align: center;
		padding-top: 45%;

		image {
			border-radius: 4px;
		}

		.title {
			margin-left: 10px;
		}
	}

	.login-form-content {
		text-align: center;
		margin: 20px auto;
		margin-top: 15%;
		width: 80%;

		.input-item {
			margin: 20px auto;
			background-color: #f5f6f7;
			height: 45px;
			border-radius: 20px;

			.icon {
				font-size: 38rpx;
				margin-left: 10px;
				color: #999;
			}

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

		}

		.action-btn {
			.login-btn {
				margin-top: 40px;
				height: 45px;
				border-radius: 20px;
			}

			.login-btn::after {
				border: 0px;
				border-radius: 20px;
			}

			.cu-btn.block {
				display: flex;
			}

			.cu-btn.lg {
				padding: 0 40rpx;
				font-size: 32rpx;
			}

			.bg-blue {
				background-color: #0081ff;
				color: #ffffff;
			}

			.cu-btn {
				position: relative;
				border: 0rpx;
				display: inline-flex;
				align-items: center;
				justify-content: center;
				box-sizing: border-box;
				line-height: 1;
				text-align: center;
				text-decoration: none;
				overflow: visible;
				margin-left: initial;		transform: translate(0rpx, 0rpx);
				margin-right: initial;
			}
		}
	}
}
</style>