<template>
  <div class="login-container">
    <div class="login-form-wrapper">
      <!-- 登录表单卡片 -->
      <div class="login-card">
        <!-- 标题区域 -->
        <div class="login-header">
          <h2 class="login-title">欢迎回来</h2>
          <p class="login-subtitle">请登录您的账号继续访问</p>
        </div>

        <!-- 表单区域 -->
        <form @submit.prevent="handleLogin" class="login-form">
          <!-- 用户名输入 -->
          <div class="form-group">
            <div class="input-wrapper">
              <!-- <i class="el-icon-user input-icon"></i> -->
              <input id="username" v-model="loginForm.username" type="text" placeholder="请输入用户名" class="form-input"
                :class="{ 'input-error': loginForm.username }" @blur="validateField('username')" />
            </div>
            <span v-if="errors.username" class="error-message">{{ errors.username }}</span>
          </div>

          <!-- 密码输入 -->
          <div class="form-group">
            <div class="input-wrapper">
              <!-- <i class="el-icon-lock input-icon"></i> -->
              <input id="password" v-model="loginForm.password" :type="showPassword ? 'text' : 'password'"
                placeholder="请输入密码" class="form-input" :class="{ 'input-error': loginForm.password }"
                @blur="validateField('password')" />
              <i :class="showPassword ? 'el-icon-view' : 'el-icon-view-off'" class="toggle-password-icon"
                @click="togglePassword"></i>
            </div>
            <span v-if="errors.password" class="error-message">{{ errors.password }}</span>
          </div>

          <!-- 记住我和忘记密码 -->
          <div class="form-options">
            <label class="remember-me">
              <input type="checkbox" v-model="loginForm.rememberMe" class="remember-checkbox" />
              <span>记住我</span>
            </label>
            <a href="#" class="forgot-password" @click.prevent="handleForgotPassword">忘记密码？</a>
          </div>

          <!-- 登录按钮 -->
          <button type="submit" class="login-button" :disabled="isLoading">
            <span v-if="!isLoading">登录</span>
            <span v-else class="loading-text">
              <i class="el-icon-loading"></i> 登录中...
            </span>
          </button>

          <!-- 其他登录方式 -->
          <div class="other-login-methods">
            <div class="divider">
              <span>其他登录方式</span>
            </div>
            <div class="social-login-buttons">
              <button type="button" class="social-button wechat" @click="handleSocialLogin('wechat')">
                <i class="el-icon-chat-dot-square"></i>
              </button>
              <button type="button" class="social-button qq" @click="handleSocialLogin('qq')">
                <i class="el-icon-message"></i>
              </button>
              <button type="button" class="social-button github" @click="handleSocialLogin('github')">
                <i class="el-icon-document-checked"></i>
              </button>
            </div>
          </div>
        </form>

        <!-- 注册提示 -->
        <div class="register-prompt">
          <span>还没有账号？</span>
          <router-link to="/register" class="register-link">立即注册</router-link>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import { loginService } from '@/services'
import { ElMessage } from 'element-plus'
// 全局状态管理
import { useGlobalStore } from '@/store/globalStore'
const globalStore = useGlobalStore()
const router = useRouter()
const errors = ref({
  username: null,
  password: null,
})
const loginForm = reactive({
  username: null,
  password: null,
})

// 切换密码显示状态
const togglePassword = () => {
  form.showPassword = !form.showPassword
}

// 验证单个字段
const validateField = (field) => {
  // console.log('validateField', field)
  errors.value = {}

  if (field === 'username' && !loginForm.username) {
    errors.value.username = '请输入用户名'
  }

  if (field === 'password' && !loginForm.password) {
    errors.value.password = '请输入密码'
  } else if (field === 'password' && loginForm.password.length < 6) {
    errors.value.password = '密码长度至少为6位'
  }
}

// 验证整个表单
const validateForm = () => {
  errors.value = {}
  let isValid = true

  if (!loginForm.username) {
    errors.value.username = '请输入用户名'
    isValid = false
  }

  if (!loginForm.password) {
    errors.value.password = '请输入密码'
    isValid = false
  } else if (loginForm.password.length < 6) {
    errors.value.password = '密码长度至少为6位'
    isValid = false
  }
  return isValid;
}
// 处理登录
const handleLogin = async () => {
  if (!validateForm()) {
    return
  }
  try {
    // 模拟登录请求
    let user = await (await loginService.login(loginForm)).data
    if (!user) {
      ElMessage.error('登录失败，请检查用户名和密码')
      return
    }
    // console.log('登录成功', user)
    // 这里应该是实际的登录API调用
    // console.log('登录请求数据:', loginForm)

    // 模拟登录成功
    ElMessage.success('登录成功')
    // 登录成功后，设置全局状态为已登录
    globalStore.setLoginStatus(true)
    // 登录成功后，添加全局状态
    globalStore.setValue('loginhomestatus', {
      status: 1 // 2:删除  1:已发布 0:发布登录
    })
    // console.log('globalStore.Login', globalStore.Login)
    // 保存登录状态token
    if (user.token) {
      localStorage.setItem('token', user.token)
    }
    if (user.username) {
      // 记住用户名
      globalStore.setUsername(user.username)
    }
    // 跳转到首页
    router.push('/')
  } catch (error) {
    ElMessage.error('登录失败，请检查用户名和密码')
    console.error('登录错误:', error)
  } finally {
    globalStore.setLoading(false)
  }
}

// 处理忘记密码
const handleForgotPassword = () => {
  ElMessage.info('忘记密码功能开发中')
}
// 处理第三方登录
const handleSocialLogin = (provider) => {
  ElMessage.info(`${provider} 登录功能开发中`)
}

