<template>
  <div class="wrapper">
    <div class="screen">
      <div class="container">
        <div class="main">
          <div class="schoolIcon">
            <img src="/HangZhouIcon.png" alt="杭州电子信息技术职业学校">
            <span>杭州电子信息技术职业学校</span>
          </div>
          <div id="sign-in-p" class="page active max">
            <form @submit="Login">
              <h2>用户名</h2>
              <input class="entry" type="text" placeholder="用户名" v-model="username">
              <h2>密码</h2>
              <input class="entry" type="password" placeholder="密码" v-model="password">
              <input class="submit" type="submit" value="登录">
            </form>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import {onMounted, ref} from 'vue';
import {login, autoLogin} from '../../api/user';
import {ElMessage} from 'element-plus';
import {useStore} from "../../store";
import {useRouter} from 'vue-router';

const username = ref<string>();
const password = ref<string>();
const store = useStore();
const router = useRouter();

// 验证用户名和密码的格式
const verify = () => {
  let valid = true;
  if (!username.value || !/^[a-zA-Z0-9_-]{3,16}$/.test(username.value)) {
    ElMessage.error("用户名格式不正确，应为3-16位字母或数字及下划线！");
    valid = false;
  }
  if (!password.value || !/^[a-zA-Z0-9_-~!@#$%^&*+=:><,./]{5,128}$/.test(password.value)) {
    ElMessage.error("密码格式不正确，应为5-128位允许的特殊字符！");
    valid = false;
  }
  return valid;
};

// 登录函数
const Login = () => {
  if (username.value && password.value) {
    if (verify()) {
      login(username.value, password.value).then(res => {
        let canLogin = res.data.data.canLogin;
        if (canLogin) {
          // 登录成功，进行用户数据处理并跳转
          store.$patch({
            userinfo: {
              username: res.data.data.username,
              headimg: res.data.data.base64,
              auth: res.data.data.auth,
              exclusiveColor: res.data.data.exclusiveColor,
              id: res.data.data.id
            }
          });
          router.push("/home/chat");
          ElMessage.success("登录成功！");
        } else {
          ElMessage.error("用户名或密码错误！");
        }
      }).catch(() => {
        ElMessage.error("登录请求失败！");
      });
    }
  } else {
    ElMessage.error("请输入用户名和密码！");
  }
};

onMounted(() => {
  let token = window.localStorage.getItem("token");
  if (token) {
    autoLogin().then(res => {
      let data = res.data.data;
      if (data.status) {
        ElMessage.success("已为您自动登录成功, 即将进行跳转");
        store.$patch({
          userinfo: data.userinfo
        });
        router.push("/home/chat");
      } else {
        ElMessage.error("自动登录失败，请重新输入账号和密码进行登录");
      }
    }).catch(() => {
      window.localStorage.clear();
      store.$reset();
      ElMessage.error("自动登录错误，请重新登录");
    });
  }
});

document.addEventListener("keydown", event => {
  if (event.key === 'Enter') {
    Login();
  }
});
</script>

<style scoped>
* {
  box-sizing: border-box;
}

.wrapper {
  height: 100%;
  width: 100%;
  opacity: .7;
  background: linear-gradient(225deg, rgba(65, 184, 131, 1) 0%, rgba(44, 0, 165, 1) 99%);
}

.screen {
  display: flex; /* 使用 flex 布局 */
  justify-content: center; /* 水平居中 */
  align-items: flex-start; /* 垂直居中 */
  height: 100vh;
}

.page {
  opacity: 0;
  width: 100%;
  max-width: 500px;
  padding-top: 10%;
  transition: .5s;
  overflow: hidden;
}

.page.active {
  opacity: 1;
  z-index: 10;
  transform: translateX(0);
}

#screen-right, #screen-left {
  background: white;
  height: 100vh;
  width: 100%;
  position: absolute;
  top: 0;
  transition: transform 0.3s ease;
}

.schoolIcon {
  display: flex;
  justify-content: center;
  align-items: center;
  margin: 10px;
}

.schoolIcon span {
  font-size: 1.5em;
  margin-left: 10px;
}

.max {
  max-width: 500px;
}

#screen-right {
  right: 100%;
}

#screen-left {
  left: 100%;
}

.container {
  padding: 10% 5% 5% 5%;
}

.main {
  position: relative;
  width: 100%;
}

.forgot {
  border-top: 1px solid white;
  text-align: center;
  margin-top: 10%;
}

li, h2, h3 {
  color: white;
  font-family: Arial, Helvetica, sans-serif;
}

h2 {
  padding: 10px 0;
}

h3 {
  display: inline-block;
  padding-top: 25px;
}

li, h2 {
  text-transform: uppercase;
}

li {
  display: inline-block;
  margin-right: 20%;
  padding-bottom: .25em;
  font-size: 1.3em;
  position: relative;
}

li:after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  width: 0;
  border-bottom: 2px solid #FFFFFF;
  transition: 0.4s;
}

.sign-btn {
  cursor: pointer;
}

.sign-btn:hover:after {
  width: 100%;
}

.sign-btn.active:after {
  border-bottom: 3px solid #3366FF;
  width: 100%;
}

.page {
  opacity: 0;
  position: absolute;
  width: 100%;
  padding-top: 10%;
  transition: .5s;
  overflow: hidden;
}

#sign-in-p.active, #sign-up-p.active {
  opacity: 1;
  z-index: 10;
  transform: translateX(0);
}

#sign-in-p {
  transform: translateX(100%);
}

.entry {
  width: 100%;
  height: 2.3em;
  border-radius: 25px;
  padding-left: 1em;
}

.entry {
  background-color: rgba(255, 255, 255, 0.2);
  color: white;
  border: 2px solid rgba(255, 255, 255, 0);
  transition: 0.6s;
}

.entry:focus {
  background-color: rgba(196, 182, 224, 0.6);
  border: 2px solid rgba(255, 255, 255, 1);
  outline: 0;
  transition: 0.6s;
}

.entry:hover {
  background-color: rgba(196, 182, 224, 0.6);
  transition: 0.6s;
}

.log-in-check {
  padding-left: 1%;
  cursor: pointer;
}

input[type="checkbox"]:checked {
  background-color: #1161ed;
}

.submit {
  width: 100%;
  border-radius: 25px;
  padding: .75em 0;
  margin-top: 10%;
  background-color: #1161ed;
  border: none;
  color: white;
  font-weight: bold;
  transition: .2s;
  cursor: pointer;
}

.submit:focus {
  outline: 0;
}

.submit:active {
  background-color: #0C49B3;
}
</style>
