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

<main class="wrap">

			<div class="slider" id="svcList">
            <?php
                $kategoriDigital = $db->table('produkdigital')->whereIn('idcat', [1, 2])->where('status', 1)->get();

                foreach ($kategoriDigital->getResult() as $res) {
                $url = ($res->idcat == 1)
                    ? site_url("ppob/" . strtolower(trim($res->url)))
                    : site_url("ppob/pascabayar/" . rawurlencode(trim($res->deskripsi)));
            ?>

            <div class="svc" data-name="Layanan" data-key="<?= htmlspecialchars($res->nama, ENT_QUOTES, 'UTF-8') ?>">
                <a href="<?= $url ?>" class="svc-link">
                <div class="svc-ic">
                   <img src="<?= site_url("admin/kategori/" . $res->variasi) ?>" alt="ppob"
                  width="50" height="50" style="object-fit:contain;">
                </div>
                <div class="svc-txt"><?= htmlspecialchars($res->nama, ENT_QUOTES, 'UTF-8') ?></div>
                </a>
             </div>
             <?php } ?>
            </div>

<!-- ================= PRODUK ================= -->
<div class="section" style="margin-top:18px" id="sec-produk">
  <h2 style="font-size:28px;margin-top:0">10 Produk</h2>
  <div class="muted" style="margin-bottom:10px">Produk Best Seller Kami</div>

  <div class="slider" id="productSlider">
    <?php
    // ===== Query produk (logika dari kode lama) =====
    $produkList = $db->table('produk')
        ->where('preorder !=', 1)
        ->where('status', 1)
        ->orderBy('id DESC, tglupdate DESC')
        ->limit(18)
        ->get();

    $totalproduk = 0;
    $level = (int) (session()->get('lvl') ?? 0);

    foreach ($produkList->getResult() as $r) {

      // ===== Harga sesuai level =====
      if ($level == 5)      $hargaDasar = (int)$r->hargadistri;
      elseif ($level == 4)  $hargaDasar = (int)$r->hargaagensp;
      elseif ($level == 3)  $hargaDasar = (int)$r->hargaagen;
      elseif ($level == 2)  $hargaDasar = (int)$r->hargareseller;
      else                  $hargaDasar = (int)$r->harga;

      // ===== Diskon (angka) =====
      $diskon = (int)$r->diskon;

      // ===== Variasi & stok =====
      $variasiList = $db->table('produkvariasi')->where('idproduk', $r->id)->get();

      $totalstok = ($variasiList->getNumRows() > 0) ? 0 : (int)$r->stok;
      $hargaVar  = [];

      foreach ($variasiList->getResult() as $rv) {
        $totalstok += (int)$rv->stok;

        if ($level == 5)      $hargaVar[] = (int)$rv->hargadistri;
        elseif ($level == 4)  $hargaVar[] = (int)$rv->hargaagensp;
        elseif ($level == 3)  $hargaVar[] = (int)$rv->hargaagen;
        elseif ($level == 2)  $hargaVar[] = (int)$rv->hargareseller;
        else                  $hargaVar[] = (int)$rv->harga;
      }

      // ===== Skip stok habis, batasi 10 produk =====
      if ($totalstok <= 0 || $totalproduk >= 10) continue;
      $totalproduk++;

      // ===== Ambil range harga =====
      if (count($hargaVar) > 0) {
        $hargaMin = min($hargaVar);
        $hargaMax = max($hargaVar);
      } else {
        $hargaMin = $hargaMax = $hargaDasar;
      }

      // ===== Hitung harga akhir setelah diskon =====
      if ($diskon > 0) {
        $hargaMinAkhir = (int)round($hargaMin - ($hargaMin * $diskon / 100));
        $hargaMaxAkhir = (int)round($hargaMax - ($hargaMax * $diskon / 100));
      } else {
        $hargaMinAkhir = $hargaMin;
        $hargaMaxAkhir = $hargaMax;
      }

      // ===== Format teks harga =====
      $hargaAwal = ($hargaMin != $hargaMax)
        ? "Rp " . $func->formUang($hargaMin) . " - " . $func->formUang($hargaMax)
        : "Rp " . $func->formUang($hargaMin);

      $hargaAkhir = ($hargaMinAkhir != $hargaMaxAkhir)
        ? "Rp " . $func->formUang($hargaMinAkhir) . " - " . $func->formUang($hargaMaxAkhir)
        : "Rp " . $func->formUang($hargaMinAkhir);

      // ===== Gambar produk =====
      $img = $func->getFoto($r->id, "utama");
      $nama = htmlspecialchars($r->nama, ENT_QUOTES, 'UTF-8');
      $urlProduk = site_url('produk/' . $r->url);
    ?>

      <?php $labelJasa = ($r->jasa == 1) ? "Layanan Jasa" : "Produk Digital"; ?>
      <div class="product" data-service="<?= ($r->jasa == 1) ? 'Jasa' : 'Produk' ?>">
        <div class="pmedia">
          <a href="<?= $urlProduk ?>" class="pimg">
            <img src="<?= $img ?>" alt="<?= $nama ?>" loading="lazy" decoding="async">
          </a>

          <div class="pinfo">
            <p class="pname"><?= $nama ?></p>
            <p class="pmeta"><?= $labelJasa ?></p>
          </div>
        </div>

        <div class="pfooter">
          <div class="price-row">
            <div>
              <?php if ($diskon > 0): ?>
                <div class="muted" style="font-size:12px;text-decoration:line-through;opacity:.85">
                  <?= $hargaAwal ?>
                </div>
              <?php endif; ?>
              <div class="price"><?= $hargaAkhir ?></div>
            </div>
            <div class="muted" style="font-size:12px">• <?= ($r->jasa == 1) ? 'Jasa' : 'Produk' ?></div>
          </div>
        </div>
      </div>

    <?php } ?>

    <?php if ($totalproduk == 0): ?>
      <div class="muted">Produk tidak tersedia</div>
    <?php endif; ?>

  </div>
</div>

</main>
