<?php
$row = is_array($row ?? null) ? $row : null;
$isEdit = (bool) ($isEdit ?? false);
$errorMessage = (string) ($errorMessage ?? '');
$csrfToken = (string) ($csrfToken ?? '');

$rowId = (int) ($row['id'] ?? 0);
$rowKey = (string) ($row['key'] ?? '');
$rowLabel = (string) ($row['label_pl'] ?? '');
$rowColor = (string) ($row['color'] ?? '#6c757d');
$rowSortOrder = (int) ($row['sort_order'] ?? 100);
$rowIsTerminal = !empty($row['is_terminal']);

$action = $isEdit
    ? '/settings/delivery-statuses/' . $rowId . '/update'
    : '/settings/delivery-statuses';
?>

<section class="card">
  <h2 class="section-title"><?= $isEdit ? 'Edytuj status przesyłki' : 'Nowy status przesyłki' ?></h2>

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

  <form action="<?= $e($action) ?>" method="post" novalidate class="mt-12">
    <input type="hidden" name="_token" value="<?= $e($csrfToken) ?>">

    <div class="form-grid-2">
      <label class="form-field">
        <span class="field-label">Klucz *</span>
        <?php if ($isEdit): ?>
          <input class="form-control" type="text" value="<?= $e($rowKey) ?>" disabled>
          <small class="muted">Klucz nie może być zmieniany po utworzeniu.</small>
        <?php else: ?>
          <input class="form-control" type="text" name="key" maxlength="50" required
                 pattern="[a-z][a-z0-9_]{0,49}"
                 placeholder="np. custom_status"
                 value="<?= $e($rowKey) ?>">
          <small class="muted">Małe litery, cyfry, _, max 50 znaków, zaczyna się literą.</small>
        <?php endif; ?>
      </label>

      <label class="form-field">
        <span class="field-label">Etykieta *</span>
        <input class="form-control" type="text" name="label_pl" maxlength="100" required
               placeholder="np. Mój status" value="<?= $e($rowLabel) ?>">
      </label>
    </div>

    <div class="form-grid-2 mt-0">
      <label class="form-field">
        <span class="field-label">Kolor</span>
        <input class="form-control" type="color" name="color" value="<?= $e($rowColor) ?>">
      </label>

      <label class="form-field">
        <span class="field-label">Kolejność</span>
        <input class="form-control" type="number" name="sort_order"
               min="0" max="9999" value="<?= $rowSortOrder ?>">
      </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_terminal" value="1"<?= $rowIsTerminal ? ' checked' : '' ?>>
        <span class="field-label" style="margin:0">Końcowy</span>
      </label>
    </div>

    <div class="form-actions mt-16">
      <button type="submit" class="btn btn--primary"><?= $isEdit ? 'Zapisz zmiany' : 'Utwórz status' ?></button>
      <a href="/settings/delivery-statuses?tab=statuses" class="btn btn--secondary">Anuluj</a>
    </div>
  </form>
</section>
