<template>
  <div class="loginPage">
    <div class="loginCard">
      <div class="logo">
        <img src="./assets/login.webp" alt="" class="img" />
      </div>
      <div class="contant">
        <div class="title">欢迎登录</div>
        <div class="subTitle"> 请输入您的账号和密码登录 </div>
        <div class="form">
          <a-form
            ref="formRef"
            :model="formState"
            name="basic"
            :label-col="{ span: 6 }"
            :wrapper-col="{ span: 24 }"
            autocomplete="off"
            @keydown.enter="login"
          >
            <a-form-item
              label=""
              name="account"
              :rules="[{ required: true, message: '请输入账号' }]"
            >
              <a-input v-model:value.trim="formState.account" placeholder="请输入账号" />
            </a-form-item>

            <a-form-item
              label=""
              name="password"
              :rules="[{ required: true, message: '请输入密码' }]"
            >
              <a-input-password v-model:value.trim="formState.password" placeholder="请输入密码" />
            </a-form-item>
          </a-form>
          <a-button type="primary" block :loading="sbumitLoading" @click="login">登录</a-button>
        </div>
        <div class="footer"> 合肥雷能信息技术有限公司 </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { message, type FormInstance } from 'ant-design-vue'
import { useUserStore } from '@/stores/user'

defineOptions({
  name: 'LoginIndex',
})

const userStore = useUserStore()

interface FormState {
  account: string
  password: string
}

const formState = reactive<FormState>({
  account: '',
  password: '',
})

const formRef = ref<FormInstance>()
const sbumitLoading = ref(false)

const login = () => {
  formRef.value
    ?.validate()
    .then(async () => {
      sbumitLoading.value = true
      try {
        await userStore.login({
          account: formState.account,
          password: formState.password,
        })
        message.success('登录成功')
        sbumitLoading.value = false
      } catch (error) {
        console.error('登录失败', error)
        // const errorMessage = error instanceof Error ? error.message : '登录失败'
        // message.error(errorMessage)
        sbumitLoading.value = false
      }
    })
    .catch(() => {
      sbumitLoading.value = false
    })
}
</script>

<style scoped lang="less">
.loginPage {
  width: 100vw;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  z-index: 0;

  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: url('./assets/bg1.png') no-repeat center center;
    background-size: cover;
    filter: blur(2px);
    z-index: -1;
  }

  .loginCard {
    width: 750px;
    height: 500px;
    background-color: #fff;
    border-radius: 10px;
    overflow: hidden;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
    display: flex;
    transition: box-shadow 0.3s ease;

    .logo {
      width: 60%;
      height: 100%;
      background: url('./assets/bg.png') no-repeat center center;
      .img {
        width: 100%;
        height: 100%;
        object-fit: cover;
      }
    }
    .contant {
      width: 40%;
      height: 100%;
      padding: 20px;
      display: flex;
      flex-direction: column;

      .title {
        font-size: 24px;
        font-weight: bold;
        text-align: left;
        text-align: center;
        margin-top: 80px;
        margin-bottom: 20px;
      }
      .subTitle {
        font-size: 16px;
        margin-bottom: 20px;
        text-align: left;
        text-align: center;
        color: #99a1b7;
      }

      .form {
        flex-grow: 1;
      }

      .footer {
        font-size: 14px;
        color: #99a1b7;
        text-align: center;
        margin-top: 20px;
      }
    }
  }
}
</style>
