<?php
include '../koneksi.php';

$queryartikel = "SELECT * FROM detail_artikel ORDER BY tanggal DESC LIMIT 5"; 
$result = $conn->query($queryartikel);

$limit = 5; 

$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$page = max($page, 1); // Pastikan halaman minimal 1
$offset = ($page - 1) * $limit;

$search = isset($_GET['q']) ? trim($_GET['q']) : '';

if (!empty($search)) {
    $totalQuery = "SELECT COUNT(*) AS total FROM detail_artikel WHERE judul_artikel LIKE ? OR deskripsi_artikel LIKE ?";
    $stmtTotal = $conn->prepare($totalQuery);
    $likeSearch = "%$search%";
    $stmtTotal->bind_param("ss", $likeSearch, $likeSearch);
    $stmtTotal->execute();
    $resultTotal = $stmtTotal->get_result();
    $totalRow = $resultTotal->fetch_assoc();
    $totalArticles = $totalRow['total'];
    $stmtTotal->close();

    $queryartikelall = "SELECT * FROM detail_artikel WHERE judul_artikel LIKE ? OR deskripsi_artikel LIKE ? ORDER BY tanggal DESC LIMIT ? OFFSET ?";
    $stmt = $conn->prepare($queryartikelall);
    $stmt->bind_param("ssii", $likeSearch, $likeSearch, $limit, $offset);
} else {
    $totalQuery = "SELECT COUNT(*) AS total FROM detail_artikel";
    $totalResult = $conn->query($totalQuery);
    $totalRow = $totalResult->fetch_assoc();
    $totalArticles = $totalRow['total'];

    $queryartikelall = "SELECT * FROM detail_artikel ORDER BY tanggal DESC LIMIT ? OFFSET ?";
    $stmt = $conn->prepare($queryartikelall);
    $stmt->bind_param("ii", $limit, $offset);
}

$totalPages = ceil($totalArticles / $limit);

$stmt->execute();
$resultall = $stmt->get_result();

$stmt->close();
$conn->close();
?>


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

<head>
  <meta charset="utf-8">
  <meta content="width=device-width, initial-scale=1.0" name="viewport">

  <title>Pencarian untuk: <?php echo htmlspecialchars($search); ?> | Portal UMKM</title>
  <meta content="" name="description">
  <meta content="" name="keywords">

  <!-- Favicons -->
  <link href="../assets/img/favicon.png" rel="icon">
  <link href="../assets/img/apple-touch-icon.png" rel="apple-touch-icon">

  <!-- Google Fonts -->
  <link href="https://fonts.googleapis.com/css?family=Open+Sans:300,300i,400,400i,600,600i,700,700i|Nunito:300,300i,400,400i,600,600i,700,700i|Poppins:300,300i,400,400i,500,500i,600,600i,700,700i" rel="stylesheet">

  <!-- Vendor CSS Files -->
  <link href="../assets/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
  <link href="../assets/vendor/bootstrap-icons/bootstrap-icons.css" rel="stylesheet">

  <!-- Template Main CSS File -->
  <link href="../assets/css/style.css" rel="stylesheet">

</head>

