<template>
  <div class="login-page">
    <div class="login-header">
      <div class="logo">🛵</div>
      <h1>电动车租赁</h1>
      <p>骑手端</p>
    </div>
    
    <div class="login-form">
      <el-form ref="formRef" :model="loginForm" :rules="rules" size="large">
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="请输入账号"
            :prefix-icon="User"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            :prefix-icon="Lock"
            show-password
          />
        </el-form-item>
      </el-form>
      
      <el-button type="primary" size="large" :loading="loading" class="login-btn" @click="handleLogin">
        登录
      </el-button>
      
      <div class="demo-hint">
        <p>演示账号：rider_demo / demo123</p>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'
import request from '../utils/request'

const router = useRouter()
const loading = ref(false)
const formRef = ref(null)

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

const rules = {
  username: [{ required: true, message: '请输入账号', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
}

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

  loading.value = true
  try {
    const res = await request.post('/auth/login', {
      username: loginForm.value.username.trim(),
      password: loginForm.value.password.trim()
    })

    if (res.success && res.data && res.data.token) {
      localStorage.setItem('token', res.data.token)
      localStorage.setItem('riderInfo', JSON.stringify(res.data))
      ElMessage.success('登录成功')
      router.push('/')
    } else {
      ElMessage.error(res.message || '登录失败')
    }
  } catch (e) {
    ElMessage.error(e.response?.data?.message || '登录失败，请检查账号密码')
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.login-page {
  min-height: 100vh;
  background: #F7F7F7;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.login-header {
  text-align: center;
  color: #1A1A1A;
  margin-bottom: 40px;
}

.logo {
  font-size: 64px;
  margin-bottom: 10px;
}

.login-header h1 {
  font-size: 28px;
  margin-bottom: 8px;
  font-weight: 600;
  color: #1A1A1A;
}

.login-header p {
  font-size: 13px;
  color: #B2B2B2;
}

.login-form {
  width: 100%;
  max-width: 360px;
  background: #fff;
  border-radius: 16px;
  padding: 30px;
  box-shadow: 0 2px 12px rgba(0,0,0,0.06);
}

.login-form :deep(.el-input__wrapper) {
  border-radius: 12px;
}

.login-btn {
  width: 100%;
  background: #FF6B00;
  border-color: #FF6B00;
  font-size: 18px;
  border-radius: 12px;
  margin-top: 20px;
  height: 48px;
}

.demo-hint {
  text-align: center;
  color: #B2B2B2;
  font-size: 12px;
  margin-top: 20px;
}
</style>
