<?php
/**
 * modules/seans-baslat.php - ELITE READER & SMART GRID (v16)
 */
global $pdo;

$id = (int)($_GET['id'] ?? 0);
$hastalar = $pdo->query("SELECT id, ad, soyad, tc_no FROM hastalar WHERE durum='aktif' ORDER BY ad ASC")->fetchAll();

$hasta = null;
if ($id > 0) {
    $h_sorgu = $pdo->prepare("SELECT * FROM hastalar WHERE id = ?");
    $h_sorgu->execute([$id]);
    $hasta = $h_sorgu->fetch();
}

$cihazlar = $pdo->query("
    SELECT c.*, 
    (SELECT COUNT(*) FROM seans_programi s WHERE s.cihaz_id = c.id AND s.bitis_vakti IS NULL) as aktif_seans
    FROM cihazlar c WHERE c.durum = 'Aktif' ORDER BY c.oda_no ASC, c.id ASC
")->fetchAll();

$odalar = array_unique(array_column($cihazlar, 'oda_no'));
?>

<style>
    .elite-v16 { max-width: 1200px; margin: 0 auto; animation: fadeIn 0.4s ease; font-family: 'Inter', sans-serif; }
    
    /* ÜST PANEL: HASTA KARTI */
    .patient-card-top {
        background: #fff; border: 2px solid #e2e8f0; border-radius: 24px;
        padding: 25px 35px; margin-bottom: 30px; display: flex; align-items: center; justify-content: space-between;
        box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.05);
    }
    .p-select-elite { border: none; font-size: 1.4rem; font-weight: 800; color: #0f172a; width: 100%; outline: none; cursor: pointer; background: transparent; letter-spacing: -0.5px; }

    /* FİLTRE BAR: DAHA NET */
    .filter-bar { display: flex; flex-wrap: wrap; gap: 12px; margin-bottom: 25px; align-items: center; padding: 0 5px; }
    .f-pill {
        padding: 10px 20px; border-radius: 14px; background: #fff; border: 2px solid #e2e8f0;
        font-size: 0.85rem; font-weight: 700; color: #475569; cursor: pointer; transition: 0.3s;
    }
    .f-pill.active { background: #0f172a; color: #fff; border-color: #0f172a; transform: translateY(-2px); box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1); }
    .f-pill.status-pill.active { background: #2563eb; color: #fff; border-color: #2563eb; }

    /* CİHAZ GRİDİ: OKUNABİLİR VE NET */
    .device-grid-elite { display: grid; grid-template-columns: repeat(auto-fill, minmax(135px, 1fr)); gap: 15px; margin-bottom: 35px; }
    .d-card-elite { position: relative; cursor: pointer; transition: 0.2s; }
    .d-card-elite input { position: absolute; opacity: 0; }
    .d-inner {
        background: #fff; border: 2px solid #e2e8f0; border-radius: 20px; padding: 20px;
        text-align: center; transition: 0.3s;
    }
    .d-card-elite:hover:not(.dolu) .d-inner { border-color: #2563eb; transform: translateY(-3px); }
    .d-card-elite.dolu { opacity: 0.4; cursor: not-allowed; }
    .d-card-elite input:checked + .d-inner { border-color: #2563eb; background: #eff6ff; border-width: 3px; }
    
    .d-name { display: block; font-size: 1.1rem; font-weight: 900; color: #1e293b; margin-top: 5px; }
    .d-room { display: block; font-size: 0.75rem; color: #64748b; font-weight: 800; text-transform: uppercase; letter-spacing: 1px; }

    /* FORM ALANI: DEVASA VE OKUNAKLI */
    .form-grid-elite { 
        background: #fff; border: 2px solid #e2e8f0; border-radius: 32px; padding: 40px; 
        display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 30px;
        box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.05);
    }
    .s-group { display: flex; flex-direction: column; gap: 12px; }
    .s-label { font-size: 0.85rem; font-weight: 800; color: #64748b; text-transform: uppercase; letter-spacing: 1.5px; padding-left: 5px; }
    .s-input { 
        width: 100%; border: 2px solid #cbd5e1; border-radius: 18px; padding: 18px; 
        font-weight: 800; font-size: 1.3rem; outline: none; background: #f8fafc; transition: 0.3s;
        box-sizing: border-box;
    }
    .s-input:focus { border-color: #2563eb; background: #fff; box-shadow: 0 0 0 5px rgba(37, 99, 235, 0.1); }
    .s-input::placeholder { font-weight: 400; color: #94a3b8; font-size: 1rem; }

    .btn-launch-elite {
        grid-column: 1 / -1; background: #2563eb; color: #fff; border: none; 
        padding: 25px; border-radius: 20px; font-weight: 900; font-size: 1.2rem; 
        cursor: pointer; margin-top: 20px; transition: 0.4s;
        display: flex; align-items: center; justify-content: center; gap: 15px;
    }
    .btn-launch-elite:hover { background: #1e40af; transform: translateY(-4px); box-shadow: 0 20px 25px -5px rgba(37, 99, 235, 0.3); }

    @media (max-width: 768px) {
        .form-grid-elite { grid-template-columns: 1fr; padding: 25px; }
        .device-grid-elite { grid-template-columns: repeat(2, 1fr); }
    }
</style>

<div class="elite-v16">
    
    <div class="patient-card-top">
        <div style="flex:1">
            <select class="p-select-elite" onchange="location.href='?sayfa=yeni-seans&id='+this.value">
                <option value="">--- Hasta Seçiniz ---</option>
                <?php foreach($hastalar as $h_opt): ?>
                    <option value="<?php echo $h_opt['id']; ?>" <?php echo ($id == $h_opt['id']) ? 'selected' : ''; ?>>
                        <?php echo mb_strtoupper($h_opt['ad'].' '.$h_opt['soyad'], 'UTF-8'); ?>
                    </option>
                <?php endforeach; ?>
            </select>
        </div>
        <?php if($hasta): ?>
            <div style="text-align: right; border-left: 2px solid #f1f5f9; padding-left: 30px;">
                <b style="font-size: 1.6rem; color: #2563eb;"><?php echo $hasta['kuru_agirlik']; ?> <small style="font-size: 0.9rem;">kg</small></b>
                <div style="font-size: 0.7rem; color: #94a3b8; font-weight: 900; letter-spacing: 1px;">KURU AĞIRLIK</div>
            </div>
        <?php endif; ?>
    </div>

    <?php if($hasta): ?>
    <form action="islem.php" method="POST">
        <input type="hidden" name="hasta_id" value="<?php echo $id; ?>">

        <div class="filter-bar">
            <div class="f-pill status-pill active" data-filter="status" data-val="all" onclick="setFilter(this)">TÜMÜ</div>
            <div class="f-pill status-pill" data-filter="status" data-val="empty" onclick="setFilter(this)">BOŞ CİHAZLAR</div>
            <div style="width:2px; height:25px; background:#e2e8f0; margin:0 10px;"></div>
            <div class="f-pill oda-pill active" data-filter="oda" data-val="all" onclick="setFilter(this)">TÜM ODALAR</div>
            <?php foreach($odalar as $oda): ?>
                <div class="f-pill oda-pill" data-filter="oda" data-val="<?php echo $oda; ?>" onclick="setFilter(this)"><?php echo $oda; ?></div>
            <?php endforeach; ?>
        </div>

        <div class="device-grid-elite">
            <?php foreach($cihazlar as $c): $isFull = ($c['aktif_seans'] > 0); ?>
                <label class="d-card-elite <?php echo $isFull ? 'dolu' : 'bos'; ?>" 
                       data-oda="<?php echo $c['oda_no']; ?>" 
                       data-status="<?php echo $isFull ? 'full' : 'empty'; ?>">
                    <input type="radio" name="cihaz_id" value="<?php echo $c['id']; ?>" <?php echo $isFull ? 'disabled' : 'required'; ?>>
                    <div class="d-inner">
                        <span class="d-room"><?php echo $c['oda_no']; ?></span>
                        <span class="d-name">Cihaz #<?php echo $c['id']; ?></span>
                        <div style="margin-top:8px; font-size:0.65rem; color:#94a3b8; font-weight:600;"><?php echo $c['marka_model']; ?></div>
                    </div>
                </label>
            <?php endforeach; ?>
        </div>

        <div class="form-grid-elite">
            <div class="s-group">
                <label class="s-label"><i class="fas fa-heartbeat"></i> Tansiyon (Büyük / Küçük)</label>
                <div style="display:flex; gap:15px;">
                    <input type="number" name="tans_sys" class="s-input" placeholder="120" required>
                    <input type="number" name="tans_dia" class="s-input" placeholder="80" required>
                </div>
            </div>

            <div class="s-group">
                <label class="s-label"><i class="fas fa-weight"></i> Giriş Kilosu (KG)</label>
                <input type="number" step="0.1" name="giris_kilo" class="s-input" placeholder="75.5" required>
            </div>

            <div class="s-group">
                <label class="s-label"><i class="fas fa-wave-square"></i> Nabız (BPM)</label>
                <input type="number" name="nabiz" class="s-input" placeholder="72" required>
            </div>

            <div class="s-group">
                <label class="s-label"><i class="fas fa-tint"></i> Hedef UF (Litre)</label>
                <input type="number" step="0.1" name="hedef_uf" class="s-input" placeholder="2.5" required>
            </div>

            <div class="s-group">
                <label class="s-label"><i class="fas fa-filter"></i> Diyalizör Seçimi</label>
                <select name="diyalizor" class="s-input">
                    <option value="FX80">High-Flux FX 80</option>
                    <option value="FX100">High-Flux FX 100</option>
                    <option value="FX60">Low-Flux FX 60</option>
                </select>
            </div>

            <button type="submit" name="seans_baslat_kaydet" class="btn-launch-elite">
                <i class="fas fa-play-circle"></i> SEANSI RESMİ OLARAK BAŞLAT
            </button>
        </div>
    </form>
    <?php else: ?>
        <div style="text-align:center; padding:120px; background:#fff; border-radius:32px; border:2px dashed #e2e8f0;">
            <i class="fas fa-user-plus fa-4x" style="color:#e2e8f0; margin-bottom:25px;"></i>
            <h2 style="color:#64748b; font-weight:900;">HASTA SEÇİMİ BEKLENİYOR</h2>
            <p style="color:#94a3b8;">Tedavi sürecini başlatmak için lütfen yukarıdan bir hasta seçin.</p>
        </div>
    <?php endif; ?>
</div>

<script>
let currentFilters = { status: 'all', oda: 'all' };

function setFilter(btn) {
    const filterType = btn.getAttribute('data-filter');
    const filterVal = btn.getAttribute('data-val');

    document.querySelectorAll('.' + filterType + '-pill').forEach(p => p.classList.remove('active'));
    btn.classList.add('active');

    currentFilters[filterType] = filterVal;
    applyFilters();
}

function applyFilters() {
    document.querySelectorAll('.d-card-elite').forEach(card => {
        const cardOda = card.getAttribute('data-oda');
        const cardStatus = card.getAttribute('data-status');

        const odaMatch = (currentFilters.oda === 'all' || currentFilters.oda === cardOda);
        const statusMatch = (currentFilters.status === 'all' || (currentFilters.status === 'empty' && cardStatus === 'empty'));

        card.style.display = (odaMatch && statusMatch) ? 'block' : 'none';
    });
}
</script>