<template>
  <div class="login-container">
    <div class="login-background">
      <div class="shapes">
        <div class="shape shape-1"></div>
        <div class="shape shape-2"></div>
        <div class="shape shape-3"></div>
        <div class="particles"></div>
      </div>
    </div>

    <div class="login-content">
      <div class="login-header">
        <div class="logo-wrapper">
          <div class="logo-text">
            <div class="logo-title">
              <span class="highlight">C/C++</span>
              <span>在线学习平台</span>
            </div>
            <p class="logo-subtitle">Knowledge & Practice & Growth</p>
          </div>
        </div>
      </div>

      <div class="login-box">
        <div class="form-tabs">
          <div
            class="tab-item"
            :class="{ active: activeTab === 'login' }"
            @click="activeTab = 'login'"
          >
            登录
          </div>
          <div
            class="tab-item"
            :class="{ active: activeTab === 'register' }"
            @click="activeTab = 'register'"
          >
            注册
          </div>
          <div class="tab-line" :class="activeTab"></div>
        </div>

        <div class="form-container" v-if="activeTab === 'login'">
          <el-form
            ref="loginFormRef"
            :model="loginForm"
            :rules="loginRules"
            class="login-form"
          >
            <el-form-item prop="username">
              <div class="custom-input" :class="{ focused: activeInput === 'username' }">
                <el-icon><User /></el-icon>
                <input
                  v-model="loginForm.username"
                  type="text"
                  placeholder="请输入用户名"
                  @focus="activeInput = 'username'"
                  @blur="activeInput = ''"
                >
              </div>
            </el-form-item>

            <el-form-item prop="password">
              <div class="custom-input" :class="{ focused: activeInput === 'password' }">
                <el-icon><Lock /></el-icon>
                <input
                  v-model="loginForm.password"
                  :type="showPassword ? 'text' : 'password'"
                  placeholder="请输入密码"
                  @focus="activeInput = 'password'"
                  @blur="activeInput = ''"
                >
                <el-icon
                  class="password-toggle"
                  @click="showPassword = !showPassword"
                >
                  <View v-if="showPassword" />
                  <Hide v-else />
                </el-icon>
              </div>
            </el-form-item>

            <div class="form-options">
              <el-checkbox v-model="rememberMe">记住我</el-checkbox>
              <el-button type="text" @click="forgotPassword">忘记密码？</el-button>
            </div>

            <el-button
              type="primary"
              class="submit-btn"
              :loading="loading"
              @click="handleLogin"
            >
              登录
            </el-button>

            <div class="divider">
              <span>其他登录方式</span>
            </div>

            <div class="social-login">
              <button class="social-btn wechat" title="微信登录">
                <el-icon><ChatDotRound /></el-icon>
              </button>
              <button class="social-btn qq" title="QQ登录">
                <el-icon><Message /></el-icon>
              </button>
              <button class="social-btn github" title="Github登录">
                <el-icon><Position /></el-icon>
              </button>
            </div>
          </el-form>
        </div>

        <div class="form-container" v-else>
          <el-form
            ref="registerFormRef"
            :model="registerForm"
            :rules="registerRules"
            class="register-form"
          >
            <el-form-item prop="username">
              <div class="custom-input">
                <el-icon><User /></el-icon>
                <input
                  v-model="registerForm.username"
                  type="text"
                  placeholder="请设置用户名"
                >
              </div>
            </el-form-item>

            <el-form-item prop="email">
              <div class="custom-input">
                <el-icon><Message /></el-icon>
                <input
                  v-model="registerForm.email"
                  type="email"
                  placeholder="请输入邮箱"
                >
                <el-button
                  class="verify-btn"
                  type="primary"
                  :disabled="cooldown > 0"
                  @click="sendVerifyCode"
                >
                  {{ cooldown > 0 ? `${cooldown}s` : '发送验证码' }}
                </el-button>
              </div>
            </el-form-item>

            <el-form-item prop="verifyCode">
              <div class="custom-input">
                <el-icon><Key /></el-icon>
                <input
                  v-model="registerForm.verifyCode"
                  type="text"
                  placeholder="请输入验证码"
                  maxlength="6"
                >
              </div>
            </el-form-item>

            <el-form-item prop="password">
              <div class="custom-input">
                <el-icon><Lock /></el-icon>
                <input
                  v-model="registerForm.password"
                  :type="showPassword ? 'text' : 'password'"
                  placeholder="请设置密码"
                >
                <el-icon
                  class="password-toggle"
                  @click="showPassword = !showPassword"
                >
                  <View v-if="showPassword" />
                  <Hide v-else />
                </el-icon>
              </div>
            </el-form-item>

            <el-form-item prop="confirmPassword">
              <div class="custom-input">
                <el-icon><Lock /></el-icon>
                <input
                  v-model="registerForm.confirmPassword"
                  :type="showPassword ? 'text' : 'password'"
                  placeholder="请确认密码"
                >
              </div>
            </el-form-item>

            <el-button
              type="primary"
              class="submit-btn"
              :loading="loading"
              @click="handleRegister"
            >
              注册
            </el-button>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '../stores/user'
