<div class="d-flex justify-content-between align-items-center tw-mb-8 flex-wrap tw-gap-4">
    <div>
        <h3 class="tw-font-black tw-text-2xl tw-text-gray-900 tw-mb-1 tw-tracking-tight">
            Hoş geldin, <?php echo $_SESSION['admin_name'] ?? 'Yönetici'; ?> 👋
        </h3>
        <p class="tw-text-gray-500 tw-text-sm tw-font-medium tw-mb-0">İşte emlak portföyünün bugünkü durumu.</p>
    </div>
    <div>
        <a href="<?php echo BASE_URL; ?>/admin/property_add" class="btn tw-bg-blue-600 hover:tw-bg-blue-700 tw-text-white tw-rounded-xl tw-font-bold tw-shadow-lg tw-shadow-blue-500/30 tw-transition-all hover:tw--translate-y-0.5 tw-px-5 tw-py-2.5 d-flex align-items-center">
            <i class="fa-solid fa-plus tw-mr-2"></i> Yeni İlan Ekle
        </a>
    </div>
</div>

<div class="row g-4 tw-mb-8">
    <div class="col-md-6 col-xl-3">
        <div class="tw-bg-white tw-rounded-[1.5rem] tw-shadow-sm hover:tw-shadow-md tw-border tw-border-gray-100 tw-p-6 tw-transition-all tw-duration-300 hover:tw--translate-y-1 tw-h-full d-flex align-items-center justify-content-between tw-group">
            <div>
                <p class="tw-text-gray-400 tw-text-[10px] tw-font-extrabold tw-uppercase tw-tracking-widest tw-mb-1">TOPLAM PORTFÖY</p>
                <h2 class="tw-text-3xl tw-font-black tw-text-gray-900 tw-mb-0"><?php echo $stats['total_properties']; ?></h2>
            </div>
            <div class="tw-w-14 tw-h-14 tw-bg-blue-50 tw-text-blue-600 tw-rounded-2xl d-flex align-items-center justify-content-center tw-text-2xl group-hover:tw-scale-110 tw-transition-transform">
                <i class="fa-solid fa-building"></i>
            </div>
        </div>
    </div>
    
    <div class="col-md-6 col-xl-3">
        <div class="tw-bg-white tw-rounded-[1.5rem] tw-shadow-sm hover:tw-shadow-md tw-border tw-border-gray-100 tw-p-6 tw-transition-all tw-duration-300 hover:tw--translate-y-1 tw-h-full d-flex align-items-center justify-content-between tw-group">
            <div>
                <p class="tw-text-gray-400 tw-text-[10px] tw-font-extrabold tw-uppercase tw-tracking-widest tw-mb-1">BEKLEYEN MESAJ</p>
                <h2 class="tw-text-3xl tw-font-black <?php echo $stats['unread_messages'] > 0 ? 'tw-text-red-500' : 'tw-text-gray-900'; ?> tw-mb-0">
                    <?php echo $stats['unread_messages']; ?>
                </h2>
            </div>
            <div class="tw-w-14 tw-h-14 tw-bg-red-50 tw-text-red-500 tw-rounded-2xl d-flex align-items-center justify-content-center tw-text-2xl position-relative group-hover:tw-scale-110 tw-transition-transform">
                <i class="fa-solid fa-envelope"></i>
                <?php if($stats['unread_messages'] > 0): ?>
                    <span class="position-absolute tw-top-3 tw-right-3 tw-w-3 tw-h-3 tw-bg-red-500 tw-rounded-full tw-border-2 tw-border-white tw-shadow-sm"></span>
                <?php endif; ?>
            </div>
        </div>
    </div>

    <div class="col-md-6 col-xl-3">
        <div class="tw-bg-white tw-rounded-[1.5rem] tw-shadow-sm hover:tw-shadow-md tw-border tw-border-gray-100 tw-p-6 tw-transition-all tw-duration-300 hover:tw--translate-y-1 tw-h-full d-flex align-items-center justify-content-between tw-group">
            <div>
                <p class="tw-text-gray-400 tw-text-[10px] tw-font-extrabold tw-uppercase tw-tracking-widest tw-mb-1">DANIŞMANLAR</p>
                <h2 class="tw-text-3xl tw-font-black tw-text-gray-900 tw-mb-0"><?php echo $stats['total_agents']; ?></h2>
            </div>
            <div class="tw-w-14 tw-h-14 tw-bg-emerald-50 tw-text-emerald-500 tw-rounded-2xl d-flex align-items-center justify-content-center tw-text-2xl group-hover:tw-scale-110 tw-transition-transform">
                <i class="fa-solid fa-users"></i>
            </div>
        </div>
    </div>

    <div class="col-md-6 col-xl-3">
        <div class="tw-bg-white tw-rounded-[1.5rem] tw-shadow-sm hover:tw-shadow-md tw-border tw-border-gray-100 tw-p-6 tw-transition-all tw-duration-300 hover:tw--translate-y-1 tw-h-full d-flex align-items-center justify-content-between tw-group">
            <div>
                <p class="tw-text-gray-400 tw-text-[10px] tw-font-extrabold tw-uppercase tw-tracking-widest tw-mb-1">GÖRÜNTÜLENME</p>
                <h2 class="tw-text-3xl tw-font-black tw-text-gray-900 tw-mb-0"><?php echo $stats['monthly_views']; ?></h2>
            </div>
            <div class="tw-w-14 tw-h-14 tw-bg-purple-50 tw-text-purple-500 tw-rounded-2xl d-flex align-items-center justify-content-center tw-text-2xl group-hover:tw-scale-110 tw-transition-transform">
                <i class="fa-solid fa-chart-line"></i>
            </div>
        </div>
    </div>
