/* =============================================================
   bulk-actions.js
   Shared JS for vouchers.php and users.php
   Handles: bulk user select (release), bulk claim select (revoke)
   Loaded via: admin/partials/footer.php
   ============================================================= */

/* ── User bulk-select (for bulk release) ── */
function toggleAllUsers(cb) {
  document.querySelectorAll('.usr-sel').forEach(c => c.checked = cb.checked);
  updateUserBulkBar();
}

function updateUserBulkBar() {
  const sel   = document.querySelectorAll('.usr-sel:checked');
  const panel = document.getElementById('bulk-release-panel');
  const cnt   = document.getElementById('bulk-sel-count');
  const btn   = document.getElementById('bulk-release-btn');
  const bcnt  = document.getElementById('bulk-btn-count');
  const all   = document.getElementById('usr-sel-all');
  const total = document.querySelectorAll('.usr-sel').length;
  panel.style.display  = sel.length > 0 ? 'block' : 'none';
  cnt.textContent      = sel.length + ' user(s) selected';
  bcnt.textContent     = sel.length;
  btn.disabled         = sel.length === 0;
  all.indeterminate    = sel.length > 0 && sel.length < total;
  all.checked          = sel.length === total && total > 0;
}

function clearBulkSel() {
  document.querySelectorAll('.usr-sel').forEach(c => c.checked = false);
  document.getElementById('usr-sel-all').checked = false;
  document.getElementById('bulk-release-panel').style.display = 'none';
}

function submitBulkRelease() {
  const sel = Array.from(document.querySelectorAll('.usr-sel:checked')).map(c => c.dataset.id);
  if (!sel.length) { alert('Select at least one user.'); return; }
  const vId = document.querySelector('[name=voucher_id]').value;
  if (!vId) { alert('Please select a voucher.'); return; }
  if (!confirm('Release this voucher to ' + sel.length + ' user(s)?')) return;
  const container = document.getElementById('bulk-hidden-ids');
  container.innerHTML = '';
  sel.forEach(id => {
    const inp = document.createElement('input');
    inp.type = 'hidden'; inp.name = 'customer_ids[]'; inp.value = id;
    container.appendChild(inp);
  });
  document.getElementById('bulk-release-form').submit();
}

/* ── Claim bulk-select (for bulk revoke) ── */
function toggleAllClaims(cb) {
  document.querySelectorAll('.cl-sel').forEach(c => c.checked = cb.checked);
  updateClaimBulkBar();
}

function updateClaimBulkBar() {
  const sel   = document.querySelectorAll('.cl-sel:checked');
  const btn   = document.getElementById('bulk-revoke-btn');
  const all   = document.getElementById('cl-sel-all');
  const total = document.querySelectorAll('.cl-sel').length;
  if (btn) btn.style.display = sel.length > 0 ? 'inline-flex' : 'none';
  if (all) {
    all.indeterminate = sel.length > 0 && sel.length < total;
    all.checked       = sel.length === total && total > 0;
  }
}

function submitBulkRevoke() {
  const sel = Array.from(document.querySelectorAll('.cl-sel:checked')).map(c => c.dataset.id);
  if (!sel.length) { alert('Select at least one claim.'); return; }
  if (!confirm('Revoke ' + sel.length + ' selected claim(s)? This will delete the claims and hide the associated vouchers.')) return;
  const container = document.getElementById('bulk-revoke-ids');
  container.innerHTML = '';
  sel.forEach(id => {
    const inp = document.createElement('input');
    inp.type = 'hidden'; inp.name = 'claim_ids[]'; inp.value = id;
    container.appendChild(inp);
  });
  document.getElementById('bulk-revoke-form').submit();
}