<template>
  <div class="login-page">
    <div class="login-container">
      <div class="login-form">
        <div class="form-header">
          <h1>登录</h1>
          <p>欢迎回到在线学习平台</p>
        </div>

        <n-form
          ref="formRef"
          :model="formData"
          :rules="rules"
          size="large"
          @submit.prevent="handleSubmit"
        >
          <n-form-item path="email" label="邮箱">
            <n-input
              v-model:value="formData.email"
              placeholder="请输入邮箱地址"
              type="email"
            >
              <template #prefix>
                <n-icon>
                  <MailOutline />
                </n-icon>
              </template>
            </n-input>
          </n-form-item>

          <n-form-item path="password" label="密码">
            <n-input
              v-model:value="formData.password"
              placeholder="请输入密码"
              type="password"
              show-password-on="mousedown"
            >
              <template #prefix>
                <n-icon>
                  <LockClosedOutline />
                </n-icon>
              </template>
            </n-input>
          </n-form-item>

          <n-form-item>
            <div class="form-options">
              <n-checkbox v-model:checked="rememberMe">
                记住我
              </n-checkbox>
              <n-button text type="primary">
                忘记密码？
              </n-button>
            </div>
          </n-form-item>

          <n-form-item>
            <n-button
              type="primary"
              size="large"
              block
              :loading="userStore.isLoading"
              attr-type="submit"
            >
              登录
            </n-button>
          </n-form-item>
        </n-form>

        <div class="form-footer">
          <p>
            还没有账号？
            <n-button text type="primary" @click="$router.push('/register')">
              立即注册
            </n-button>
          </p>
        </div>

        <!-- 社交登录 -->
        <div class="social-login">
          <n-divider>或使用以下方式登录</n-divider>
          <n-space justify="center">
            <n-button circle size="large">
              <n-icon size="20">
                <LogoGithub />
              </n-icon>
            </n-button>
            <n-button circle size="large">
              <n-icon size="20">
                <LogoGoogle />
              </n-icon>
            </n-button>
            <n-button circle size="large">
              <n-icon size="20">
                <LogoWechat />
              </n-icon>
            </n-button>
          </n-space>
        </div>
      </div>

      <!-- 侧边图片 -->
      <div class="login-image">
        <img src="https://via.placeholder.com/600x800" alt="登录" />
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { useMessage, type FormInst, type FormRules } from 'naive-ui'
import { useUserStore } from '@/stores/user'
import {
  MailOutline,
  LockClosedOutline,
  LogoGithub,
  LogoGoogle,
  LogoWechat
} from '@vicons/ionicons5'

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

const formRef = ref<FormInst | null>(null)
const rememberMe = ref(false)

// 表单数据
const formData = reactive({
  email: '',
  password: ''
})

// 表单验证规则
const rules: FormRules = {
  email: [
    {
      required: true,
      message: '请输入邮箱地址',
      trigger: ['input', 'blur']
    },
    {
      type: 'email',
      message: '请输入有效的邮箱地址',
      trigger: ['input', 'blur']
    }
  ],
  password: [
    {
      required: true,
      message: '请输入密码',
      trigger: ['input', 'blur']
    },
    {
      min: 6,
      message: '密码长度不能少于6位',
      trigger: ['input', 'blur']
    }
  ]
}

// 处理表单提交
const handleSubmit = async () => {
  if (!formRef.value) return

  try {
    await formRef.value.validate()
    
    const result = await userStore.login({
      email: formData.email,
      password: formData.password
    })

    if (result.success) {
      message.success(result.message)
      // 登录成功后跳转到首页或之前的页面
      const redirect = router.currentRoute.value.query.redirect as string
      router.push(redirect || '/')
    } else {
      message.error(result.message)
    }
  } catch (error) {
    console.error('表单验证失败:', error)
  }
}
</script>

<style scoped>
.login-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
}

.login-container {
  display: grid;
  grid-template-columns: 1fr 1fr;
  max-width: 1000px;
  width: 100%;
  background: white;
  border-radius: 16px;
  overflow: hidden;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
}

.login-form {
  padding: 60px 40px;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.form-header {
  text-align: center;
  margin-bottom: 40px;
}

.form-header h1 {
  font-size: 2rem;
  color: #333;
  margin-bottom: 8px;
}

.form-header p {
  color: #666;
  font-size: 1rem;
}

.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
}

.form-footer {
  text-align: center;
  margin-top: 24px;
}

.social-login {
  margin-top: 32px;
}

.login-image {
  background: #f8f9fa;
  display: flex;
  align-items: center;
  justify-content: center;
}

.login-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

@media (max-width: 768px) {
  .login-container {
    grid-template-columns: 1fr;
    max-width: 400px;
  }
  
  .login-image {
    display: none;
  }
  
  .login-form {
    padding: 40px 24px;
  }
}
</style>
