<template>
  <div class="register-page">
    <div class="register-container">
      <div class="register-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="username" label="用户名">
            <n-input
              v-model:value="formData.username"
              placeholder="请输入用户名"
            >
              <template #prefix>
                <n-icon>
                  <PersonOutline />
                </n-icon>
              </template>
            </n-input>
          </n-form-item>

          <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 path="confirmPassword" label="确认密码">
            <n-input
              v-model:value="formData.confirmPassword"
              placeholder="请再次输入密码"
              type="password"
              show-password-on="mousedown"
            >
              <template #prefix>
                <n-icon>
                  <LockClosedOutline />
                </n-icon>
              </template>
            </n-input>
          </n-form-item>

          <n-form-item path="agreement">
            <n-checkbox v-model:checked="formData.agreement">
              我已阅读并同意
              <n-button text type="primary">用户协议</n-button>
              和
              <n-button text type="primary">隐私政策</n-button>
            </n-checkbox>
          </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('/login')">
              立即登录
            </n-button>
          </p>
        </div>
      </div>

      <!-- 侧边图片 -->
      <div class="register-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 {
  PersonOutline,
  MailOutline,
  LockClosedOutline
} from '@vicons/ionicons5'

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

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

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

// 表单验证规则
const rules: FormRules = {
  username: [
    {
      required: true,
      message: '请输入用户名',
      trigger: ['input', 'blur']
    },
    {
      min: 2,
      max: 20,
      message: '用户名长度应在2-20个字符之间',
      trigger: ['input', 'blur']
    }
  ],
  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']
    }
  ],
  confirmPassword: [
    {
      required: true,
      message: '请确认密码',
      trigger: ['input', 'blur']
    },
    {
      validator: (_rule, value) => {
        return value === formData.password
      },
      message: '两次输入的密码不一致',
      trigger: ['input', 'blur']
    }
  ],
  agreement: [
    {
      validator: (_rule, value) => {
        return value === true
      },
      message: '请同意用户协议和隐私政策',
      trigger: ['change']
    }
  ]
}

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

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

    if (result.success) {
      message.success(result.message)
      // 注册成功后跳转到首页
      router.push('/')
    } else {
      message.error(result.message)
    }
  } catch (error) {
    console.error('表单验证失败:', error)
  }
}
</script>

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

.register-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);
}

.register-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-footer {
  text-align: center;
  margin-top: 24px;
}

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

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

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