<?= view('customer/menu', ['func' => $func, 'db' => $db]) ?>

<main class="wrap">
  <div class="ppob-page">
    <div class="ppob-card">
      <div class="ppob-head">
        <div class="ppob-title">Histori Transaksi Digital</div>
        <div class="ppob-subtitle">
          Cari atau filter transaksi Pulsa, Data, Token PLN, dan produk digital lainnya.
          <?php if ($tanggalDefault) { ?>
            Menampilkan <b>semua transaksi</b> — gunakan filter tanggal di bawah untuk mempersempit.
          <?php } ?>
        </div>
      </div>

      <form method="get" action="<?= site_url('manage/historidigital') ?>" class="historidigital-filter">
        <div class="historidigital-fieldgroup">
          <label class="historidigital-fieldlabel" for="cari">Cari Nama Produk / Nomor HP / Order ID</label>
          <input type="text" id="cari" name="cari" class="historidigital-input" value="<?= htmlspecialchars($cari, ENT_QUOTES, 'UTF-8') ?>" placeholder="Contoh: Pulsa, Token PLN, 0812... (berlaku di semua kategori)">
        </div>

        <div class="historidigital-fieldgroup">
          <label class="historidigital-fieldlabel" for="dari">Dari Tanggal</label>
          <input type="date" id="dari" name="dari" class="historidigital-input" value="<?= htmlspecialchars($dari, ENT_QUOTES, 'UTF-8') ?>">
        </div>

        <div class="historidigital-fieldgroup">
          <label class="historidigital-fieldlabel" for="sampai">Sampai Tanggal</label>
          <input type="date" id="sampai" name="sampai" class="historidigital-input" value="<?= htmlspecialchars($sampai, ENT_QUOTES, 'UTF-8') ?>">
        </div>

        <div class="historidigital-actions">
          <button type="submit" class="btn-pilih">Terapkan Filter</button>
          <?php if ($cari !== '' || ! $tanggalDefault) { ?>
            <a href="<?= site_url('manage/historidigital') ?>" class="btn-batal">Reset Filter</a>
          <?php } ?>
        </div>
      </form>
    </div>

    <div class="ppob-card ppob-card-soft">
      <div class="ppob-title-sm">Daftar Transaksi <?= ($totalRows > 0) ? "($totalRows)" : '' ?></div>

      <div class="historidigital-list">
        <?php
        $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'],
        ];
        ?>
        <?php if (count($rows) === 0) { ?>
          <div class="ppob-empty">Tidak ada transaksi yang cocok dengan pencarian/filter Anda.</div>
        <?php } else { ?>
          <?php foreach ($rows as $r) {
              $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;
          ?>
            <a class="historidigital-row" href="<?= site_url('manage/strukdigital/' . $r->id) ?>">
              <div class="historidigital-field">
                <span class="historidigital-label">Nama Produk</span>
                <span class="historidigital-value historidigital-value-strong"><?= htmlspecialchars($namaProduk, ENT_QUOTES, 'UTF-8') ?></span>
              </div>
              <div class="historidigital-field">
                <span class="historidigital-label">Tujuan</span>
                <span class="historidigital-value"><?= htmlspecialchars($r->variasi, ENT_QUOTES, 'UTF-8') ?></span>
              </div>
              <?php if ($isPascabayar && ! empty($r->keterangan)) { ?>
                <div class="historidigital-field">
                  <span class="historidigital-label">Nama</span>
                  <span class="historidigital-value"><?= htmlspecialchars($r->keterangan, ENT_QUOTES, 'UTF-8') ?></span>
                </div>
              <?php } ?>
              <div class="historidigital-field">
                <span class="historidigital-label">Order ID</span>
                <span class="historidigital-value"><?= htmlspecialchars($r->orderid, ENT_QUOTES, 'UTF-8') ?></span>
              </div>
              <div class="historidigital-field">
                <span class="historidigital-label">Tanggal Transaksi</span>
                <span class="historidigital-value"><?= $func->ubahTgl('d M Y H:i', $r->tgl_transaksi) ?> WIB</span>
              </div>
              <div class="historidigital-field">
                <span class="historidigital-label">Harga</span>
                <span class="historidigital-value historidigital-value-price">Rp <?= $func->formUang($r->harga * $r->jumlah) ?></span>
              </div>
              <div class="historidigital-field">
                <span class="historidigital-label">Status</span>
                <span class="status-pill <?= $st['class'] ?>"><?= $st['text'] ?></span>
              </div>
            </a>
          <?php } ?>
        <?php } ?>
      </div>

      <?php if ($totalRows > $perpage) { ?>
        <div class="pagination justify-content-center" style="margin-top:14px">
          <?= $func->createPagination($totalRows, $page, $perpage) ?>
        </div>
      <?php } ?>
    </div>
  </div>
</main>

<script>
  function refreshTabel(page) {
    var params = new URLSearchParams(window.location.search);
    params.set('page', page);
    window.location.href = "<?= site_url('manage/historidigital') ?>?" + params.toString();
  }
</script>
