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

    <div class="login-content">
      <div class="login-card">
        <!-- 品牌标识 -->
        <div class="brand-section">
          <div class="brand-logo">
            <i class="fas fa-cube"></i>
          </div>
          <h1 class="brand-title">工业模型软件管理统一平台</h1>
          <p class="brand-subtitle">Industrial CAD Management Platform</p>
        </div>

        <!-- 登录表单 -->
        <form @submit.prevent="handleLogin" class="login-form">
          <h2 class="form-title">系统登录</h2>

          <!-- 错误提示 -->
          <div v-if="loginError" class="error-message">
            <i class="fas fa-exclamation-triangle"></i>
            <span>{{ loginError }}</span>
          </div>

          <!-- 用户名输入 -->
          <div class="form-group">
            <label for="username" class="form-label">
              <i class="fas fa-user"></i>
              用户名
            </label>
            <input
              id="username"
              v-model="formData.username"
              type="text"
              class="form-input"
              :class="{ 'error': errors.username }"
              placeholder="请输入用户名"
              autocomplete="username"
              @blur="validateField('username')"
              @input="clearFieldError('username')"
            />
            <div v-if="errors.username" class="field-error">
              {{ errors.username }}
            </div>
          </div>

          <!-- 密码输入 -->
          <div class="form-group">
            <label for="password" class="form-label">
              <i class="fas fa-lock"></i>
              密码
            </label>
            <div class="password-input-wrapper">
              <input
                id="password"
                v-model="formData.password"
                :type="showPassword ? 'text' : 'password'"
                class="form-input"
                :class="{ 'error': errors.password }"
                placeholder="请输入密码"
                autocomplete="current-password"
                @blur="validateField('password')"
                @input="clearFieldError('password')"
              />
              <button
                type="button"
                class="password-toggle"
                @click="togglePassword"
                :title="showPassword ? '隐藏密码' : '显示密码'"
              >
                <i :class="showPassword ? 'fas fa-eye-slash' : 'fas fa-eye'"></i>
              </button>
            </div>
            <div v-if="errors.password" class="field-error">
              {{ errors.password }}
            </div>
          </div>

          <!-- 记住我 -->
          <div class="form-options">
            <label class="checkbox-wrapper">
              <input
                v-model="formData.rememberMe"
                type="checkbox"
                class="checkbox-input"
              />
              <span class="checkbox-custom"></span>
              <span class="checkbox-label">记住我</span>
            </label>
          </div>

          <!-- 登录按钮 -->
          <button
            type="submit"
            class="login-button"
            :disabled="isLoading || !isFormValid"
            :class="{ 'loading': isLoading }"
          >
            <span v-if="!isLoading">
              <i class="fas fa-sign-in-alt"></i>
              登录
            </span>
            <span v-else>
              <i class="fas fa-spinner fa-spin"></i>
              登录中...
            </span>
          </button>

          <!-- 帮助信息 -->
          <div class="help-text">
            <p>联系系统管理员获取账号信息</p>
          </div>
        </form>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '@/stores/auth'
import { getValidationRules } from '@/config/auth'

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

// 获取验证规则配置
const validationRules = getValidationRules()

// 表单数据
const formData = ref({
  username: '',
  password: '',
  rememberMe: false
})

// 表单验证错误
const errors = ref({
  username: '',
  password: ''
})

// UI状态
const showPassword = ref(false)

// 计算属性
const isLoading = computed(() => authStore.isLoading)
const loginError = computed(() => authStore.loginError)

const isFormValid = computed(() => {
  return formData.value.username.trim() &&
         formData.value.password.trim() &&
         !errors.value.username &&
         !errors.value.password
})

// 方法
const togglePassword = () => {
  showPassword.value = !showPassword.value
}

