<?= 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">Blog Terbaru</div>
      </div>

      <div class="ppob-grid" style="gap:16px">
        <?php
        $page = request()->getGet("page") ?: 1;
        $orderby = request()->getGet("orderby") ?: "id DESC";
        $perpage = 12;

        $dbs = $db->table("blog")->get();
        $blogList = $db->table("blog")->limit($perpage, ($page - 1) * $perpage)->orderBy($orderby)->get();

        if ($blogList->getNumRows() > 0) {
          foreach ($blogList->getResult() as $res) {
        ?>
            <a href="<?= site_url('blog/' . $res->url) ?>" class="ppob-grid-card" style="width:220px;align-items:flex-start;text-align:left">
              <div class="ppob-grid-icon" style="width:100%;height:120px;border-radius:12px">
                <img src="<?= base_url("admin/uploads/" . $res->img) ?>" alt="<?= htmlspecialchars($func->potong($res->judul, 40, "..."), ENT_QUOTES, 'UTF-8') ?>" style="width:100%;height:100%;object-fit:cover;border-radius:12px">
              </div>
              <div class="ppob-grid-name" style="white-space:normal;font-size:13.5px"><?= htmlspecialchars($func->potong($res->judul, 60, "..."), ENT_QUOTES, 'UTF-8') ?></div>
              <div class="nominal-k" style="margin:0"><?= htmlspecialchars($func->potong(strip_tags($res->konten), 70, "..."), ENT_QUOTES, 'UTF-8') ?></div>
            </a>
        <?php
          }
        } else {
          echo "<div class='ppob-empty'>Belum ada postingan.</div>";
        }
        ?>
      </div>

      <div class="pagination justify-content-center mt-3">
        <?= $func->createPagination($dbs->getNumRows(), $page, $perpage) ?>
      </div>
    </div>
  </div>
</main>

<script type="text/javascript">
  function refreshTabel(page) {
    window.location.href = "<?= site_url("blog") ?>?page=" + page;
  }
</script>