// mounted() {
//   // 从localStorage中恢复用户名
//   const savedUsername = localStorage.getItem('username')
//   if (savedUsername) {
//     loginForm.username = savedUsername
//     loginForm.rememberMe = true
//   }
// }
</script>

<style scoped>
/* 登录页面容器 */
.login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #f5f7fa;
  padding: 20px;
  background-image: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  position: relative;
  overflow: hidden;
}

/* 添加背景装饰 */
.login-container::before,
.login-container::after {
  content: '';
  position: absolute;
  width: 300px;
  height: 300px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
  z-index: 0;
}

.login-container::before {
  top: -100px;
  right: -100px;
}

.login-container::after {
  bottom: -100px;
  left: -100px;
}

/* 登录表单包装器 */
.login-form-wrapper {
  position: relative;
  z-index: 1;
  width: 100%;
  max-width: 450px;
}

/* 登录卡片 */
.login-card {
  background: white;
  border-radius: 20px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
  padding: 40px;
  transition: transform 0.3s ease, box-shadow 0.3s ease;
}

.login-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 25px 50px rgba(0, 0, 0, 0.15);
}

/* 登录头部 */
.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-title {
  font-size: 28px;
  font-weight: 700;
  color: #333;
  margin-bottom: 10px;
  background: linear-gradient(90deg, #667eea, #764ba2);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.login-subtitle {
  font-size: 16px;
  color: #666;
  margin: 0;
}

/* 表单样式 */
.login-form {
  width: 100%;
}

.form-group {
  margin-bottom: 24px;
  position: relative;
}

.form-label {
  display: block;
  font-size: 14px;
  font-weight: 500;
  color: #333;
  margin-bottom: 8px;
}

.input-wrapper {
  position: relative;
  display: flex;
  align-items: center;
}

.form-input {
  width: 100%;
  height: 48px;
  padding: 0 45px;
  border: 2px solid #e1e5e9;
  border-radius: 12px;
  font-size: 16px;
  transition: all 0.3s ease;
  background-color: #fafbfc;
}

.form-input:focus {
  outline: none;
  border-color: #667eea;
  background-color: white;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

.form-input.input-error {
  border-color: #f56565;
}

.input-icon {
  position: absolute;
  left: 15px;
  color: #94a3b8;
  font-size: 18px;
}

.toggle-password-icon {
  position: absolute;
  right: 15px;
  color: #94a3b8;
  font-size: 18px;
  cursor: pointer;
  transition: color 0.3s ease;
}

.toggle-password-icon:hover {
  color: #667eea;
}

.error-message {
  display: block;
  font-size: 12px;
  color: #f56565;
  margin-top: 4px;
}

/* 表单选项 */
.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
}

.remember-me {
  display: flex;
  align-items: center;
  cursor: pointer;
  user-select: none;
}

.remember-checkbox {
  margin-right: 8px;
  width: 16px;
  height: 16px;
  cursor: pointer;
}

.forgot-password {
  font-size: 14px;
  color: #667eea;
  text-decoration: none;
  transition: color 0.3s ease;
}

.forgot-password:hover {
  color: #5a67d8;
  text-decoration: underline;
}

/* 登录按钮 */
.login-button {
  width: 100%;
  height: 48px;
  background: linear-gradient(90deg, #667eea, #764ba2);
  color: white;
  border: none;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
}

.login-button:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 10px 20px rgba(102, 126, 234, 0.4);
}

.login-button:active:not(:disabled) {
  transform: translateY(0);
}

.login-button:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.loading-text {
  display: flex;
  align-items: center;
}

.loading-text i {
  margin-right: 8px;
}

/* 其他登录方式 */
.other-login-methods {
  margin-top: 30px;
}

.divider {
  position: relative;
  text-align: center;
  margin-bottom: 20px;
}

.divider span {
  position: relative;
  background: white;
  padding: 0 20px;
  color: #666;
  font-size: 14px;
}

.divider::before {
  content: '';
  position: absolute;
  top: 50%;
  left: 0;
  right: 0;
  height: 1px;
  background: #e1e5e9;
  z-index: 0;
}

.social-login-buttons {
  display: flex;
  justify-content: center;
  gap: 16px;
}

.social-button {
  width: 44px;
  height: 44px;
  border: 2px solid #e1e5e9;
  border-radius: 50%;
  background: white;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  transition: all 0.3s ease;
}

.social-button:hover {
  transform: translateY(-3px);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}

.social-button.wechat:hover {
  border-color: #07c160;
  color: #07c160;
}

.social-button.qq:hover {
  border-color: #1da1f2;
  color: #1da1f2;
}

.social-button.github:hover {
  border-color: #333;
  color: #333;
}

/* 注册提示 */
.register-prompt {
  text-align: center;
  margin-top: 30px;
  font-size: 14px;
  color: #666;
}

.register-link {
  color: #667eea;
  text-decoration: none;
  font-weight: 500;
  transition: color 0.3s ease;
}

.register-link:hover {
  color: #5a67d8;
  text-decoration: underline;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .login-container {
    padding: 10px;
  }

  .login-card {
    padding: 30px 20px;
    border-radius: 16px;
  }

  .login-title {
    font-size: 24px;
  }

  .form-input {
    height: 44px;
    font-size: 15px;
  }

  .login-button {
    height: 44px;
  }
}

@media (max-width: 480px) {
  .login-card {
    padding: 25px 15px;
  }

  .social-login-buttons {
    gap: 12px;
  }

  .social-button {
    width: 40px;
    height: 40px;
    font-size: 18px;
  }
}
</style>