<template>
  <div class="login-page">
    <div class="login-card">
      <div class="login-header">
        <div class="logo">LM</div>
        <h1>LAN Manager</h1>
        <p>轻量级局域网资产管理平台</p>
      </div>
      <el-form :model="form" class="login-form">
        <el-form-item>
          <div class="input-wrap">
            <el-icon class="input-icon"><User /></el-icon>
            <el-input v-model="form.username" placeholder="用户名" size="large" @keydown.enter="login" />
          </div>
        </el-form-item>
        <el-form-item>
          <div class="input-wrap">
            <el-icon class="input-icon"><Lock /></el-icon>
            <el-input v-model="form.password" type="password" show-password placeholder="密码" size="large" @keydown.enter="login" />
          </div>
        </el-form-item>
        <el-button type="primary" size="large" class="login-btn" :loading="loading" @click="login">登录</el-button>
        <div class="login-guest">
          <el-button text size="small" @click="guestLogin">访客模式进入</el-button>
        </div>
      </el-form>
    </div>
    <div class="login-footer">
      © LAN Manager
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { User, Lock } from '@element-plus/icons-vue'
import { login as apiLogin, guestLogin as apiGuest } from '@/api'
import { setAuth } from '@/router'
import { ElMessage } from 'element-plus'

const router = useRouter()
const form = ref({ username: '', password: '' })
const loading = ref(false)

async function login() {
  if (!form.value.username || !form.value.password) {
    ElMessage.warning('请输入用户名和密码')
    return
  }
  loading.value = true
  try {
    const res = await apiLogin(form.value)
    setAuth(res.data)
    ElMessage.success('登录成功')
    router.push('/')
  } catch (e) {}
  loading.value = false
}

async function guestLogin() {
  loading.value = true
  try {
    const res = await apiGuest()
    setAuth(res.data)
    ElMessage.success('已进入访客模式')
    router.push('/')
  } catch (e) {}
  loading.value = false
}
</script>

<style scoped>
.login-page {
  min-height: 100vh;
  background: linear-gradient(135deg, #0f172a 0%, #1e293b 50%, #0f172a 100%);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.login-card {
  width: 100%;
  max-width: 400px;
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 20px;
  padding: 36px;
  backdrop-filter: blur(10px);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.35);
}

.login-header {
  text-align: center;
  margin-bottom: 28px;
}
.logo {
  width: 56px;
  height: 56px;
  border-radius: 14px;
  background: linear-gradient(135deg, #3b82f6, #60a5fa);
  color: #fff;
  font-weight: 800;
  font-size: 22px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 14px;
  box-shadow: 0 10px 25px rgba(59, 130, 246, 0.35);
}
.login-header h1 {
  color: #fff;
  font-size: 22px;
  font-weight: 700;
  margin: 0 0 6px;
}
.login-header p {
  color: rgba(255, 255, 255, 0.55);
  font-size: 13px;
  margin: 0;
}

.login-form {
  width: 100%;
}
.login-form :deep(.el-form-item) {
  margin-bottom: 16px;
}
.login-form :deep(.el-form-item__content) {
  display: block;
  width: 100%;
}

.input-wrap {
  position: relative;
  width: 100%;
}
.input-wrap :deep(.el-input) {
  width: 100%;
}
.input-wrap :deep(.el-input__wrapper) {
  padding-left: 38px;
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.08);
  box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.1) inset;
}
.input-wrap :deep(.el-input__inner) {
  color: #fff;
  font-size: 14px;
}
.input-wrap :deep(.el-input__inner::placeholder) {
  color: rgba(255, 255, 255, 0.45);
}
.input-icon {
  position: absolute;
  left: 12px;
  top: 50%;
  transform: translateY(-50%);
  color: rgba(255, 255, 255, 0.5);
  font-size: 16px;
  z-index: 1;
}

.login-btn {
  width: 100%;
  border-radius: 10px;
  font-weight: 600;
  letter-spacing: 0.5px;
  margin-top: 6px;
}
.login-guest {
  text-align: center;
  margin-top: 14px;
}
.login-guest :deep(.el-button) {
  color: rgba(255, 255, 255, 0.55);
}
.login-guest :deep(.el-button:hover) {
  color: #fff;
}

.login-footer {
  margin-top: 24px;
  font-size: 12px;
  color: rgba(255, 255, 255, 0.35);
}
</style>
