/**
 * তজিয়া গ্রুপ — Admin JavaScript
 */
document.addEventListener('DOMContentLoaded', () => {
  // Sidebar toggle (mobile)
  const sidebarToggle = document.getElementById('adminSidebarToggle');
  const sidebar = document.querySelector('.admin-sidebar');
  if (sidebarToggle && sidebar) {
    sidebarToggle.addEventListener('click', () => sidebar.classList.toggle('active'));
    document.addEventListener('click', (e) => {
      if (window.innerWidth <= 768 && !e.target.closest('.admin-sidebar') && !e.target.closest('#adminSidebarToggle')) {
        sidebar.classList.remove('active');
      }
    });
  }

  // Modal
  document.querySelectorAll('[data-modal]').forEach(btn => {
    btn.addEventListener('click', () => {
      const modal = document.getElementById(btn.dataset.modal);
      if (modal) modal.classList.add('active');
    });
  });
  document.querySelectorAll('.modal-overlay').forEach(overlay => {
    overlay.addEventListener('click', (e) => {
      if (e.target === overlay) overlay.classList.remove('active');
    });
  });
  document.querySelectorAll('[data-close-modal]').forEach(btn => {
    btn.addEventListener('click', () => btn.closest('.modal-overlay').classList.remove('active'));
  });

  // Delete confirmation
  document.querySelectorAll('[data-confirm]').forEach(el => {
    el.addEventListener('click', (e) => {
      if (!confirm(el.dataset.confirm || 'আপনি কি নিশ্চিত?')) e.preventDefault();
    });
  });

  // Image preview
  document.querySelectorAll('input[type="file"][data-preview]').forEach(input => {
    input.addEventListener('change', () => {
      const preview = document.getElementById(input.dataset.preview);
      if (preview && input.files[0]) {
        const reader = new FileReader();
        reader.onload = (e) => { preview.src = e.target.result; preview.style.display = 'block'; };
        reader.readAsDataURL(input.files[0]);
      }
    });
  });

  // Quick actions (approve/reject via AJAX)
  document.querySelectorAll('.quick-action').forEach(btn => {
    btn.addEventListener('click', () => {
      const action = btn.dataset.action;
      const id = btn.dataset.id;
      const csrf = btn.dataset.csrf;
      const formData = new FormData();
      formData.append('quick_action', '1');
      formData.append('action', action);
      formData.append('couple_id', id);
      formData.append('csrf_token', csrf);
      fetch('couples.php', { method: 'POST', body: formData })
        .then(r => r.json())
        .then(data => {
          if (data.success) {
            btn.closest('tr')?.remove();
            showToast(data.message || 'সফল!');
          } else {
            showToast(data.message || 'ত্রুটি!', 'error');
          }
        })
        .catch(() => showToast('সার্ভার ত্রুটি!', 'error'));
    });
  });
});

function showToast(msg, type = 'success') {
  let container = document.querySelector('.toast-container');
  if (!container) { container = document.createElement('div'); container.className = 'toast-container'; document.body.appendChild(container); }
  const toast = document.createElement('div');
  toast.className = 'toast ' + type;
  toast.textContent = msg;
  container.appendChild(toast);
  setTimeout(() => toast.remove(), 4000);
}