</div>

<div class="row g-4">
    
    <div class="col-lg-8">
        <div class="tw-bg-white tw-rounded-[1.5rem] tw-shadow-sm tw-border tw-border-gray-100 h-100 tw-overflow-hidden d-flex flex-column">
            
            <div class="tw-p-6 tw-border-b tw-border-gray-100 d-flex justify-content-between align-items-center">
                <div class="d-flex align-items-center">
                    <div class="tw-w-10 tw-h-10 tw-bg-blue-50 tw-text-blue-600 tw-rounded-xl d-flex align-items-center justify-content-center tw-mr-3">
                        <i class="fa-solid fa-list tw-text-lg"></i>
                    </div>
                    <h5 class="tw-font-black tw-text-lg tw-text-gray-800 mb-0">Son Eklenen İlanlar</h5>
                </div>
                <a href="<?php echo BASE_URL; ?>/admin/properties" class="btn tw-bg-gray-50 hover:tw-bg-gray-100 tw-text-blue-600 tw-font-bold tw-text-xs tw-rounded-lg tw-px-4 tw-py-2 tw-transition-colors">
                    Tümünü Yönet
                </a>
            </div>
            
            <div class="table-responsive tw-p-2 tw-flex-grow-1">
                <table class="table align-middle tw-mb-0 tw-border-transparent">
                    <thead>
                        <tr>
                            <th class="tw-bg-transparent tw-text-gray-400 tw-text-[10px] tw-font-extrabold tw-uppercase tw-tracking-widest tw-pb-3 tw-pl-4 tw-border-b-2 tw-border-gray-50">İlan Bilgisi</th>
                            <th class="tw-bg-transparent tw-text-gray-400 tw-text-[10px] tw-font-extrabold tw-uppercase tw-tracking-widest tw-pb-3 tw-border-b-2 tw-border-gray-50">Fiyat</th>
                            <th class="tw-bg-transparent tw-text-gray-400 tw-text-[10px] tw-font-extrabold tw-uppercase tw-tracking-widest tw-pb-3 tw-border-b-2 tw-border-gray-50">Konum</th>
                            <th class="tw-bg-transparent tw-text-gray-400 tw-text-[10px] tw-font-extrabold tw-uppercase tw-tracking-widest tw-pb-3 tw-border-b-2 tw-border-gray-50">Kategori</th>
                            <th class="tw-bg-transparent tw-text-gray-400 tw-text-[10px] tw-font-extrabold tw-uppercase tw-tracking-widest tw-pb-3 tw-pr-4 tw-border-b-2 tw-border-gray-50 text-end">İşlem</th>
                        </tr>
                    </thead>
                    <tbody class="tw-divide-y tw-divide-gray-50">
                        <?php if (!empty($latest_properties)): ?>
                            <?php foreach ($latest_properties as $prop): ?>
                            <tr class="hover:tw-bg-gray-50/80 tw-transition-colors">
                                <td class="tw-pl-4 tw-py-3">
                                    <div class="d-flex align-items-center">
                                        <div class="tw-w-12 tw-h-12 tw-rounded-xl tw-overflow-hidden tw-flex-shrink-0 tw-mr-3 tw-shadow-sm">
                                            <img src="<?php echo !empty($prop->image) ? BASE_URL . '/' . $prop->image : 'https://placehold.co/100?text=Yok'; ?>" class="tw-w-full tw-h-full tw-object-cover">
                                        </div>
                                        <div>
                                            <div class="tw-font-bold tw-text-gray-900 tw-text-sm text-truncate" style="max-width: 200px;">
                                                <?php echo htmlspecialchars($prop->title); ?>
                                            </div>
                                            <div class="tw-text-[11px] tw-font-medium tw-text-gray-400">ID: #<?php echo $prop->id; ?></div>
                                        </div>
                                    </div>
                                </td>
                                <td>
                                    <span class="tw-font-black tw-text-gray-800 tw-text-sm">₺<?php echo number_format($prop->price, 0, ',', '.'); ?></span>
                                </td>
                                <td>
                                    <span class="tw-text-gray-500 tw-text-xs tw-font-medium d-flex align-items-center">
                                        <i class="fa-solid fa-location-dot tw-text-gray-300 tw-mr-1.5"></i> <?php echo htmlspecialchars($prop->location); ?>
                                    </span>
                                </td>
                                <td>
                                    <?php if($prop->category == 'sale'): ?>
                                        <span class="tw-bg-red-50 tw-text-red-600 tw-px-2.5 tw-py-1 tw-rounded-md tw-text-[10px] tw-font-black tw-uppercase tw-tracking-widest">SATILIK</span>
                                    <?php else: ?>
                                        <span class="tw-bg-blue-50 tw-text-blue-600 tw-px-2.5 tw-py-1 tw-rounded-md tw-text-[10px] tw-font-black tw-uppercase tw-tracking-widest">KİRALIK</span>
                                    <?php endif; ?>
                                </td>
                                <td class="text-end tw-pr-4">
                                    <a href="<?php echo BASE_URL; ?>/property/detail/<?php echo $prop->id; ?>" target="_blank" class="btn tw-bg-gray-50 hover:tw-bg-blue-50 tw-text-gray-400 hover:tw-text-blue-600 tw-p-2 tw-rounded-lg tw-transition-colors" title="Görüntüle">
                                        <i class="fa-solid fa-eye"></i>
                                    </a>
                                    <a href="<?php echo BASE_URL; ?>/admin/property_edit/<?php echo $prop->id; ?>" class="btn tw-bg-gray-50 hover:tw-bg-emerald-50 tw-text-gray-400 hover:tw-text-emerald-600 tw-p-2 tw-rounded-lg tw-transition-colors tw-ml-1" title="Düzenle">
                                        <i class="fa-solid fa-pen-to-square"></i>
                                    </a>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        <?php else: ?>
                            <tr>
                                <td colspan="5" class="text-center tw-py-10">
                                    <div class="tw-text-gray-300 tw-mb-2"><i class="fa-solid fa-folder-open tw-text-3xl"></i></div>
                                    <div class="tw-text-gray-500 tw-font-medium tw-text-sm">Henüz ilan eklenmemiş.</div>
                                </td>
                            </tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <div class="col-lg-4">
        <div class="tw-bg-white tw-rounded-[1.5rem] tw-shadow-sm tw-border tw-border-gray-100 h-100 d-flex flex-column">
            
            <div class="tw-p-6 tw-border-b tw-border-gray-100 d-flex justify-content-between align-items-center">
                <div class="d-flex align-items-center">
                    <div class="tw-w-10 tw-h-10 tw-bg-red-50 tw-text-red-500 tw-rounded-xl d-flex align-items-center justify-content-center tw-mr-3">
                        <i class="fa-solid fa-inbox tw-text-lg"></i>
                    </div>
                    <h5 class="tw-font-black tw-text-lg tw-text-gray-800 mb-0">Gelen Kutusu</h5>
                </div>
            </div>
            
            <div class="tw-flex-grow-1 tw-overflow-y-auto" style="max-height: 400px;">
                <div class="tw-divide-y tw-divide-gray-50">
                    <?php if (!empty($latest_messages)): ?>
                        <?php foreach ($latest_messages as $msg): ?>
                        <a href="<?php echo BASE_URL; ?>/admin/messages" class="tw-block tw-p-5 hover:tw-bg-gray-50/80 tw-transition-colors text-decoration-none">
                            <div class="d-flex align-items-start">
                                <img src="https://ui-avatars.com/api/?name=<?php echo urlencode($msg->sender_name); ?>&background=f1f5f9&color=64748b&size=48" class="tw-w-10 tw-h-10 tw-rounded-full tw-mr-3 tw-border tw-border-gray-100">
                                
                                <div class="tw-w-full">
                                    <div class="d-flex justify-content-between align-items-center tw-mb-1">
                                        <h6 class="mb-0 tw-text-sm tw-font-bold <?php echo ($msg->is_read == 0) ? 'tw-text-gray-900' : 'tw-text-gray-500'; ?>">
                                            <?php echo htmlspecialchars($msg->sender_name); ?>
                                        </h6>
                                        <span class="tw-text-[10px] tw-font-medium tw-text-gray-400">
                                            <?php echo date('d M', strtotime($msg->created_at)); ?>
                                        </span>
                                    </div>
                                    
                                    <p class="tw-text-xs tw-text-gray-500 tw-mb-2 tw-line-clamp-1 tw-leading-relaxed">
                                        <?php echo htmlspecialchars($msg->message_content); ?>
                                    </p>
                                    
                                    <div class="d-flex justify-content-between align-items-center">
                                        <span class="tw-bg-gray-100 tw-text-gray-600 tw-px-2 tw-py-1 tw-rounded-md tw-text-[10px] tw-font-bold tw-border tw-border-gray-200">
                                            <i class="fa-solid fa-home tw-mr-1 tw-text-gray-400"></i> <?php echo substr($msg->property_title, 0, 18) . '...'; ?>
                                        </span>
                                        <?php if($msg->is_read == 0): ?>
                                            <span class="tw-w-2.5 tw-h-2.5 tw-bg-red-500 tw-rounded-full tw-shadow-sm" title="Okunmadı"></span>
                                        <?php endif; ?>
                                    </div>
                                </div>
                            </div>
                        </a>
                        <?php endforeach; ?>
                    <?php else: ?>
                        <div class="text-center tw-py-12">
                            <div class="tw-bg-gray-50 tw-w-16 tw-h-16 tw-rounded-full d-flex align-items-center justify-content-center tw-mx-auto tw-mb-3">
                                <i class="fa-regular fa-envelope-open tw-text-2xl tw-text-gray-400"></i>
                            </div>
                            <p class="tw-text-sm tw-font-medium tw-text-gray-500">Henüz yeni mesaj yok.</p>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
            
            <div class="tw-p-4 tw-border-t tw-border-gray-100 text-center">
                <a href="<?php echo BASE_URL; ?>/admin/messages" class="tw-text-xs tw-font-bold tw-uppercase tw-tracking-wider tw-text-blue-600 hover:tw-text-blue-800 text-decoration-none tw-transition-colors">
                    Tüm Mesajları Görüntüle <i class="fa-solid fa-arrow-right tw-ml-1"></i>
                </a>
            </div>
            
        </div>
    </div>

</div>