<?= 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"><?= htmlspecialchars(str_replace('%20', ' ', $jenispasca), ENT_QUOTES, 'UTF-8') ?></div>
        <div class="ppob-subtitle">Pilih produk di bawah untuk melanjutkan cek tagihan.</div>
      </div>
    </div>

    <div class="ppob-card ppob-card-soft">
      <div class="ppob-title-sm">Pilihan Produk</div>

      <div class="nominal-wrap">
        <?php
        $csrfname = csrf_token();
        $csrfhash = csrf_hash();
        $idproduk = null;
        $set = $func->getSetting("semua");
        $adaProduk = false;

        if ($set->jalur_h2h == 1) {
          $datalist = $db->table("h2h")->get()->getResult();
          foreach ($datalist as $r) {
            $brand = str_replace("%20", " ", $jenispasca);
            $pro = $db->table("produkdigital")->where("deskripsi", $brand)->limit(1)->get()->getRow();
            if ($pro !== null) {
              $idproduk = $pro->id;
            }

            if ($r->brand == $brand) {
              $adaProduk = true;

              if ($r->buyer_product_status == 1) {
                $status = "<span class='status-pill status-on'>ON</span>";
                $tombol = "<button type='submit' class='btn-pilih'>Pilih</button>";
              } else {
                $status = "<span class='status-pill status-off'>OFF</span>";
                $tombol = "";
              }
              $skuEsc = htmlspecialchars($r->buyer_sku_code, ENT_QUOTES, 'UTF-8');
              $categoryEsc = htmlspecialchars($r->category, ENT_QUOTES, 'UTF-8');
              $productNameEsc = htmlspecialchars($r->product_name, ENT_QUOTES, 'UTF-8');
              $descEsc = htmlspecialchars($r->desc, ENT_QUOTES, 'UTF-8');
              echo "
                  <div class='nominal-item'>
                    <div class='nominal-info'>
                      <p class='nominal-k'>Kode: $skuEsc $categoryEsc</p>
                      <p class='nominal-k'>Produk: $productNameEsc</p>
                      <p class='nominal-k'>Keterangan: $descEsc</p>
                      <p class='nominal-harga'>Harga: Rp " . $func->formUang($hargapulsa ?? 0) . "</p>
                      <p class='nominal-k'>Status: $status</p>
                    </div>
                    <div class='nominal-action'>
                    <form action='" . site_url('ppob/cektagihan') . "' method='POST' >
                    <input type='hidden' name='$csrfname' value='$csrfhash' />
                    <input type='hidden' name='h2h' value='1' />
                    <input type='hidden' name='idproduk' value='$idproduk' />
                    <input type='hidden' name='keterangan' value='$descEsc' />
                    <input type='hidden' name='produk' value='$productNameEsc' />
                    <input type='hidden' name='kode' value='$skuEsc' />
                    $tombol
                    </form>
                    </div>
                  </div>
                  ";
            }
          }
        }
        if ($set->jalur_digiflazz == 1 && isset($data->data) && is_array($data->data)) {
          foreach ($data->data as $r) {
            $brand = str_replace("%20", " ", $jenispasca);
            $pro = $db->table("produkdigital")->where("deskripsi", $brand)->limit(1)->get()->getRow();
            if ($pro !== null) {
              $idproduk = $pro->id;
            }

            if ($r->category == "Pascabayar" and $r->brand == $brand) {
              $adaProduk = true;

              if ($r->buyer_product_status == 1) {
                $status = "<span class='status-pill status-on'>ON</span>";
                $tombol = "<button type='submit' class='btn-pilih'>Pilih</button>";
              } else {
                $status = "<span class='status-pill status-off'>OFF</span>";
                $tombol = "";
              }
              $skuEsc = htmlspecialchars($r->buyer_sku_code, ENT_QUOTES, 'UTF-8');
              $categoryEsc = htmlspecialchars($r->category, ENT_QUOTES, 'UTF-8');
              $productNameEsc = htmlspecialchars($r->product_name, ENT_QUOTES, 'UTF-8');
              $descEsc = htmlspecialchars($r->desc, ENT_QUOTES, 'UTF-8');
              echo "
                  <div class='nominal-item'>
                    <div class='nominal-info'>
                      <p class='nominal-k'>Kode: $skuEsc $categoryEsc</p>
                      <p class='nominal-k'>Produk: $productNameEsc</p>
                      <p class='nominal-k'>Keterangan: $descEsc</p>
                      <p class='nominal-k'>Status: $status</p>
                    </div>
                    <div class='nominal-action'>
                    <form action='" . site_url('ppob/cektagihan') . "' method='POST' >
                    <input type='hidden' name='$csrfname' value='$csrfhash' />
                    <input type='hidden' name='h2h' value='0' />
                    <input type='hidden' name='idproduk' value='$idproduk' />
                    <input type='hidden' name='keterangan' value='$descEsc' />
                    <input type='hidden' name='produk' value='$productNameEsc' />
                    <input type='hidden' name='kode' value='$skuEsc' />
                    $tombol
                    </form>
                    </div>
                  </div>
                  ";
            }
          }
        }

        if (!$adaProduk) {
          echo "<div class='ppob-empty'>Produk untuk kategori ini belum tersedia.</div>";
        }
        ?>
      </div>
    </div>
  </div>
</main>
