<?php
/**
 * NEPHRO ERP - DASHBOARD "FLIGHT MODE"
 */
global $pdo;

// 1. İstatistikleri Topla
$toplam_hasta = $pdo->query("SELECT COUNT(*) FROM hastalar WHERE durum='aktif'")->fetchColumn();
$aktif_seans = $pdo->query("SELECT COUNT(*) FROM seans_programi WHERE bitis_vakti IS NULL")->fetchColumn();
$toplam_cihaz = $pdo->query("SELECT COUNT(*) FROM cihazlar WHERE durum='Aktif'")->fetchColumn();
$doluluk_orani = ($toplam_cihaz > 0) ? round(($aktif_seans / $toplam_cihaz) * 100) : 0;

// 2. Kritik Durumdaki Hastaları Bul (Son gözleminde tansiyonu 100 altı veya nabzı 100 üstü olanlar)
$kritik_sql = "SELECT h.ad, h.soyad, g.tansiyon_sys, g.nabiz, c.oda_no, s.id as seans_id
               FROM seans_gozlemleri g
               JOIN seans_programi s ON g.seans_id = s.id
               JOIN hastalar h ON s.hasta_id = h.id
               JOIN cihazlar c ON s.cihaz_id = c.id
               WHERE s.bitis_vakti IS NULL 
               AND g.id IN (SELECT MAX(id) FROM seans_gozlemleri GROUP BY seans_id)
               AND (g.tansiyon_sys < 100 OR g.nabiz > 100 OR g.tansiyon_sys > 160)
               LIMIT 5";
$kritik_hastalar = $pdo->query($kritik_sql)->fetchAll();
?>

