<?php
$pageTitle = 'কাপল খুঁজুন';
require_once 'header.php';
$db = getDB();

// AJAX live search
if (isset($_GET['ajax'])) {
    header('Content-Type: application/json');
    $q = '%' . trim($_GET['q'] ?? '') . '%';
    $stmt = $db->prepare("SELECT id, boy_name, girl_name, district FROM couples WHERE status='approved' AND (boy_name LIKE ? OR girl_name LIKE ? OR nickname LIKE ? OR district LIKE ?) LIMIT 8");
    $stmt->execute([$q, $q, $q, $q]);
    echo json_encode($stmt->fetchAll());
    exit;
}

// Full search
$query = trim($_GET['q'] ?? '');
$district = trim($_GET['district'] ?? '');
$year = trim($_GET['year'] ?? '');
$results = [];
$searched = false;

if ($query || $district || $year) {
    $searched = true;
    $sql = "SELECT * FROM couples WHERE status='approved'";
    $params = [];

    if ($query) {
        $sql .= " AND (boy_name LIKE ? OR girl_name LIKE ? OR nickname LIKE ? OR short_story LIKE ?)";
        $like = '%' . $query . '%';
        $params = array_merge($params, [$like, $like, $like, $like]);
    }
    if ($district) {
        $sql .= " AND district = ?";
        $params[] = $district;
    }
    if ($year) {
        $sql .= " AND YEAR(marriage_date) = ?";
        $params[] = $year;
    }

    $sql .= " ORDER BY created_at DESC LIMIT 50";
    $stmt = $db->prepare($sql);
    $stmt->execute($params);
    $results = $stmt->fetchAll();
}
?>

<section class="section" style="padding-top:120px;">
  <div class="container">
    <h2 class="section-title">🔍 কাপল খুঁজুন</h2>

    <!-- Live Search -->
    <div class="search-box" style="margin-bottom:32px;">
      <i class="fas fa-search"></i>
      <input type="text" id="liveSearch" placeholder="নাম, জেলা বা কীওয়ার্ড দিয়ে খুঁজুন..." value="<?= e($query) ?>">
      <div class="search-suggestions" id="searchSuggestions"></div>
    </div>

    <!-- Advanced Filters -->
    <form method="GET" class="glass reveal" style="margin-bottom:40px;">
      <h3 style="margin-bottom:16px;"><i class="fas fa-filter"></i> এডভান্সড ফিল্টার</h3>
      <div class="form-row">
        <div class="form-group">
          <label>নাম / কীওয়ার্ড</label>
          <input type="text" name="q" class="form-control" value="<?= e($query) ?>" placeholder="নাম লিখুন...">
        </div>
        <div class="form-group">
          <label>জেলা</label>
          <select name="district" class="form-control">
            <option value="">— সব জেলা —</option>
            <?php foreach (getDistricts() as $d): ?>
            <option value="<?= e($d) ?>" <?= $district === $d ? 'selected' : '' ?>><?= e($d) ?></option>
            <?php endforeach; ?>
          </select>
        </div>
      </div>
      <div class="form-row">
        <div class="form-group">
          <label>বিয়ের বছর</label>
          <input type="number" name="year" class="form-control" value="<?= e($year) ?>" placeholder="যেমন: ২০২৫" min="2000" max="2030">
        </div>
        <div class="form-group" style="display:flex;align-items:flex-end;">
          <button type="submit" class="btn btn-primary"><i class="fas fa-search"></i> খুঁজুন</button>
        </div>
      </div>
    </form>

    <!-- Results -->
    <?php if ($searched): ?>
    <h3 style="margin-bottom:20px;">📋 ফলাফল: <?= bnNum(count($results)) ?> টি পাওয়া গেছে</h3>
    <?php if ($results): ?>
    <div class="card-grid">
      <?php foreach ($results as $c): ?>
      <a href="couple.php?id=<?= $c['id'] ?>" class="couple-card reveal">
        <?php if ($c['photo']): ?>
          <img class="card-img" src="<?= UPLOAD_URL . e($c['photo']) ?>" alt="<?= e($c['boy_name']) ?>">
        <?php else: ?>
          <div class="card-img" style="background:var(--gradient);display:flex;align-items:center;justify-content:center;font-size:3rem;">💑</div>
        <?php endif; ?>
        <div class="card-body">
          <div class="card-title"><?= e($c['boy_name']) ?> ❤ <?= e($c['girl_name']) ?></div>
          <div class="card-meta">
            <?php if ($c['district']): ?><span><i class="fas fa-map-marker-alt"></i> <?= e($c['district']) ?></span><?php endif; ?>
            <?php if ($c['marriage_date']): ?><span><i class="fas fa-calendar"></i> <?= bnDate($c['marriage_date']) ?></span><?php endif; ?>
          </div>
          <?php if ($c['funny_title']): ?><span class="card-badge"><?= e($c['funny_title']) ?></span><?php endif; ?>
        </div>
      </a>
      <?php endforeach; ?>
    </div>
    <?php else: ?>
    <div class="glass" style="text-align:center;padding:48px;">
      <p style="font-size:3rem;">😢</p>
      <p style="color:var(--text-muted);">কোনো ফলাফল পাওয়া যায়নি। অন্য কীওয়ার্ড দিয়ে চেষ্টা করুন!</p>
    </div>
    <?php endif; ?>
    <?php endif; ?>
  </div>
</section>

<?php require_once 'footer.php'; ?>
