<template>
  <div class="login-page">
    <div class="login-box">
      <div class="login-header">
        <el-icon><Monitor /></el-icon>
        <span>管理后台</span>
      </div>
      <p class="login-tip">请使用账号密码登录</p>
      <el-form
        ref="formRef"
        :model="form"
        :rules="rules"
        @submit.prevent="handleSubmit"
      >
        <el-form-item prop="username">
          <el-input
            v-model="form.username"
            placeholder="请输入账号"
            :prefix-icon="User"
            autocomplete="username"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="form.password"
            type="password"
            placeholder="请输入密码"
            :prefix-icon="Lock"
            autocomplete="current-password"
            show-password
            @keyup.enter="handleSubmit"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            :loading="loading"
            class="login-btn"
            native-type="submit"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>
      <p class="login-hint">演示账号：admin / 123456</p>
    </div>
  </div>
</template>

<script>
import { markRaw } from 'vue'
import { ElMessage } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'
import request from '@/utils/request'

export default {
  name: 'Login',
  data() {
    return {
      loading: false,
      form: { username: '', password: '' },
      rules: {
        username: [{ required: true, message: '请输入账号', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
      },
      User: markRaw(User),
      Lock: markRaw(Lock)
    }
  },
  methods: {
    handleSubmit() {
      this.$refs.formRef.validate(async (valid) => {
        if (!valid) return
        this.loading = true
        try {
          const res = await request.post('/api/login', this.form)
          localStorage.setItem('token', res.data.token)
          localStorage.setItem('username', res.data.username)
          ElMessage.success('登录成功')
          const redirect = this.$route.query.redirect || '/'
          this.$router.replace(redirect)
        } finally {
          this.loading = false
        }
      })
    }
  }
}
</script>

<style scoped>
.login-page {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #304156 0%, #1f2d3d 100%);
}

.login-box {
  width: 380px;
  padding: 36px 32px 24px;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.2);
}

.login-header {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  color: #304156;
  font-size: 22px;
  font-weight: bold;
}
.login-header .el-icon { font-size: 26px; }

.login-tip {
  margin: 10px 0 24px;
  color: #909399;
  text-align: center;
  font-size: 13px;
}

.login-btn {
  width: 100%;
}

.login-hint {
  margin: 8px 0 0;
  color: #c0c4cc;
  text-align: center;
  font-size: 12px;
}
</style>