<body>

    <!-- ======= Header ======= -->
    <header id="header" class="header fixed-top">
      <div class="container-fluid container-xl d-flex align-items-center justify-content-between">
  
        <a href="/" class="logo d-flex align-items-center">
          <img src="../assets/img/logo.png" alt="Portal UMKM">
          <span>Portal UMKM</span>
        </a>
  
        <nav id="navbar" class="navbar">
          <ul>
            <li><a class="nav-link scrollto" href="/#beranda">Beranda</a></li>
            <li><a class="nav-link scrollto" href="/#keunggulan">Keunggulan</a></li>
            <li><a class="nav-link scrollto" href="/#langkah">Langkah Memulai</a></li>
            <li><a class="nav-link scrollto" href="/#pertanyaan">Pertanyaan</a></li>
            <li><a class="active" href="../blog">Blog</a></li>
            <li><a class="getstarted scrollto" href="../login">Masuk</a></li>
            <!--<li id="google_translate_element"  title="Ubah Bahasa" class="ms-3"></li>-->
          </ul>
          <i class="bi bi-list mobile-nav-toggle"></i>
        </nav><!-- .navbar -->
  
      </div>
    </header><!-- End Header -->

  <main id="main">

    <!-- ======= Breadcrumbs ======= -->
    <section class="breadcrumbs">
      <div class="container">

        <ol>
          <li><a href="/">Beranda</a></li>
          <li><a href="../blog">Blog</a></li>
        </ol>
        <h1>Pencarian untuk: <?php echo htmlspecialchars($search); ?></h1>

      </div>
    </section><!-- End Breadcrumbs -->

    <!-- ======= Blog Section ======= -->
    <section id="blog" class="blog">
      <div class="container">

        <div class="row">

        <div class="col-lg-8 entries">
        <?php if ($resultall->num_rows > 0) { ?>
              <?php while ($row = $resultall->fetch_assoc()) { ?>
                  <article class="entry">
                      <div class="entry-img">
                      <img src="../uploads/artikel/<?= !empty($row['gambar_artikel']) ? htmlspecialchars($row['gambar_artikel']) : ''; ?>" alt="<?= htmlspecialchars($row['judul_artikel']); ?>" class="img-fluid"  style="width: 100%; height: 450px; object-fit: cover;">
                      </div>

                      <h2 class="entry-title">
                          <a href="../blog/<?= htmlspecialchars($row['slug']); ?>"><?= htmlspecialchars($row['judul_artikel']); ?></a>
                      </h2>

                      <div class="entry-meta">
                          <ul>
                              <li class="d-flex align-items-center"><i class="bi bi-person"></i> 
                                  Admin
                              </li>
                              <li class="d-flex align-items-center"><i class="bi bi-clock"></i> 
                                  <time datetime="<?= $row['tanggal']; ?>">
                                      <?= date("d M Y", strtotime($row['tanggal'])); ?>
                                  </time>
                              </li>
                          </ul>
                      </div>

                      <div class="entry-content">
                          <p>
                              <?= substr($row['deskripsi_artikel'], 0, 200) . '...'; ?>
                          </p>
                          <div class="read-more">
                              <a href="../blog/<?= htmlspecialchars($row['slug']); ?>">Baca Selengkapnya</a>
                          </div>
                      </div>
                  </article><!-- End blog entry -->
                  <?php } ?>
                  <?php } else { ?>
                    <div class="text-center py-5">
                        <i class="bi bi-file-earmark-text fs-1 text-muted"></i>
                        <h5 class="mt-3 text-muted">Oops! Tidak ada artikel yang ditemukan.</h5>
                        <p class="text-muted">Coba cari dengan kata kunci lain atau kembali ke <a href="../blog" class="text-primary fw-bold">halaman utama blog</a>.</p>
                    </div>
                  <?php } ?>

              <!-- Pagination -->
              <div class="blog-pagination">
                  <ul class="justify-content-center">
                      <?php if ($page > 1) { ?>
                          <li><a href="?page=<?= $page - 1 ?>">&laquo; Prev</a></li>
                      <?php } ?>

                      <?php for ($i = 1; $i <= $totalPages; $i++) { ?>
                          <li class="<?= ($i == $page) ? 'active' : ''; ?>">
                              <a href="?page=<?= $i ?>"><?= $i ?></a>
                          </li>
                      <?php } ?>

                      <?php if ($page < $totalPages) { ?>
                          <li><a href="?page=<?= $page + 1 ?>">Next &raquo;</a></li>
                      <?php } ?>
                  </ul>
              </div>
          </div>

          <div class="col-lg-4 px-0 mt-3 mt-lg-0">

            <div class="sidebar">

            <h3 class="sidebar-title">Cari</h3>
            <div class="sidebar-item search-form">
                <form action="search.php" method="GET">
                    <input type="text" name="q" placeholder="Cari artikel..." required>
                    <button type="submit"><i class="bi bi-search"></i></button>
                </form>
            </div>

              <h3 class="sidebar-title">Artikel Terbaru</h3>
              <div class="sidebar-item recent-posts">
                
              <?php while ($row = $result->fetch_assoc()) { ?>
                  <div class="post-item clearfix">
                      <a href="../blog/<?= htmlspecialchars($row['slug']); ?>">
                        <img src="../uploads/artikel/<?= !empty($row['gambar_artikel']) ? htmlspecialchars($row['gambar_artikel']) : ''; ?>" alt="<?= htmlspecialchars($row['judul_artikel']); ?>">
                        <h4><?= htmlspecialchars($row['judul_artikel']); ?></h4>
                        <time datetime="<?= $row['tanggal']; ?>"><?= date("d M Y", strtotime($row['tanggal'])); ?></time>
                      </a>
                  </div>
              <?php } ?>

              </div><!-- End sidebar recent posts-->

            </div><!-- End sidebar -->

          </div><!-- End blog sidebar -->

        </div>

      </div>
    </section><!-- End Blog Section -->

  </main><!-- End #main -->

  <!-- ======= Footer ======= -->
  <footer id="footer" class="footer">

    <div class="footer-top">
      <div class="container">
        <div class="row gy-4">
          <div class="col-lg-5 col-md-12 footer-info">
            <a href="/" class="logo d-flex align-items-center">
              <img src="../assets/img/logo.png" alt="Portal UMKM">
              <span>Portal UMKM</span>
            </a>
            <p>PortalUMKM.com adalah platform gratis untuk membuat landing page profesional bagi pelaku UMKM di Indonesia.            </p>
          </div>

          <div class="col-lg-4 col-6 footer-links">
            <h4>Halaman</h4>
            <ul>
             <li><i class="bi bi-chevron-right"></i> <a href="/">Beranda</a></li>
              <li><i class="bi bi-chevron-right"></i> <a href="../blog/">Blog</a></li>
              <li><i class="bi bi-chevron-right"></i> <a href="../blog/syarat-dan-ketentuan">Syarat dan Ketentuan</a></li>            
            </ul>
          </div>

          <div class="col-lg-3 col-md-12 footer-contact text-start">
            <h4 class="mb-3">Kontak</h4>
            <p>
                JL KH Wahid Hasyim
                Jawa Timur, 64483<br>
                Indonesia
            </p>
            <p>
                <strong>WA:</strong> <a href="https://wa.me/6288805517338" class="text-decoration-none text-black">+62 888 0551 7338</a><br>
                <strong>Email:</strong> <a href="mailto:ahmadansori1111@gmail.com" class="text-decoration-none text-black">ahmadansori1111@gmail.com</a>
            </p>
        </div>
        </div>
      </div>
    </div>

    <div class="container">
      <div class="copyright">
        &copy; <?php echo date("Y"); ?> <strong><span>Portal UMKM</span></strong>. All Rights Reserved
      </div>
     
    </div>
  </footer><!-- End Footer -->


  <a href="#" class="back-to-top d-flex align-items-center justify-content-center"><i class="bi bi-arrow-up-short"></i></a>

  <!-- Vendor JS Files -->
   <script src="../assets/vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
  <script type="text/javascript" src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>

  <!-- Template Main JS File -->
  <script src="../assets/js/main.js"></script>
  <script src="../assets/js/custom.js"></script>

</body>

</html>