<?php
/** @var array<string, mixed> $settings */
$settings = is_array($settings ?? null) ? $settings : [];
$integrationId = (int) ($settings['integration_id'] ?? 0);
$prefix = (string) ($settings['account_prefix'] ?? '');
$departmentId = (string) ($settings['department_id'] ?? '');
$defaultKind = (string) ($settings['default_kind'] ?? 'vat');
$defaultPaymentDays = (int) ($settings['default_payment_to_days'] ?? 7);
$isActive = (bool) ($settings['is_active'] ?? true);
$hasToken = (bool) ($settings['has_api_token'] ?? false);
$lastTestAt = (string) ($settings['last_test_at'] ?? '');
$lastTestStatus = (string) ($settings['last_test_status'] ?? '');
$lastTestMessage = (string) ($settings['last_test_message'] ?? '');

$flashSave = trim((string) ($flashSave ?? ''));
$flashTest = trim((string) ($flashTest ?? ''));
$flashError = trim((string) ($flashError ?? ''));
?>

<section class="card">
  <h2 class="section-title">Integracja Fakturownia</h2>
  <p class="muted mt-12">Jedna globalna konfiguracja konta Fakturowni do wystawiania faktur delegowanych.</p>

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

<section class="card mt-16">
  <h3 class="section-title">Konfiguracja</h3>
  <div class="muted mt-12">
    Token:
    <strong><?= $e($hasToken ? 'zapisany' : 'brak') ?></strong>
    &nbsp; Status:
    <strong><?= $e($isActive ? 'aktywna' : 'nieaktywna') ?></strong>
  </div>

  <form class="statuses-form mt-16" action="/settings/integrations/fakturownia/save" method="post" novalidate>
    <input type="hidden" name="_token" value="<?= $e($csrfToken ?? '') ?>">

    <label class="form-field">
      <span class="field-label">Prefix konta (subdomena) *</span>
      <input class="form-control" type="text" name="account_prefix" maxlength="63" value="<?= $e($prefix) ?>" pattern="[a-z0-9][a-z0-9-]{1,62}" required>
      <span class="muted">Wpisz sama subdomene, bez koncowki .fakturownia.pl.</span>
    </label>

    <label class="form-field">
      <span class="field-label">Token API <?= $hasToken ? '' : '*' ?></span>
      <input class="form-control" type="password" name="api_token" autocomplete="new-password" placeholder="<?= $hasToken ? '********' : '' ?>" <?= $hasToken ? '' : 'required' ?>>
      <span class="muted"><?= $hasToken ? 'Token jest zapisany. Wpisz nowy, aby go nadpisac.' : 'Token API z Fakturowni (Ustawienia > Konta uzytkownikow > API).' ?></span>
    </label>

    <label class="form-field">
      <span class="field-label">ID departamentu (opcjonalnie)</span>
      <input class="form-control" type="text" name="department_id" maxlength="64" value="<?= $e($departmentId) ?>">
    </label>

    <div class="form-grid-2 mt-0">
      <label class="form-field">
        <span class="field-label">Domyslny typ dokumentu</span>
        <select class="form-control" name="default_kind">
          <option value="vat" <?= $defaultKind === 'vat' ? 'selected' : '' ?>>Faktura VAT</option>
          <option value="proforma" <?= $defaultKind === 'proforma' ? 'selected' : '' ?>>Proforma</option>
          <option value="invoice_other" <?= $defaultKind === 'invoice_other' ? 'selected' : '' ?>>Inna</option>
        </select>
      </label>

      <label class="form-field">
        <span class="field-label">Domyslny termin platnosci (dni)</span>
        <input class="form-control" type="number" name="default_payment_to_days" min="0" max="120" value="<?= $defaultPaymentDays ?>">
      </label>
    </div>

    <label class="form-field">
      <span class="field-label">Status</span>
      <label>
        <input type="checkbox" name="is_active" value="1" <?= $isActive ? 'checked' : '' ?>>
        Integracja aktywna
      </label>
    </label>

    <div class="form-actions">
      <button type="submit" class="btn btn--primary">Zapisz</button>
    </div>
  </form>
</section>

<section class="card mt-16">
  <h3 class="section-title">Test polaczenia</h3>
  <p class="muted mt-12">Wykonuje GET <code><?= $e('https://' . ($prefix !== '' ? $prefix : '{prefix}') . '.fakturownia.pl/account.json') ?></code> z zapisanym tokenem.</p>

  <form action="/settings/integrations/fakturownia/test" method="post" class="mt-12">
    <input type="hidden" name="_token" value="<?= $e($csrfToken ?? '') ?>">
    <input type="hidden" name="id" value="<?= $integrationId ?>">
    <button type="submit" class="btn btn--secondary">Testuj polaczenie</button>
  </form>

  <?php if ($lastTestAt !== ''): ?>
    <div class="muted mt-12">
      Ostatni test: <strong><?= $e($lastTestAt) ?></strong>
      <?php if ($lastTestStatus !== ''): ?>
        <span class="badge badge--<?= $lastTestStatus === 'ok' ? 'success' : 'muted' ?>"><?= $e(strtoupper($lastTestStatus)) ?></span>
      <?php endif; ?>
      <?php if ($lastTestMessage !== ''): ?>
        <div><?= $e($lastTestMessage) ?></div>
      <?php endif; ?>
    </div>
  <?php endif; ?>
</section>
