<?php
/**
 * HASTA DÜZENLEME - PREMIUM (Sade + Şık)
 */
global $pdo;

$id = (int)($_GET['id'] ?? 0);
if ($id <= 0) {
    echo "<div style='padding:25px;background:#fff1f2;border:1px solid #fecaca;border-radius:18px;color:#991b1b'>Geçersiz ID</div>";
    return;
}

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

    if (!$h) {
        echo "<div style='background:#fef2f2; color:#991b1b; padding:30px; border-radius:20px; border:1px solid #fecaca;'>
                <h3 style='margin:0 0 8px'>HATA: Kayıt bulunamadı.</h3>
                <p style='margin:0 0 12px'>Düzenlenmek istenen hasta sistemde mevcut değil.</p>
                <a href='?sayfa=hasta-liste' style='color:#991b1b;font-weight:800'>Listeye Dön</a>
              </div>";
        return;
    }
} catch (PDOException $e) {
    die("Hata: " . $e->getMessage());
}

function checkSelect($val, $dbVal) { return ((string)$val === (string)$dbVal) ? 'selected' : ''; }

$adSoyad  = trim(($h['ad'] ?? '') . ' ' . ($h['soyad'] ?? ''));
$initials = mb_strtoupper(mb_substr($h['ad'] ?? 'A', 0, 1) . mb_substr($h['soyad'] ?? 'A', 0, 1), 'UTF-8');
$durum    = $h['durum'] ?? 'aktif';
$hep      = $h['hepatit_durumu'] ?? 'Negatif';

$durBadge = ($durum==='aktif') ? 'ok' : (($durum==='pasif') ? 'warn' : 'bad');
$hepBadge = ($hep==='Negatif') ? 'ok' : 'bad';
?>

<style>
:root{
    --p:#2563eb;
    --bg:#f8fafc;
    --card:#ffffff;
    --text:#0f172a;
    --muted:#64748b;
    --soft:#e2e8f0;
    --ok:#16a34a;
    --warn:#f59e0b;
    --bad:#ef4444;
}

.p-wrap{max-width:1100px;margin:0 auto;padding-bottom:40px;}
.topbar{
    background:var(--card);
    border:1px solid #f1f5f9;
    border-radius:22px;
    padding:18px;
    display:flex;
    align-items:center;
    justify-content:space-between;
    gap:14px;
    box-shadow:0 10px 30px rgba(0,0,0,0.03);
    margin-bottom:16px;
}
.top-left{display:flex;align-items:center;gap:14px;min-width:0;}
.avatar{
    width:52px;height:52px;border-radius:16px;
    background:#eff6ff;color:var(--p);
    display:flex;align-items:center;justify-content:center;
    font-weight:900;flex:0 0 auto;
}
.title{min-width:0;}
.title h2{margin:0;font-size:1.15rem;color:var(--text);font-weight:900;}
.title p{margin:4px 0 0;color:var(--muted);font-size:.9rem;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;}

