<template>
  <div class="register-page">
    <div class="register-container">
      <div class="register-content">
        <div class="register-header">
          <h2 style="margin-top: 0;">用户注册</h2>
          <p class="sub-title">请填写以下信息完成注册</p>
        </div>
        
        <a-form 
          ref="registerForm"
          :model="formData"
          :rules="rules"
          layout="vertical"
        >
          <a-form-item label="邀请码" name="invitation_code">
            <a-input
              v-model:value="formData.invitation_code"
              placeholder="请输入邀请码"
              size="large"
            >
              <template #prefix>
                <GiftOutlined style="color: rgba(0,0,0,.25)" />
              </template>
            </a-input>
          </a-form-item>

          <a-form-item label="用户名" name="username">
            <a-input
              v-model:value="formData.username"
              placeholder="请输入用户名"
              size="large"
            >
              <template #prefix>
                <UserOutlined style="color: rgba(0,0,0,.25)" />
              </template>
            </a-input>
          </a-form-item>

          <a-form-item label="密码" name="password">
            <a-input-password
              v-model:value="formData.password"
              placeholder="请输入密码"
              size="large"
            >
              <template #prefix>
                <LockOutlined style="color: rgba(0,0,0,.25)" />
              </template>
            </a-input-password>
          </a-form-item>

          <a-form-item label="确认密码" name="confirm_password">
            <a-input-password
              v-model:value="formData.confirm_password"
              placeholder="请再次输入密码"
              size="large"
            >
              <template #prefix>
                <LockOutlined style="color: rgba(0,0,0,.25)" />
              </template>
            </a-input-password>
          </a-form-item>

          <a-form-item label="真实姓名" name="real_name">
            <a-input
              v-model:value="formData.real_name"
              placeholder="请输入真实姓名"
              size="large"
            >
              <template #prefix>
                <IdcardOutlined style="color: rgba(0,0,0,.25)" />
              </template>
            </a-input>
          </a-form-item>

          <a-form-item label="手机号" name="mobile">
            <a-input
              v-model:value="formData.mobile"
              placeholder="请输入手机号"
              size="large"
            >
              <template #prefix>
                <MobileOutlined style="color: rgba(0,0,0,.25)" />
              </template>
            </a-input>
          </a-form-item>

          <a-form-item label="邮箱" name="email">
            <a-input
              v-model:value="formData.email"
              placeholder="请输入邮箱"
              size="large"
            >
              <template #prefix>
                <MailOutlined style="color: rgba(0,0,0,.25)" />
              </template>
            </a-input>
          </a-form-item>

          <a-form-item>
            <a-button 
              type="primary" 
              :loading="loading"
              size="large"
              block
              @click="handleRegister"
            >
              注册
            </a-button>
          </a-form-item>

          <div class="form-footer">
            <p class="login-tip">
              已有账号？<router-link to="/login" class="login-link">立即登录</router-link>
            </p>
          </div>
        </a-form>
      </div>
    </div>
  </div>
</template>

<script>
import { defineComponent, reactive, ref, onMounted } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import axios from 'axios';
import { message } from 'ant-design-vue';
import { 
  UserOutlined,
  LockOutlined,
  MobileOutlined,
  MailOutlined,
  IdcardOutlined,
  GiftOutlined
} from '@ant-design/icons-vue';

