<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Daftar Akun — E-Book SMA</title>
<meta name="description" content="Daftar akun baru untuk mengakses koleksi E-Book SMA.">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="<?= base_url('assets/css/style.css') ?>" rel="stylesheet">
</head>
<body class="auth-body">

<div class="auth-bg">
  <div class="auth-wrapper">
    <div class="auth-left d-none d-lg-flex">
      <div class="auth-left-content">
        <div class="auth-logo-big"><i class="fas fa-book-open"></i></div>
        <h1 class="auth-brand-title">E-Book SMA</h1>
        <p class="auth-brand-sub">Bergabunglah dengan ribuan pelajar yang belajar lebih cerdas.</p>
        <div class="auth-features">
          <div class="auth-feature-item"><i class="fas fa-check-circle"></i> Gratis selamanya</div>
          <div class="auth-feature-item"><i class="fas fa-check-circle"></i> Materi lengkap semua mapel</div>
          <div class="auth-feature-item"><i class="fas fa-check-circle"></i> Update konten rutin</div>
          <div class="auth-feature-item"><i class="fas fa-check-circle"></i> Komunitas belajar aktif</div>
        </div>
      </div>
    </div>
    <div class="auth-right">
      <div class="auth-card">
        <div class="auth-card-header">
          <div class="auth-mobile-logo d-lg-none"><i class="fas fa-book-open"></i></div>
          <h2 class="auth-title">Buat Akun Baru 🎓</h2>
          <p class="auth-subtitle">Daftar gratis dan mulai belajar sekarang</p>
        </div>

        <div id="alertBox" class="alert d-none" role="alert"></div>

        <form id="registerForm" novalidate>
          <div class="form-floating-custom">
            <label for="nama"><i class="fas fa-user"></i> Nama Lengkap</label>
            <input type="text" id="nama" name="nama" class="form-input" placeholder="Nama lengkap Anda" required>
          </div>
          <div class="form-floating-custom mt-3">
            <label for="email"><i class="fas fa-envelope"></i> Email</label>
            <input type="email" id="email" name="email" class="form-input" placeholder="nama@email.com" required>
            <small id="emailFeedback" class="form-text"></small>
          </div>
          <div class="form-floating-custom mt-3">
            <label for="password"><i class="fas fa-lock"></i> Password</label>
            <div class="input-password-wrap">
              <input type="password" id="password" name="password" class="form-input" placeholder="Min. 6 karakter" required>
              <button type="button" class="btn-eye" id="togglePassword"><i class="fas fa-eye"></i></button>
            </div>
            <div class="password-strength mt-2" id="strengthBar"><div class="strength-fill" id="strengthFill"></div></div>
            <small id="strengthText" class="form-text"></small>
          </div>
          <div class="form-floating-custom mt-3">
            <label for="confirm_password"><i class="fas fa-lock"></i> Konfirmasi Password</label>
            <input type="password" id="confirm_password" name="confirm_password" class="form-input" placeholder="Ulangi password" required>
          </div>

          <button type="submit" class="btn-primary-custom mt-4" id="registerBtn">
            <span class="btn-text"><i class="fas fa-user-plus me-2"></i>Daftar Sekarang</span>
            <span class="btn-loader d-none"><span class="spinner-border spinner-border-sm"></span> Mendaftar...</span>
          </button>
        </form>

        <div class="auth-divider"><span>atau</span></div>
        <p class="text-center auth-switch">
          Sudah punya akun? <a href="<?= base_url('login') ?>" class="auth-link">Masuk di sini</a>
        </p>
      </div>
    </div>
  </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
<script>
const BASE_URL = '<?= base_url() ?>';

document.getElementById('togglePassword').addEventListener('click', function() {
  const inp = document.getElementById('password');
  const icon = this.querySelector('i');
  inp.type = inp.type === 'password' ? 'text' : 'password';
  icon.className = inp.type === 'password' ? 'fas fa-eye' : 'fas fa-eye-slash';
});

document.getElementById('password').addEventListener('input', function() {
  const val = this.value;
  const fill = document.getElementById('strengthFill');
  const text = document.getElementById('strengthText');
  let strength = 0;
  if (val.length >= 6) strength++;
  if (/[A-Z]/.test(val)) strength++;
  if (/[0-9]/.test(val)) strength++;
  if (/[^A-Za-z0-9]/.test(val)) strength++;
  const levels = ['', 'Lemah', 'Cukup', 'Kuat', 'Sangat Kuat'];
  const colors = ['', '#ef4444', '#f59e0b', '#3b82f6', '#10b981'];
  fill.style.width = (strength * 25) + '%';
  fill.style.background = colors[strength];
  text.textContent = strength > 0 ? 'Kekuatan: ' + levels[strength] : '';
  text.style.color = colors[strength];
});

document.getElementById('registerForm').addEventListener('submit', async function(e) {
  e.preventDefault();
  const btn = document.getElementById('registerBtn');
  const nama = document.getElementById('nama').value.trim();
  const email = document.getElementById('email').value.trim();
  const password = document.getElementById('password').value;
  const confirm = document.getElementById('confirm_password').value;

  if (!nama || !email || !password || !confirm) {
    showAlert('danger', 'Semua field wajib diisi.'); return;
  }
  if (password !== confirm) {
    showAlert('danger', 'Password dan konfirmasi password tidak cocok.'); return;
  }
  if (password.length < 6) {
    showAlert('danger', 'Password minimal 6 karakter.'); return;
  }

  setLoading(btn, true);

  try {
    const fd = new FormData();
    fd.append('nama', nama);
    fd.append('email', email);
    fd.append('password', password);

    const res = await fetch(BASE_URL + 'api/register', { method: 'POST', body: fd });
    const data = await res.json();

    if (data.status) {
      showAlert('success', '✅ Registrasi berhasil! Mengalihkan ke halaman login...');
      setTimeout(() => window.location.href = BASE_URL + 'login', 1500);
    } else {
      showAlert('danger', '❌ ' + data.message);
      setLoading(btn, false);
    }
  } catch(err) {
    showAlert('danger', '❌ Koneksi gagal. Periksa server Anda.');
    setLoading(btn, false);
  }
});

function showAlert(type, msg) {
  const a = document.getElementById('alertBox');
  a.className = `alert alert-${type} fade-in`;
  a.innerHTML = msg;
}
function setLoading(btn, state) {
  btn.querySelector('.btn-text').classList.toggle('d-none', state);
  btn.querySelector('.btn-loader').classList.toggle('d-none', !state);
  btn.disabled = state;
}
</script>
</body>
</html>
