<template>
  <div class="login-container">
    <div class="w-440px bg-#061d42 px-40px py-30px rounded-8px" style="transform: scale(5)">
      <div class="flex items-center justify-center mx-20px">
        <img width="80" src="@/assets/logo2.png"/>
      </div>
      <h2 class="login-title text-center text-28px mb-40px">System Login</h2>
      <el-form :model="loginForm" :rules="rules" ref="loginFormRef">
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" placeholder="username" prefix-icon="User" size="large">
            <template #prefix>
              <User />
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="password"
            prefix-icon="Lock"
            show-password
            size="large"
          >
            <template #prefix>
              <Lock/>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="code">
          <div class="w-full flex gap-2">
            <el-input class="flex-1" v-model="loginForm.code" placeholder="verification code" >
              <template #prefix>
                <img width="10" src="@/assets/ver.svg"/>
              </template>
            </el-input>
            <el-image class="flex-1 h-40px" :src="captchaImage" @click="getCaptcha" />
          </div>
        </el-form-item>
        <el-form-item>
          <el-checkbox v-model="remeberme">Remember me</el-checkbox>
        </el-form-item>
        <el-form-item>
          <div class="w-full flex gap-40px items-center justify-center">
            <el-button type="primary" class="w-full" @click="handleLogin" size="large"> Login </el-button>
          </div>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import { Login, ChaptchaImage } from '@/api';
import { User, Lock } from '@element-plus/icons-vue';

const router = useRouter();
const loginFormRef = ref();
const captchaImage = ref();
const remeberme = ref(false);

const loginForm = reactive({
  username: localStorage.getItem('username') || '',
  password: localStorage.getItem('password') || '',
  code: '',
  uuid: '',
});

const rules = {
  username: [
    { required: true, message: 'Please input username', trigger: 'blur' },
    {
      min: 3,
      max: 20,
      message: 'Length should be between 3 and 20 characters',
      trigger: 'blur',
    },
  ],
  password: [
    { required: true, message: 'Please input password', trigger: 'blur' },
    {
      min: 6,
      max: 20,
      message: 'Length should be between 6 and 20 characters',
      trigger: 'blur',
    },
  ],
};

// 登录
const handleLogin = () => {
  loginFormRef.value.validate(async (valid: boolean) => {
    if (valid) {
      const res = await Login(loginForm);
      if (res.code !== 200) {
        ElMessage.success(res.msg);
        return;
      }
      if (res.token) {
        localStorage.setItem('token', res.token);
      }
      if(remeberme.value) {
        localStorage.setItem('username', loginForm.username);
        localStorage.setItem('password', loginForm.password);
      } else {
        localStorage.removeItem('username');
        localStorage.removeItem('password');
      }
      ElMessage.success('Login successful');
      router.push('/');
    } else {
      ElMessage.error('Login failed, Please check the input');
      return false;
    }
  });
};

// 获取验证码
const getCaptcha = async () => {
  const res = await ChaptchaImage();
  loginForm.uuid = res?.uuid;
  captchaImage.value = 'data:image/png;base64,' + res?.img;
};

onMounted(() => {
  getCaptcha();
});
</script>

<style lang="less" scoped>
.login-container {
  height: 100%;
  background: #072652;
  display: flex;
  justify-content: center;
  align-items: center;
}
::v-deep(.el-form-item) {
    margin-bottom: 22px;
  }
</style>