<style>
    .dash-container { animation: fadeIn 0.8s ease; }
    
    /* STATS GRID */
    .stats-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 20px; margin-bottom: 30px; }
    .stat-card { 
        background: #fff; padding: 25px; border-radius: 24px; border: 1px solid #f1f5f9;
        display: flex; align-items: center; gap: 20px; transition: 0.3s;
    }
    .stat-card:hover { transform: translateY(-5px); box-shadow: 0 15px 30px rgba(0,0,0,0.05); }
    .stat-icon { width: 60px; height: 60px; border-radius: 18px; display: flex; align-items: center; justify-content: center; font-size: 1.5rem; }
    
    /* RENK TEMALARI */
    .bg-blue { background: #eff6ff; color: #2563eb; }
    .bg-green { background: #f0fdf4; color: #16a34a; }
    .bg-orange { background: #fff7ed; color: #ea580c; }
    .bg-red { background: #fef2f2; color: #dc2626; }

    /* KRİTİK PANEL */
    .dashboard-main { display: grid; grid-template-columns: 2fr 1fr; gap: 30px; }
    .glass-panel { background: #fff; border-radius: 30px; padding: 25px; border: 1px solid #f1f5f9; }
    .panel-title { font-weight: 800; font-size: 1.1rem; color: #0f172a; margin-bottom: 20px; display: flex; align-items: center; gap: 10px; }

    /* KRİTİK LİSTE */
    .critical-item { 
        display: flex; align-items: center; justify-content: space-between; 
        padding: 15px; background: #fff1f2; border-radius: 16px; margin-bottom: 12px;
        border: 1px solid #ffe4e6;
    }
    .pulse-red { width: 10px; height: 10px; background: #ef4444; border-radius: 50%; animation: pulseRed 1.5s infinite; }
    
    @keyframes pulseRed { 0% { transform: scale(0.95); box-shadow: 0 0 0 0 rgba(239, 68, 68, 0.7); } 70% { transform: scale(1); box-shadow: 0 0 0 10px rgba(239, 68, 68, 0); } 100% { transform: scale(0.95); box-shadow: 0 0 0 0 rgba(239, 68, 68, 0); } }

    /* HIZLI AKSİYONLAR */
    .action-btn { 
        display: flex; align-items: center; gap: 12px; padding: 15px; 
        background: #f8fafc; border-radius: 15px; text-decoration: none; color: #475569;
        font-weight: 700; margin-bottom: 10px; transition: 0.2s; border: 1px solid transparent;
    }
    .action-btn:hover { background: #fff; border-color: #e2e8f0; color: #2563eb; transform: translateX(5px); }

    @media (max-width: 1000px) { .dashboard-main { grid-template-columns: 1fr; } }
</style>

<div class="dash-container">
    <div class="stats-grid">
        <div class="stat-card">
            <div class="stat-icon bg-blue"><i class="fas fa-users"></i></div>
            <div>
                <span style="color:#64748b; font-size:0.8rem; font-weight:700;">TOPLAM HASTA</span>
                <div style="font-size:1.5rem; font-weight:900;"><?php echo $toplam_hasta; ?></div>
            </div>
        </div>
        <div class="stat-card">
            <div class="stat-icon bg-green"><i class="fas fa-bed"></i></div>
            <div>
                <span style="color:#64748b; font-size:0.8rem; font-weight:700;">AKTİF SEANS</span>
                <div style="font-size:1.5rem; font-weight:900;"><?php echo $aktif_seans; ?></div>
            </div>
        </div>
        <div class="stat-card">
            <div class="stat-icon bg-orange"><i class="fas fa-percentage"></i></div>
            <div>
                <span style="color:#64748b; font-size:0.8rem; font-weight:700;">DOLULUK ORANI</span>
                <div style="font-size:1.5rem; font-weight:900;">%<?php echo $doluluk_orani; ?></div>
            </div>
        </div>
        <div class="stat-card">
            <div class="stat-icon bg-red"><i class="fas fa-microchip"></i></div>
            <div>
                <span style="color:#64748b; font-size:0.8rem; font-weight:700;">AKTİF CİHAZ</span>
                <div style="font-size:1.5rem; font-weight:900;"><?php echo $toplam_cihaz; ?></div>
            </div>
        </div>
    </div>

    <div class="dashboard-main">
        <div class="glass-panel">
            <div class="panel-title">
                <i class="fas fa-heartbeat" style="color:#ef4444;"></i> KRİTİK TAKİP (LİMİT DIŞI VERİLER)
            </div>
            
            <?php if(empty($kritik_hastalar)): ?>
                <div style="text-align:center; padding:40px; color:#94a3b8;">
                    <i class="fas fa-check-shield fa-3x" style="color:#10b981; margin-bottom:15px;"></i>
                    <p>Şu an tüm hastaların vital bulguları normal sınırlarda.</p>
                </div>
            <?php else: ?>
                <?php foreach($kritik_hastalar as $kh): ?>
                <div class="critical-item">
                    <div style="display:flex; align-items:center; gap:15px;">
                        <div class="pulse-red"></div>
                        <div>
                            <b style="color:#991b1b;"><?php echo $kh['ad'].' '.$kh['soyad']; ?></b>
                            <div style="font-size:0.75rem; color:#b91c1c;"><?php echo $kh['oda_no']; ?> • Son Veri Analizi</div>
                        </div>
                    </div>
                    <div style="text-align:right;">
                        <span style="font-weight:800; color:#dc2626; font-size:1.1rem;">
                            <?php echo $kh['tansiyon_sys']; ?>/<?php echo $kh['nabiz']; ?>
                        </span>
                        <div style="font-size:0.65rem; font-weight:700; color:#e11d48;">TANS / NABIZ</div>
                    </div>
                    <a href="index.php?sayfa=aktif-seans-detay&seans_id=<?php echo $kh['seans_id']; ?>" 
                       style="background:#dc2626; color:#fff; padding:8px 15px; border-radius:10px; text-decoration:none; font-size:0.75rem; font-weight:800;">MÜDAHALE ET</a>
                </div>
                <?php endforeach; ?>
            <?php endif; ?>

            <div style="margin-top:30px;">
                <div class="panel-title"><i class="fas fa-chart-line" style="color:#2563eb;"></i> DOLULUK GRAFİĞİ</div>
                <div style="height:10px; background:#f1f5f9; border-radius:5px; overflow:hidden; margin-top:15px;">
                    <div style="width:<?php echo $doluluk_orani; ?>%; height:100%; background:linear-gradient(90deg, #3b82f6, #2563eb); border-radius:5px;"></div>
                </div>
                <p style="font-size:0.8rem; color:#64748b; margin-top:10px;">Toplam 40 cihazın <?php echo $aktif_seans; ?> adedi şu an kullanımda.</p>
            </div>
        </div>

        <div class="glass-panel">
            <div class="panel-title"><i class="fas fa-bolt" style="color:#eab308;"></i> HIZLI AKSİYONLAR</div>
            
            <a href="index.php?sayfa=seans-baslat" class="action-btn">
                <i class="fas fa-plus-circle" style="color:#2563eb;"></i> Yeni Seans Başlat
            </a>
            <a href="index.php?sayfa=hasta-kayit" class="action-btn">
                <i class="fas fa-user-plus" style="color:#16a34a;"></i> Yeni Hasta Kaydı
            </a>
            <a href="index.php?sayfa=cihazlar" class="action-btn">
                <i class="fas fa-tools" style="color:#64748b;"></i> Cihaz Durumları
            </a>
            <a href="index.php?sayfa=stok-takip" class="action-btn">
                <i class="fas fa-box" style="color:#ea580c;"></i> Sarf Malzeme Stok
            </a>

            <div style="background:#f8fafc; border-radius:20px; padding:15px; margin-top:25px; border:1px dashed #cbd5e1;">
                <b style="font-size:0.8rem; color:#475569; display:block; margin-bottom:10px;">GÜNLÜK ÖZET</b>
                <div style="font-size:0.8rem; color:#64748b; line-height:1.6;">
                    • Bugün planlanan seans: 50<br>
                    • Tamamlanan seans: 12<br>
                    • Arızalı cihaz sayısı: 2<br>
                    • Hepatit+ Hasta sayısı: 3
                </div>
            </div>
        </div>
    </div>
</div>