<!DOCTYPE html>
<html lang="en">

<head>
  <title>Daftar Artikel</title>
  <?php include "header.php"; ?>
  <?php
    include "admin/config/koneksi.php";
    error_reporting(E_ALL ^ (E_ALL));
    //
    $kategori = $_GET['kategori'];
    $queryK  = "SELECT * FROM tbl_kategori WHERE nama='".$kategori."'"; 
    $sqlK    = mysqli_query($connection, $queryK); 
    $dataK   = mysqli_fetch_array($sqlK);
    $id_kategori = $dataK['id_kategori'];
    $kategori = $dataK['nama'];
  ?>

</head>

<body>

  <div class="body-inner">
    <!-- Header start -->
    <?php include "logo.php"; ?>
    <!-- Header End -->

    <!-- Main Nav Start -->
    <?php include "menu.php"; ?>
    <!-- Menu wrapper end -->

    <!-- Page Title Start -->
   
    <!-- Page title end -->

    <!-- 1rd Block Wrapper Start -->
    <section class="utf_block_wrapper">
      <div class="container">
        <div class="row">
          <div class="col-lg-8 col-md-12" style=" margin-top: -36px; ">
            <div class="block category-listing category-style2">
              <h3 class="new_look"  style=" color: white; background: #009688; padding: 10px;text-transform: capitalize; "><span><?php echo $kategori; ?></span></h3>

              <?php
              $batas   = 5;
              $halaman = @$_GET['halaman'];
              if (empty($halaman)) {
                $posisi  = 0;
                $halaman = 1;
              } else {
                $posisi  = ($halaman - 1) * $batas;
              }
              
              //
              $query   = "SELECT * FROM tbl_artikel WHERE id_kategori='".$id_kategori."' order by id_artikel desc LIMIT $posisi,$batas";
              $tampil  = mysqli_query($connection, $query);
              $no      = $posisi + 1;
              $angka   = 0;
              while ($data = mysqli_fetch_array($tampil)) {
                $newstext   = substr($data['deskripsi'], 0, 200);  
                $newstext   = html_entity_decode($newstext);
                $newstext   = strip_tags($newstext);
                $last_space = strrpos($newstext, ' ');
                
                if ($last_space !== false) {
                    $newstext = substr($newstext, 0, $last_space);
                }
                
                $angka++; ?>

              <div class="utf_post_block_style post-list clearfix">
                <div class="row">
                  <div class="col-lg-5 col-md-6">
                    <div class="utf_post_thumb thumb-float-style"> <a
                        href="detail.php?id_artikel=<?php echo $data['id_artikel']; ?>">
                        <img class="img-fluid" src="images/artikel/cropped/<?php 
                        $thumbnail_parts = explode('.', $data['thumbnail']);
                        $thumbnail_name = $thumbnail_parts[0]; // nama file
                        $thumbnail_extension = end($thumbnail_parts); // ekstensi file
                        $new_thumbnail_name = $thumbnail_name . '_360x233.' . $thumbnail_extension; 
                        echo $new_thumbnail_name;
                        ?>" alt="" />
                      </a> </div>
                  </div>
                  <div class="col-lg-7 col-md-6">
                    <div class="utf_post_content">
                      <?php
                      // Array asosiatif nama bulan dalam bahasa Indonesia
                      $bulan_indonesia = array(
                        1 => 'Januari',
                        2 => 'Februari',
                        3 => 'Maret',
                        4 => 'April',
                        5 => 'Mei',
                        6 => 'Juni',
                        7 => 'Juli',
                        8 => 'Agustus',
                        9 => 'September',
                        10 => 'Oktober',
                        11 => 'November',
                        12 => 'Desember'
                    );

                    // Mengambil tanggal dari data
                    $tanggal = strtotime($data['tanggal']);
                    $day = date('d', $tanggal); // Ambil hari
                    $month = date('n', $tanggal); // Ambil bulan dalam bentuk angka
                    $year = date('Y', $tanggal); // Ambil tahun

                    // Format tanggal dengan menggunakan nama bulan dalam bahasa Indonesia
                    $tanggal_format = $day . ' ' . $bulan_indonesia[$month] . ' ' . $year;
                    ?>
                     <?php echo htmlspecialchars($tanggal_format, ENT_QUOTES, 'UTF-8'); ?>
                      <h2 class="utf_post_title title-large"> <a
                          href="<?php echo urlencode(strtolower(str_replace(' ', '-', $data['judul_seo']))); ?>"><?php echo $data['judul']; ?></a>
                      </h2>
                      <p><?php echo $newstext; ?></p>
                    </div>
                  </div>
                </div>
              </div>
              <?php $no++;
              } ?>

            </div>
            <?php
            $query2     = mysqli_query($connection, "select * from tbl_artikel WHERE id_kategori='".$id_kategori."'");
            $jmldata    = mysqli_num_rows($query2);
            $jmlhalaman = ceil($jmldata / $batas);
            ?>
            <div class="paging">
              <ul class="pagination">
                <?php
                for ($i = 1; $i <= $jmlhalaman; $i++)
                  if ($i != $halaman) {
                    echo "<li><a style='border-radius: 1px; ' href='daftar-artikel?kategori=".$kategori."&halaman=$i#view'>$i</a></li>";
                  } else {
                    echo "<li class='active'><a href='#' style=' border-radius: 1px; '>$i</a></li>";
                  }
                ?>

              </ul>
            </div>
          </div>

          <?php include "sidebar.php"; ?>
        </div>
      </div>
  </div>
  </div>
  </section>
  <!-- 1rd Block Wrapper End -->

  <!-- Footer Start -->
  <?php include "footer.php"; ?>
  <!-- Footer End -->
  </div>

  <!-- Javascript Files -->
  <script src="js/jquery-3.2.1.min.js"></script>
  <script src="js/bootstrap.min.js"></script>
  <script src="js/owl.carousel.min.js"></script>
  <script src="js/jquery.colorbox.js"></script>
  <script src="js/smoothscroll.js"></script>
  <script src="js/custom_script.js"></script>
  <script>
    /* Loading Js*/
    $(window).on('load', function () {
      setTimeout(function () {
        $(".preloader").delay(700).fadeOut(700).addClass('loaded');
      }, 800);
    });
  </script>
</body>

</html>