<template>
  <div class="login-form">
    <div>登陆</div>
    <form class="login-form-container" @submit.prevent="handleLogin">
      <div class="input-container">
        <label for="username">QQ号</label>
        <input
            type="text"
            id="username"
            v-model="username"
            placeholder="请输入QQ号"
            :class="{ 'error': usernameError }"
        />
        <span class="error-message" v-if="usernameError">{{ usernameError }}</span>
      </div>
      <div class="input-container">
        <label for="password">密码</label>
        <input
            type="password"
            id="password"
            v-model="password"
            placeholder="请输入密码"
            :class="{ 'error': passwordError }"
        />
        <span class="error-message" v-if="passwordError">{{ passwordError }}</span>
      </div>
      <div class="input-container captcha-container">
        <label for="captcha">验证码</label>
        <div class="captcha-wrapper">
          <input
              type="text"
              id="captcha"
              v-model="captcha"
              placeholder="请输入验证码"
              :class="{ 'error': captchaError }"
          />
          <img 
            v-if="captchaImage" 
            :src="captchaImage" 
            alt="验证码" 
            class="captcha-image" 
            @click="refreshCaptcha"
          />
        </div>
        <span class="error-message" v-if="captchaError">{{ captchaError }}</span>
      </div>
      <div class="login-button">
        <button type="submit">登录</button>
      </div>
      <div class="register-link">
        <a @click.prevent="$emit('register')">注册账号</a>
      </div>
    </form>
    <ErrorDialog
      :visible="showError"
      :title="errorTitle"
      :message="errorMessage"
      @close="showError = false"
    />
  </div>
</template>

<script setup>
import { ref, onMounted, computed } from 'vue'
import { useRouter } from 'vue-router'
import { userLogin, getCaptcha } from '../api/login'
import ErrorDialog from './ErrorDialog.vue'

const router = useRouter()
const username = ref('')
const password = ref('')
const captcha = ref('')
const captchaImage = ref('')
const captchaToken = ref('')

// 错误信息
const usernameError = ref('')
const passwordError = ref('')
const captchaError = ref('')

// 错误弹窗相关
const showError = ref(false)
const errorTitle = ref('错误提示')
const errorMessage = ref('')

const showErrorMessage = (message, title = '错误提示') => {
  errorMessage.value = message
  errorTitle.value = title
  showError.value = true
}

const refreshCaptcha = async () => {
  try {
    const response = await getCaptcha()
    // 直接使用API返回的img（base64格式的验证码图片）
    captchaImage.value = `data:image/png;base64,${response.img}`
    captchaToken.value = response.token
    captcha.value = '' // 清空验证码输入
  } catch (error) {
    console.error('获取验证码失败:', error)
    // 优先使用API返回的detail字段
    const apiError = error.response?.data?.detail || error.response?.data?.message
    if (apiError) {
      showErrorMessage(apiError)
    } else {
      showErrorMessage('获取验证码失败，请刷新页面重试')
    }
  }
}


const handleLogin = async () => {
  try {
    // 验证表单
    if (!validateForm()) {
      return
    }

    // 调用登录 API
    const response = await userLogin(
        username.value,
        password.value,
        captchaToken.value,
        captcha.value
    )

    // 登录成功，API中的loginSuccess函数会处理跳转
    // 不需要在这里再次设置localStorage或跳转路由
    console.log('登录成功')
  } catch (error) {
    console.error('登录失败:', error)
    if (error.response) {
      // 优先使用API返回的detail字段
      const apiError = error.response?.data?.detail || error.response?.data?.message
      if (apiError) {
        showErrorMessage(apiError)
      } else {
        showErrorMessage('登录失败，请稍后重试')
      }
    } else {
      showErrorMessage(error.message || '登录失败，请稍后重试')
    }
    // 登录失败时刷新验证码
    refreshCaptcha()
  }
}

const validateForm = () => {
  // 重置所有错误信息
  usernameError.value = ''
  passwordError.value = ''
  captchaError.value = ''

  // 验证用户名
  if (!username.value) {
    usernameError.value = '请输入QQ号码'
    return false
  }
  // 修正正则表达式：只允许纯数字
  if (!/^\d+$/.test(username.value)) {
    usernameError.value = 'QQ号码只能包含数字'
    return false
  }
  if (username.value.length < 4) {
    usernameError.value = 'QQ号码长度不能小于4个字符'
    return false
  }

  // 验证密码
  if (!password.value) {
    passwordError.value = '请输入密码'
    return false
  }
  if (password.value.length < 4) {
    passwordError.value = '密码长度不能小于4个字符'
    return false
  }
  if (password.value.length > 20) {
    passwordError.value = '密码长度不能超过20个字符'
    return false
  }

  // 验证验证码
  if (!captcha.value) {
    captchaError.value = '请输入验证码'
    return false
  }
  if (captcha.value.length !== 4) {
    captchaError.value = '验证码长度不正确'
    return false
  }

  return true
}

