<template>
  <div class="login-container">
    <div class="login-background">
      <div class="background-overlay"></div>
    </div>
    
    <div class="login-content">
      <div class="login-form-container">
        <!-- Logo和标题 -->
        <div class="login-header">
          <img src="@/assets/logo.svg" alt="Logo" class="logo" />
          <h1 class="title">宁夏智慧养殖监管平台</h1>
          <p class="subtitle">政府端管理后台</p>
        </div>
        
        <!-- 登录表单 -->
        <a-form
          ref="formRef"
          :model="formData"
          :rules="rules"
          class="login-form"
          @finish="handleLogin"
        >
          <a-form-item name="username">
            <a-input
              v-model:value="formData.username"
              size="large"
              placeholder="请输入用户名"
              :prefix="h(UserOutlined)"
            />
          </a-form-item>
          
          <a-form-item name="password">
            <a-input-password
              v-model:value="formData.password"
              size="large"
              placeholder="请输入密码"
              :prefix="h(LockOutlined)"
            />
          </a-form-item>
          
          <a-form-item name="captcha" v-if="showCaptcha">
            <div class="captcha-container">
              <a-input
                v-model:value="formData.captcha"
                size="large"
                placeholder="请输入验证码"
                :prefix="h(SafetyOutlined)"
                style="flex: 1; margin-right: 12px;"
              />
              <div class="captcha-image" @click="refreshCaptcha">
                <img :src="captchaUrl" alt="验证码" />
              </div>
            </div>
          </a-form-item>
          
          <a-form-item>
            <div class="login-options">
              <a-checkbox v-model:checked="formData.remember">
                记住我
              </a-checkbox>
              <a href="#" class="forgot-password">忘记密码？</a>
            </div>
          </a-form-item>
          
          <a-form-item>
            <a-button
              type="primary"
              html-type="submit"
              size="large"
              :loading="loading"
              class="login-button"
              block
            >
              登录
            </a-button>
          </a-form-item>
        </a-form>
        
        <!-- 其他登录方式 -->
        <div class="other-login" v-if="false">
          <a-divider>其他登录方式</a-divider>
          <div class="other-login-buttons">
            <a-button type="text" class="other-login-btn">
              <wechat-outlined />
            </a-button>
            <a-button type="text" class="other-login-btn">
              <alipay-outlined />
            </a-button>
          </div>
        </div>
        
        <!-- 版权信息 -->
        <div class="login-footer">
          <p>© 2025 宁夏智慧养殖监管平台 版权所有</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted, h } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '@/stores/auth'
import { message } from 'ant-design-vue'
import {
  UserOutlined,
  LockOutlined,
  SafetyOutlined,
  WechatOutlined,
  AlipayOutlined
} from '@ant-design/icons-vue'

const router = useRouter()
const authStore = useAuthStore()

// 响应式数据
const formRef = ref()
const loading = ref(false)
const showCaptcha = ref(false)
const captchaUrl = ref('')

// 表单数据
const formData = reactive({
  username: '',
  password: '',
  captcha: '',
  remember: false
})

// 表单验证规则
const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度为3-20个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度为6-20个字符', trigger: 'blur' }
  ],
  captcha: [
    { required: true, message: '请输入验证码', trigger: 'blur', validator: () => showCaptcha.value }
  ]
}

// 登录处理
const handleLogin = async (values) => {
  loading.value = true
  
  try {
    const success = await authStore.login({
      username: values.username,
      password: values.password,
      captcha: values.captcha,
      remember: values.remember
    })
    
    if (success) {
      message.success('登录成功')
      
      // 跳转到首页或之前访问的页面
      const redirect = router.currentRoute.value.query.redirect || '/'
      router.push(redirect)
    }
  } catch (error) {
    console.error('登录失败:', error)
    
    // 登录失败后显示验证码
    if (!showCaptcha.value) {
      showCaptcha.value = true
      refreshCaptcha()
    }
  } finally {
    loading.value = false
  }
}