.right-tools{display:flex;align-items:center;gap:10px;flex-wrap:wrap;justify-content:flex-end;}
.badge{
    padding:6px 10px;border-radius:999px;
    font-size:.78rem;font-weight:900;
    border:1px solid #f1f5f9;
    background:var(--bg);
    color:var(--muted);
    display:inline-flex; align-items:center; gap:6px;
}
.badge.ok{background:#ecfdf5;color:#15803d;border-color:#bbf7d0;}
.badge.bad{background:#fff1f2;color:#be123c;border-color:#fecaca;}
.badge.warn{background:#fffbeb;color:#92400e;border-color:#fde68a;}

.iconbtn{
    width:42px;height:42px;border-radius:14px;
    display:inline-flex;align-items:center;justify-content:center;
    border:1px solid #f1f5f9;
    background:#fff;
    color:var(--muted);
    text-decoration:none;
    transition:.18s;
}
.iconbtn:hover{background:var(--bg);color:var(--text);transform:translateY(-1px);}

.card{
    background:var(--card);
    border:1px solid #f1f5f9;
    border-radius:22px;
    padding:22px;
    box-shadow:0 10px 40px rgba(0,0,0,0.03);
}

.sec{
    margin:18px 0 12px;
    font-size:.72rem;font-weight:900;color:var(--p);
    text-transform:uppercase; letter-spacing:1.4px;
    padding-bottom:8px; border-bottom:2px solid var(--bg);
}

.grid{display:grid;grid-template-columns:1fr 1fr; gap:14px;}
.grid3{display:grid;grid-template-columns:1fr 1fr 1fr; gap:14px;}
@media(max-width:900px){ .grid,.grid3{grid-template-columns:1fr;} }

.fg label{display:block;font-size:.84rem;font-weight:800;color:#475569;margin-bottom:8px;}
.input{
    width:100%;
    padding:14px 16px;
    border:2px solid #f1f5f9;
    border-radius:14px;
    background:#fbfdff;
    outline:none;
    color:var(--text);
    font-weight:700;
    transition:.2s;
    box-sizing:border-box;
}
.input:focus{
    border-color:#93c5fd;
    box-shadow:0 0 0 4px rgba(59,130,246,0.12);
    background:#fff;
}

.subgrid{display:grid;grid-template-columns:1fr 1fr; gap:14px;}
@media(max-width:900px){ .subgrid{grid-template-columns:1fr;} }

.subbox{
    background:var(--bg);
    border:1px solid #f1f5f9;
    border-radius:18px;
    padding:16px;
}
.subhead{
    display:flex;align-items:center;justify-content:space-between;gap:10px;
    margin-bottom:10px;
}
.subhead b{color:var(--text);}
.smallnote{font-size:.82rem;color:var(--muted);}

.footerbar{
    margin-top:16px;
    display:flex;
    gap:12px;
    justify-content:flex-end;
    flex-wrap:wrap;
}
.btn-primary{
    border:none;
    border-radius:14px;
    padding:14px 18px;
    font-weight:900;
    cursor:pointer;
    display:inline-flex;
    align-items:center;
    justify-content:center;
    gap:10px;
    transition:.2s;
    background:var(--p);
    color:#fff;
    box-shadow:0 10px 22px rgba(37,99,235,0.22);
    min-width:220px;
}
.btn-primary:hover{background:#1d4ed8;transform:translateY(-1px);}
</style>

<div class="p-wrap">

    <div class="topbar">
        <div class="top-left">
            <div class="avatar"><?php echo htmlspecialchars($initials); ?></div>
            <div class="title">
                <h2>Hasta Güncelle</h2>
                <p><b><?php echo htmlspecialchars($adSoyad); ?></b> • T.C: <?php echo htmlspecialchars($h['tc_no'] ?? ''); ?></p>
            </div>
        </div>

        <div class="right-tools">
            <span class="badge <?php echo $durBadge; ?>"><i class="fas fa-circle"></i><?php echo strtoupper(htmlspecialchars($durum)); ?></span>
            <span class="badge <?php echo $hepBadge; ?>"><i class="fas fa-shield-virus"></i><?php echo htmlspecialchars($hep); ?></span>

            <a class="iconbtn" href="?sayfa=hasta-liste" title="Liste">
                <i class="fas fa-list"></i>
            </a>
            <a class="iconbtn" href="?sayfa=hasta-detay&id=<?php echo (int)$h['id']; ?>" title="Detay">
                <i class="fas fa-eye"></i>
            </a>
        </div>
    </div>

    <form action="islem.php" method="POST" class="card">
        <input type="hidden" name="id" value="<?php echo (int)$h['id']; ?>">

        <div class="sec">Kimlik & Durum</div>
        <div class="grid">
            <div class="fg">
                <label>T.C. Kimlik Numarası</label>
                <input type="text" name="tc_no" class="input" value="<?php echo htmlspecialchars($h['tc_no'] ?? ''); ?>" maxlength="11" required>
            </div>
            <div class="fg">
                <label>Hasta Durumu</label>
                <select name="durum" class="input">
                    <option value="aktif" <?php echo checkSelect('aktif',$h['durum']); ?>>AKTİF</option>
                    <option value="pasif" <?php echo checkSelect('pasif',$h['durum']); ?>>PASİF</option>
                    <option value="vefat" <?php echo checkSelect('vefat',$h['durum']); ?>>VEFAT</option>
                    <option value="nakil" <?php echo checkSelect('nakil',$h['durum']); ?>>NAKİL</option>
                </select>
            </div>
        </div>

        <div class="grid" style="margin-top:14px;">
            <div class="fg">
                <label>Adı</label>
                <input type="text" name="ad" class="input" value="<?php echo htmlspecialchars($h['ad'] ?? ''); ?>" required>
            </div>
            <div class="fg">
                <label>Soyadı</label>
                <input type="text" name="soyad" class="input" value="<?php echo htmlspecialchars($h['soyad'] ?? ''); ?>" required>
            </div>
        </div>

        <div class="grid" style="margin-top:14px;">
            <div class="fg">
                <label>Cinsiyet</label>
                <select name="cinsiyet" class="input">
                    <option value="Erkek" <?php echo checkSelect('Erkek',$h['cinsiyet']); ?>>Erkek</option>
                    <option value="Kadin" <?php echo checkSelect('Kadin',$h['cinsiyet']); ?>>Kadın</option>
                </select>
            </div>
            <div class="fg">
                <label>Doğum Tarihi</label>
                <input type="date" name="dogum_tarihi" class="input" value="<?php echo htmlspecialchars($h['dogum_tarihi'] ?? ''); ?>">
            </div>
        </div>

        <div class="grid" style="margin-top:14px;">
            <div class="fg">
                <label>Kan Grubu</label>
                <select name="kan_grubu" class="input">
                    <option value="">Seçiniz...</option>
                    <?php
                    $gruplar = ['A Rh(+)','A Rh(-)','B Rh(+)','B Rh(-)','AB Rh(+)','AB Rh(-)','0 Rh(+)','0 Rh(-)'];
                    foreach($gruplar as $g){
                        $sel = checkSelect($g, $h['kan_grubu'] ?? '');
                        echo "<option value='".htmlspecialchars($g)."' $sel>".htmlspecialchars($g)."</option>";
                    }
                    ?>
                </select>
            </div>
            <div class="fg">
                <label>Telefon</label>
                <input type="tel" name="telefon" class="input" value="<?php echo htmlspecialchars($h['telefon'] ?? ''); ?>">
            </div>
        </div>

        <div class="sec">Klinik</div>
        <div class="grid3">
            <div class="fg">
                <label>Hepatit Durumu</label>
                <select name="hepatit_durumu" class="input">
                    <option value="Negatif" <?php echo checkSelect('Negatif',$h['hepatit_durumu']); ?>>Negatif</option>
                    <option value="HbsAg+" <?php echo checkSelect('HbsAg+',$h['hepatit_durumu']); ?>>HbsAg+</option>
                    <option value="Anti-HCV+" <?php echo checkSelect('Anti-HCV+',$h['hepatit_durumu']); ?>>Anti-HCV+</option>
                    <option value="HIV+" <?php echo checkSelect('HIV+',$h['hepatit_durumu']); ?>>HIV+</option>
                </select>
            </div>

            <div class="fg">
                <label>Damar Yolu</label>
                <select name="damar_yolu" class="input">
                    <option value="">Seçiniz...</option>
                    <option value="AVF" <?php echo checkSelect('AVF',$h['damar_yolu']); ?>>AVF</option>
                    <option value="AVG" <?php echo checkSelect('AVG',$h['damar_yolu']); ?>>AVG</option>
                    <option value="Kateter" <?php echo checkSelect('Kateter',$h['damar_yolu']); ?>>Kateter</option>
                    <option value="Greft" <?php echo checkSelect('Greft',$h['damar_yolu']); ?>>Greft</option>
                </select>
            </div>

            <div class="fg">
                <label>Diyabet</label>
                <select name="diyabet_durumu" class="input">
                    <option value="0" <?php echo checkSelect('0',$h['diyabet_durumu']); ?>>Yok</option>
                    <option value="1" <?php echo checkSelect('1',$h['diyabet_durumu']); ?>>Var</option>
                </select>
            </div>
        </div>

        <div class="grid" style="margin-top:14px;">
            <div class="fg">
                <label>Kuru Ağırlık (kg)</label>
                <input type="number" step="0.01" name="kuru_agirlik" class="input" value="<?php echo htmlspecialchars($h['kuru_agirlik'] ?? ''); ?>">
            </div>
            <div class="fg">
                <label>Adres</label>
                <input type="text" name="adres" class="input" value="<?php echo htmlspecialchars($h['adres'] ?? ''); ?>">
            </div>
        </div>

        <div class="sec">Acil Durum Yakınları</div>

        <div class="subgrid">
            <div class="subbox">
                <div class="subhead">
                    <b>Yakın 1</b>
                    <span class="smallnote">Birincil iletişim</span>
                </div>
                <div class="fg">
                    <label>Ad Soyad</label>
                    <input type="text" name="yakin_ad_soyad" class="input" value="<?php echo htmlspecialchars($h['yakin_ad_soyad'] ?? ''); ?>">
                </div>
                <div class="fg" style="margin-top:14px;">
                    <label>Yakınlık Derecesi</label>
                    <input type="text" name="yakin_yakinlik_derecesi" class="input" value="<?php echo htmlspecialchars($h['yakin_yakinlik_derecesi'] ?? ''); ?>">
                </div>
                <div class="fg" style="margin-top:14px;">
                    <label>Telefon</label>
                    <input type="tel" name="yakin_telefon" class="input" value="<?php echo htmlspecialchars($h['yakin_telefon'] ?? ''); ?>">
                </div>
            </div>

            <div class="subbox">
                <div class="subhead">
                    <b>Yakın 2</b>
                    <span class="smallnote">Alternatif kişi</span>
                </div>
                <div class="fg">
                    <label>Ad Soyad</label>
                    <input type="text" name="yakin2_ad_soyad" class="input" value="<?php echo htmlspecialchars($h['yakin2_ad_soyad'] ?? ''); ?>">
                </div>
                <div class="fg" style="margin-top:14px;">
                    <label>Telefon</label>
                    <input type="tel" name="yakin2_telefon" class="input" value="<?php echo htmlspecialchars($h['yakin2_telefon'] ?? ''); ?>">
                </div>
                <div class="smallnote" style="margin-top:10px;">
                    İstersen boş bırakabilirsin.
                </div>
            </div>
        </div>

        <div class="footerbar">
            <button type="submit" name="hasta_guncelle" class="btn-primary">
                <i class="fas fa-save"></i> Kaydet
            </button>
        </div>
    </form>

</div>
