<script setup lang="ts">
import { useUserStore } from '@/stores/user'
import type { FormInstance } from 'element-plus'
import config from '@/config/defaultSetting'
import loginLogo from '@/assets/images/login.png'

const userStore = useUserStore()

const formData = reactive({
  username: '',
  password: ''
})
const formRef = ref<FormInstance>()
const formRules = {
  username: {
    required: true,
    message: '请输入用户名'
  },
  password: {
    required: true,
    message: '请输入密码'
  }
}

const loading = ref(false)
const handleSubmit = () => {
  loading.value = true
  formRef.value?.validate(async valid => {
    if (valid) {
      try {
        await userStore.login(formData)
      } catch (error) {
        loading.value = false
      }
    }
    loading.value = false
  })
}
</script>

<template>
  <div class="flex h-screen w-screen">
    <div class="flex items-center justify-center w-50% h-screen" style="background: var(--el-color-primary)">
      <el-image :src="loginLogo" style="width: 600px"></el-image>
    </div>
    <div class="flex items-center justify-center w-50% h-screen bg-white flex">
      <el-form
        class="w-50%"
        :model="formData"
        ref="formRef"
        :rules="formRules"
        size="large"
        @keyup.enter="handleSubmit"
      >
        <div class="flex items-center justify-center mb-50px">
          <img src="/logo.png" alt="logo" class="w-60px mr-2" />
          <h1>{{ config.title }}</h1>
        </div>
        <!-- <div class="text-24px mb-4">登录</div> -->
        <el-form-item prop="username">
          <el-input v-model="formData.username" placeholder="请输入用户名">
            <template #prefix>
              <el-icon :size="16"><User /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="password" v-model="formData.password" placeholder="请输入密码">
            <template #prefix>
              <el-icon :size="16"><Lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :loading="loading" @click="handleSubmit" class="w-full">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
