<?php
$orderRow = is_array($order ?? null) ? $order : [];
$itemsList = is_array($items ?? null) ? $items : [];
$configsList = is_array($configs ?? null) ? $configs : [];
$sellerData = is_array($seller ?? null) ? $seller : [];
$buyerAddr = is_array($buyerAddress ?? null) ? $buyerAddress : null;
$autoNip = (string) ($autoTaxNumber ?? '');
$existingInvoicesList = is_array($existingInvoices ?? null) ? $existingInvoices : [];
$hasExistingInvoices = $existingInvoicesList !== [];
$orderIdVal = (int) ($orderId ?? 0);
$errorMsg = (string) ($errorMessage ?? '');

$buyerNameDefault = trim((string) ($buyerAddr['name'] ?? ''));
$buyerCompanyDefault = trim((string) ($buyerAddr['company_name'] ?? ''));
$buyerStreetDefault = trim(((string) ($buyerAddr['street_name'] ?? '')) . ' ' . ((string) ($buyerAddr['street_number'] ?? '')));
$buyerCityDefault = trim((string) ($buyerAddr['city'] ?? ''));
$buyerPostalDefault = trim((string) ($buyerAddr['zip_code'] ?? ''));
$buyerEmailDefault = trim((string) ($buyerAddr['email'] ?? $orderRow['buyer_email'] ?? ''));
?>

