// Sinkronisasi Tema
const savedTheme = localStorage.getItem('nm_theme') || 'light-mode';
document.body.className = savedTheme;

document.addEventListener('DOMContentLoaded', () => {
  // Jika sudah login, langsung alihkan ke admin.html
  if (sessionStorage.getItem('nm_admin_logged_in') === 'true') {
    window.location.href = 'admin.html';
    return;
  }

  const loginForm = document.getElementById('login-form');
  const errorMsg = document.getElementById('error-message');

  loginForm.addEventListener('submit', (e) => {
    e.preventDefault();
    
    const user = document.getElementById('username').value;
    const pass = document.getElementById('password').value;

    // Kredensial bawaan (Sesuai kesepakatan)
    if (user === 'admin' && pass === 'admin123') {
      // Sembunyikan error jika sebelumnya muncul
      errorMsg.style.display = 'none';
      
      // Simpan status login di session storage (hilang saat browser ditutup)
      sessionStorage.setItem('nm_admin_logged_in', 'true');
      
      // Alihkan ke halaman admin
      window.location.href = 'admin.html';
    } else {
      // Tampilkan pesan error
      errorMsg.style.display = 'block';
      
      // Efek getar (shake) pada form
      const wrapper = document.querySelector('.login-wrapper');
      wrapper.style.transform = 'translateX(10px)';
      setTimeout(() => wrapper.style.transform = 'translateX(-10px)', 50);
      setTimeout(() => wrapper.style.transform = 'translateX(10px)', 100);
      setTimeout(() => wrapper.style.transform = 'translateX(-10px)', 150);
      setTimeout(() => wrapper.style.transform = 'translateX(0)', 200);
    }
  });
});