import { ElMessage } from 'element-plus'
import {
  User, Lock, View, Hide, Message,
  ChatDotRound, Position, Key
} from '@element-plus/icons-vue'
import { registerMethod, sendVerifyCodeMethod } from "@/api/user.js"

const router = useRouter()
const userStore = useUserStore()
const activeTab = ref('login')
const showPassword = ref(false)
const loading = ref(false)
const rememberMe = ref(false)
const activeInput = ref('')
const cooldown = ref(0)

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

const registerForm = reactive({
  username: '',
  email: '',
  verifyCode: '',
  password: '',
  confirmPassword: ''
})

const loginRules = {
  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' }
  ]
}

const registerRules = {
  username: [
    { required: true, message: '请设置用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  email: [
    { required: true, message: '请输入邮箱地址', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
  ],
  verifyCode: [
    { required: true, message: '请输入验证码', trigger: 'blur' },
    { len: 6, message: '验证码长度应为6位', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请设置密码', trigger: 'blur' },
    { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请确认密码', trigger: 'blur' },
    { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
  ]
}

const handleLogin = async () => {
  loading.value = true
  try {
    if (loginForm.username === 'admin' && loginForm.password === 'admin123') {
      await userStore.mockAdminLogin()
      ElMessage.success('登录成功')
      router.push('/')
    } else {
      ElMessage.error('用户名或密码错误')
    }
  } catch (error) {
    ElMessage.error('登录失败: ' + error.message)
  } finally {
    loading.value = false
  }
}

const handleRegister = () => {
  if (registerForm.password !== registerForm.confirmPassword){
    ElMessage.error('两次密码不一致')
    return
  }

  registerMethod(registerForm)

}

const forgotPassword = () => {
  ElMessage.info('忘记密码功能开发中')
}

const sendVerifyCode = async () => {
  if (!registerForm.email) {
    ElMessage.warning('请先输入邮箱地址')
    return
  }

  try {
    var axiosResponse = await sendVerifyCodeMethod(registerForm.email);
    ElMessage.success('验证码已发送')
    cooldown.value = 60
    const timer = setInterval(() => {
      cooldown.value--
      if (cooldown.value <= 0) {
        clearInterval(timer)
      }
    }, 1000)

    console.log(axiosResponse)

  } catch (error) {
    ElMessage.error('验证码发送失败: ' + error.message)
  }
}
</script>

<style lang="scss" scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #1a365d 0%, #2563eb 100%);
  position: relative;
  overflow: hidden;
  padding: 20px;

  .login-background {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 0;

    .shapes {
      position: relative;
      height: 100%;

      .shape {
        position: absolute;
        background: rgba(255, 255, 255, 0.1);
        backdrop-filter: blur(5px);
        border-radius: 50%;
        animation: float 6s ease-in-out infinite;

        &-1 {
          width: 300px;
          height: 300px;
          top: -150px;
          right: -100px;
          background: radial-gradient(circle at center, rgba(96, 165, 250, 0.2), transparent);
        }

        &-2 {
          width: 200px;
          height: 200px;
          bottom: -100px;
          left: -50px;
          background: radial-gradient(circle at center, rgba(59, 130, 246, 0.2), transparent);
          animation-delay: -2s;
        }

        &-3 {
          width: 150px;
          height: 150px;
          bottom: 50%;
          right: 10%;
          background: radial-gradient(circle at center, rgba(37, 99, 235, 0.2), transparent);
          animation-delay: -4s;
        }
      }

      .particles {
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background-image: radial-gradient(circle at center, rgba(255, 255, 255, 0.1) 1px, transparent 1px);
        background-size: 50px 50px;
        animation: particlesAnimation 20s linear infinite;
        opacity: 0.3;
      }
    }
  }

  .login-content {
    position: relative;
    z-index: 1;
    width: 100%;
    max-width: 460px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 30px;

    .login-header {
      text-align: center;

      .logo-wrapper {
        .logo-text {
          .logo-title {
            font-size: 32px;
            font-weight: bold;
            margin-bottom: 12px;
            color: white;

            .highlight {
              background: linear-gradient(to right, #60a5fa, #34d399);
              -webkit-background-clip: text;
              -webkit-text-fill-color: transparent;
              padding-right: 8px;
            }
          }

          .logo-subtitle {
            font-size: 16px;
            opacity: 0.8;
            color: #e0e7ff;
            font-weight: 300;
            letter-spacing: 2px;
          }
        }
      }
    }

    .login-box {
      background: rgba(255, 255, 255, 0.95);
      backdrop-filter: blur(10px);
      border-radius: 16px;
      padding: 30px;
      width: 100%;
      max-width: 460px;
      box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);

      .form-container {
        width: 100%;
      }

      .login-form,
      .register-form {
        width: 100%;
      }

      .custom-input {
        width: 100%;
        background: #f8fafc;
        border: 2px solid transparent;
        border-radius: 12px;
        padding: 12px 16px;
        display: flex;
        align-items: center;
        transition: all 0.3s ease;
        margin-bottom: 20px;

        &:hover {
          background: #fff;
          border-color: #e2e8f0;
        }

        &.focused {
          background: #fff;
          border-color: #4080ff;
          box-shadow: 0 0 0 4px rgba(64, 128, 255, 0.1);
        }

        .el-icon {
          font-size: 18px;
          color: #94a3b8;
          margin-right: 12px;
        }

        input {
          width: 100%;
          flex: 1;
          border: none;
          background: none;
          outline: none;
          font-size: 15px;
          color: #1e293b;

          &::placeholder {
            color: #94a3b8;
          }
        }

        .password-toggle {
          cursor: pointer;
          color: #94a3b8;
          transition: color 0.3s;

          &:hover {
            color: #4080ff;
          }
        }

        .verify-btn {
          padding: 6px 12px;
          height: 32px;
          font-size: 14px;
          border-radius: 6px;
          margin-left: 8px;
          flex-shrink: 0;

          &:disabled {
            cursor: not-allowed;
            opacity: 0.6;
          }
        }
      }

      .form-options {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin: 24px 0;

        .el-checkbox {
          .el-checkbox__label {
            color: #64748b;
          }
        }

        .el-button {
          color: #4080ff;

          &:hover {
            color: #2563eb;
          }
        }
      }

      .submit-btn {
        width: 100%;
        height: 44px;
        font-size: 16px;
        font-weight: 500;
        border-radius: 12px;
        background: linear-gradient(135deg, #4080ff 0%, #2563eb 100%);
        border: none;
        margin-bottom: 24px;

        &:hover {
          transform: translateY(-1px);
          box-shadow: 0 4px 12px rgba(37, 99, 235, 0.2);
        }

        &:active {
          transform: translateY(0);
        }
      }

      .divider {
        position: relative;
        text-align: center;
        margin: 32px 0;

        &::before,
        &::after {
          content: '';
          position: absolute;
          top: 50%;
          width: calc(50% - 30px);
          height: 1px;
          background: #e2e8f0;
        }

        &::before { left: 0; }
        &::after { right: 0; }

        span {
          background: white;
          padding: 0 12px;
          color: #64748b;
          font-size: 14px;
        }
      }

      .form-tabs {
        display: flex;
        margin-bottom: 30px;
        position: relative;
        border-bottom: 2px solid #e2e8f0;

        .tab-item {
          flex: 1;
          text-align: center;
          padding: 12px;
          font-size: 16px;
          color: #64748b;
          cursor: pointer;
          transition: all 0.3s;

          &.active {
            color: #4080ff;
            font-weight: 500;
          }
        }

        .tab-line {
          position: absolute;
          bottom: -2px;
          left: 0;
          width: 50%;
          height: 2px;
          background: #4080ff;
          transition: transform 0.3s;

          &.register {
            transform: translateX(100%);
          }
        }
      }

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

        .social-btn {
          width: 40px;
          height: 40px;
          border-radius: 50%;
          border: none;
          background: #f3f4f6;
          cursor: pointer;
          transition: all 0.3s;
          display: flex;
          align-items: center;
          justify-content: center;

          .el-icon {
            font-size: 18px;
            color: #666;
          }

          &:hover {
            transform: translateY(-2px);
          }

          &.wechat:hover {
            background: #07c160;
            .el-icon { color: white; }
          }

          &.qq:hover {
            background: #12b7f5;
            .el-icon { color: white; }
          }

          &.github:hover {
            background: #333;
            .el-icon { color: white; }
          }
        }
      }
    }
  }
}

@keyframes float {
  0%, 100% {
    transform: translate(0, 0);
  }
  50% {
    transform: translate(0, -20px);
  }
}

@keyframes particlesAnimation {
  from {
    transform: translateY(0);
  }
  to {
    transform: translateY(-50px);
  }
}

@media (max-width: 768px) {
  .login-container {
    padding: 15px;

    .login-content {
      .login-header {
        .logo-wrapper {
          .logo-text {
            .logo-title {
              font-size: 28px;
            }
            .logo-subtitle {
              font-size: 14px;
            }
          }
        }
      }

      .login-box {
        padding: 25px 20px;

        .custom-input {
          padding: 10px 14px;

          input {
            font-size: 14px;
          }
        }

        .submit-btn {
          height: 40px;
          font-size: 14px;
        }
      }
    }
  }
}
</style>