<section class="card">
  <div class="order-details-head">
    <div>
      <a href="/orders/<?= $e((string) $orderIdVal) ?>" class="order-back-link">&larr; Powrot do zamowienia</a>
      <h2 class="section-title mt-12">Wystaw fakture</h2>
      <div class="order-details-sub mt-4">
        Zamowienie <?= $e((string) ($orderRow['internal_order_number'] ?? ('#' . $orderIdVal))) ?>
      </div>
    </div>
  </div>

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

  <?php if ($hasExistingInvoices): ?>
    <?php
      ob_start();
      ?>
      <strong>Uwaga!</strong> Do tego zamowienia wystawiono juz <?= $e((string) count($existingInvoicesList)) ?> fakture/y:
      <ul class="mt-4">
        <?php foreach ($existingInvoicesList as $ei): ?>
          <li>
            <strong><?= $e((string) ($ei['invoice_number'] ?? '-')) ?></strong>
            — <?= $e(substr((string) ($ei['issue_date'] ?? ''), 0, 16)) ?>,
            <?= $e(number_format((float) ($ei['total_gross'] ?? 0), 2, '.', ' ')) ?> PLN
            (<?= $e((string) ($ei['config_name'] ?? '-')) ?><?php if (trim((string) ($ei['external_invoice_id'] ?? '')) !== ''): ?>, Fakturownia<?php endif; ?>)
          </li>
        <?php endforeach; ?>
      </ul>
      <?php
      $existingInvoicesHtml = ob_get_clean();
    ?>
    <div class="mt-12"><?php $type='warning'; $messageHtml = $existingInvoicesHtml; $dismissible=true; include dirname(__DIR__) . '/components/alert.php'; unset($messageHtml); ?></div>
  <?php endif; ?>

  <form id="invoice-create-form" method="post" action="/orders/<?= $e((string) $orderIdVal) ?>/invoice/store" class="mt-16">
    <input type="hidden" name="_token" value="<?= $e((string) ($csrfToken ?? '')) ?>">

    <div class="form-grid-2">
      <div class="form-group">
        <label class="form-label" for="config_id">Konfiguracja faktury</label>
        <select name="config_id" id="config_id" class="form-control" required>
          <option value="">— wybierz konfiguracje —</option>
          <?php foreach ($configsList as $cfg): ?>
            <?php
              $cfgId = (int) ($cfg['id'] ?? 0);
              $cfgIsDelegated = (int) ($cfg['is_delegated'] ?? 0) === 1;
              $cfgIntName = trim((string) ($cfg['integration_name'] ?? ''));
              $cfgLabel = trim((string) ($cfg['name'] ?? ''))
                . ' — '
                . ($cfgIsDelegated ? ('Fakturownia' . ($cfgIntName !== '' ? ': ' . $cfgIntName : '')) : 'Lokalnie')
                . ' (' . trim((string) ($cfg['number_format'] ?? '')) . ')';
            ?>
            <option value="<?= $e((string) $cfgId) ?>"><?= $e($cfgLabel) ?></option>
          <?php endforeach; ?>
        </select>
      </div>

      <div class="form-group">
        <label class="form-label" for="issue_date">Data wystawienia</label>
        <input type="datetime-local" name="issue_date" id="issue_date" class="form-control" value="<?= $e(date('Y-m-d\TH:i')) ?>" required>
      </div>
    </div>

    <h3 class="section-title mt-16">Dane nabywcy</h3>
    <div class="form-grid-2">
      <div class="form-group">
        <label class="form-label" for="buyer_tax_number">NIP nabywcy</label>
        <div style="display:flex;gap:8px;align-items:flex-start;">
          <input type="text" name="buyer_tax_number" id="buyer_tax_number" class="form-control"
                 value="<?= $e($autoNip) ?>"
                 placeholder="np. 1234567890"
                 style="flex:1;">
          <button type="button" id="btn-gus-lookup" class="btn btn--secondary" style="white-space:nowrap;">
            Pobierz z GUS
          </button>
        </div>
        <?php if ($autoNip !== ''): ?>
          <small class="muted">Auto-wykryty z payload zamowienia. Mozesz nadpisac lub kliknac "Pobierz z GUS".</small>
        <?php else: ?>
          <small class="muted">Wpisz NIP i kliknij "Pobierz z GUS" — dane firmy zostana wypelnione automatycznie.</small>
        <?php endif; ?>
      </div>

      <div class="form-group">
        <label class="form-label" for="buyer_name">Imie i nazwisko</label>
        <input type="text" name="buyer_name" id="buyer_name" class="form-control"
               value="<?= $e($buyerNameDefault) ?>">
      </div>

      <div class="form-group">
        <label class="form-label" for="buyer_company_name">Nazwa firmy</label>
        <input type="text" name="buyer_company_name" id="buyer_company_name" class="form-control"
               value="<?= $e($buyerCompanyDefault) ?>">
      </div>

      <div class="form-group">
        <label class="form-label" for="buyer_email">Email</label>
        <input type="email" name="buyer_email" id="buyer_email" class="form-control"
               value="<?= $e($buyerEmailDefault) ?>">
      </div>

      <div class="form-group">
        <label class="form-label" for="buyer_street">Ulica i numer</label>
        <input type="text" name="buyer_street" id="buyer_street" class="form-control"
               value="<?= $e($buyerStreetDefault) ?>">
      </div>

      <div class="form-group">
        <label class="form-label" for="buyer_city">Miejscowosc</label>
        <input type="text" name="buyer_city" id="buyer_city" class="form-control"
               value="<?= $e($buyerCityDefault) ?>">
      </div>

      <div class="form-group">
        <label class="form-label" for="buyer_postal_code">Kod pocztowy</label>
        <input type="text" name="buyer_postal_code" id="buyer_postal_code" class="form-control"
               value="<?= $e($buyerPostalDefault) ?>">
      </div>
    </div>

    <h3 class="section-title mt-16">Pozycje zamowienia</h3>
    <div class="table-wrap mt-8">
      <table class="table table--details">
        <thead>
          <tr>
            <th>Lp.</th>
            <th>Nazwa</th>
            <th>Ilosc</th>
            <th>Cena brutto</th>
            <th>VAT</th>
            <th>Suma brutto</th>
          </tr>
        </thead>
        <tbody>
          <?php if ($itemsList === []): ?>
            <tr><td colspan="6" class="muted">Brak pozycji</td></tr>
          <?php endif; ?>
          <?php $totalGross = 0.0; ?>
          <?php foreach ($itemsList as $idx => $item): ?>
            <?php
              $qty = (float) ($item['quantity'] ?? 0);
              $price = $item['original_price_with_tax'] !== null ? (float) $item['original_price_with_tax'] : (float) ($item['price_gross'] ?? 0);
              $vat = (float) ($item['vat'] ?? 23);
              $sum = $qty * $price;
              $totalGross += $sum;
            ?>
            <tr>
              <td><?= $e((string) ($idx + 1)) ?></td>
              <td><?= $e((string) ($item['original_name'] ?? $item['name'] ?? '')) ?></td>
              <td><?= $e((string) $qty) ?></td>
              <td><?= $e(number_format($price, 2, '.', ' ')) ?></td>
              <td><?= $e(number_format($vat, 0, '.', '')) ?>%</td>
              <td><?= $e(number_format($sum, 2, '.', ' ')) ?></td>
            </tr>
          <?php endforeach; ?>
          <?php
            $deliveryPrice = (float) ($orderRow['delivery_price'] ?? 0);
            if ($deliveryPrice > 0) {
                $totalGross += $deliveryPrice;
          ?>
            <tr>
              <td><?= $e((string) (count($itemsList) + 1)) ?></td>
              <td>Koszt wysylki</td>
              <td>1</td>
              <td><?= $e(number_format($deliveryPrice, 2, '.', ' ')) ?></td>
              <td>23%</td>
              <td><?= $e(number_format($deliveryPrice, 2, '.', ' ')) ?></td>
            </tr>
          <?php } ?>
        </tbody>
        <tfoot>
          <tr>
            <td colspan="5" class="text-right"><strong>Razem brutto:</strong></td>
            <td><strong><?= $e(number_format($totalGross, 2, '.', ' ')) ?> <?= $e((string) ($orderRow['currency'] ?? 'PLN')) ?></strong></td>
          </tr>
        </tfoot>
      </table>
    </div>

    <h3 class="section-title mt-16">Sprzedawca (z ustawien firmy)</h3>
    <div class="receipt-seller-preview mt-8">
      <dl class="order-kv">
        <dt>Firma</dt><dd><?= $e((string) ($sellerData['company_name'] ?? '-')) ?></dd>
        <dt>NIP</dt><dd><?= $e((string) ($sellerData['tax_number'] ?? '-')) ?></dd>
        <dt>Adres</dt><dd><?= $e((string) ($sellerData['street'] ?? '')) ?>, <?= $e((string) ($sellerData['postal_code'] ?? '')) ?> <?= $e((string) ($sellerData['city'] ?? '')) ?></dd>
      </dl>
    </div>

    <div class="mt-16">
      <?php if ($hasExistingInvoices): ?>
        <button type="button" id="invoice-submit-btn" class="btn btn--primary">Wystaw fakture</button>
      <?php else: ?>
        <button type="submit" class="btn btn--primary">Wystaw fakture</button>
      <?php endif; ?>
      <a href="/orders/<?= $e((string) $orderIdVal) ?>" class="btn btn--secondary ml-8">Anuluj</a>
    </div>
  </form>
