<template>
  <div class="login-container">
    <!-- 背景装饰 -->
    <div class="bg-decoration">
      <div class="decoration-circle circle-1"></div>
      <div class="decoration-circle circle-2"></div>
      <div class="decoration-circle circle-3"></div>
      <div class="decoration-line line-1"></div>
      <div class="decoration-line line-2"></div>
    </div>

    <!-- Logo -->
    <div class="logo-area">
      <div class="logo">
        <span class="logo-icon">◇</span>
        <span class="logo-text">楠溪屿</span>
      </div>
    </div>

    <!-- 登录卡片 - 偏右位置 -->
    <div class="login-wrapper">
      <div class="login-box">
        <div class="login-header">
          <h1>欢迎回来</h1>
          <p>登录您的管理员账户</p>
        </div>

        <a-form
          :model="loginForm"
          :rules="loginRules"
          ref="formRef"
          class="login-form"
          @finish="handleLogin"
        >
          <a-form-item name="username">
            <a-input
              v-model:value="loginForm.username"
              placeholder="用户名"
              size="large"
              class="custom-input"
            >
              <template #prefix>
                <UserOutlined class="input-icon" />
              </template>
            </a-input>
          </a-form-item>

          <a-form-item name="password">
            <a-input-password
              v-model:value="loginForm.password"
              placeholder="密码"
              size="large"
              class="custom-input"
            >
              <template #prefix>
                <LockOutlined class="input-icon" />
              </template>
            </a-input-password>
          </a-form-item>

          <a-form-item name="captcha">
            <div class="captcha-row">
              <a-input
                v-model:value="loginForm.captcha"
                placeholder="验证码"
                size="large"
                class="captcha-input custom-input"
              >
                <template #prefix>
                  <SafetyCertificateOutlined class="input-icon" />
                </template>
              </a-input>
              <div class="captcha-image" @click="refreshCaptcha" title="点击刷新验证码">
                <img v-if="captchaImage" :src="captchaImage" alt="验证码" />
                <a-spin v-else size="small" />
              </div>
            </div>
          </a-form-item>

          <a-form-item>
            <a-checkbox v-model:checked="rememberMe">记住我</a-checkbox>
          </a-form-item>

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

        <div class="login-footer">
          <span class="footer-text">程序员论坛社区管理平台</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { message } from 'ant-design-vue'
import { UserOutlined, LockOutlined, SafetyCertificateOutlined } from '@ant-design/icons-vue'
import { useUserStore } from '@/stores'
import type { FormInstance, Rule } from 'ant-design-vue/es/form'

const router = useRouter()
const userStore = useUserStore()

const formRef = ref<FormInstance>()
const loading = ref(false)
const captchaImage = ref('')
const captchaKey = ref('')
const rememberMe = ref(false)

// 登录表单
const loginForm = reactive({
  username: '',
  password: '',
  captcha: ''
})

// 表单验证规则
const loginRules: Record<string, Rule[]> = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能少于6位', trigger: 'blur' }
  ],
  captcha: [
    { required: true, message: '请输入验证码', trigger: 'blur' }
  ]
}

// 获取验证码
async function refreshCaptcha() {
  try {
    const data = await userStore.getCaptcha()
    captchaImage.value = data.captchaImage
    captchaKey.value = data.captchaKey
  } catch (error) {
    console.error('获取验证码失败:', error)
  }
}

// 登录处理
async function handleLogin() {
  loading.value = true
  try {
    await userStore.login({
      ...loginForm,
      captchaKey: captchaKey.value
    })
    message.success('登录成功，欢迎回来！')
    router.push('/')
  } catch (error: unknown) {
    const errorMessage = error instanceof Error ? error.message : '登录失败'
    message.error(errorMessage)
    refreshCaptcha()
  } finally {
    loading.value = false
  }
}

onMounted(() => {
  refreshCaptcha()
})
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  position: relative;
  overflow: hidden;
  background: linear-gradient(135deg, #e8f4fc 0%, #f5f0fa 50%, #fef6f0 100%);
}

/* 背景装饰 */
.bg-decoration {
  position: absolute;
  inset: 0;
  pointer-events: none;
  overflow: hidden;
}