export default defineComponent({
  name: 'register',
  components: {
    UserOutlined,
    LockOutlined,
    MobileOutlined,
    MailOutlined,
    IdcardOutlined,
    GiftOutlined
  },
  setup() {
    const router = useRouter();
    const route = useRoute();
    const registerForm = ref();
    const loading = ref(false);
    
    const formData = reactive({
      invitation_code: '',
      username: '',
      password: '',
      confirm_password: '',
      real_name: '',
      mobile: '',
      email: ''
    });
    
    const rules = {
      invitation_code: [
        { required: true, message: '请输入邀请码' },
        { len: 20, message: '邀请码长度必须为20位' }
      ],
      username: [
        { required: true, message: '请输入用户名' },
        { min: 4, message: '用户名长度不能小于4位' }
      ],
      password: [
        { required: true, message: '请输入密码' },
        { min: 6, message: '密码长度不能小于6位' }
      ],
      confirm_password: [
        { required: true, message: '请确认密码' },
        { 
          validator: (rule, value) => {
            if (value && value !== formData.password) {
              return Promise.reject('两次输入的密码不一致');
            }
            return Promise.resolve();
          }
        }
      ],
      real_name: [
        { required: true, message: '请输入真实姓名' }
      ],
      mobile: [
        { required: true, message: '请输入手机号' },
        { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号' }
      ],
      email: [
        { required: true, message: '请输入邮箱' },
        { type: 'email', message: '请输入正确的邮箱格式' }
      ]
    };

    // 从 URL 获取邀请码
    onMounted(() => {
      const code = route.query.code;
      if (code) {
        formData.invitation_code = code;
      }
    });

    // 处理注册
    const handleRegister = async () => {
      try {
        loading.value = true;
        await registerForm.value.validate();
        
        const response = await axios.post(`${import.meta.env.VITE_BASE_AI_API}/user/register_by_invitation?code=${formData.invitation_code}`, {
         /*  invitation_code: formData.invitation_code, */
          username: formData.username,
          password: formData.password,
          confirm_password: formData.confirm_password,
          real_name: formData.real_name,
          mobile: formData.mobile,
          email: formData.email
        });

        if (response.data.code === 200) {
          message.success('注册成功');
          router.push('/login');
        } else {
          message.error(response.data.message || '注册失败');
        }
      } catch (error) {
        console.error('注册失败:', error);
        message.error(error.response?.data?.message || '注册失败，请检查输入信息');
      } finally {
        loading.value = false;
      }
    };

    return {
      registerForm,
      formData,
      rules,
      loading,
      handleRegister
    };
  }
});
</script>

<style lang="less" scoped>
.register-page {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  padding: 24px 16px;
  background: radial-gradient(1200px 600px at 10% 10%, rgba(255, 255, 255, 0.12), transparent 60%),
    radial-gradient(1200px 600px at 90% 90%, rgba(255, 255, 255, 0.08), transparent 60%),
    linear-gradient(135deg, #6a8cff 0%, #8a6cf0 45%, #7a5fd6 100%);
  
  .register-container {
    width: 100%;
    max-width: 420px;
    margin: 0 auto;
  }
  
  .register-content {
    position: relative;
    background: rgba(255, 255, 255, 0.96);
    padding: 40px;
    border-radius: 18px;
    box-shadow: 0 12px 40px rgba(31, 38, 135, 0.2);
    backdrop-filter: blur(10px);
    border: 1px solid rgba(255, 255, 255, 0.6);
    animation: slideUp 0.55s ease-out;
    transition: box-shadow 0.25s ease, transform 0.25s ease;

    &:hover {
      box-shadow: 0 16px 48px rgba(31, 38, 135, 0.24);
      transform: translateY(-1px);
    }
  }
  
  .register-header {
    text-align: center;
    margin-bottom: 32px;
    
    h2 {
      font-size: 28px;
      color: #1a1a1a;
      margin-bottom: 10px;
      font-weight: 700;
      letter-spacing: 0.3px;
    }
    
    .sub-title {
      color: #70757d;
      font-size: 14px;
      line-height: 1.6;
    }
  }

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

    .login-tip {
      color: #666;
      font-size: 14px;
      
      .login-link {
        color: #5e7bff;
        font-weight: 500;
        
        &:hover {
          color: #7a5fd6;
          text-decoration: underline;
        }
      }
    }
  }
}

@keyframes slideUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

// 响应式适配
@media (max-width: 480px) {
  .register-content {
    padding: 24px;
    border-radius: 14px;
  }
  
  .register-header h2 {
    font-size: 24px;
  }
}

// Ant Design 表单细节优化（scoped 下使用 :deep）
:deep(.ant-form) {
  .ant-form-item {
    margin-bottom: 18px;
  }

  .ant-form-item-label > label {
    color: #30343b;
    font-weight: 600;
    letter-spacing: 0.2px;
  }

  .ant-form-item-control-input-content {
    line-height: 1;
  }

  .ant-form-item-explain-error {
    margin-top: 6px;
    font-size: 12px;
  }
}

// 输入框视觉与可用性
:deep(.ant-input-affix-wrapper),
:deep(.ant-input-password .ant-input) {
  background: #fafbff;
}

:deep(.ant-input),
:deep(.ant-input-password .ant-input) {
  padding: 8px 12px;
}

:deep(.ant-input-affix-wrapper) {
  border-radius: 10px;
  border-color: #e6e8f0;
  transition: box-shadow 0.2s ease, border-color 0.2s ease, background-color 0.2s ease;

  &:hover {
    border-color: #b8c1ff;
    background: #f7f8ff;
  }

  &.ant-input-affix-wrapper-focused,
  &:focus-within {
    border-color: #6a8cff;
    box-shadow: 0 0 0 3px rgba(106, 140, 255, 0.18);
    background: #fff;
  }
}

// 占位符颜色微调
:deep(.ant-input::placeholder) {
  color: #9aa0a6;
}

// 密码框前缀图标颜色统一
:deep(.ant-input-prefix) {
  color: rgba(0, 0, 0, 0.35);
}

// 主按钮强化
:deep(.ant-btn.ant-btn-primary) {
  height: 44px;
  border-radius: 10px;
  font-weight: 600;
  letter-spacing: 0.2px;
  box-shadow: 0 8px 18px rgba(106, 140, 255, 0.35);
  background-image: linear-gradient(135deg, #6a8cff, #7a5fd6);
  border: none;
  transition: transform 0.15s ease, box-shadow 0.2s ease, filter 0.2s ease;

  &:hover {
    filter: brightness(1.02);
    transform: translateY(-1px);
    box-shadow: 0 10px 22px rgba(106, 140, 255, 0.42);
  }

  &:active {
    transform: translateY(0);
    box-shadow: 0 6px 12px rgba(106, 140, 255, 0.3);
  }
}

// 轻微分隔感
.register-content::after {
  content: '';
  position: absolute;
  inset: 0;
  pointer-events: none;
  border-radius: inherit;
  box-shadow: inset 0 0 0 1px rgba(100, 115, 255, 0.06);
}
</style>
