<?php
/** @var array<string, mixed>|null $config */
$config = is_array($config ?? null) ? $config : null;
$isEdit = $config !== null;
$cid = (int) ($config['id'] ?? 0);

$name = (string) ($config['name'] ?? '');
$numberFormat = (string) ($config['number_format'] ?? 'PAR/%N/%M/%Y');
$numberingType = (string) ($config['numbering_type'] ?? 'monthly');
$saleDateSource = (string) ($config['sale_date_source'] ?? 'issue_date');
$orderReference = (string) ($config['order_reference'] ?? 'none');
$isNamed = ((int) ($config['is_named'] ?? 0)) === 1;
$isActive = $isEdit ? ((int) ($config['is_active'] ?? 0)) === 1 : true;

$success = trim((string) ($successMessage ?? ''));
$error = trim((string) ($errorMessage ?? ''));
?>

<section class="card">
  <p class="muted" style="margin-bottom:8px"><a href="/settings/accounting">Ksiegowosc</a> &raquo; <a href="/settings/accounting/receipts">Paragony</a></p>
  <h2 class="section-title"><?= $isEdit ? 'Edycja konfiguracji paragonu' : 'Nowa konfiguracja paragonu' ?></h2>

  <?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">
  <form action="/settings/accounting/receipts/save" method="post" novalidate class="mt-12">
    <input type="hidden" name="_token" value="<?= $e($csrfToken ?? '') ?>">
    <?php if ($isEdit): ?>
      <input type="hidden" name="id" value="<?= $cid ?>">
    <?php endif; ?>

    <div class="form-grid-2">
      <label class="form-field">
        <span class="field-label">Nazwa *</span>
        <input class="form-control" type="text" name="name" maxlength="128" required value="<?= $e($name) ?>">
      </label>
      <label class="form-field">
        <span class="field-label">Format numeru *</span>
        <input class="form-control" type="text" name="number_format" maxlength="64" required placeholder="PAR/%N/%M/%Y" value="<?= $e($numberFormat) ?>">
        <small class="field-hint"><code>%N</code> = numer, <code>%M</code> = miesiac, <code>%Y</code> = rok</small>
      </label>
    </div>

    <div class="form-grid-3 mt-0">
      <label class="form-field">
        <span class="field-label">Numerowanie</span>
        <select class="form-control" name="numbering_type">
          <option value="monthly"<?= $numberingType === 'monthly' ? ' selected' : '' ?>>Miesieczne</option>
          <option value="yearly"<?= $numberingType === 'yearly' ? ' selected' : '' ?>>Roczne</option>
        </select>
      </label>
      <label class="form-field">
        <span class="field-label">Data sprzedazy</span>
        <select class="form-control" name="sale_date_source">
          <option value="issue_date"<?= $saleDateSource === 'issue_date' ? ' selected' : '' ?>>Data wystawienia</option>
          <option value="order_date"<?= $saleDateSource === 'order_date' ? ' selected' : '' ?>>Data zamowienia</option>
          <option value="payment_date"<?= $saleDateSource === 'payment_date' ? ' selected' : '' ?>>Data platnosci</option>
        </select>
      </label>
      <label class="form-field">
        <span class="field-label">Numer referencyjny zamowienia</span>
        <select class="form-control" name="order_reference">
          <option value="none"<?= $orderReference === 'none' ? ' selected' : '' ?>>Brak</option>
          <option value="orderpro"<?= $orderReference === 'orderpro' ? ' selected' : '' ?>>orderPRO</option>
          <option value="integration"<?= $orderReference === 'integration' ? ' selected' : '' ?>>Zewnetrzny</option>
        </select>
      </label>
    </div>

    <div class="form-grid-2 mt-0">
      <label class="form-field" style="display:flex;align-items:center;gap:6px;flex-direction:row">
        <input type="checkbox" name="is_named" value="1"<?= $isNamed ? ' checked' : '' ?>>
        <span class="field-label" style="margin:0">Paragon imienny (z danymi kupujacego)</span>
      </label>
      <label class="form-field" style="display:flex;align-items:center;gap:6px;flex-direction:row">
        <input type="checkbox" name="is_active" value="1"<?= $isActive ? ' checked' : '' ?>>
        <span class="field-label" style="margin:0">Konfiguracja aktywna</span>
      </label>
    </div>

    <div class="form-actions mt-16">
      <button type="submit" class="btn btn--primary"><?= $isEdit ? 'Zapisz zmiany' : 'Dodaj konfiguracje' ?></button>
      <a href="/settings/accounting/receipts" class="btn btn--secondary">Anuluj</a>
    </div>
  </form>
</section>
