<?php
$csrfname = csrf_token();
$csrfhash = csrf_hash();

if (! function_exists('renderItemPpob')) {
function renderItemPpob($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');
    $keteranganEsc  = htmlspecialchars($r->desc, ENT_QUOTES, 'UTF-8');

    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: {$keteranganEsc}</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'>
            <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='hargabeli' value='{$r->harga}' />
            <input type='hidden' name='keterangan' value='{$keteranganEsc}' />
            <input type='hidden' name='idpo' value='{$skuEsc}' />
            {$tombol}
            </form>
            </div>
        </div>
        ";
}
}

if (count($grup) === 0) {
    echo "<div class='ppob-empty'>Nominal untuk pilihan ini belum tersedia.</div>";
} elseif (count($grup) === 1) {
    foreach (reset($grup) as $item) {
        echo renderItemPpob($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 renderItemPpob($func, $item, $csrfname, $csrfhash);
        }
        echo '</div>';
        $i++;
    }
    echo '</div>';
}
