<template>
  <div class="login-container">
    <div class="login-card">
      <div class="login-header">
        <h1 class="login-title">DeployHelper</h1>
        <p class="login-subtitle">部署管理系统</p>
      </div>
      
      <t-form 
        ref="form" 
        :data="formData" 
        :rules="rules"
        @submit="handleSubmit"
      >
        <t-form-item name="username">
          <t-input
            v-model="formData.username"
            placeholder="请输入用户名"
            size="large"
          >
            <template #prefix-icon>
              <t-icon name="user" />
            </template>
          </t-input>
        </t-form-item>
        
        <t-form-item name="password">
          <t-input
            v-model="formData.password"
            type="password"
            placeholder="请输入密码"
            size="large"
            @keyup.enter="handleSubmit"
          >
            <template #prefix-icon>
              <t-icon name="lock-on" />
            </template>
          </t-input>
        </t-form-item>
        
        <t-form-item>
          <t-button
            type="submit"
            theme="primary"
            size="large"
            block
            :loading="loading"
          >
            登录
          </t-button>
        </t-form-item>
      </t-form>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { MessagePlugin } from 'tdesign-vue-next'
import { userApi } from '@/api/user'
import auth from '@/utils/auth'

const router = useRouter()
const form = ref()
const loading = ref(false)

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

const rules = {
  username: [
    { required: true, message: '请输入用户名', type: 'error' }
  ],
  password: [
    { required: true, message: '请输入密码', type: 'error' }
  ]
}

const handleSubmit = async () => {
  const valid = await form.value.validate()
  if (!valid) return
  
  loading.value = true
  
  try {
    const response = await userApi.login(formData)
    
    if (response.data.code === 0) {
      const { token, userInfo } = response.data.data
      
      // 保存认证信息
      auth.setToken(token)
      auth.setUserInfo(userInfo)
      
      MessagePlugin.success('登录成功')
      
      // 跳转到首页
      router.push('/home')
    } else {
      MessagePlugin.error(response.data.message || '登录失败')
    }
  } catch (error) {
    console.error('登录错误:', error)
    MessagePlugin.error('登录失败，请检查网络连接')
  } finally {
    loading.value = false
  }
}
</script>

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

.login-card {
  width: 100%;
  max-width: 400px;
  background: white;
  border-radius: 12px;
  padding: 40px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
}

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

.login-title {
  font-size: 28px;
  font-weight: bold;
  color: #0052d9;
  margin: 0 0 8px 0;
}

.login-subtitle {
  font-size: 14px;
  color: #8c8c8c;
  margin: 0;
}

:deep(.t-form-item) {
  margin-bottom: 24px;
}

:deep(.t-input) {
  border-radius: 8px;
}

:deep(.t-button) {
  border-radius: 8px;
  height: 48px;
  font-size: 16px;
}
</style> 