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

$kos_manager = null;
$all_kos = [];
$error_message = '';
$search_query = isset($_GET['cari']) ? trim($_GET['cari']) : '';
$kampus_nama = 'UNSOED'; // Nama Kampus

try {
    $kos_manager = new Kos();
    $all_kos = $kos_manager->getKosByKampus($kampus_nama, $search_query);
} catch (Exception $e) {
    error_log("Error di " . basename(__FILE__) . ": " . $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 - Area Kos <?= htmlspecialchars($kampus_nama) ?></title>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
  <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600&display=swap" rel="stylesheet">
  <link href="/style.css" rel="stylesheet"> 
  <style>
    /* ... (salin style CSS dari area_ump.php) ... */
  </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="/area_unsoed.php">
        <input class="form-control search-input" 
               type="search" 
               name="cari" 
               placeholder="Cari kos di sekitar <?= htmlspecialchars($kampus_nama) ?>" 
               value="<?= htmlspecialchars($search_query) ?>">
        <button class="btn btn-success search-btn" type="submit">Cari</button>
      </form>
    </div>
  </nav>

  <div class="container">
    <h2 class="section-title">Kos Area <span style="color: var(--primary-color)"><?= htmlspecialchars($kampus_nama) ?></span></h2>
    
    <?php if (empty($all_kos) && !empty($search_query)): ?>
      <div class="alert alert-info">
        Tidak ditemukan kos yang sesuai dengan pencarian "<?= htmlspecialchars($search_query) ?>".
        <a href="/area_unsoed.php" class="alert-link">Tampilkan semua kos di area ini</a>
      </div>
    <?php elseif (empty($all_kos)): ?>
        <div class="alert alert-info">Belum ada data kos untuk area <?= htmlspecialchars($kampus_nama) ?>.</div>
    <?php else: ?>
      <div class="row">
        <?php foreach ($all_kos as $kos): ?>
          <div class="col-md-4">
            <a href="/detail_kos.php?id=<?= htmlspecialchars($kos['id']) ?>" class="text-decoration-none text-dark">
              <div class="card kos-card h-100">
                <span class="kos-badge"><?= htmlspecialchars($kos['kategori']) ?></span>
                <img src="/assets/images/kos/<?= htmlspecialchars($kos['gambar_utama'] ?? 'default.jpg') ?>" 
                     onerror="this.onerror=null;this.src='/assets/images/default.jpg';" 
                     class="card-img-top kos-image" 
                     alt="<?= htmlspecialchars($kos['nama']) ?>">
                <div class="card-body">
                  <div class="mb-2 kos-rating">
                    ⭐ <?= htmlspecialchars($kos['rating']) ?>
                  </div>
                  <h5 class="card-title"><?= htmlspecialchars($kos['nama']) ?></h5>
                  <p class="card-text kos-location"><?= htmlspecialchars($kos['lokasi']) ?></p>
                  <p class="kos-price"><?= Kos::formatRupiah($kos['harga']) ?>/tahun</p>
                </div>
              </div>
            </a>
          </div>
        <?php endforeach; ?>
      </div>
    <?php endif; ?>
  </div>

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