<?php
$set = $func->getSetting('semua');

$statusLabel = [
    0 => ['text' => 'Belum Bayar', 'class' => 'status-off'],
    1 => ['text' => 'Diproses', 'class' => 'status-off'],
    3 => ['text' => 'Selesai', 'class' => 'status-on'],
    4 => ['text' => 'Batal', 'class' => 'status-off'],
];
$st = $statusLabel[(int) $r->status_transaksi] ?? ['text' => 'Status Lain', 'class' => 'status-off'];

$isPascabayar = ((int) $r->idcat_kategori === 2);
$namaKategori = ! empty($r->nama_kategori) ? $r->nama_kategori : 'Produk telah dihapus';
$detailProduk = (! $isPascabayar && ! empty($r->keterangan)) ? trim($r->keterangan) : '';
$namaProduk   = ($detailProduk !== '' && strcasecmp($detailProduk, $namaKategori) !== 0)
    ? $namaKategori . ' - ' . $detailProduk
    : $namaKategori;

$total = $r->harga * $r->jumlah;
?>
<?= view('customer/menu', ['func' => $func, 'db' => $db]) ?>

<main class="wrap">
  <div class="struk-wrap">

    <div class="struk-toolbar">
      <a href="<?= site_url('manage/historidigital') ?>" class="btn-batal">&larr; Kembali</a>
      <button type="button" class="btn-pilih" onclick="window.print()">🖨️ Print Struk</button>
    </div>

    <div class="struk-paper" id="strukPaper">
      <div class="struk-toko">
        <div class="struk-toko-nama"><?= htmlspecialchars($set->nama, ENT_QUOTES, 'UTF-8') ?></div>
        <div class="struk-toko-sub">Struk Transaksi Digital</div>
      </div>

      <div class="struk-dash"></div>

      <div class="struk-row">
        <span>Order ID</span>
        <span><?= htmlspecialchars($r->orderid, ENT_QUOTES, 'UTF-8') ?></span>
      </div>
      <div class="struk-row">
        <span>Tanggal</span>
        <span><?= $func->ubahTgl('d M Y H:i', $r->tgl_transaksi) ?> WIB</span>
      </div>
      <?php if (! empty($r->selesai) && (int) $r->status_transaksi === 3) { ?>
        <div class="struk-row">
          <span>Selesai</span>
          <span><?= $func->ubahTgl('d M Y H:i', $r->selesai) ?> WIB</span>
        </div>
      <?php } ?>
      <div class="struk-row">
        <span>Status</span>
        <span class="status-pill <?= $st['class'] ?>"><?= $st['text'] ?></span>
      </div>

      <div class="struk-dash"></div>

      <div class="struk-item">
        <div class="struk-item-nama"><?= htmlspecialchars($namaProduk, ENT_QUOTES, 'UTF-8') ?></div>
        <div class="struk-row">
          <span>Tujuan</span>
          <span><?= htmlspecialchars($r->variasi, ENT_QUOTES, 'UTF-8') ?></span>
        </div>
        <?php if ($isPascabayar && ! empty($r->keterangan)) { ?>
          <div class="struk-row">
            <span>Nama Pelanggan</span>
            <span><?= htmlspecialchars($r->keterangan, ENT_QUOTES, 'UTF-8') ?></span>
          </div>
        <?php } ?>
        <?php if (! empty($r->idpo)) { ?>
          <div class="struk-row">
            <span>Kode Produk</span>
            <span><?= htmlspecialchars($r->idpo, ENT_QUOTES, 'UTF-8') ?></span>
          </div>
        <?php } ?>
        <div class="struk-row">
          <span>Harga Satuan</span>
          <span>Rp <?= $func->formUang($r->harga) ?></span>
        </div>
        <div class="struk-row">
          <span>Jumlah</span>
          <span>x<?= $r->jumlah ?></span>
        </div>
      </div>

      <div class="struk-dash"></div>

      <div class="struk-row struk-total">
        <span>Total Bayar</span>
        <span>Rp <?= $func->formUang($total) ?></span>
      </div>

      <div class="struk-dash"></div>

      <div class="struk-footer">
        Terima kasih telah bertransaksi di <?= htmlspecialchars($set->nama, ENT_QUOTES, 'UTF-8') ?>.<br>
        Struk ini sah tanpa tanda tangan.
      </div>
    </div>
  </div>
</main>