.decoration-circle {
  position: absolute;
  border-radius: 50%;
  opacity: 0.6;
}

.circle-1 {
  width: 300px;
  height: 300px;
  background: linear-gradient(135deg, rgba(114, 197, 229, 0.3) 0%, rgba(114, 197, 229, 0.1) 100%);
  top: -50px;
  left: -100px;
}

.circle-2 {
  width: 200px;
  height: 200px;
  background: linear-gradient(135deg, rgba(250, 176, 162, 0.4) 0%, rgba(250, 176, 162, 0.1) 100%);
  bottom: 100px;
  left: 10%;
}

.circle-3 {
  width: 400px;
  height: 400px;
  background: linear-gradient(135deg, rgba(186, 220, 231, 0.3) 0%, rgba(186, 220, 231, 0.1) 100%);
  bottom: -150px;
  right: -100px;
}

.decoration-line {
  position: absolute;
  height: 1px;
  background: rgba(250, 176, 162, 0.5);
}

.line-1 {
  width: 100%;
  top: 45%;
  transform: rotate(-2deg);
}

.line-2 {
  width: 100%;
  top: 55%;
  transform: rotate(1deg);
}

/* Logo区域 */
.logo-area {
  position: absolute;
  top: 40px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 10;
}

.logo {
  display: flex;
  align-items: center;
  gap: 8px;
}

.logo-icon {
  font-size: 28px;
  color: #1890ff;
  font-weight: bold;
}

.logo-text {
  font-size: 24px;
  font-weight: 600;
  color: #1a1a2e;
}

/* 登录卡片包装器 - 偏右布局 */
.login-wrapper {
  min-height: 100vh;
  display: flex;
  justify-content: flex-end;
  align-items: center;
  padding-right: 12%;
}

.login-box {
  width: 380px;
  padding: 40px 36px;
  background: #fff;
  border-radius: 16px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.08);
  position: relative;
  z-index: 10;
}

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

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

.login-header p {
  font-size: 14px;
  color: #8c8c8c;
}

/* 表单样式 */
.login-form {
  margin-top: 24px;
}

/* 统一输入框样式 */
.custom-input,
.custom-input :deep(.ant-input),
.custom-input :deep(.ant-input-password) {
  border-radius: 8px !important;
  background: #fff !important;
}

.custom-input {
  border: 1px solid #e0e0e0;
}

.custom-input:hover {
  border-color: #91caff;
}

.custom-input:focus,
.custom-input:focus-within,
.custom-input.ant-input-affix-wrapper-focused {
  border-color: #1890ff;
  box-shadow: 0 0 0 2px rgba(24, 144, 255, 0.1);
}

.input-icon {
  color: #b0b0b0;
  font-size: 16px;
}

.captcha-row {
  display: flex;
  gap: 12px;
}

.captcha-input {
  flex: 1;
}

.captcha-image {
  width: 110px;
  height: 40px;
  cursor: pointer;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  overflow: hidden;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #fff;
  transition: all 0.3s;
}

.captcha-image:hover {
  border-color: #91caff;
}

.captcha-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

/* 覆盖 antd 默认样式 */
:deep(.ant-input-affix-wrapper) {
  background: #fff;
}

:deep(.ant-input) {
  background: #fff;
}

:deep(.ant-checkbox-wrapper) {
  color: #666;
}

/* 登录按钮 */
.login-btn {
  height: 44px;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 500;
  background: linear-gradient(135deg, #1890ff 0%, #096dd9 100%);
  border: none;
  box-shadow: 0 4px 12px rgba(24, 144, 255, 0.3);
}

.login-btn:hover {
  background: linear-gradient(135deg, #40a9ff 0%, #1890ff 100%);
  box-shadow: 0 6px 16px rgba(24, 144, 255, 0.4);
}

/* 底部文字 */
.login-footer {
  text-align: center;
  margin-top: 24px;
  padding-top: 20px;
  border-top: 1px solid #f0f0f0;
}

.footer-text {
  font-size: 12px;
  color: #bfbfbf;
}

/* 响应式 */
@media (max-width: 768px) {
  .login-wrapper {
    justify-content: center;
    padding-right: 0;
    padding: 20px;
  }

  .login-box {
    width: 100%;
    max-width: 380px;
  }
}
</style>

