<template>
  <div class="login-container">
    <el-card class="login-box">
      <h2 class="login-title">通用型流程审批系统</h2>
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginRules"
        @keyup.enter="handleLogin"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="用户名 / 手机号"
            :prefix-icon="User"
            size="large"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="密码"
            :prefix-icon="Lock"
            size="large"
            show-password
          />
        </el-form-item>
        <el-form-item>
          <el-checkbox v-model="remember">记住用户名</el-checkbox>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            size="large"
            :loading="loading"
            style="width: 100%"
            @click="handleLogin"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { User, Lock } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import type { FormInstance, FormRules } from 'element-plus'
import { useUserStore } from '@/stores/user-store'

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()

const loginFormRef = ref<FormInstance>()
const loading = ref(false)

const loginForm = reactive({
  username: '',
  password: ''
})

const remember = ref(false)

const loginRules: FormRules = {
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
}

async function handleLogin() {
  const valid = await loginFormRef.value?.validate().catch(() => false)
  if (!valid) return

  loading.value = true
  try {
    await userStore.loginAction({ ...loginForm })
    await userStore.fetchUserInfo()
    if (remember.value) {
      localStorage.setItem('login_username', loginForm.username)
      localStorage.setItem('login_remember', 'true')
    } else {
      localStorage.removeItem('login_username')
      localStorage.removeItem('login_remember')
    }
    ElMessage.success('登录成功')
    const redirect = route.query.redirect as string
    router.push(redirect || '/')
  } catch {
    // error handled by request interceptor
  } finally {
    loading.value = false
  }
}

onMounted(() => {
  const savedUsername = localStorage.getItem('login_username')
  const savedRemember = localStorage.getItem('login_remember')
  if (savedRemember === 'true' && savedUsername) {
    loginForm.username = savedUsername
    remember.value = true
  }
})
</script>

<style scoped>
.login-container {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: url('@/assets/login-bg.png') center/cover no-repeat;
}
.login-box {
  width: 400px;
  padding: 20px;
  transform: translateX(calc(25vw + 3cm));
}
.login-title {
  text-align: center;
  margin-bottom: 20px;
  color: #303133;
}
.login-type-switch {
  display: flex;
  justify-content: center;
  margin-bottom: 20px;
}
</style>