const validateField = (field) => {
  switch (field) {
    case 'username':
      if (!formData.value.username.trim()) {
        errors.value.username = '请输入用户名'
      } else if (formData.value.username.length < validationRules.MIN_USERNAME_LENGTH) {
        errors.value.username = `用户名至少${validationRules.MIN_USERNAME_LENGTH}个字符`
      } else {
        errors.value.username = ''
      }
      break

    case 'password':
      if (!formData.value.password.trim()) {
        errors.value.password = '请输入密码'
      } else if (formData.value.password.length < validationRules.MIN_PASSWORD_LENGTH) {
        errors.value.password = `密码至少${validationRules.MIN_PASSWORD_LENGTH}个字符`
      } else {
        errors.value.password = ''
      }
      break
  }
}

const clearFieldError = (field) => {
  if (errors.value[field]) {
    errors.value[field] = ''
  }
}

const validateForm = () => {
  validateField('username')
  validateField('password')
  return !errors.value.username && !errors.value.password
}

const handleLogin = async () => {
  // 清除之前的错误
  authStore.clearError()

  // 验证表单
  if (!validateForm()) {
    return
  }

  // 执行登录
  const result = await authStore.login({
    username: formData.value.username.trim(),
    password: formData.value.password
  })

  if (result.success) {
    // 登录成功，跳转到主页面
    router.push('/')
  }
}

// 组件挂载时恢复记住的用户名
onMounted(() => {
  const rememberedUsername = localStorage.getItem('rememberedUsername')
  if (rememberedUsername) {
    formData.value.username = rememberedUsername
    formData.value.rememberMe = true
  }
})

</script>

<style scoped>
.login-container {
  height: 100vh;
  width: 100vw;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  background: var(--color-bg-gradient-main);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  overflow: hidden;
  margin: 0;
  padding: 0;
}

.login-background {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  overflow: hidden;
}

.bg-decoration {
  position: relative;
  width: 100%;
  height: 100%;
}

.circle {
  position: absolute;
  border-radius: 50%;
  background: var(--color-gradient-brand-bg);
  animation: float var(--login-float-duration) ease-in-out infinite;
}

.circle-1 {
  width: var(--login-animation-large);
  height: var(--login-animation-large);
  top: var(--login-circle-1-top);
  left: var(--login-circle-1-left);
  animation-delay: var(--login-animation-delay-1);
}

.circle-2 {
  width: var(--login-animation-medium);
  height: var(--login-animation-medium);
  top: var(--login-circle-2-top);
  right: var(--login-circle-2-right);
  animation-delay: var(--login-animation-delay-2);
}

.circle-3 {
  width: var(--login-animation-small);
  height: var(--login-animation-small);
  bottom: var(--login-circle-3-bottom);
  left: var(--login-circle-3-left);
  animation-delay: var(--login-animation-delay-3);
}

@keyframes float {
  0%, 100% { transform: translateY(0px) rotate(0deg); }
  50% { transform: translateY(var(--login-float-distance)) rotate(180deg); }
}

.login-content {
  position: relative;
  z-index: 1;
  width: 100%;
  max-width: var(--login-card-max-width);
  padding: var(--login-card-padding-small);
}

.login-card {
  background: var(--color-bg-dark-95);
  border-radius: var(--login-card-border-radius);
  padding: var(--login-card-padding);
  box-shadow: 0 20px 40px var(--color-shadow-black-3);
  backdrop-filter: blur(10px);
  border: 1px solid var(--color-white-rgb-1);
}

.brand-section {
  text-align: center;
  margin-bottom: var(--login-brand-margin-bottom);
}

.brand-logo {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: var(--login-icon-size);
  height: var(--login-icon-size);
  background: var(--color-primary-gradient);
  border-radius: var(--login-card-border-radius);
  margin-bottom: var(--login-brand-logo-margin-bottom);
  box-shadow: 0 4px 15px var(--color-primary-rgb-3);
}

.brand-logo i {
  font-size: var(--login-title-size);
  color: var(--color-text-primary);
}

.brand-title {
  font-size: var(--login-subtitle-size);
  font-weight: 600;
  color: var(--color-text-primary);
  margin: 0 0 var(--login-brand-title-margin-bottom) 0;
  line-height: 1.4;
}

.brand-subtitle {
  font-size: var(--login-text-size);
  color: var(--color-text-secondary);
  margin: 0;
  font-weight: 400;
}

.login-form {
  width: 100%;
}

