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

<style>
  .input-nohp{
    width: 100%;
    margin-bottom: 8px;
    padding: 9px 12px;
    border: 1px solid rgba(15, 23, 42, .14);
    border-radius: 10px;
    font-size: 13.5px;
  }
  html[data-theme="dark"] .input-nohp{
    border-color: rgba(255,255,255,.14);
    background: rgba(255,255,255,.04);
    color: #fff;
  }
</style>

<main class="wrap">
  <div class="ppob-page">
    <div class="ppob-card ppob-card-soft">
      <div class="ppob-title-sm">Pilihan Nominal</div>

      <div class="nominal-wrap">
        <?php
        $csrfname = csrf_token();
        $csrfhash = csrf_hash();

        if (! function_exists('renderItemPpobPra')) {
        function renderItemPpobPra($func, $r, $csrfname, $csrfhash)
        {
            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');

            // Versi asli (CI3) submit ke ppob/masukno -- method itu TIDAK PERNAH
            // ada di controller manapun, jadi tombol "Pilih" di halaman ini
            // sebenarnya 404 juga di sistem lama. Diperbaiki di sini dengan
            // submit langsung ke ppob/tambahkeranjangppob (endpoint yang sama
            // dipakai Pulsa/Paket Data/Token PLN), dengan tambahan kolom
            // No HP/ID Akun supaya tetap ada cara mengisi tujuan top up.
            return "
                <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($r->harga) . "</p>
                    <p class='nominal-k'>Status: {$status}</p>
                  </div>
                  <div class='nominal-action'>
                  <form action='" . site_url('ppob/tambahkeranjangppob') . "' method='POST' class='form-pra'>
                  <input type='hidden' name='{$csrfname}' value='{$csrfhash}' />
                  <input type='hidden' name='h2h' value='0' />
                  <input type='hidden' name='idproduk' value='{$r->idproduk}' />
                  <input type='hidden' name='keterangan' value='{$descEsc}' />
                  <input type='hidden' name='hargabeli' value='{$r->harga}' />
                  <input type='hidden' name='hargadata' value='{$r->harga}' />
                  <input type='hidden' name='idpo' value='{$skuEsc}' />
                  <input type='text' name='nohp' class='input-nohp' placeholder='No HP / ID Akun / ID Game' required />
                  {$tombol}
                  </form>
                  </div>
                </div>
                ";
        }
        }

        if (count($grup) === 0) {
            echo "<div class='ppob-empty'>Nominal untuk produk ini belum tersedia.</div>";
        } elseif (count($grup) === 1) {
            foreach (reset($grup) as $item) {
                echo renderItemPpobPra($func, $item, $csrfname, $csrfhash);
            }
        } else {
            echo "<div class='nominal-tabs-wrap'>";
            echo "<div class='nominal-tabs'>";
            $i = 0;
            foreach ($grup as $label => $items) {
                $active = ($i === 0) ? 'active' : '';
                echo "<button type='button' class='nominal-tab {$active}' data-target='nomtab-{$i}'>" . htmlspecialchars($label, ENT_QUOTES, 'UTF-8') . '</button>';
                $i++;
            }
            echo '</div>';
            $i = 0;
            foreach ($grup as $label => $items) {
                $active = ($i === 0) ? 'active' : '';
                echo "<div class='nominal-panel {$active}' id='nomtab-{$i}'>";
                foreach ($items as $item) {
                    echo renderItemPpobPra($func, $item, $csrfname, $csrfhash);
                }
                echo '</div>';
                $i++;
            }
            echo '</div>';
        }
        ?>
      </div>
    </div>
  </div>
</main>
