<?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 = 'UMP'; // 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>
    :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-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; }
    .kos-card { border-radius: 16px; overflow: hidden; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); transition: transform 0.3s ease; margin-bottom: 2rem; }
    .kos-card:hover { transform: translateY(-5px); }
    .kos-image { height: 200px; object-fit: cover; width: 100%; }
    .kos-badge { position: absolute; top: 10px; left: 10px; background-color: #6c757d; color: white; padding: 5px 10px; border-radius: 8px; font-weight: 600; }
    .kos-rating { color: #000; font-weight: 600; }
    .kos-price { color: #e63946; font-weight: 600; font-size: 1.1rem; }
    .kos-location { color: #6c757d; font-size: 0.9rem; }
  </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_ump.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>

  <?php if (!empty($error_message)): ?>
    <div class="container mt-3">
      <div class="alert alert-danger"><?= htmlspecialchars($error_message) ?></div>
    </div>
  <?php endif; ?>

  <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_ump.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>