<?php
require_once __DIR__ . '/../config/Database.php';
require_once __DIR__ . '/../models/Kos.php';

$kos_manager = null;
$all_kos = [];
$error_message = '';
$search = isset($_GET['cari']) ? trim($_GET['cari']) : '';

try {
    $kos_manager = new Kos();
    $all_kos = $kos_manager->getAllKos($search);
    
} catch (Exception $e) {
    error_log("Error in index.php: " . $e->getMessage());
    $error_message = "Terjadi gangguan sistem. Silakan coba lagi nanti.";
}

?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Carikos - Landing Page</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="/style.css" rel="stylesheet"> 
    <style>
        :root { --primary-color: #6a0dad; --secondary-color: #4b0082; }
        body { font-family: 'Poppins', sans-serif; background-color: #f8f9fa; }
        .navbar-brand { font-weight: 600; color: var(--primary-color) !important; font-size: 1.8rem; }
        .search-container { max-width: 600px; box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); border-radius: 12px; }
        .search-input { border: 1px solid #ddd; border-radius: 12px 0 0 12px; padding: 12px 20px; font-size: 1rem; }
        .search-btn { background-color: var(--primary-color); border: none; border-radius: 0 12px 12px 0; padding: 12px 24px; font-weight: 600; }
        .section-title { color: var(--primary-color); font-weight: 600; margin: 2rem 0 1.5rem; }
        .card-kos img {
          height: 200px;
          object-fit: cover;
        }
        .discount-text {
          text-decoration: line-through;
          color: gray;
          font-size: 0.9rem;
        }
        .price-now {
          color: #e53935;
          font-weight: bold;
        }
        .badge-kategori {
          position: absolute;
          top: 10px;
          left: 10px;
        }
        .hero-section {
          background-color: #f8f9fa;
          padding: 3rem 0;
          border-bottom: 1px solid #dee2e6;
        }
        footer {
          background-color: #343a40;
          color: white;
        }
        .footer-links a {
          color: white;
          text-decoration: none;
        }
        .footer-links a:hover {
          text-decoration: underline;
        }
    </style>
</head>
<body>

<nav class="navbar navbar-light bg-light py-1 p-3 sticky-top shadow-sm">
  <div class="container">
    <a class="navbar-brand" href="/index.php">CariKos</a>
    <form class="d-flex w-50" method="GET" action="/index.php">
      <input class="form-control search-input "
      type="search" 
      name="cari" 
      placeholder="Masukkan nama lokasi/area/alamat" 
      value="<?= htmlspecialchars($search) ?>">
      <button class="btn btn-success search-btn" type="submit">Cari</button>
    </form>
  </div>
</nav>

<div id="promoCarousel" class="carousel slide mb-4" data-bs-ride="carousel">
  <div class="carousel-inner">
    <?php
    $active_carousel = 'active';
    foreach ($all_kos as $index => $row) {
        if ($index >= 5) break; 
    ?>
    <div class="carousel-item <?= $active_carousel ?>">
      <img src="/assets/images/kos/<?= htmlspecialchars($row['gambar_utama'] ?? 'default.jpg') ?>" 
           class="d-block w-100" 
           alt="<?= htmlspecialchars($row['nama']) ?>" 
           style="height: 400px; object-fit: cover;"
           onerror="this.onerror=null;this.src='/assets/images/default.jpg';">
      <div class="carousel-caption d-none d-md-block">
        <h5><?= htmlspecialchars($row['nama']) ?></h5>
        <p><?= htmlspecialchars($row['lokasi']) ?> | <?= Kos::formatRupiah($row['harga']) ?></p>
      </div>
    </div>
    <?php
      $active_carousel = '';
    }
    if (empty($all_kos)) {
        echo '<div class="carousel-item active"><img src="https://via.placeholder.com/1400x400?text=Tidak+Ada+Kos+Tersedia" class="d-block w-100" alt="Placeholder" style="height: 400px; object-fit: cover;"></div>';
    }
    ?>
  </div>
  <button class="carousel-control-prev" type="button" data-bs-target="#promoCarousel" data-bs-slide="prev">
    <span class="carousel-control-prev-icon"></span>
  </button>
  <button class="carousel-control-next" type="button" data-bs-target="#promoCarousel" data-bs-slide="next">
    <span class="carousel-control-next-icon"></span>
  </button>
</div>

<div class="container">
    <h2 class="section-title"><span style="color: var(--primary-color)">Rekomendasi Kost</span></h2>
  <div class="row">
    <?php
    if (empty($all_kos)) {
        echo '<div class="col-12"><p class="text-center">Tidak ada kos yang ditemukan.</p></div>';
    } else {
        foreach ($all_kos as $kos):
    ?>
    <div class="col-md-3 mb-4">
      <a href="/detail_kos.php?id=<?= htmlspecialchars($kos['id']) ?>" class="text-decoration-none text-dark">
        <div class="card card-kos h-100 position-relative d-flex flex-column">
          <span class="badge bg-secondary badge-kategori"><?= htmlspecialchars($kos['kategori']) ?></span>
          <img src="/assets/images/kos/<?= htmlspecialchars($kos['gambar_utama'] ?? 'default.jpg') ?>" 
               class="card-img-top" 
               alt="<?= htmlspecialchars($kos['nama']) ?>"
               onerror="this.onerror=null;this.src='/assets/images/default.jpg';">
          <div class="card-body d-flex flex-column">
            <div class="mb-1">
              ⭐ <?= htmlspecialchars($kos['rating']) ?> 
            </div>
            <h6><?= htmlspecialchars($kos['nama']) ?></h6>
            <small class="mb-2"><?= htmlspecialchars($kos['lokasi']) ?></small>
            <div class="mt-auto">
              <div class="price-now"><?= Kos::formatRupiah($kos['harga']) ?> 
                <small>(Per tahun)</small>
              </div>
            </div>
          </div>
        </div>
      </a>
    </div>
    <?php
        endforeach;
    }
    ?>
  </div>
</div>

<div class="container">
    <h2 class="section-title"><span style="color: var(--primary-color)">Area Kost Sekitar Kampus</span></h2>
  
  <div class="row">
    <?php
    $terpopuler = [
      ['nama' => 'UMP', 'gambar' => 'logo_univ/Logo UMP.png', 'link' => 'area_ump.php'],
      ['nama' => 'UNSOED', 'gambar' => 'logo_univ/Logo_Unsoed.png', 'link' => 'area_unsoed.php'],
      ['nama' => 'UHB', 'gambar' => 'logo_univ/logo_UHB.png', 'link' => 'area_uhb.php'],
      ['nama' => 'AMIKOM', 'gambar' => 'logo_univ/Logo_Amikom.png', 'link' => 'area_amikom.php'],
      ['nama' => 'UIN', 'gambar' => 'logo_univ/Logo_UIN.png', 'link' => 'area_uin.php'],
      ['nama' => 'UNWIKU', 'gambar' => 'logo_univ/Logo_Unwiku.png', 'link' => 'area_unwiku.php'],
      ['nama' => 'UNU', 'gambar' => 'logo_univ/Logo_Unu.jpg', 'link' => 'area_unu.php'],
      ['nama' => 'ITTP', 'gambar' => 'logo_univ/Logo_Telkom.png', 'link' => 'area_ittp.php'],
      ['nama' => 'BSI', 'gambar' => 'logo_univ/Logo_BSI.png', 'link' => 'area_bsi.php'],
    ];
    foreach($terpopuler as $area) {
    ?>
    <div class="col-md-4 mb-4">
      <a href="/<?= htmlspecialchars($area['link']) ?>" class="text-decoration-none">
        <div class="card border-0">
          <div class="position-relative">
            <img src="/assets/images/<?= htmlspecialchars($area['gambar']) ?>" 
     class="card-img-top p-3 bg-white" 
     alt="<?= htmlspecialchars($area['nama']) ?>" 
     style="height: 180px; object-fit: contain; border-radius: 8px;" 
     onerror="this.onerror=null;this.src='/assets/images/logo_univ';">

            <div class="position-absolute bottom-0 start-0 p-3 w-100" style="background: linear-gradient(transparent, rgba(0,0,0,0.7)); border-radius: 0 0 8px 8px;">
              <h5 class="card-title text-white mb-0"><?= htmlspecialchars($area['nama']) ?></h5>
            </div>
          </div>
        </div>
      </a>
    </div>
    <?php } ?>
  </div>
</div>

<div class="hero-section" id="tentangkami">
  <div class="container">
    <h2 class="text-center mb-4">CariKos - Website  Anak Kos di Banyumas</h2>
    <p class="lead text-center">
      CariKos adalah sebuah platform digital yang dirancang khusus untuk membantu masyarakat dalam menemukan kos-kosan terbaik di wilayah Kabupaten Banyumas. Kami memahami bahwa mencari tempat tinggal yang nyaman, aman, dan sesuai kebutuhan bisa menjadi tantangan, terutama bagi mahasiswa, pekerja, atau pendatang baru di daerah ini. Oleh karena itu, CariKos hadir sebagai solusi praktis dan terpercaya. Dengan menghadirkan berbagai pilihan kos yang telah diverifikasi, lengkap dengan informasi harga, fasilitas dan lokasi, kami berkomitmen untuk memberikan pengalaman pencarian kos yang mudah, cepat, dan transparan.. Kami percaya bahwa tempat tinggal yang tepat akan menunjang kenyamanan hidup dan produktivitas penghuninya. Karena itu, misi kami adalah menjadi jembatan antara pemilik kos dan pencari kos dengan layanan yang akurat, ramah, dan selalu diperbarui.</p>
  </div>
</div>

<footer class="py-3">
  <div class="container">
    <div class="row">

      <div class="col-md-12">
        <div class="row">
          <div class="col-md-3 mb-4">
            <h5 class="mb-3">CARIKOS</h5>
            <ul class="list-unstyled footer-links">
              <li class="mb-2"><a href="#tentangkami">Tentang Kami</a></li>
              <li class="mb-2"><a href="https://wa.me/6283869816926" target="_blank" rel="noopener noreferrer">Promosikan Kost Anda</a></li>
              <li class="mb-2"><a href="https://wa.me/6283869816926" target="_blank" rel="noopener noreferrer">Pusat Bantuan</a></li>
              <li class="mb-2"><a href="developers.php" target="_blank" rel="noopener noreferrer">Developers</a></li>
            </ul>
          </div>
          <div class="col-md-3 mb-4">
            <h5 class="mb-3">KEBIJAKAN</h5>
            <ul class="list-unstyled footer-links">
              <li class="mb-2"><a href="/kebijakan_privasi.php">Kebijakan Privasi</a></li>
              <li class="mb-2"><a href="/s_k.php">Syarat dan Ketentuan Umum</a></li>
            </ul>
          </div>
          <div class="col-md-3 mb-4">
  <h5 class="mb-3">DEVELOPERS</h5>
  <ul class="list-unstyled footer-links">
    <li class="mb-2"><a href="https://zaidan243.devport.my.id/" target="_blank">Zaidan Umar</a></li>
    <li class="mb-2"><a href="https://sugeng278.devport.my.id/" target="_blank">Sugeng Pambudi</a></li>
    <li class="mb-2"><a href="https://devy245.devport.my.id/" target="_blank">Devy Lusiana</a></li>
    <li class="mb-2"><a href="https://aisyah270.devport.my.id/" target="_blank">Aisyah Nur Ramadhani</a></li>
  </ul>
</div>
          <div class="col-md-3 mb-4">
            <h5 class="mb-3">HUBUNGI KAMI</h5>
            <ul class="list-unstyled footer-links">
              <li class="mb-2"><a href="mailto:carikos@gmail.com">carikos@gmail.com</a></li>
              <li class="mb-2"><a href="https://wa.me/6282225634660" target="_blank" rel="noopener noreferrer">+62 82225634660 (Zaidan)</a></li>
              <li class="mb-2"><a href="https://wa.me/6283869816926" target="_blank" rel="noopener noreferrer">+62 83869816926 (Sugeng)</a></li>
              <li class="mb-2"><a href="https://wa.me/6282136068560" target="_blank" rel="noopener noreferrer">+62 82136068560 (Devy)</a></li>
              <li class="mb-2"><a href="https://wa.me/6285721681375" target="_blank" rel="noopener noreferrer">+62 85721681375 (Aisyah)</a></li>
            </ul>
          </div>
        </div>
      </div>
    </div>
    
    <div class="border-top pt-3 mt-3">
      <div class="row">
        <div class="col-md-12 text-md-end">
          <small>© 2025 carikos.devport.my.id All rights reserved</small>
        </div>
      </div>
    </div>
  </div>
</footer>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>