<template>
  <div class="register-wrapper">
    <div class="register-side">
      <div class="brand">
        <img src="/logo.png" alt="Logo" class="logo-img" />
        <span class="brand-text">Hertz Admin</span>
      </div>
      <div class="illustration-container">
        <img src="@/assets/login_svg/bg_4.svg" alt="Illustration" class="illustration" />
      </div>
      <div class="slogan">
        <h2>加入Hertz Admin</h2>
        <p>开始您的旅程，今天就注册</p>
      </div>
    </div>

    <div class="form-side">
      <div class="form-content">
        <div class="form-header">
          <h2>创建账号</h2>
          <!-- <p class="sub-title">请输入您的详细信息进行注册</p> -->
        </div>

        <el-form label-position="top" size="large" @submit.prevent="submit" class="register-form">
          <el-form-item label="用户名">
            <el-input v-model="form.username" autocomplete="username" placeholder="请选择一个用户名" :prefix-icon="User" />
          </el-form-item>
          <el-form-item label="密码">
            <el-input v-model="form.password" type="password" autocomplete="new-password" show-password placeholder="请创建一个密码" :prefix-icon="Lock" /> 
          </el-form-item>
          <el-form-item label="确认密码">
            <el-input v-model="form.confirmPassword" type="password" autocomplete="new-password" show-password placeholder="请确认您的密码" :prefix-icon="Lock" />
          </el-form-item>
          <el-form-item label="昵称">
            <el-input v-model="form.nickname" placeholder="请输入您的昵称" :prefix-icon="Postcard" /> 
          </el-form-item>
          <el-form-item label="验证码">
            <div class="captcha-row">
                <el-input v-model="form.code" placeholder="请输入验证码" :prefix-icon="Key" class="captcha-input" @keyup.enter="submit" />
                <img v-if="captchaImg" :src="captchaImg" @click="fetchCaptcha" class="captcha-img" alt="captcha" />
              </div>
          </el-form-item>
          
          <div class="form-actions">
            <el-button type="primary" :loading="loading" @click="submit" class="submit-btn">注册</el-button>
          </div>
          
          <div class="form-footer">
            <span class="text-gray">已经有账号？</span>
            <el-button link type="primary" @click="router.push('/login')">登录</el-button>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { useAuthStore } from '../stores/auth'
import { getCaptcha } from '../api/auth'

import { User, Lock, Postcard, Key } from '@element-plus/icons-vue'

const auth = useAuthStore()
const router = useRouter()

const loading = ref(false)
const captchaImg = ref('')
const form = reactive({
  username: '',
  password: '',
  confirmPassword: '',
  nickname: '',
  uuid: '',
  code: '',
})

async function fetchCaptcha() {
  try {
    const res = await getCaptcha()
    if (res && res.img) {
      form.uuid = res.uuid
      const base64 = res.img
      captchaImg.value = base64.startsWith('data:') ? base64 : 'data:image/png;base64,' + base64
    }
  } catch (e) {
    console.error(e)
  }
}

onMounted(() => {
  fetchCaptcha()
})

async function submit() {
  if (form.password !== form.confirmPassword) {
    ElMessage.error('两次输入的密码不一致')
    return
  }
  loading.value = true
  try {
    const { confirmPassword, ...payload } = form
    await auth.register(payload)
    ElMessage.success('注册成功，请登录')
    await router.replace('/login')
  } catch (e) {
    ElMessage.error(e?.response?.data?.message || '注册失败')
    await fetchCaptcha()
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.register-wrapper {
  display: flex;
  min-height: 100vh;
  width: 100%;
  overflow: hidden;
}

.register-side {
  flex: 1;
  background: linear-gradient(135deg, #1e3c72 0%, #2a5298 100%);
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  padding: 40px;
  color: white;
  position: relative;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.logo-img {
  width: 66px;
  height: 36px;
  border-radius: 8px;
  padding: 4px;
}

.brand-text {
  font-size: 24px;
  font-weight: 700;
  letter-spacing: 0.5px;
}

.illustration-container {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
}

.illustration {
  max-width: 60%;
  min-height: 400px;
  object-fit: contain;
}

.slogan {
  text-align: center;
  margin-bottom: 20px;
}

.slogan h2 {
  font-size: 28px;
  font-weight: 600;
  margin-bottom: 8px;
}

.slogan p {
  font-size: 16px;
  opacity: 0.9;
}

.form-side {
  width: 600px;
  background: #fff;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 40px;
}

.form-content {
  width: 100%;
  max-width: 400px;
  margin-bottom: 40px;
}

.form-header {
  margin-bottom: 32px;
}

.form-header h2 {
  font-size: 32px;
  font-weight: 700;
  color: #1a1a1a;
  margin-bottom: 8px;
}

.sub-title {
  color: #909399;
  font-size: 16px;
}

.register-form :deep(.el-form-item__label) {
  font-weight: 500;
  color: #303133;
}

.captcha-row {
  display: flex;
  gap: 12px;
  width: 100%;
}

.captcha-input {
  flex: 1;
}

.captcha-img {
  height: 40px;
  border-radius: 4px;
  border: 1px solid #dcdfe6;
  cursor: pointer;
}

.form-actions {
  margin-top: 24px;
  margin-bottom: 16px;
}

.submit-btn {
  width: 100%;
  height: 44px;
  font-size: 16px;
  font-weight: 500;
}

.form-footer {
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 14px;
  gap: 4px;
}

.text-gray {
  color: #909399;
}

@media (max-width: 900px) {
  .register-side {
    display: none;
  }
  .form-side {
    width: 100%;
  }
}
</style>

