<template>
  <form class="login-form" @submit.prevent="handleLogin">
    <!-- 邮箱输入 -->
    <div class="form-group">
      <label class="form-label" for="email">{{ t('login.email_label') }}</label>
      <div class="input-wrapper" :class="{ 'focused': isEmailFocused }">
        <input
          id="email"
          v-model="form.email"
          type="email"
          class="form-input"
          :class="{ 'error': emailError }"
          :placeholder="t('login.email_placeholder')"
          @focus="isEmailFocused = true"
          @blur="isEmailFocused = false"
          :disabled="loading"
          autocomplete="email"
        />
        <div v-if="emailError" class="input-error-icon">
          <el-icon><WarningFilled /></el-icon>
        </div>
      </div>
      <div v-if="emailError" class="error-message">{{ emailError }}</div>
    </div>
    
    <!-- 密码输入 -->
    <div class="form-group">
      <label class="form-label" for="password">{{ t('login.password_label') }}</label>
      <div class="input-wrapper" :class="{ 'focused': isPasswordFocused }">
        <input
          id="password"
          v-model="form.password"
          :type="showPassword ? 'text' : 'password'"
          class="form-input"
          :class="{ 'error': passwordError }"
          :placeholder="t('login.password_placeholder')"
          @focus="isPasswordFocused = true"
          @blur="isPasswordFocused = false"
          :disabled="loading"
          autocomplete="current-password"
        />
        <button
          type="button"
          class="password-toggle"
          @click="showPassword = !showPassword"
          :disabled="loading"
        >
          <el-icon v-if="showPassword"><View /></el-icon>
          <el-icon v-else><Hide /></el-icon>
        </button>
      </div>
      <div v-if="passwordError" class="error-message">{{ passwordError }}</div>
    </div>
    
    <!-- 登录按钮 -->
    <button
      type="submit"
      class="login-submit-button"
      :disabled="loading || !isFormValid"
    >
      <span class="button-text">
        <span v-if="!loading">{{ t('login.email_login') }}</span>
        <span v-else>{{ t('login.email_logging') }}</span>
      </span>
      <div v-if="loading" class="loading-spinner"></div>
    </button>
    
    <!-- 功能预留提示 -->
    <div class="feature-notice" v-if="false">
      <el-icon class="info-icon"><InfoFilled /></el-icon>
      <span>{{ t('login.email_login_notice') }}</span>
    </div>
  </form>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useI18n } from 'vue-i18n'
import { WarningFilled, View, Hide, InfoFilled } from '@element-plus/icons-vue'
import { useVuelidate } from '@vuelidate/core'
import { required, email, minLength } from '@vuelidate/validators'
const { t } = useI18n()
const emit = defineEmits(['login', 'error'])
const props = defineProps({
  loading: {
    type: Boolean,
    default: false
  }
})
// 表单数据
const form = ref({
  email: '',
  password: ''
})

// 输入状态
const isEmailFocused = ref(false)
const isPasswordFocused = ref(false)
const showPassword = ref(false)

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

// 表单验证规则
const rules = {
  email: { required, email },
  password: { required, minLength: minLength(6) }
}

const v$ = useVuelidate(rules, form)

// 计算属性：表单是否有效
const isFormValid = computed(() => {
  return form.value.email && form.value.password && !emailError.value && !passwordError.value
})

// 实时验证
const validateEmail = () => {
  if (!form.value.email) {
    emailError.value = t('login.email_empty_error')
  } else if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(form.value.email)) {
    emailError.value = t('login.email_invalid_error')
  } else {
    emailError.value = ''
  }
}

const validatePassword = () => {
  if (!form.value.password) {
    passwordError.value = t('login.password_empty_error')
  } else if (form.value.password.length < 6) {
    passwordError.value = t('login.password_min_error')
  } else {
    passwordError.value = ''
  }
}

// 监听表单变化，实时验证
watch(() => form.value.email, validateEmail)
watch(() => form.value.password, validatePassword)

// 处理登录
const handleLogin = async () => {
  // 验证表单
  await v$.value.$validate()
  if (v$.value.$invalid) {
    validateEmail()
    validatePassword()
    return
  }
  emit('login', form.value)
}
</script>

