<template>
  <n-modal v-model:show="showModal" :mask-closable="false" :close-on-esc="false" class="login-modal">
    <div class="login-modal-container">
      <!-- 关闭按钮 -->
      <button class="close-btn" @click="closeModal">
        <img src="/images/icon/shut-down.png" alt="关闭" class="close-icon" />
      </button>

      <div class="login-content">
        <h2 class="form-title">账号登录</h2>

        <form @submit.prevent="handleLogin">
          <div class="form-group">
            <div class="input-wrapper">
              <img src="/images/auth/account.png" alt="账号" class="input-icon" />
              <input v-model="loginForm.account" type="text" placeholder="请输入账号" class="form-input" required />
            </div>
          </div>

          <div class="form-group">
            <div class="input-wrapper">
              <img src="/images/auth/login-password.png" alt="密码" class="input-icon" />
              <input v-model="loginForm.password" type="password" placeholder="请输入密码" class="form-input" required />
            </div>
          </div>

          <div class="form-options">
            <label class="checkbox-wrapper">
              <input v-model="loginForm.remember" type="checkbox" />
              <span class="checkbox-text">下次自动登录</span>
            </label>
            <a href="#" class="forgot-password">忘记密码</a>
          </div>

          <button type="submit" class="submit-btn" :disabled="isLoading">
            {{ isLoading ? '登录中...' : '登录' }}
          </button>
        </form>

        <div class="form-footer">
          <p>登录即代表同意我们的 <a href="#" class="link">《服务协议和隐私政策》</a></p>
        </div>
      </div>
    </div>
  </n-modal>
</template>

<script setup lang="ts">
import { ref, reactive, computed } from 'vue'
import { useMessage } from 'naive-ui'
import { useUserStore } from '@/stores/user'
import { AuthApi } from '@/api'

interface Props {
  show: boolean
}

interface Emits {
  (e: 'update:show', value: boolean): void
  (e: 'success'): void
}

const props = defineProps<Props>()
const emit = defineEmits<Emits>()

const message = useMessage()
const userStore = useUserStore()

const showModal = computed({
  get: () => props.show,
  set: (value) => emit('update:show', value)
})

const isLoading = ref(false)

// 登录表单数据
const loginForm = reactive({
  account: '',
  password: '',
  remember: false
})

// 关闭模态框
const closeModal = () => {
  showModal.value = false
}

// 处理登录
const handleLogin = async () => {
  if (!loginForm.account || !loginForm.password) {
    message.warning('请填写完整的登录信息')
    return
  }

  if (loginForm.password.length < 3) {
    message.warning('密码长度不能少于3位')
    return
  }

  isLoading.value = true
  try {
    console.log('🚀 开始登录:', { account: loginForm.account, password: '***' })
    console.log('🔍 表单密码长度:', loginForm.password?.length)
    console.log('🔍 表单密码内容:', loginForm.password)

    // 判断输入的是手机号还是邮箱
    const isPhone = /^[0-9]+$/.test(loginForm.account)

    const loginParams = {
      ...(isPhone ? { phone: loginForm.account } : { email: loginForm.account }),
      password: loginForm.password
    }

    console.log('🔍 准备发送的登录参数:', loginParams)

    // 调用登录API
    const response = await AuthApi.login(loginParams)

    console.log('✅ 登录响应:', response)

    if (response.code === 200 || response.code === 0) {
      const { user, token, refreshToken } = response.data

      // 保存用户信息和token到store
      userStore.user = user
      userStore.token = token

      // 保存到本地存储
      localStorage.setItem('X-Access-Token', token)
      localStorage.setItem('token', token)
      localStorage.setItem('refreshToken', refreshToken || '')
      localStorage.setItem('user', JSON.stringify(user))

      // 如果选择了记住我，设置更长的过期时间
      if (loginForm.remember) {
        localStorage.setItem('rememberMe', 'true')
      }

      message.success('登录成功！')
      emit('success')
      closeModal()

      // 清空表单
      loginForm.account = ''
      loginForm.password = ''
      loginForm.remember = false
    } else {
      console.error('❌ 登录失败 - 响应码错误:', response)
      message.error(response.message || '登录失败')
    }

  } catch (error: any) {
    console.error('❌ 登录异常:', error)

    // 处理不同类型的错误
    if (error.response?.status === 401) {
      message.error('邮箱或密码错误')
    } else if (error.response?.status === 429) {
      message.error('登录尝试过于频繁，请稍后再试')
    } else if (error.response?.data?.message) {
      // 显示后端返回的具体错误信息
      message.error(error.response.data.message)
    } else if (error.message) {
      // 显示错误对象中的消息
      message.error(error.message)
    } else {
      message.error('网络错误，请检查网络连接')
    }
  } finally {
    isLoading.value = false
  }
}
</script>

