<?php
/**
 * NEPHRO ERP - PREMIUM SETTINGS (v8.3 - Error Fixed)
 */
global $pdo;

// h() fonksiyonu güvenliği
if (!function_exists('h')) { 
    function h($s){ return htmlspecialchars((string)($s ?? ''), ENT_QUOTES, "UTF-8"); } 
}

// Oturum kontrolü ve ID alma
$user_id = $_SESSION['user_id'] ?? 0;

if ($user_id > 0) {
    // Mevcut kullanıcı bilgilerini çek
    $me = $pdo->prepare("SELECT * FROM kullanicilar WHERE id = ?");
    $me->execute([$user_id]);
    $profil = $me->fetch(PDO::FETCH_ASSOC);
} else {
    $profil = false; // Eğer ID yoksa false ata
}

// Tüm kullanıcılar listesi
$users = $pdo->query("SELECT * FROM kullanicilar ORDER BY ad_soyad ASC")->fetchAll(PDO::FETCH_ASSOC);
?>

<style>
    :root { --p: #4361ee; --s: #64748b; --bg: #f8fafc; --card: #ffffff; --dark: #0f172a; --danger: #ef4444; --success: #10b981; }
    .settings-wrapper { max-width: 1000px; margin: 0 auto; padding: 15px; }
    
    .settings-nav { display: flex; gap: 10px; margin-bottom: 25px; background: #fff; padding: 10px; border-radius: 20px; border: 1px solid #e2e8f0; overflow-x: auto; }
    .nav-link { padding: 12px 25px; border-radius: 15px; color: var(--s); text-decoration: none; font-weight: 700; font-size: 0.9rem; transition: 0.3s; white-space: nowrap; cursor: pointer; border: none; background: none; }
    .nav-link.active { background: var(--p); color: white; box-shadow: 0 10px 15px -3px rgba(67, 97, 238, 0.3); }

    .settings-panel { display: none; background: white; border-radius: 30px; padding: 35px; border: 1px solid #e2e8f0; box-shadow: 0 10px 25px -5px rgba(0,0,0,0.05); }
    .settings-panel.active { display: block; animation: fadeIn 0.4s ease-out; }
    @keyframes fadeIn { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: translateY(0); } }

    .form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; margin-bottom: 25px; }
    .input-lux { width: 100%; padding: 14px; border-radius: 15px; border: 1px solid #e2e8f0; background: #f8fafc; outline: none; font-size: 0.95rem; }
    .input-lux:focus { border-color: var(--p); background: #fff; box-shadow: 0 0 0 4px rgba(67, 97, 238, 0.1); }
    
    label { display: block; font-size: 11px; font-weight: 800; color: var(--s); margin-bottom: 8px; text-transform: uppercase; }

    .user-list-item { display: flex; justify-content: space-between; align-items: center; padding: 15px; border-radius: 18px; background: #f8fafc; margin-bottom: 10px; border: 1px solid #f1f5f9; }
    .btn-save { background: var(--p); color: white; border: none; padding: 16px 40px; border-radius: 18px; font-weight: 800; cursor: pointer; width: 100%; }

    @media (max-width: 768px) { .form-grid { grid-template-columns: 1fr; } .settings-panel { padding: 20px; } }
</style>

<div class="settings-wrapper">
    <div class="settings-nav">
        <button class="nav-link active" onclick="showTab(event, 'merkez')"><i class="fas fa-hospital"></i> Kurum Ayarları</button>
        <button class="nav-link" onclick="showTab(event, 'profil')"><i class="fas fa-user-circle"></i> Profilim</button>
        <button class="nav-link" onclick="showTab(event, 'kullanicilar')"><i class="fas fa-users-cog"></i> Personel</button>
    </div>

    <div id="merkez" class="settings-panel active">
        <h3 style="margin-bottom:25px;"><i class="fas fa-id-card" style="color:var(--p)"></i> Merkez Bilgileri</h3>
        <form action="islem.php" method="POST">
            <div class="form-grid">
                <div><label>Merkez Adı</label><input type="text" name="merkez_adi" class="input-lux" value="NEPHRO Diyaliz"></div>
                <div><label>E-Posta</label><input type="email" name="merkez_eposta" class="input-lux" value="destek@nephro.com"></div>
            </div>
            <button type="submit" name="merkez_guncelle" class="btn-save">AYARLARI KAYDET</button>
        </form>
    </div>

    <div id="profil" class="settings-panel">
        <h3 style="margin-bottom:25px;"><i class="fas fa-lock" style="color:var(--warning)"></i> Kişisel Bilgiler</h3>
        <form action="islem.php" method="POST">
            <div class="form-grid">
                <div>
                    <label>Adınız Soyadınız</label>
                    <input type="text" name="ad_soyad" class="input-lux" value="<?= h($profil['ad_soyad'] ?? 'Bilinmeyen Kullanıcı') ?>">
                </div>
                <div>
                    <label>Kullanıcı Adı</label>
                    <input type="text" class="input-lux" value="<?= h($profil['kullanici_adi'] ?? 'oturum-yok') ?>" disabled style="opacity:0.6">
                </div>
                <div>
                    <label>Yeni Şifre</label>
                    <input type="password" name="yeni_sifre" class="input-lux" placeholder="Değiştirmeyecekseniz boş bırakın">
                </div>
                <div>
                    <label>Şifre Tekrar</label>
                    <input type="password" name="yeni_sifre_tekrar" class="input-lux" placeholder="Şifreyi onaylayın">
                </div>
            </div>
            <button type="submit" name="profil_guncelle" class="btn-save">BİLGİLERİMİ GÜNCELLE</button>
        </form>
    </div>

    <div id="kullanicilar" class="settings-panel">
        <div style="display:flex; justify-content:space-between; align-items:center; margin-bottom:25px;">
            <h3><i class="fas fa-user-shield" style="color:var(--success)"></i> Sistem Kullanıcıları</h3>
            <button onclick="document.getElementById('newUserModal').style.display='block'" class="btn-save" style="width:auto; padding:10px 20px; font-size:0.8rem;">+ EKLE</button>
        </div>

        <div class="user-list">
            <?php foreach($users as $u): ?>
            <div class="user-list-item">
                <div style="display:flex; align-items:center; gap:15px;">
                    <div style="width:40px; height:40px; background:var(--p); color:white; border-radius:10px; display:flex; align-items:center; justify-content:center; font-weight:800;"><?= mb_substr($u['ad_soyad'],0,1) ?></div>
                    <div>
                        <b style="display:block;"><?= h($u['ad_soyad']) ?></b>
                        <small style="color:var(--s)"><?= h($u['rol']) ?></small>
                    </div>
                </div>
                <a href="islem.php?islem=kullanici_sil&id=<?= $u['id'] ?>" style="color:var(--danger)" onclick="return confirm('Kullanıcıyı sileyim mi?')"><i class="fas fa-trash-alt"></i></a>
            </div>
            <?php endforeach; ?>
        </div>
    </div>
</div>

<script>
function showTab(evt, tabId) {
    var i, panel, nav;
    panel = document.getElementsByClassName("settings-panel");
    for (i = 0; i < panel.length; i++) { panel[i].classList.remove("active"); }
    nav = document.getElementsByClassName("nav-link");
    for (i = 0; i < nav.length; i++) { nav[i].classList.remove("active"); }
    document.getElementById(tabId).classList.add("active");
    evt.currentTarget.classList.add("active");
}
</script>