<style scoped>
/* 登录表单容器 */
.login-form {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

/* 表单组 */
.form-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

/* 表单标签 */
.form-label {
  font-size: 14px;
  font-weight: 600;
  color: #374151;
  margin-left: 4px;
}

/* 输入框容器 */
.input-wrapper {
  position: relative;
  display: flex;
  align-items: center;
  transition: all 0.2s ease;
}

.input-wrapper.focused {
  transform: translateY(-1px);
}

/* 输入框样式 */
.form-input {
  width: 100%;
  height: 48px;
  padding: 0 16px;
  border: 2px solid #E5E7EB;
  border-radius: 12px;
  background: white;
  font-size: 16px;
  color: #1F2937;
  transition: all 0.2s ease;
  outline: none;
}

.form-input:focus {
  border-color: #7C3AED;
  box-shadow: 0 0 0 3px rgba(167, 139, 250, 0.15);
  background: #FAFBFF;
}

.form-input.error {
  border-color: #EF4444;
  background: #FEF2F2;
}

.form-input:disabled {
  background: #F9FAFB;
  color: #9CA3AF;
  cursor: not-allowed;
}

.form-input::placeholder {
  color: #9CA3AF;
  font-weight: 400;
}

/* 密码可见性切换按钮 */
.password-toggle {
  position: absolute;
  right: 12px;
  background: none;
  border: none;
  color: #6B7280;
  cursor: pointer;
  padding: 4px;
  border-radius: 6px;
  transition: all 0.2s ease;
}

.password-toggle:hover:not(:disabled) {
  color: #6B46C1;
  background: rgba(107, 70, 193, 0.1);
}

.password-toggle:disabled {
  cursor: not-allowed;
  opacity: 0.5;
}

/* 错误图标 */
.input-error-icon {
  position: absolute;
  right: 12px;
  color: #EF4444;
  font-size: 16px;
}



/* 登录提交按钮 */
.login-submit-button {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  height: 48px;
  background: linear-gradient(135deg, #7C3AED, #6B46C1);
  border: none;
  border-radius: 12px;
  color: white;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s ease;
  position: relative;
  overflow: hidden;
  box-shadow: 
    0 4px 6px -1px rgba(167, 139, 250, 0.4),
    0 2px 4px -1px rgba(167, 139, 250, 0.2);
}

.login-submit-button:hover:not(:disabled) {
  background: linear-gradient(135deg, #8B5CF6, #7C3AED);
  transform: translateY(-1px);
  box-shadow: 
    0 8px 15px -3px rgba(167, 139, 250, 0.5),
    0 4px 6px -2px rgba(167, 139, 250, 0.3);
}

.login-submit-button:active:not(:disabled) {
  transform: translateY(0);
  box-shadow: 
    0 4px 6px -1px rgba(107, 70, 193, 0.3),
    0 2px 4px -1px rgba(107, 70, 193, 0.2);
}

.login-submit-button:disabled {
  background: linear-gradient(135deg, #D1D5DB, #9CA3AF);
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

/* 按钮文字 */
.button-text {
  font-weight: 600;
  letter-spacing: 0.025em;
}

/* 加载状态指示器 */
.loading-spinner {
  width: 18px;
  height: 18px;
  border: 2px solid transparent;
  border-top: 2px solid white;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* 功能提示 */
.feature-notice {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 12px 16px;
  background: rgba(139, 92, 246, 0.1);
  border: 1px solid rgba(139, 92, 246, 0.2);
  border-radius: 8px;
  color: #6B46C1;
  font-size: 13px;
  font-weight: 500;
  text-align: center;
}

.info-icon {
  font-size: 14px;
  flex-shrink: 0;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .form-input {
    height: 44px;
    font-size: 15px;
  }
  
  .login-submit-button {
    height: 44px;
    font-size: 15px;
  }
  
  .password-toggle {
    right: 10px;
  }
}

@media (max-width: 480px) {
  .form-input {
    height: 42px;
    font-size: 14px;
    padding: 0 14px;
  }
  
  .login-submit-button {
    height: 42px;
    font-size: 14px;
  }
}
</style>