export default {
  name: 'LoginModal'
}

<style scoped>
.login-modal-container {
  position: relative;
  background: white;
  border-radius: 5px;
  width: 346px;
  height: 326px;
  overflow: hidden;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
}

.close-btn {
  position: absolute;
  top: 15px;
    right: 15px;
    background: none;
    border: none;
    color: #999;
    cursor: pointer;
    padding: 4px;
    border-radius: 4px;
    transition: all 0.2s;
    z-index: 10;
  }
  
  .close-btn:hover {
    color: #666;
    background: #f5f5f5;
  }
  
  .close-icon {
    width: 14px;
    height: 14px;
    object-fit: contain;
  }
  
  .login-content {
    padding: 32px;
  }
  
  .form-title {
    font-size: 16px;
    color: #333;
    margin: 0 0 17px 0;
    text-align: center;
  }
  
  .form-group {
    margin-bottom: 12px;
  }
  
  .input-wrapper {
    position: relative;
    display: flex;
    align-items: center;
  }
  
  .input-icon {
    position: absolute;
    left: 12px;
    z-index: 2;
    width: 12px;
    height: 12px;
    object-fit: contain;
  }
  
  .form-input {
    min-width: 278px;
    height: 41px;
    padding: 0 16px 0 30px;
    border: 1px solid #D8D8D8;
    border-radius: 6px;
    font-size: 12px;
    color: #D9D9D9;
    background: #fff;
    transition: all 0.2s;
  }
  
  .form-input:focus {
    outline: none;
    border-color: #1890ff;
    background: white;
    box-shadow: 0 0 0 2px rgba(24, 144, 255, 0.1);
  }
  
  .form-input::placeholder {
    color: #999;
  }
  
  .form-options {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 24px;
  }
  
  .checkbox-wrapper {
    display: flex;
    align-items: center;
    cursor: pointer;
    font-size: 10px;
    color: #666;
  }
  
  .checkbox-wrapper input[type="checkbox"] {
    margin-right: 8px;
    width: 12px;
    height: 12px;
  }
  
  .checkbox-text {
    color: #999999;
  }
  
  .forgot-password {
    color: #0288D1;
    text-decoration: none;
    font-size: 10px;
  }
  
  .forgot-password:hover {
    text-decoration: underline;
  }
  
  .submit-btn {
    width: 278px;
    height: 40px;
    background: #0288D1;
    color: white;
    border: none;
    border-radius: 6px;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
    margin-bottom: 5px;
  }
  
  .submit-btn:hover:not(:disabled) {
    background: #40a9ff;
  }
  
  .submit-btn:disabled {
    background: #d9d9d9;
    cursor: not-allowed;
  }
  
  .form-footer {
    text-align: center;
  }
  
  .form-footer p {
    text-align: left;
    font-size: 10px;
  color: #999;
  /* line-height: 1.4; */
  margin: 0;
}

.link {
  color: #1890ff;
  text-decoration: none;
}

.link:hover {
  text-decoration: underline;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .login-modal-container {
    width: 95vw;
    margin: 20px;
  }

  .login-content {
    padding: 30px 20px;
  }

  .form-title {
    font-size: 20px;
    margin-bottom: 20px;
  }
}
</style>
