<?php require_once __DIR__ . '/../layouts/header.php'; ?>

<div class="tw-relative tw-h-[200px] md:tw-h-[300px] d-flex align-items-center justify-content-center tw-mt-[-80px] tw-pt-[80px]"> <div class="tw-absolute tw-inset-0">
        <img src="https://images.unsplash.com/photo-1560518883-ce09059eeffa?ixlib=rb-4.0.3&auto=format&fit=crop&w=1920&q=80" 
             class="tw-w-full tw-h-full tw-object-cover" alt="Tüm İlanlar">
        <div class="tw-absolute tw-inset-0 tw-bg-gradient-to-b tw-from-[#0f172a]/90 tw-to-[#0f172a]/60"></div>
    </div>
    <div class="container tw-relative tw-z-10 text-center text-white">
        <h1 class="tw-font-black tw-text-3xl md:tw-text-5xl tw-mb-3 tw-tracking-tight">Tüm Gayrimenkuller</h1>
        <p class="tw-text-blue-100 tw-text-sm md:tw-text-base tw-font-medium tw-max-w-xl tw-mx-auto">İhtiyaçlarınıza ve bütçenize en uygun satılık veya kiralık gayrimenkulleri keşfedin.</p>
    </div>
</div>

<div class="tw-bg-white tw-border-b tw-border-gray-100 tw-relative md:tw-sticky tw-top-[76px] tw-z-40 tw-shadow-sm"> <div class="container tw-py-4">
        <form action="<?php echo BASE_URL; ?>/properties" method="GET" class="row g-3 align-items-end">
            
            <div class="col-md-3">
                <label class="tw-text-[10px] tw-font-extrabold tw-text-gray-400 tw-uppercase tw-tracking-widest tw-mb-1.5 tw-px-2">Konum Ara</label>
                <div class="input-group tw-bg-gray-50 tw-rounded-xl tw-border tw-border-gray-100 focus-within:tw-border-blue-300 tw-transition-colors tw-overflow-hidden">
                    <span class="input-group-text bg-transparent border-0 tw-pl-4"><i class="fa-solid fa-location-dot tw-text-blue-500"></i></span>
                    <input type="text" name="location" class="form-control border-0 tw-bg-transparent tw-font-semibold tw-text-sm shadow-none" placeholder="İlçe, mahalle..." value="<?php echo htmlspecialchars($filters['location'] ?? ''); ?>">
                </div>
            </div>

            <div class="col-md-2">
                <label class="tw-text-[10px] tw-font-extrabold tw-text-gray-400 tw-uppercase tw-tracking-widest tw-mb-1.5 tw-px-2">Durum</label>
                <div class="tw-bg-gray-50 tw-rounded-xl tw-border tw-border-gray-100 focus-within:tw-border-blue-300 tw-transition-colors">
                    <select name="category" class="form-select border-0 tw-bg-transparent tw-font-semibold tw-text-sm shadow-none cursor-pointer">
                        <option value="">Fark Etmez</option>
                        <option value="sale" <?php echo (isset($filters['category']) && $filters['category'] == 'sale') ? 'selected' : ''; ?>>Satılık</option>
                        <option value="rent" <?php echo (isset($filters['category']) && $filters['category'] == 'rent') ? 'selected' : ''; ?>>Kiralık</option>
                    </select>
                </div>
            </div>

            <div class="col-md-2">
                <label class="tw-text-[10px] tw-font-extrabold tw-text-gray-400 tw-uppercase tw-tracking-widest tw-mb-1.5 tw-px-2">Emlak Tipi</label>
                <div class="tw-bg-gray-50 tw-rounded-xl tw-border tw-border-gray-100 focus-within:tw-border-blue-300 tw-transition-colors">
                    <select name="type" class="form-select border-0 tw-bg-transparent tw-font-semibold tw-text-sm shadow-none cursor-pointer">
                        <option value="">Tümü</option>
                        <?php if(!empty($categories)): ?>
                            <?php foreach($categories as $cat): ?>
                                <option value="<?php echo $cat->slug; ?>" <?php echo (isset($filters['type']) && $filters['type'] == $cat->slug) ? 'selected' : ''; ?>>
                                    <?php echo htmlspecialchars($cat->name); ?>
                                </option>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </select>
                </div>
            </div>

            <div class="col-md-3">
                <label class="tw-text-[10px] tw-font-extrabold tw-text-gray-400 tw-uppercase tw-tracking-widest tw-mb-1.5 tw-px-2">Fiyat Aralığı (TL)</label>
                <div class="input-group tw-bg-gray-50 tw-rounded-xl tw-border tw-border-gray-100 focus-within:tw-border-blue-300 tw-transition-colors tw-overflow-hidden">
                    <input type="number" name="min_price" class="form-control border-0 tw-bg-transparent tw-font-semibold tw-text-sm shadow-none tw-px-3" placeholder="Min" value="<?php echo htmlspecialchars($filters['min_price'] ?? ''); ?>">
                    <span class="input-group-text tw-bg-gray-100 border-0 tw-text-gray-400 tw-text-xs tw-px-2">-</span>
                    <input type="number" name="max_price" class="form-control border-0 tw-bg-transparent tw-font-semibold tw-text-sm shadow-none tw-px-3" placeholder="Max" value="<?php echo htmlspecialchars($filters['max_price'] ?? ''); ?>">
                </div>
            </div>

            <div class="col-md-2">
                <button type="submit" class="btn tw-bg-blue-600 hover:tw-bg-blue-700 tw-text-white w-100 tw-rounded-xl tw-font-bold tw-py-2.5 tw-shadow-md tw-transition-all hover:tw--translate-y-0.5 tw-text-sm">
                    <i class="fa-solid fa-filter tw-mr-1"></i> Filtrele
                </button>
            </div>

        </form>
    </div>
