<template>
  <div class="login-container">
    <div class="login-form">
      <div class="login-header">
        <h1>{{ appName }}</h1>
        <p>欢迎回来，请登录您的账号</p>
      </div>

      <a-form
        :model="formState"
        name="login"
        autocomplete="off"
        @finish="onFinish"
        @finishFailed="onFinishFailed"
      >
        <a-form-item
          name="username"
          :rules="[{ required: true, message: '请输入用户名!' }]"
        >
          <a-input
            v-model:value="formState.username"
            size="large"
            placeholder="用户名"
          >
            <template #prefix>
              <UserOutlined />
            </template>
          </a-input>
        </a-form-item>

        <a-form-item
          name="password"
          :rules="[{ required: true, message: '请输入密码!' }]"
        >
          <a-input-password
            v-model:value="formState.password"
            size="large"
            placeholder="密码"
          >
            <template #prefix>
              <LockOutlined />
            </template>
          </a-input-password>
        </a-form-item>

        <a-form-item>
          <a-button
            type="primary"
            html-type="submit"
            size="large"
            :loading="loading"
            block
          >
            登录
          </a-button>
        </a-form-item>
      </a-form>

      <div class="login-footer">
        <p>© 2025 结伴客系统 - 后台管理系统 v{{ appVersion }}</p>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import { UserOutlined, LockOutlined } from '@ant-design/icons-vue'
import { message } from 'ant-design-vue'
import { useAppStore } from '@/stores/app'
import { authAPI } from '@/api'

interface FormState {
  username: string
  password: string
}

const router = useRouter()
const appStore = useAppStore()
const loading = ref(false)

const formState = reactive<FormState>({
  username: '',
  password: ''
})

const appName = import.meta.env.VITE_APP_NAME || '结伴客后台管理系统'
const appVersion = import.meta.env.VITE_APP_VERSION || '1.0.0'

const onFinish = async (values: FormState) => {
  loading.value = true
  
  try {
    // 使用store的login方法，它会处理token保存和权限设置
    await appStore.login(values)
    
    message.success('登录成功！')
    
    // 跳转到首页或重定向页面
    const redirect = router.currentRoute.value.query.redirect as string
    router.push(redirect || '/dashboard')
    
  } catch (error: any) {
    console.error('登录失败:', error)
    const errorMessage = error.message || '登录失败，请检查用户名和密码'
    message.error(errorMessage)
  } finally {
    loading.value = false
  }
}

const onFinishFailed = (errorInfo: any) => {
  console.log('表单验证失败:', errorInfo)
  message.warning('请填写完整的登录信息')
}
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
}

.login-form {
  width: 100%;
  max-width: 400px;
  padding: 40px;
  background: white;
  border-radius: 8px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

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

.login-header p {
  color: #666;
  margin: 0;
}

.login-footer {
  text-align: center;
  margin-top: 30px;
  padding-top: 20px;
  border-top: 1px solid #f0f0f0;
}

.login-footer {
  text-align: center;
  margin-top: 30px;
  color: #999;
  font-size: 12px;
}

:deep(.ant-input-affix-wrapper) {
  border-radius: 6px;
}

:deep(.ant-btn) {
  border-radius: 6px;
  height: 44px;
  font-size: 16px;
  font-weight: 500;
}
</style>