<?php
/** @var array<int, array<string, mixed>> $configs */
$configs = is_array($configs ?? null) ? $configs : [];
$success = trim((string) ($successMessage ?? ''));
$error = trim((string) ($errorMessage ?? ''));
?>

<section class="card">
  <p class="muted" style="margin-bottom:8px"><a href="/settings/accounting">&larr; Ksiegowosc</a></p>
  <h2 class="section-title">Konfiguracje paragonow</h2>
  <p class="muted mt-12">Zarzadzaj konfiguracjami wystawiania paragonow.</p>

  <?php if ($error !== ''): ?>
    <div class="mt-12"><?php $type='danger'; $message=(string) $error; $dismissible=true; include dirname(__DIR__) . '/components/alert.php'; ?></div>
  <?php endif; ?>
  <?php if ($success !== ''): ?>
    <div class="mt-12"><?php $type='success'; $message=(string) $success; $dismissible=true; include dirname(__DIR__) . '/components/alert.php'; ?></div>
  <?php endif; ?>
</section>

<section class="card mt-16">
  <h3 class="section-title">Lista konfiguracji</h3>

  <div class="form-actions mt-12">
    <a class="btn btn--primary" href="/settings/accounting/receipts/new">Dodaj konfiguracje</a>
  </div>

  <?php if ($configs === []): ?>
    <p class="muted mt-12">Brak konfiguracji paragonow. Dodaj pierwsza powyzej.</p>
  <?php else: ?>
    <div class="table-wrap mt-12">
      <table class="table">
        <thead>
          <tr>
            <th>Nazwa</th>
            <th>Format numeru</th>
            <th>Numerowanie</th>
            <th>Status</th>
            <th>Akcje</th>
          </tr>
        </thead>
        <tbody>
        <?php foreach ($configs as $cfg):
          $cid = (int) ($cfg['id'] ?? 0);
          $isActive = ((int) ($cfg['is_active'] ?? 0)) === 1;
        ?>
          <tr>
            <td><?= $e((string) ($cfg['name'] ?? '')) ?></td>
            <td><code><?= $e((string) ($cfg['number_format'] ?? '')) ?></code></td>
            <td><?= ((string) ($cfg['numbering_type'] ?? 'monthly')) === 'yearly' ? 'Roczne' : 'Miesieczne' ?></td>
            <td>
              <?php if ($isActive): ?>
                <span class="badge badge--success">Aktywna</span>
              <?php else: ?>
                <span class="badge badge--muted">Nieaktywna</span>
              <?php endif; ?>
            </td>
            <td style="white-space:nowrap">
              <a href="/settings/accounting/receipts/edit?id=<?= $cid ?>" class="btn btn--sm btn--secondary">Edytuj</a>
              <form action="/settings/accounting/receipts/toggle" method="post" style="display:inline">
                <input type="hidden" name="_token" value="<?= $e($csrfToken ?? '') ?>">
                <input type="hidden" name="id" value="<?= $cid ?>">
                <button type="submit" class="btn btn--sm btn--secondary">
                  <?= $isActive ? 'Dezaktywuj' : 'Aktywuj' ?>
                </button>
              </form>
              <form action="/settings/accounting/receipts/delete" method="post" style="display:inline" class="js-confirm-delete">
                <input type="hidden" name="_token" value="<?= $e($csrfToken ?? '') ?>">
                <input type="hidden" name="id" value="<?= $cid ?>">
                <button type="button" class="btn btn--sm btn--danger js-delete-btn">Usun</button>
              </form>
            </td>
          </tr>
        <?php endforeach; ?>
        </tbody>
      </table>
    </div>
  <?php endif; ?>
</section>
