<?php
/**
 * NEPHRO ERP - CİHAZ & YATAK DOLULUK HARİTASI (v1.2 - Full Tasarım)
 */
global $pdo;

// 1. VERİLERİ ÇEK (Sütun hatalarına karşı korumalı sorgu)
$sql = "SELECT c.*, s.id as seans_id, s.baslangic_vakti, h.ad, h.soyad, h.cinsiyet
        FROM cihazlar c
        LEFT JOIN seans_programi s ON c.id = s.cihaz_id AND s.durum = 'Basladi' AND s.bitis_vakti IS NULL
        LEFT JOIN hastalar h ON s.hasta_id = h.id
        WHERE c.durum = 'Aktif'
        ORDER BY c.oda_no ASC, c.id ASC";

try {
    $cihazlar = $pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
    die("<div style='padding:20px; background:#fee2e2; border-radius:15px; color:#ef4444;'>
            <b>Veritabanı Hatası:</b> Sorgu çalıştırılamadı. Lütfen veritabanı sütunlarını kontrol edin.<br>
            Hata: " . $e->getMessage() . "
         </div>");
}

// 2. ODALARA GÖRE GRUPLA
$odalar = [];
foreach ($cihazlar as $c) {
    $oda_adi = !empty($c['oda_no']) ? "ODA " . $c['oda_no'] : "DİĞER ÜNİTELER";
    $odalar[$oda_adi][] = $c;
}

// 3. GENEL İSTATİSTİKLER
$toplam_cihaz = count($cihazlar);
$dolu_cihaz = 0;
foreach($cihazlar as $c) if(!empty($c['seans_id'])) $dolu_cihaz++;
$bos_cihaz = $toplam_cihaz - $dolu_cihaz;
$doluluk_orani = ($toplam_cihaz > 0) ? round(($dolu_cihaz / $toplam_cihaz) * 100) : 0;
?>

<style>
    .floor-wrap { padding: 20px; animation: fadeIn 0.4s ease; font-family: 'Inter', sans-serif; }
    
    /* Üst İstatistik Barı */
    .floor-stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 20px; margin-bottom: 30px; }
    .f-stat-card { background: #fff; padding: 20px; border-radius: 20px; border: 1px solid #e2e8f0; display: flex; align-items: center; gap: 15px; box-shadow: 0 4px 6px -1px rgba(0,0,0,0.02); }
    .f-stat-card i { width: 45px; height: 45px; border-radius: 12px; display: flex; align-items: center; justify-content: center; font-size: 1.2rem; }
    .f-val { display: block; font-size: 1.5rem; font-weight: 900; color: #1e293b; line-height: 1; }
    .f-tit { font-size: 0.6rem; font-weight: 800; color: #94a3b8; text-transform: uppercase; letter-spacing: 0.5px; }

    /* Oda Başlıkları */
    .room-section { margin-bottom: 40px; }
    .room-header { 
        display: flex; align-items: center; gap: 10px; margin-bottom: 20px; padding-bottom: 10px; 
        border-bottom: 2px solid #f1f5f9; font-size: 1.2rem; font-weight: 900; color: #0f172a; 
    }

    /* Yatak Grid Yapısı */
    .bed-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 20px; }

    /* Yatak Kartı */
    .bed-card { 
        background: #fff; border: 2px solid #f1f5f9; border-radius: 24px; padding: 20px; 
        transition: 0.3s; position: relative; text-align: center;
    }
    .bed-card.occupied { border-color: #10b981; background: #f0fdf4; }
    .bed-card.vacant { border-style: dashed; border-color: #cbd5e1; opacity: 0.8; }
    
    .bed-icon { font-size: 2.2rem; margin-bottom: 12px; color: #cbd5e1; }
    .occupied .bed-icon { color: #10b981; }
    
    .bed-id { position: absolute; top: 12px; right: 15px; font-size: 0.7rem; font-weight: 900; color: #94a3b8; }
    .occupied .bed-id { color: #10b981; }

    .p-name { display: block; font-size: 0.9rem; font-weight: 800; color: #1e293b; margin-bottom: 4px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
    .p-time { font-size: 0.7rem; font-weight: 700; color: #64748b; }

    /* Animasyonlu Nabız Noktası */
    .pulse { width: 8px; height: 8px; background: #10b981; border-radius: 50%; display: inline-block; margin-right: 5px; animation: pulse-green 1.5s infinite; }
    @keyframes pulse-green { 0% { box-shadow: 0 0 0 0 rgba(16, 185, 129, 0.7); } 70% { box-shadow: 0 0 0 10px rgba(16, 185, 129, 0); } 100% { box-shadow: 0 0 0 0 rgba(16, 185, 129, 0); } }

    .btn-action { margin-top: 15px; display: inline-block; font-size: 0.7rem; font-weight: 800; text-decoration: none; color: #2563eb; }
</style>

<div class="floor-wrap">
    <div style="display:flex; justify-content:space-between; align-items:flex-end; margin-bottom:25px;">
        <div>
            <h1 style="margin:0; font-weight:900; color:#0f172a;">Klinik Yerleşim Planı</h1>
            <p style="margin:5px 0 0; color:#64748b; font-weight:600;">Anlık Yatak ve Cihaz Durum Paneli</p>
        </div>
        <div style="text-align:right;">
            <div style="font-size: 2rem; font-weight: 900; color: #2563eb; line-height:1;">%<?php echo $doluluk_orani; ?></div>
            <div style="font-size: 0.65rem; font-weight: 800; color: #94a3b8;">GENEL DOLULUK</div>
        </div>
    </div>

    <div class="floor-stats">
        <div class="f-stat-card">
            <i class="fas fa-bed" style="background:#eff6ff; color:#2563eb;"></i>
            <div><span class="f-val"><?php echo $toplam_cihaz; ?></span><span class="f-tit">TOPLAM KAPASİTE</span></div>
        </div>
        <div class="f-stat-card">
            <i class="fas fa-user-check" style="background:#f0fdf4; color:#10b981;"></i>
            <div><span class="f-val"><?php echo $dolu_cihaz; ?></span><span class="f-tit">DOLU YATAK</span></div>
        </div>
        <div class="f-stat-card">
            <i class="fas fa-door-open" style="background:#f8fafc; color:#64748b;"></i>
            <div><span class="f-val"><?php echo $bos_cihaz; ?></span><span class="f-tit">MÜSAİT YATAK</span></div>
        </div>
    </div>

    <?php foreach ($odalar as $oda_baslik => $cihazlar_grubu): ?>
        <div class="room-section">
            <div class="room-header">
                <i class="fas fa-hospital-alt" style="color:#2563eb;"></i>
                <?php echo $oda_baslik; ?>
                <span style="font-size:0.75rem; color:#94a3b8; margin-left:10px;">(<?php echo count($cihazlar_grubu); ?> Cihaz)</span>
            </div>
            
            <div class="bed-grid">
                <?php foreach ($cihazlar_grubu as $c): 
                    $is_occupied = !empty($c['seans_id']);
                ?>
                    <div class="bed-card <?php echo $is_occupied ? 'occupied' : 'vacant'; ?>">
                        <span class="bed-id">#<?php echo $c['id']; ?></span>
                        
                        <div class="bed-icon">
                            <i class="fas <?php echo ($c['cinsiyet'] == 'Kadın') ? 'fa-female' : 'fa-bed'; ?>"></i>
                        </div>

                        <?php if($is_occupied): ?>
                            <span class="p-name"><?php echo mb_strtoupper(($c['ad'] ?? '').' '.($c['soyad'] ?? ''), 'UTF-8'); ?></span>
                            <div class="p-time">
                                <span class="pulse"></span>
                                <?php echo date('H:i', strtotime($c['baslangic_vakti'])); ?>'de başladı
                            </div>
                            <a href="?sayfa=aktif-seans-detay&seans_id=<?php echo $c['seans_id']; ?>" class="btn-action">İzleme Paneli <i class="fas fa-chevron-right"></i></a>
                        <?php else: ?>
                            <span class="p-name" style="color:#cbd5e1;">BOŞ</span>
                            <div class="p-time">Müsait</div>
                            <a href="?sayfa=seans-baslat" class="btn-action" style="color:#94a3b8;">Hasta Bağla <i class="fas fa-plus"></i></a>
                        <?php endif; ?>
                    </div>
                <?php endforeach; ?>
            </div>
        </div>
    <?php endforeach; ?>
</div>