.form-title {
  font-size: var(--login-button-text-size);
  font-weight: 600;
  color: var(--color-text-primary);
  text-align: center;
  margin: 0 0 var(--login-form-title-margin-bottom) 0;
}

.error-message {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: var(--input-padding-vertical) var(--input-padding-horizontal);
  background: var(--color-error-rgb-1);
  border: 1px solid var(--color-error-rgb-3);
  border-radius: var(--size-border-radius);
  color: var(--color-text-error);
  font-size: var(--login-text-size);
  margin-bottom: var(--login-error-margin-bottom);
}

.form-group {
  margin-bottom: var(--login-form-group-margin-bottom);
}

.form-label {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: var(--login-text-size);
  font-weight: 500;
  color: var(--color-text-secondary);
  margin-bottom: var(--login-form-label-margin-bottom);
}

.form-input {
  width: 100%;
  padding: var(--input-padding-vertical) var(--input-padding-horizontal);
  background: var(--color-white-rgb-05);
  border: 1px solid var(--color-white-rgb-1);
  border-radius: var(--size-border-radius);
  color: var(--color-text-primary);
  font-size: var(--login-text-size);
  transition: var(--transition-all);
  box-sizing: border-box;
}

.form-input:focus {
  outline: none;
  border-color: var(--color-border-active);
  box-shadow: 0 0 0 3px var(--color-primary-rgb);
  background: var(--color-white-rgb-1);
}

.form-input.error {
  border-color: var(--color-border-error);
}

.form-input::placeholder {
  color: var(--color-text-tertiary);
}

.password-input-wrapper {
  position: relative;
}

.password-toggle {
  position: absolute;
  right: 12px;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  color: var(--color-text-secondary);
  cursor: pointer;
  padding: 4px;
  transition: color 0.3s ease;
}

.password-toggle:hover {
  color: var(--color-text-primary);
}

.field-error {
  font-size: var(--font-size-xs);
  color: var(--color-text-error);
  margin-top: var(--login-field-error-margin-top);
}

.form-options {
  margin-bottom: var(--login-form-options-margin-bottom);
}

.checkbox-wrapper {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  user-select: none;
}

.checkbox-input {
  display: none;
}

.checkbox-custom {
  width: 16px;
  height: 16px;
  border: 1px solid var(--color-white-rgb-3);
  border-radius: 4px;
  background: var(--color-white-rgb-05);
  position: relative;
  transition: var(--transition-all);
}

.checkbox-input:checked + .checkbox-custom {
  background: var(--color-primary);
  border-color: var(--color-border-active);
}

.checkbox-input:checked + .checkbox-custom::after {
  content: '✓';
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  color: var(--color-text-primary);
  font-size: 12px;
  font-weight: bold;
}

.checkbox-label {
  font-size: var(--login-text-size);
  color: var(--color-text-secondary);
}

.login-button {
  width: 100%;
  padding: var(--login-button-padding);
  background: var(--color-primary-gradient);
  border: none;
  border-radius: var(--size-border-radius);
  color: var(--color-text-primary);
  font-size: var(--font-size-lg);
  font-weight: 600;
  cursor: pointer;
  transition: var(--transition-all);
  box-shadow: 0 4px 15px var(--color-primary-rgb-3);
}

.login-button:hover:not(:disabled) {
  transform: var(--login-button-transform);
  box-shadow: 0 6px 20px var(--color-primary-rgb-4);
}

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

.login-button:disabled {
  opacity: var(--login-button-disabled-opacity);
  cursor: not-allowed;
  transform: none;
}

.login-button.loading {
  background: var(--color-primary-gradient-hover);
}

.help-text {
  text-align: center;
  margin-top: var(--login-help-margin-top);
}

.help-text p {
  font-size: var(--font-size-sm);
  color: var(--color-text-tertiary);
  margin: 0;
}

/* 响应式设计 */
@media (max-width: 480px) {
  .login-content {
    padding: var(--login-content-padding-mobile);
  }

  .login-card {
    padding: var(--login-card-padding-mobile);
  }

  .brand-title {
    font-size: var(--login-title-size-mobile);
  }

  .circle {
    display: none;
  }
}
</style>