<?php
$exportQueryVal = (string) ($exportQuery ?? '');
$csrfTokenVal = (string) ($csrfToken ?? '');
$queryData = is_array($tableList['query'] ?? null) ? $tableList['query'] : [];
?>

<section class="card">
  <div class="orders-head">
    <div>
      <h2 class="section-title"><?= $e($t('accounting.title')) ?></h2>
    </div>
    <div class="accounting-export-actions">
      <span class="muted js-accounting-selection-info"></span>
      <button type="button" class="btn btn--secondary js-accounting-export-selected" disabled><?= $e($t('accounting.export_selected')) ?></button>
      <button type="button" class="btn btn--primary js-accounting-export-all"><?= $e($t('accounting.export_all')) ?></button>
    </div>
  </div>
</section>

<form id="accountingExportForm" method="post" action="/accounting/export" style="display:none;">
  <input type="hidden" name="_token" value="<?= $e($csrfTokenVal) ?>">
  <input type="hidden" name="export_all" value="0" id="exportAllFlag">
  <?php foreach ($queryData as $key => $value): ?>
    <?php if ($value !== '' && $value !== null && $key !== 'page' && $key !== 'per_page' && $key !== 'sort' && $key !== 'sort_dir'): ?>
      <input type="hidden" name="<?= $e((string) $key) ?>" value="<?= $e((string) $value) ?>">
    <?php endif; ?>
  <?php endforeach; ?>
  <div id="exportSelectedIds"></div>
</form>

<?php require __DIR__ . '/../components/table-list.php'; ?>

<script>
(function() {
  var form = document.getElementById('accountingExportForm');
  var exportAllFlag = document.getElementById('exportAllFlag');
  var exportSelectedIds = document.getElementById('exportSelectedIds');
  var exportSelectedBtn = document.querySelector('.js-accounting-export-selected');
  var exportAllBtn = document.querySelector('.js-accounting-export-all');
  var selectionInfo = document.querySelector('.js-accounting-selection-info');
  var tableRoot = document.querySelector('[data-table-list-id="accounting"]');

  function getCheckedIds() {
    if (!tableRoot) return [];
    var items = tableRoot.querySelectorAll('.js-table-select-item:checked');
    var ids = [];
    for (var i = 0; i < items.length; i++) {
      ids.push(items[i].value);
    }
    return ids;
  }

  function updateUI() {
    var ids = getCheckedIds();
    var count = ids.length;
    if (exportSelectedBtn) {
      exportSelectedBtn.disabled = count === 0;
    }
    if (selectionInfo) {
      selectionInfo.textContent = count > 0 ? 'Zaznaczono: ' + count : '';
    }
  }

  if (tableRoot) {
    tableRoot.addEventListener('change', function(e) {
      if (e.target && (e.target.classList.contains('js-table-select-item') || e.target.classList.contains('js-table-select-all'))) {
        updateUI();
      }
    });
  }

  if (exportSelectedBtn) {
    exportSelectedBtn.addEventListener('click', function() {
      var ids = getCheckedIds();
      if (ids.length === 0) return;
      exportAllFlag.value = '0';
      exportSelectedIds.innerHTML = '';
      ids.forEach(function(id) {
        var input = document.createElement('input');
        input.type = 'hidden';
        input.name = 'selected_ids[]';
        input.value = id;
        exportSelectedIds.appendChild(input);
      });
      form.submit();
    });
  }

  if (exportAllBtn) {
    exportAllBtn.addEventListener('click', function() {
      exportAllFlag.value = '1';
      exportSelectedIds.innerHTML = '';
      form.submit();
    });
  }

  updateUI();
})();
</script>