// 重置表单数据
const resetForm = () => {
  username.value = ''
  password.value = ''
  captcha.value = ''
  usernameError.value = ''
  passwordError.value = ''
  captchaError.value = ''
  refreshCaptcha()
}

// 暴露resetForm方法给父组件
defineExpose({
  resetForm
})

onMounted(() => {
  refreshCaptcha()
})

</script>

<style scoped>
.login-form {
  width: 340px;
  margin: 0 auto;
  background: rgba(255,255,255,0.95);
  border-radius: 16px;
  box-shadow: 0 4px 32px rgba(0,0,0,0.10);
  padding: 36px 32px 28px 32px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.login-form > div:first-child {
  font-size: 26px;
  font-weight: bold;
  color: #222;
  margin-bottom: 28px;
  letter-spacing: 2px;
}

.login-form-container {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.input-container {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.input-container label {
  font-size: 14px;
  color: #666;
  margin-bottom: 2px;
}

.input-container input {
  height: 40px;
  border: 1px solid #d0d7de;
  border-radius: 6px;
  padding: 0 12px;
  font-size: 15px;
  background: #f7fbfd;
  transition: border 0.2s;
}

.input-container input:focus {
  border: 1.5px solid #409eff;
  outline: none;
  background: #fff;
}

.login-button {
  margin-top: 10px;
}

.login-button button {
  width: 100%;
  height: 42px;
  background: linear-gradient(90deg, #409eff 0%, #6dd5fa 100%);
  color: #fff;
  border: none;
  border-radius: 6px;
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
  box-shadow: 0 2px 8px rgba(64,158,255,0.10);
  transition: background 0.2s;
}

.login-button button:hover {
  background: linear-gradient(90deg, #66b1ff 0%, #6dd5fa 100%);
}

.register-link {
  margin-top: 12px;
  text-align: right;
}

.register-link a {
  color: #409eff;
  font-size: 14px;
  text-decoration: none;
  cursor: pointer;
  transition: color 0.2s;
}

.register-link a:hover {
  color: #1a73e8;
  text-decoration: underline;
}

/* VAPTCHA 相关样式 */
.VAPTCHA-init-main {
  display: table;
  width: 100%;
  height: 100%;
  background-color: #f7fbfd;
  border-radius: 6px;
  border: 1px solid #d0d7de;
}

.VAPTCHA-init-loading {
  display: table-cell;
  vertical-align: middle;
  text-align: center;
}

.VAPTCHA-init-loading>a {
  display: inline-block;
  width: 18px;
  height: 18px;
  border: none;
}

.VAPTCHA-init-loading .VAPTCHA-text {
  font-family: sans-serif;
  font-size: 12px;
  color: #666;
  vertical-align: middle;
}
.error-message {
  color: #f56c6c;
  font-size: 12px;
  margin-top: 2px;
}
.input-container input.error {
  border-color: #f56c6c;
}

.captcha-container {
  margin-bottom: 16px;
  width: 100%;
}

.captcha-wrapper {
  display: flex;
  gap: 8px;
  align-items: center;
  width: 100%;
  position: relative;
}

.captcha-wrapper input {
  flex: 1;
  height: 40px;
  border: 1px solid #d0d7de;
  border-radius: 6px;
  padding: 0 12px;
  font-size: 15px;
  background: #f7fbfd;
  transition: border 0.2s;
  width: calc(100% - 120px); /* 减去验证码图片的宽度和间距 */
}

.captcha-wrapper input:focus {
  border: 1.5px solid #409eff;
  outline: none;
  background: #fff;
}

.captcha-image {
  width: 110px;
  height: 40px;
  border-radius: 6px;
  cursor: pointer;
  object-fit: cover;
  border: 1px solid #d0d7de;
  transition: all 0.3s;
}

.captcha-image:hover {
  opacity: 0.8;
}

/* 移动端适配 */
@media screen and (max-width: 480px) {
  .login-form {
    width: 90%;
    max-width: 340px;
    padding: 24px 20px 20px 20px;
  }

  .captcha-wrapper {
    gap: 6px;
  }

  .captcha-wrapper input {
    width: calc(100% - 100px);
  }

  .captcha-image {
    width: 90px;
  }

  .input-container input,
  .captcha-wrapper input {
    height: 38px;
    font-size: 14px;
  }

  .login-button button {
    height: 40px;
    font-size: 15px;
  }
}

/* 超小屏幕适配 */
@media screen and (max-width: 320px) {
  .login-form {
    padding: 20px 16px 16px 16px;
  }

  .captcha-wrapper input {
    width: calc(100% - 90px);
    padding: 0 8px;
  }

  .captcha-image {
    width: 80px;
  }
}
</style>