</section>

<script>
(function () {
    var btn = document.getElementById('btn-gus-lookup');
    if (!btn) { return; }

    btn.addEventListener('click', function () {
        var nipInput = document.getElementById('buyer_tax_number');
        var nip = (nipInput.value || '').replace(/[\s\-]/g, '');

        if (!/^\d{10}$/.test(nip)) {
            if (window.OrderProAlerts && window.OrderProAlerts.error) {
                window.OrderProAlerts.error('Wpisz poprawny NIP (10 cyfr) przed klikniem "Pobierz z GUS".');
            }
            return;
        }

        var originalLabel = btn.textContent;
        btn.disabled = true;
        btn.textContent = 'Pobieram...';

        var url = '/api/nip/lookup?nip=' + encodeURIComponent(nip);

        fetch(url, {
            method: 'GET',
            headers: { 'X-Requested-With': 'XMLHttpRequest' },
            credentials: 'same-origin'
        })
            .then(function (resp) { return resp.json().then(function (j) { return { ok: resp.ok, data: j }; }); })
            .then(function (res) {
                if (!res.ok || !res.data || !res.data.success) {
                    var msg = res.data && res.data.error ? res.data.error : 'Blad pobierania danych z GUS.';
                    throw new Error(msg);
                }
                var d = res.data.data || {};
                if (d.tax_number) { nipInput.value = d.tax_number; }
                // MF zwraca w `name` osobe fizyczna dla JDG (krs=null) lub legal name dla spolki (krs!=null).
                // Wpisujemy do wlasciwego pola wg flagi is_jdg; drugiego pola nie ruszamy.
                var nameTargetId = d.is_jdg ? 'buyer_name' : 'buyer_company_name';
                var addressFields = {
                    buyer_street: d.street,
                    buyer_postal_code: d.postal_code,
                    buyer_city: d.city
                };
                var nameTargetEl = document.getElementById(nameTargetId);
                if (nameTargetEl && d.company_name) { nameTargetEl.value = d.company_name; }
                Object.keys(addressFields).forEach(function (key) {
                    var el = document.getElementById(key);
                    if (el && addressFields[key]) { el.value = addressFields[key]; }
                });
            })
            .catch(function (err) {
                if (window.OrderProAlerts && window.OrderProAlerts.error) {
                    window.OrderProAlerts.error(err && err.message ? err.message : 'Blad GUS.');
                } else {
                    alert(err && err.message ? err.message : 'Blad GUS.');
                }
            })
            .finally(function () {
                btn.disabled = false;
                btn.textContent = originalLabel;
            });
    });
})();
</script>

<?php if ($hasExistingInvoices): ?>
<script>
document.getElementById('invoice-submit-btn').addEventListener('click', function() {
    window.OrderProAlerts.confirm({
        title: 'Wystawic kolejna fakture?',
        message: 'Do tego zamowienia wystawiono juz fakture. Czy na pewno chcesz wystawic kolejna?',
        confirmLabel: 'Wystaw',
        danger: false,
        onConfirm: function() {
            document.getElementById('invoice-create-form').submit();
        }
    });
});
</script>
<?php endif; ?>
