<script setup lang="ts">
import { ref, reactive } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'
import { useLoginStore } from '@/stores/login'

const loginStore = useLoginStore()
const formRef = ref<FormInstance>()

const rules = reactive<FormRules>({
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
})

const handleLogin = async () => {
  if (!formRef.value) return
  await formRef.value.validate((valid) => {
    if (valid) {
      loginStore.login()
    }
  })
}
</script>

<template>
  <div class="login-container">
    <div class="login-left">
      <div class="login-left-content">
        <h1 class="login-title">管理系统</h1>
        <p class="login-subtitle">高效、智能的企业管理平台</p>
      </div>
    </div>
    <div class="login-right">
      <div class="login-form-wrapper">
        <h2 class="form-title">登录</h2>
        <el-form ref="formRef" :model="loginStore" :rules="rules" size="large">
          <el-form-item prop="username">
            <el-input
              v-model="loginStore.username"
              placeholder="请输入用户名"
              :prefix-icon="User"
            />
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="loginStore.password"
              type="password"
              placeholder="请输入密码"
              :prefix-icon="Lock"
              show-password
            />
          </el-form-item>
          <el-form-item>
            <el-checkbox v-model="loginStore.remember">记住我</el-checkbox>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" class="login-btn" :loading="loginStore.loading" @click="handleLogin">登录</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<style scoped>
.login-container {
  display: flex;
  height: 100vh;
  width: 100vw;
}

.login-left {
  flex: 6;
  background: linear-gradient(135deg, #2b5876 0%, #4e4376 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
}

.login-left-content {
  text-align: center;
}

.login-title {
  font-size: 42px;
  font-weight: 700;
  margin-bottom: 16px;
}

.login-subtitle {
  font-size: 18px;
  opacity: 0.85;
}

.login-right {
  flex: 4;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #fff;
}

.login-form-wrapper {
  width: 360px;
}

.form-title {
  font-size: 28px;
  font-weight: 600;
  margin-bottom: 32px;
  color: #303133;
}

.login-btn {
  width: 100%;
}

@media (max-width: 768px) {
  .login-left {
    display: none;
  }
  .login-right {
    flex: 1;
  }
}
</style>
