<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login — E-Book SMA</title>
<meta name="description" content="Login ke platform E-Book SMA untuk mengakses koleksi buku digital.">
<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-particles"></div>
  <div class="auth-wrapper">
    <!-- Left Panel -->
    <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">Platform digital pembelajaran modern untuk siswa dan guru.</p>
        <div class="auth-features">
          <div class="auth-feature-item"><i class="fas fa-check-circle"></i> Akses ratusan ebook gratis</div>
          <div class="auth-feature-item"><i class="fas fa-check-circle"></i> Baca langsung di browser</div>
          <div class="auth-feature-item"><i class="fas fa-check-circle"></i> Download PDF kapan saja</div>
          <div class="auth-feature-item"><i class="fas fa-check-circle"></i> Multi-device & mobile friendly</div>
        </div>
      </div>
    </div>
    <!-- Right Panel: Form -->
    <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">Selamat Datang 👋</h2>
          <p class="auth-subtitle">Masuk ke akun Anda untuk melanjutkan</p>
        </div>

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

        <form id="loginForm" novalidate>
          <div class="form-floating-custom">
            <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 autocomplete="email">
          </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="Masukkan password" required autocomplete="current-password">
              <button type="button" class="btn-eye" id="togglePassword"><i class="fas fa-eye"></i></button>
            </div>
          </div>

          <button type="submit" class="btn-primary-custom mt-4" id="loginBtn">
            <span class="btn-text"><i class="fas fa-sign-in-alt me-2"></i>Masuk</span>
            <span class="btn-loader d-none"><span class="spinner-border spinner-border-sm"></span> Memproses...</span>
          </button>
        </form>

        <div class="auth-divider"><span>atau</span></div>
        <p class="text-center auth-switch">
          Belum punya akun? <a href="<?= base_url('register') ?>" class="auth-link">Daftar Sekarang</a>
        </p>

        <div class="auth-demo-creds">
          <p><i class="fas fa-info-circle"></i> Demo Akun:</p>
          <span><b>Admin:</b> admin@ebook.com / password</span><br>
          <span><b>User:</b> user@ebook.com / password</span>
        </div>
      </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');
  if (inp.type === 'password') { inp.type = 'text'; icon.className = 'fas fa-eye-slash'; }
  else { inp.type = 'password'; icon.className = 'fas fa-eye'; }
});

document.getElementById('loginForm').addEventListener('submit', async function(e) {
  e.preventDefault();
  const btn = document.getElementById('loginBtn');
  const alertBox = document.getElementById('alertBox');
  const email = document.getElementById('email').value.trim();
  const password = document.getElementById('password').value;

  if (!email || !password) {
    showAlert('danger', 'Email dan password wajib diisi.');
    return;
  }

  setLoading(btn, true);
  alertBox.className = 'alert d-none';

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

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

    if (data.status) {
      showAlert('success', '✅ ' + data.message + ' — Mengalihkan...');
      setTimeout(() => {
        window.location.href = BASE_URL + (data.data.role === 'admin' ? 'admin/dashboard' : 'user/dashboard');
      }, 800);
    } 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>
