<template>
<div class="main">
  <div class="login-form">
    <img :src="logo" class="logo" alt=""/>
    <div class="title">长春黄金研究院有限公司</div>
    <el-form :model="form" :rules="rules" ref="formRef" style="width: 100%">
      <div class="input-title">用户名</div>
      <el-form-item prop="username">
        <el-input
          v-model="form.username"
          placeholder="请输入用户名"
          prefix-icon="userIconSvg"
          style="width: 100%"
          size="large"
        >
          <template #prefix>
            <img :src="userIconSvg" alt="" class="icon" />
          </template>
        </el-input>
      </el-form-item>
      <div class="input-title">密码</div>
      <el-form-item prop="password">
        <el-input
          v-model="form.password"
          placeholder="请输入密码"
          prefix-icon="pwdIconSvg"
          type="password"
          style="width: 100%"
          size="large"
        >
          <template #prefix>
            <img :src="pwdIconSvg" alt="" class="icon"/>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button style="width: 100%; margin-top: 10px" type="primary" @click="loginHandle" :loading="loading">登录</el-button>
      </el-form-item>
    </el-form>
    <span class="info">Changchun Gold Research Institute Co.,Ltd.</span>
  </div>
</div>
</template>

<script setup lang="ts">
import { ref } from "vue";
import { useRouter } from "vue-router";
import logo from '@/assets/logo.svg'
import userIconSvg from '@/assets/user_icon.svg'
import pwdIconSvg from '@/assets/pwdw_icon.svg'
import { getCurrentUser, login, type User } from "@/services/user/userManager";
import { useUserStore } from "@/stores/user";
import {ElMessage} from "element-plus";

const form = ref({
  username: "",
  password: "",
});

const formRef = ref();

const rules = {
  username: [
    { required: true, message: "请输入用户名", trigger: "blur" },
    { min: 3, max: 10, message: "长度在 3 到 10 个字符", trigger: "blur" },
  ],
  password: [
    { required: true, message: "请输入密码", trigger: "blur" },
    { min: 3, max: 10, message: "长度在 3 到 10 个字符", trigger: "blur" },
  ]
}

const loading = ref(false);
const userStore = useUserStore();
const router = useRouter();

const loginHandle = () => {
  formRef.value.validate(async(valid: any) => {
    if (!valid) {
      return;
    }
    loading.value = true;
    const res = await login(form.value);
    if(res.success) {
      sessionStorage.setItem("token", res.data);
      const response = await getCurrentUser();
      if (response.success) {
        const { username, nickname, role } = response.data;
        userStore.setUser({ username, nickname, role } as User);
      }
      await router.push("/home");
      loading.value = false;
    }else {
      ElMessage.error(res.msg);
      loading.value = false;
    }
  })
}

</script>

<style lang="scss" scoped>
@media screen and (max-width: 1000px) {
  .main {
    .login-form {
      width: 80%;
    }
  }
}
.main {
  width: 100%;
  height: 100vh;
  background: #BDE2FE;
  display: flex;
  justify-content: center;
  align-items: center;
  .login-form {
    width: 30%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    padding: 50px 50px 10px;
    border-radius: 10px;
    background: linear-gradient(180deg, rgba(250, 252, 255, 0.51) 0%, rgba(255, 255, 255, 0.52) 100%);
    .logo {
      width: 50px;
    }
    .title {
      color: #8799AB;
      margin: 20px 0 30px 0;
    }
    .info {
      margin-top: 10px;
      color: #8799AB;
      font-size: 10px;
    }
  }
}
.icon {
  width: 15px;
}
.input-title {
  font-size: 14px;
  color: #2C3E59;
  margin-bottom: 10px;
}

</style>
