<?php
global $pdo;
$id = (int)($_GET['id'] ?? 0);

try {
    $sorgu = $pdo->prepare("SELECT * FROM hastalar WHERE id = ?");
    $sorgu->execute([$id]);
    $h = $sorgu->fetch();

    if (!$h) { 
        die("<div style='background:white; padding:50px; border-radius:20px; text-align:center;'>
                <i class='fas fa-search fa-3x' style='color:#cbd5e1; margin-bottom:20px;'></i>
                <h3>Hasta bulunamadı.</h3>
                <a href='?sayfa=hasta-liste' style='color:#2563eb; font-weight:700;'>Listeye Dön</a>
             </div>"); 
    }
} catch (PDOException $e) {
    die("Hata: " . $e->getMessage());
}
?>

<style>
    .detail-container { animation: fadeIn 0.4s ease; max-width: 1200px; margin: 0 auto; }
    
    /* PROFIL HEADER - MOBILDE ALT ALTA GELIR */
    .profile-header { 
        background: #fff; border-radius: 24px; padding: 25px; border: 1px solid #f1f5f9;
        display: flex; justify-content: space-between; align-items: center; margin-bottom: 25px;
        box-shadow: 0 4px 20px rgba(0,0,0,0.02);
    }
    .profile-main { display: flex; gap: 20px; align-items: center; }
    .p-large-avatar { 
        width: 70px; height: 70px; border-radius: 20px; display: flex; align-items: center; 
        justify-content: center; font-size: 1.8rem; font-weight: 800; flex-shrink: 0;
    }
    .p-large-avatar.Erkek { background: #eff6ff; color: #2563eb; }
    .p-large-avatar.Kadin { background: #fdf2f8; color: #db2777; }

    /* GRID SISTEMI */
    .grid-sections { display: grid; grid-template-columns: 2fr 1fr; gap: 25px; }
    
    .info-card { background: #fff; border-radius: 24px; padding: 25px; border: 1px solid #f1f5f9; margin-bottom: 25px; }
    .card-title { font-size: 0.9rem; font-weight: 800; color: #0f172a; margin-bottom: 20px; display: flex; align-items: center; gap: 10px; }
    .card-title i { color: #2563eb; width: 20px; text-align: center; }

    /* DATA ROW - MOBILDE TEK KOLON OLUR */
    .data-row { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; margin-bottom: 15px; }
    .data-item label { display: block; font-size: 0.7rem; font-weight: 800; color: #94a3b8; text-transform: uppercase; margin-bottom: 5px; letter-spacing: 0.5px; }
    .data-item span { font-size: 1rem; font-weight: 700; color: #334155; word-break: break-word; }

    .alert-strip { padding: 15px 20px; border-radius: 18px; margin-bottom: 15px; display: flex; align-items: center; gap: 15px; font-weight: 800; font-size: 0.85rem; }
    .alert-danger { background: #fff1f2; color: #be123c; border: 1px solid #ffe4e6; }
    .alert-warning { background: #fffbeb; color: #b45309; border: 1px solid #fef3c7; }

    /* --- MOBIL REVIZYONLAR --- */
    @media (max-width: 992px) {
        .grid-sections { grid-template-columns: 1fr; }
        .profile-header { flex-direction: column; text-align: center; gap: 20px; }
        .profile-main { flex-direction: column; }
        .action-btns { width: 100%; display: flex; flex-direction: column; gap: 10px; }
        .action-btns a { width: 100%; justify-content: center; box-sizing: border-box; }
    }

    @media (max-width: 576px) {
        .data-row { grid-template-columns: 1fr; gap: 15px; }
        .page-content { padding: 15px; } /* index.php'deki padding'i mobilde daraltır */
        .info-card { padding: 20px; }
    }
</style>

<div class="detail-container">
    <div class="profile-header">
        <div class="profile-main">
            <div class="p-large-avatar <?php echo $h['cinsiyet']; ?>">
                <?php echo mb_substr($h['ad'],0,1).mb_substr($h['soyad'],0,1); ?>
            </div>
            <div>
                <h2 style="margin:0; font-size: 1.5rem; letter-spacing: -0.5px;"><?php echo $h['ad'].' '.$h['soyad']; ?></h2>
                <p style="color:#64748b; margin:5px 0 0; font-weight: 600;">
                    T.C. No: <?php echo $h['tc_no']; ?> • 
                    <span style="color: <?php echo ($h['durum'] == 'aktif' ? '#10b981' : '#ef4444'); ?>;">
                        ● <?php echo strtoupper($h['durum']); ?>
                    </span>
                </p>
            </div>
        </div>
        
        <div class="action-btns">
            <a href="?sayfa=hasta-duzenle&id=<?php echo $h['id']; ?>" class="burger-btn" style="text-decoration:none; display: inline-flex; align-items: center; gap: 8px;">
                <i class="fas fa-edit"></i> Düzenle
            </a>
            <a href="?sayfa=seans-baslat&id=<?php echo $h['id']; ?>" class="burger-btn" style="background:#2563eb; color:#fff; border:none; text-decoration:none; display: inline-flex; align-items: center; gap: 8px;">
                <i class="fas fa-play"></i> Seansı Başlat
            </a>
        </div>
    </div>

    <div class="grid-sections">
        <div class="left-col">
            <div class="info-card">
                <div class="card-title"><i class="fas fa-id-card"></i> Kimlik & Demografik Bilgiler</div>
                <div class="data-row">
                    <div class="data-item"><label>Doğum Tarihi</label><span><?php echo $h['dogum_tarihi'] ?: '--'; ?></span></div>
                    <div class="data-item"><label>Cinsiyet</label><span><?php echo $h['cinsiyet']; ?></span></div>
                    <div class="data-item"><label>Kan Grubu</label><span><?php echo $h['kan_grubu']; ?></span></div>
                    <div class="data-item"><label>Kayıt Tarihi</label><span><?php echo $h['kayit_tarihi']; ?></span></div>
                </div>
            </div>

            <div class="info-card">
                <div class="card-title"><i class="fas fa-file-medical"></i> Klinik Durum Verileri</div>
                <div class="data-row">
                    <div class="data-item"><label>Damar Yolu</label><span><?php echo $h['damar_yolu'] ?: '--'; ?></span></div>
                    <div class="data-item"><label>Kuru Ağırlık</label><span><?php echo $h['kuru_agirlik'] ? $h['kuru_agirlik'].' kg' : '--'; ?></span></div>
                    <div class="data-item"><label>Hepatit Durumu</label><span><?php echo $h['hepatit_durumu']; ?></span></div>
                    <div class="data-item"><label>Diyabet</label><span><?php echo $h['diyabet_durumu'] ? 'Evet' : 'Hayır'; ?></span></div>
                </div>
            </div>
        </div>

        <div class="right-col">
            <?php if($h['hepatit_durumu'] != 'Negatif' && !empty($h['hepatit_durumu'])): ?>
            <div class="alert-strip alert-danger">
                <i class="fas fa-exclamation-triangle"></i> HEPATİT <?php echo strtoupper($h['hepatit_durumu']); ?> RİSKİ
            </div>
            <?php endif; ?>

            <?php if($h['diyabet_durumu']): ?>
            <div class="alert-strip alert-warning">
                <i class="fas fa-tint"></i> DİYABETİK TAKİP GEREKLİ
            </div>
            <?php endif; ?>

            <div class="info-card">
                <div class="card-title"><i class="fas fa-phone"></i> İletişim Bilgileri</div>
                <div class="data-item" style="margin-bottom:15px;"><label>Hasta Telefon</label><span><?php echo $h['telefon'] ?: '--'; ?></span></div>
                <div class="data-item"><label>İkamet Adresi</label><span style="font-size:0.9rem; font-weight:500;"><?php echo $h['adres'] ?: 'Adres bilgisi girilmemiş.'; ?></span></div>
            </div>

            <div class="info-card">
                <div class="card-title"><i class="fas fa-user-friends"></i> Acil Durum Yakını</div>
                <div class="data-item" style="margin-bottom:10px;"><label>Ad Soyad / Derece</label><span><?php echo $h['yakin_ad_soyad'] ?: '--'; ?> (<?php echo $h['yakin_yakinlik_derecesi']; ?>)</span></div>
                <div class="data-item"><label>İletişim</label><span><?php echo $h['yakin_telefon'] ?: '--'; ?></span></div>
            </div>
        </div>
    </div>
</div>