<template>
  <div class="login">
    <div class="form-box">
      <div class="title">
        后台管理系统
      </div>
      <a-form
        class="form"
        :model="loginState"
        :rules="rules"
        @finish="finishHandle"
        @finishFailed="finishFailedHandle"
      >
        <a-form-item
          name="username"
          class="form-item"
        >
          <a-input
            v-model:value="loginState.username"
            placeholder="用户名"
            size="large"
          >
            <template #prefix>
              <user-outlined style="color: rgba(0, 0, 0, 0.25)" />
            </template>
          </a-input>
        </a-form-item>
        <a-form-item
          name="password"
          class="form-item"
        >
          <a-input
            v-model:value="loginState.password"
            type="password"
            placeholder="密码"
            size="large"
          >
            <template #prefix>
              <lock-outlined style="color: rgba(0, 0, 0, 0.25)" />
            </template>
          </a-input>
        </a-form-item>
        <a-form-item>
          <a-button
            class="form-submit"
            type="primary"
            html-type="submit"
            size="large"
          >
            登录
          </a-button>
        </a-form-item>
      </a-form>
    </div>
  </div>
</template>
<script lang="ts" setup>
import { reactive, ref } from 'vue';
import { EnumPage } from '@/router/base';
import { useRouter } from 'vue-router';
import { message } from 'ant-design-vue'
import { useLocalStorage } from '@vueuse/core'
import type { UnwrapRef } from 'vue';
import type { FormProps } from 'ant-design-vue';
import { AppStore } from '@/store/modules/app'

const router = useRouter()
const store = AppStore()
interface loginState {
  username: string
  password: string
}
const loginState: UnwrapRef<loginState> = reactive({
  username: '',
  password: ''
})
const rules = {
  username: [
    {
      required: true,
      message: '请输入用户名',
      whitespace: true,
      trigger: 'blur'
    }
  ],
  password: [
    {
      required: true,
      message: '请输入密码',
      whitespace: true,
      trigger: 'blur'
    }
  ]
}

const finishHandle: FormProps['onFinish'] = (values: loginState) => {
  const { username, password } = values
  if (username === 'admin' && password === '123') {
    router.push(EnumPage.App).then(() => store.updateIsLogin(true))
  } else {
    message.error('用户名或密码错误')
  }
}
const finishFailedHandle: FormProps['onFinishFailed'] = (errors) => {
  console.log(errors);
}
</script>
<style lang="less" scoped>
.login {
  height: 100%;
  background: url('/images/login_bg.jpeg') center/cover no-repeat;
  display: flex;
  justify-content: center;
  align-items: center;
  .form-box {
    background-color: rgba(255, 255, 255, .8);
    height: 460px;
    width: 400px;
    border-radius: 12px;
    padding: 60px 40px;
    .title {
      text-align: center;
      font-size: 26px;
      font-weight: 500;
    }
    .form {
      margin-top: 40px;
      .form-item {
        margin-bottom: 40px;
      }
      .form-submit {
        width: 180px;
      }
      // 修复表单错误提示出现时的显示异常
      :deep(.ant-form-item-with-help) {
        margin-bottom: 16px;
      }
      // 登录按钮居中
      :deep(.ant-form-item-control-input-content) {
        text-align: center;
      }
    }

  }
}
</style>