// 刷新验证码
const refreshCaptcha = () => {
  captchaUrl.value = `/api/auth/captcha?t=${Date.now()}`
}

// 组件挂载时的处理
onMounted(() => {
  // 如果已经登录，直接跳转到首页
  if (authStore.isAuthenticated) {
    router.push('/')
  }
  
  // 初始化验证码（如果需要）
  if (showCaptcha.value) {
    refreshCaptcha()
  }
})
</script>

<style lang="scss" scoped>
.login-container {
  position: relative;
  width: 100vw;
  height: 100vh;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}

.login-background {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: #f5f5f5;
  
  .background-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.02);
  }
}

.login-content {
  position: relative;
  z-index: 10;
  width: 100%;
  max-width: 400px;
  padding: 0 24px;
}

.login-form-container {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-radius: 16px;
  padding: 40px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.login-header {
  text-align: center;
  margin-bottom: 32px;
  
  .logo {
    width: 64px;
    height: 64px;
    margin-bottom: 16px;
  }
  
  .title {
    font-size: 24px;
    font-weight: 600;
    color: #1a1a1a;
    margin-bottom: 8px;
    line-height: 1.2;
  }
  
  .subtitle {
    font-size: 14px;
    color: #666;
    margin: 0;
  }
}

.login-form {
  .ant-form-item {
    margin-bottom: 20px;
  }
  
  .ant-input-affix-wrapper,
  .ant-input {
    border-radius: 8px;
    border: 1px solid #e0e0e0;
    transition: all 0.3s;
    
    &:hover,
    &:focus {
      border-color: #1890ff;
      box-shadow: 0 0 0 2px rgba(24, 144, 255, 0.1);
    }
  }
}

.captcha-container {
  display: flex;
  align-items: center;
  
  .captcha-image {
    width: 100px;
    height: 40px;
    border: 1px solid #e0e0e0;
    border-radius: 8px;
    cursor: pointer;
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #f5f5f5;
    
    img {
      width: 100%;
      height: 100%;
      object-fit: cover;
    }
    
    &:hover {
      border-color: #1890ff;
    }
  }
}

.login-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  
  .forgot-password {
    color: #1890ff;
    text-decoration: none;
    font-size: 14px;
    
    &:hover {
      text-decoration: underline;
    }
  }
}

.login-button {
  height: 48px;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 500;
  background: linear-gradient(135deg, #1890ff 0%, #096dd9 100%);
  border: none;
  
  &:hover {
    background: linear-gradient(135deg, #40a9ff 0%, #1890ff 100%);
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(24, 144, 255, 0.3);
  }
}

.other-login {
  margin-top: 24px;
  
  .ant-divider {
    font-size: 12px;
    color: #999;
  }
  
  .other-login-buttons {
    display: flex;
    justify-content: center;
    gap: 16px;
    margin-top: 16px;
    
    .other-login-btn {
      width: 40px;
      height: 40px;
      border-radius: 50%;
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 18px;
      color: #666;
      border: 1px solid #e0e0e0;
      
      &:hover {
        color: #1890ff;
        border-color: #1890ff;
        transform: translateY(-2px);
      }
    }
  }
}

.login-footer {
  text-align: center;
  margin-top: 32px;
  padding-top: 24px;
  border-top: 1px solid #f0f0f0;
  
  p {
    font-size: 12px;
    color: #999;
    margin: 0;
  }
}

// 响应式设计
@media (max-width: 480px) {
  .login-content {
    padding: 0 16px;
  }
  
  .login-form-container {
    padding: 24px;
    border-radius: 12px;
  }
  
  .login-header {
    .title {
      font-size: 20px;
    }
    
    .logo {
      width: 48px;
      height: 48px;
    }
  }
}

// 动画效果
@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.login-form-container {
  animation: fadeInUp 0.6s ease-out;
}
</style>