</div>

<div class="tw-bg-[#f8fafc] tw-py-16 tw-min-h-[50vh]">
    <div class="container">
        
        <div class="d-flex justify-content-between align-items-center tw-mb-8">
            <h2 class="tw-text-xl tw-font-bold tw-text-gray-800 mb-0">
                Arama Sonuçları <span class="tw-text-gray-400 tw-text-base tw-font-medium tw-ml-2">(<?php echo count($properties); ?> İlan bulundu)</span>
            </h2>
            
            <?php if(!empty(array_filter($filters))): ?>
            <a href="<?php echo BASE_URL; ?>/properties" class="tw-text-sm tw-font-semibold tw-text-red-500 hover:tw-text-red-700 text-decoration-none">
                <i class="fa-solid fa-xmark tw-mr-1"></i> Filtreleri Temizle
            </a>
            <?php endif; ?>
        </div>

        <?php if(empty($properties)): ?>
            <div class="text-center tw-py-20">
                <div class="tw-w-24 tw-h-24 tw-bg-white tw-rounded-full d-flex align-items-center justify-content-center tw-mx-auto tw-mb-6 tw-shadow-sm">
                    <i class="fa-solid fa-magnifying-glass-minus tw-text-4xl tw-text-gray-300"></i>
                </div>
                <h3 class="tw-text-gray-800 tw-font-extrabold tw-mb-2">Sonuç Bulunamadı</h3>
                <p class="tw-text-gray-500 tw-max-w-md tw-mx-auto">Arama kriterlerinize uygun bir gayrimenkul bulamadık. Filtreleri değiştirerek yeniden arama yapabilirsiniz.</p>
                <a href="<?php echo BASE_URL; ?>/properties" class="btn tw-bg-white tw-border tw-border-gray-200 tw-text-gray-700 hover:tw-text-blue-600 hover:tw-border-blue-300 tw-rounded-xl tw-font-bold tw-mt-4 tw-px-6 tw-py-2.5 tw-transition-all">
                    Tüm İlanları Göster
                </a>
            </div>
        <?php else: ?>
            
            <div class="row g-4 md:g-5">
                <?php foreach($properties as $prop): 
                    // Veri hazırlığı
                    $feats = json_decode($prop->features ?? '{}');
                    $isSale = ($prop->category == 'sale');
                    $catColor = $isSale ? 'tw-bg-red-500' : 'tw-bg-blue-600';
                    $catText  = $isSale ? 'SATILIK' : 'KİRALIK';
                    $imagePath = !empty($prop->image) ? BASE_URL . '/' . $prop->image : 'https://placehold.co/600x400?text=Gorsel+Yok';
                ?>
                    <div class="col-md-6 col-lg-4">
                        <div class="card property-card h-100 tw-bg-white border-0 tw-rounded-[1.5rem] tw-shadow-sm hover:tw-shadow-2xl tw-transition-all tw-duration-300 hover:tw--translate-y-2 tw-overflow-hidden tw-group">
                            
                            <div class="position-relative img-wrapper tw-h-64 tw-overflow-hidden tw-cursor-pointer tw-p-2 pb-0">
                                <div class="tw-relative tw-h-full tw-w-full tw-rounded-[1rem] tw-overflow-hidden">
                                    <a href="<?php echo BASE_URL; ?>/properties/detail?slug=<?php echo $prop->slug; ?>" class="tw-block tw-h-full tw-w-full">
                                        <img src="<?php echo $imagePath; ?>" class="tw-w-full tw-h-full tw-object-cover tw-transition-transform tw-duration-700 group-hover:tw-scale-110" alt="<?php echo htmlspecialchars($prop->title); ?>">
                                        <div class="tw-absolute tw-inset-0 tw-bg-gradient-to-t tw-from-gray-900/80 tw-via-transparent tw-to-transparent"></div>
                                    </a>
                                    
                                    <span class="tw-absolute tw-top-3 tw-left-3 tw-z-10 <?php echo $catColor; ?> tw-text-white tw-text-[10px] tw-font-black tw-uppercase tw-tracking-widest tw-px-3 tw-py-1.5 tw-rounded-lg tw-shadow-lg">
                                        <?php echo $catText; ?>
                                    </span>

                                    <span class="tw-absolute tw-top-3 tw-right-3 tw-z-10 tw-bg-white/90 backdrop-blur-sm tw-text-gray-800 tw-text-[10px] tw-font-bold tw-uppercase tw-tracking-wider tw-px-3 tw-py-1.5 tw-rounded-lg tw-shadow-sm">
                                        <?php echo htmlspecialchars($prop->type ?? 'İlan'); ?>
                                    </span>
                                    
                                    <div class="tw-absolute tw-bottom-3 tw-left-4 tw-z-10">
                                        <h4 class="tw-text-white tw-font-black tw-mb-0 tw-text-2xl tw-drop-shadow-md">
                                            <?php echo number_format($prop->price, 0, ',', '.'); ?> ₺
                                        </h4>
                                    </div>
                                </div>
                            </div>

                            <div class="card-body tw-p-5 d-flex flex-column">
                                <h5 class="card-title tw-font-extrabold tw-text-lg tw-mb-2 tw-leading-snug line-clamp-2">
                                    <a href="<?php echo BASE_URL; ?>/properties/detail?slug=<?php echo $prop->slug; ?>" class="text-decoration-none text-dark hover:tw-text-blue-600 tw-transition-colors">
                                        <?php echo htmlspecialchars($prop->title); ?>
                                    </a>
                                </h5>
                                
                                <div class="tw-flex tw-items-center tw-mb-4 tw-text-gray-500 tw-text-sm tw-font-medium">
                                    <i class="fa-solid fa-location-dot tw-mr-2 tw-text-gray-400"></i> 
                                    <?php echo htmlspecialchars($prop->location); ?>
                                </div>
                                
                                <div class="tw-mt-auto tw-flex tw-items-center tw-gap-2 tw-flex-wrap">
                                    <?php if(isset($feats->oda) && $feats->oda != ''): ?>
                                    <span class="tw-bg-gray-50 tw-border tw-border-gray-100 tw-text-gray-600 tw-px-3 tw-py-1.5 tw-rounded-lg tw-text-xs tw-font-bold d-flex align-items-center">
                                        <i class="fa-solid fa-bed tw-text-blue-500 tw-mr-1.5"></i> <?php echo htmlspecialchars($feats->oda); ?>
                                    </span>
                                    <?php endif; ?>
                                    
                                    <?php if(isset($feats->banyo) && $feats->banyo != ''): ?>
                                    <span class="tw-bg-gray-50 tw-border tw-border-gray-100 tw-text-gray-600 tw-px-3 tw-py-1.5 tw-rounded-lg tw-text-xs tw-font-bold d-flex align-items-center">
                                        <i class="fa-solid fa-bath tw-text-blue-500 tw-mr-1.5"></i> <?php echo htmlspecialchars($feats->banyo); ?>
                                    </span>
                                    <?php endif; ?>
                                    
                                    <?php if(isset($feats->m2) && $feats->m2 != ''): ?>
                                    <span class="tw-bg-gray-50 tw-border tw-border-gray-100 tw-text-gray-600 tw-px-3 tw-py-1.5 tw-rounded-lg tw-text-xs tw-font-bold d-flex align-items-center">
                                        <i class="fa-solid fa-ruler-combined tw-text-blue-500 tw-mr-1.5"></i> <?php echo htmlspecialchars($feats->m2); ?> m²
                                    </span>
                                    <?php endif; ?>
                                </div>
                            </div>

                            <div class="tw-p-4 tw-px-5 tw-border-t tw-border-gray-50 tw-bg-gray-50/50 d-flex justify-content-between align-items-center">
                                <div class="d-flex align-items-center">
                                    <div class="tw-w-8 tw-h-8 tw-bg-blue-100 tw-text-blue-600 tw-rounded-full d-flex justify-content-center align-items-center tw-font-bold tw-text-xs tw-mr-2">
                                        <i class="fa-regular fa-user"></i>
                                    </div>
                                    <span class="tw-text-xs tw-font-bold tw-text-gray-600 tw-uppercase tw-tracking-wide">Yetkili</span>
                                </div>
                                <a href="<?php echo BASE_URL; ?>/properties/detail?slug=<?php echo $prop->slug; ?>" class="tw-text-sm tw-font-bold tw-text-blue-600 hover:tw-text-blue-800 text-decoration-none tw-flex tw-items-center tw-group-hover:tw-translate-x-1 tw-transition-transform">
                                    İncele <i class="fa-solid fa-arrow-right tw-ml-1"></i>
                                </a>
                            </div>

                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
            
        <?php endif; ?>
    </div>
</div>

<?php require_once __DIR__ . '/../layouts/footer.php'; ?>