<?php
/**
 * modules/seans-baslat.php - Hasta Listesi Tema Uyumlu
 */
global $pdo;

// URL'den gelen ID'leri al
$id      = (int)($_GET['id'] ?? 0);
$plan_id = (int)($_GET['plan_id'] ?? 0);

// Helper function
if (!function_exists('h')) {
    function h($s){ return htmlspecialchars((string)$s, ENT_QUOTES, "UTF-8"); }
}

// HASTALARI ÇEK
$hastalar = $pdo->query("SELECT id, ad, soyad, tc_no FROM hastalar WHERE durum='aktif' ORDER BY ad ASC")->fetchAll(PDO::FETCH_ASSOC);

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

// CİHAZLARI ÇEK (aktif seans kontrolü)
$cihazlar = $pdo->query("
    SELECT c.*,
    (SELECT COUNT(*) FROM seans_programi s 
     WHERE s.cihaz_id = c.id AND s.bitis_vakti IS NULL AND s.durum = 'Basladi') as aktif_seans
    FROM cihazlar c
    WHERE c.durum = 'Aktif'
    ORDER BY c.oda_no ASC, c.id ASC
")->fetchAll(PDO::FETCH_ASSOC);

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

// Takvimden geldiyse: planlanan cihazı seçili yap
$on_secili_cihaz = 0;
if ($plan_id > 0) {
    $plan_check = $pdo->prepare("SELECT cihaz_id FROM seans_programi WHERE id = ?");
    $plan_check->execute([$plan_id]);
    $on_secili_cihaz = (int)$plan_check->fetchColumn();
}

// TC mask
function mask_tc($tc) {
    $tc = (string)$tc;
    if (strlen($tc) < 11) return $tc;
    return substr($tc, 0, 3) . "******" . substr($tc, -2);
}

// İstatistikler
$bosCihaz = 0;
$doluCihaz = 0;
foreach($cihazlar as $c){
    $c['aktif_seans'] > 0 ? $doluCihaz++ : $bosCihaz++;
}
?>
<!DOCTYPE html>
<html lang="tr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Seans Başlat - NEPHRO ERP</title>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        /* ===== CSS DEĞİŞKENLERİ (Hasta Listesi ile aynı) ===== */
        :root {
            --primary: #2563eb;
            --primary-light: #dbeafe;
            --primary-dark: #1d4ed8;
            --secondary: #64748b;
            --success: #059669;
            --warning: #d97706;
            --danger: #dc2626;
            --danger-light: #fee2e2;
            --background: #f8fafc;
            --surface: #ffffff;
            --border: #e2e8f0;
            --text: #1e293b;
            --text-light: #64748b;
            --text-muted: #94a3b8;
            --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
            --shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
            --shadow-md: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
            --radius: 8px;
            --radius-lg: 12px;
            --transition: all 0.2s ease;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
            background: var(--background);
            color: var(--text);
            line-height: 1.5;
            font-size: 14px;
            font-weight: 400;
        }

        /* ===== LAYOUT ===== */
        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
            min-height: 100vh;
        }

        /* ===== HEADER ===== */
        .page-header {
            margin-bottom: 32px;
        }

        .page-header h1 {
            font-size: 28px;
            font-weight: 700;
            color: var(--text);
            margin-bottom: 4px;
            letter-spacing: -0.02em;
        }

        .page-header p {
            color: var(--text-light);
            font-size: 15px;
            font-weight: 400;
        }

        /* ===== STATS CARDS ===== */
        .stats-container {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 16px;
            margin-bottom: 32px;
        }

        .stat-card {
            background: var(--surface);
            border: 1px solid var(--border);
            border-radius: var(--radius-lg);
            padding: 20px;
            transition: var(--transition);
        }

        .stat-card:hover {
            border-color: var(--primary);
            box-shadow: var(--shadow);
        }

        .stat-value {
            font-size: 32px;
            font-weight: 700;
            margin-bottom: 8px;
            line-height: 1;
        }

        .stat-label {
            font-size: 13px;
            color: var(--text-light);
            font-weight: 500;
            text-transform: uppercase;
            letter-spacing: 0.05em;
        }

        /* ===== HASTA SEÇİMİ ===== */
        .patient-select-container {
            background: var(--surface);
            border: 1px solid var(--border);
            border-radius: var(--radius-lg);
            padding: 24px;
            margin-bottom: 24px;
        }

        .patient-select-label {
            display: block;
            font-size: 13px;
            font-weight: 600;
            color: var(--text-light);
            margin-bottom: 12px;
            text-transform: uppercase;
            letter-spacing: 0.05em;
        }

        .patient-select {
            width: 100%;
            padding: 12px 20px;
            border: 1px solid var(--border);
            border-radius: var(--radius);
            font-size: 16px;
            font-weight: 500;
            color: var(--text);
            background: var(--surface);
            cursor: pointer;
            transition: var(--transition);
        }

        .patient-select:focus {
            outline: none;
            border-color: var(--primary);
            box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
        }

        .patient-info-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 16px;
            margin-top: 20px;
        }

        .info-card {
            background: var(--background);
            border: 1px solid var(--border);
            border-radius: var(--radius);
            padding: 16px;
        }

        .info-label {
            font-size: 11px;
            color: var(--text-muted);
            font-weight: 500;
            text-transform: uppercase;
            letter-spacing: 0.05em;
            margin-bottom: 4px;
        }

        .info-value {
            font-size: 16px;
            font-weight: 600;
            color: var(--text);
        }

        /* ===== FİLTRELER ===== */
        .filter-container {
            background: var(--surface);
            border: 1px solid var(--border);
            border-radius: var(--radius-lg);
            padding: 20px;
            margin-bottom: 24px;
        }

        .filter-row {
            display: flex;
            flex-wrap: wrap;
            gap: 12px;
            align-items: center;
            margin-bottom: 16px;
        }

        .filter-group {
            display: flex;
            gap: 8px;
            flex-wrap: wrap;
        }

        .filter-btn {
            padding: 8px 16px;
            border: 1px solid var(--border);
            background: var(--background);
            border-radius: 20px;
            font-size: 13px;
            font-weight: 500;
            color: var(--text-light);
            cursor: pointer;
            transition: var(--transition);
        }

        .filter-btn:hover {
            background: #e2e8f0;
        }

        .filter-btn.active {
            background: var(--primary-light);
            border-color: var(--primary);
            color: var(--primary);
        }

        .search-box {
            flex: 1;
            min-width: 250px;
            position: relative;
        }

        .search-box input {
            width: 100%;
            padding: 8px 16px 8px 40px;
            border: 1px solid var(--border);
            border-radius: 20px;
            font-size: 14px;
            background: var(--surface);
            color: var(--text);
            transition: var(--transition);
        }

        .search-box input:focus {
            outline: none;
            border-color: var(--primary);
            box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
        }

        .search-box i {
            position: absolute;
            left: 14px;
            top: 50%;
            transform: translateY(-50%);
            color: var(--text-muted);
        }

        /* ===== CİHAZ GRİD ===== */
        .device-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
            gap: 16px;
            margin-bottom: 24px;
        }

        @media (max-width: 768px) {
            .device-grid {
                grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
            }
        }

        .device-card {
            background: var(--surface);
            border: 1px solid var(--border);
            border-radius: var(--radius-lg);
            padding: 16px;
            cursor: pointer;
            transition: var(--transition);
            position: relative;
        }

        .device-card:hover {
            border-color: var(--primary);
            transform: translateY(-2px);
            box-shadow: var(--shadow-md);
        }

        .device-card input {
            position: absolute;
            opacity: 0;
            pointer-events: none;
        }

        .device-card input:checked + .device-content {
            border-color: var(--primary);
            box-shadow: 0 0 0 2px var(--primary-light);
        }

        .device-content {
            border: 2px solid transparent;
            border-radius: var(--radius);
            padding: 12px;
            transition: var(--transition);
        }

        .device-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 12px;
        }

        .device-room {
            font-size: 12px;
            font-weight: 600;
            color: var(--text-light);
            text-transform: uppercase;
            letter-spacing: 0.05em;
        }

        .status-dot {
            width: 10px;
            height: 10px;
            border-radius: 50%;
            background: var(--success);
        }

        .status-dot.busy {
            background: var(--danger);
        }

        .device-body {
            text-align: center;
            padding: 8px 0;
        }

        .device-icon {
            font-size: 28px;
            color: #cbd5e1;
            margin-bottom: 8px;
        }

        .device-id {
            font-size: 18px;
            font-weight: 600;
            color: var(--text);
            margin-bottom: 8px;
        }

        .device-status {
            display: inline-block;
            padding: 4px 12px;
            border-radius: 20px;
            font-size: 11px;
            font-weight: 700;
            text-transform: uppercase;
            letter-spacing: 0.05em;
        }

        .status-available {
            background: rgba(5, 150, 105, 0.1);
            color: var(--success);
            border: 1px solid rgba(5, 150, 105, 0.2);
        }

        .status-busy {
            background: rgba(220, 38, 38, 0.1);
            color: var(--danger);
            border: 1px solid rgba(220, 38, 38, 0.2);
        }

        /* ===== UYARI ===== */
        .warning-container {
            background: var(--danger-light);
            border: 1px solid rgba(220, 38, 38, 0.2);
            border-radius: var(--radius-lg);
            padding: 20px;
            margin-bottom: 24px;
            display: none;
        }

        .warning-content {
            display: flex;
            align-items: center;
            justify-content: space-between;
            gap: 16px;
            flex-wrap: wrap;
        }

        .warning-text {
            display: flex;
            align-items: center;
            gap: 12px;
            color: var(--danger);
            font-weight: 500;
        }

        .warning-check {
            display: flex;
            align-items: center;
            gap: 8px;
            font-size: 14px;
            color: var(--danger);
            font-weight: 500;
        }

        .warning-check input {
            width: 18px;
            height: 18px;
        }

        /* ===== FORM ===== */
        .form-container {
            background: var(--surface);
            border: 1px solid var(--border);
            border-radius: var(--radius-lg);
            padding: 24px;
            margin-bottom: 40px;
        }

        .form-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
            gap: 20px;
            margin-bottom: 24px;
        }

        .form-group {
            margin-bottom: 16px;
        }

        .form-label {
            display: block;
            font-size: 13px;
            font-weight: 600;
            color: var(--text-light);
            margin-bottom: 8px;
            text-transform: uppercase;
            letter-spacing: 0.05em;
        }

        .form-input, .form-select {
            width: 100%;
            padding: 12px 16px;
            border: 1px solid var(--border);
            border-radius: var(--radius);
            font-size: 15px;
            font-weight: 400;
            color: var(--text);
            background: var(--surface);
            transition: var(--transition);
        }

        .form-input:focus, .form-select:focus {
            outline: none;
            border-color: var(--primary);
            box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
        }

        .inline-group {
            display: flex;
            gap: 12px;
        }

        .inline-group .form-input {
            flex: 1;
        }

        .form-hint {
            font-size: 12px;
            color: var(--text-muted);
            margin-top: 6px;
            font-weight: 400;
        }

        .submit-btn {
            width: 100%;
            padding: 16px;
            background: var(--primary);
            color: white;
            border: none;
            border-radius: var(--radius);
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: var(--transition);
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 10px;
        }

        .submit-btn:hover {
            background: var(--primary-dark);
            transform: translateY(-2px);
            box-shadow: var(--shadow-md);
        }

        /* ===== BOŞ DURUM ===== */
        .empty-state {
            text-align: center;
            padding: 60px 20px;
            background: var(--surface);
            border-radius: var(--radius-lg);
            border: 2px dashed var(--border);
        }

        .empty-icon {
            font-size: 48px;
            color: #cbd5e1;
            margin-bottom: 20px;
        }

        .empty-state h2 {
            font-size: 20px;
            font-weight: 600;
            color: var(--text);
            margin-bottom: 8px;
        }

        .empty-state p {
            color: var(--text-light);
            font-size: 15px;
            max-width: 400px;
            margin: 0 auto;
        }

        /* ===== RESPONSIVE ===== */
        @media (max-width: 768px) {
            .container {
                padding: 16px;
            }
            
            .filter-row {
                flex-direction: column;
                align-items: stretch;
            }
            
            .search-box {
                min-width: 100%;
            }
            
            .filter-group {
                justify-content: center;
            }
            
            .warning-content {
                flex-direction: column;
                align-items: flex-start;
            }
            
            .form-grid {
                grid-template-columns: 1fr;
            }
        }

        @media (max-width: 480px) {
            .stats-container {
                grid-template-columns: 1fr;
            }
            
            .device-grid {
                grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
            }
            
            .inline-group {
                flex-direction: column;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <!-- Başlık -->
        <div class="page-header">
            <h1>Seans Başlat</h1>
            <p>Hasta seçin, cihaz belirleyin ve seans parametrelerini girin</p>
        </div>

        <!-- İstatistikler -->
        <div class="stats-container">
            <div class="stat-card">
                <div class="stat-value" style="color: var(--success);"><?= $bosCihaz ?></div>
                <div class="stat-label">Müsait Cihaz</div>
            </div>
            <div class="stat-card">
                <div class="stat-value" style="color: var(--danger);"><?= $doluCihaz ?></div>
                <div class="stat-label">Meşgul Cihaz</div>
            </div>
            <div class="stat-card">
                <div class="stat-value"><?= count($cihazlar) ?></div>
                <div class="stat-label">Toplam Cihaz</div>
            </div>
            <div class="stat-card">
                <div class="stat-value" style="color: var(--primary);"><?= $id ? '1' : '0' ?></div>
                <div class="stat-label">Seçili Hasta</div>
            </div>
        </div>

        <!-- Hasta Seçimi -->
        <div class="patient-select-container">
            <label class="patient-select-label">Hasta Seçimi</label>
            <select class="patient-select" onchange="location.href='?sayfa=seans-baslat&id='+this.value+'&plan_id=<?= $plan_id ?>'">
                <option value="">Hasta seçiniz</option>
                <?php foreach ($hastalar as $h): ?>
                    <option value="<?= (int)$h['id'] ?>" <?= $id == $h['id'] ? 'selected' : '' ?>>
                        <?= h($h['ad'] . ' ' . $h['soyad']) ?>
                    </option>
                <?php endforeach; ?>
            </select>
            
            <?php if ($hasta): ?>
            <div class="patient-info-grid">
                <div class="info-card">
                    <div class="info-label">Kuru Ağırlık</div>
                    <div class="info-value"><?= h($hasta['kuru_agirlik'] ?? '0') ?> kg</div>
                </div>
                <div class="info-card">
                    <div class="info-label">TC No</div>
                    <div class="info-value"><?= mask_tc($hasta['tc_no'] ?? '') ?></div>
                </div>
                <div class="info-card">
                    <div class="info-label">Hepatit Durumu</div>
                    <div class="info-value"><?= h($hasta['hepatit_durumu'] ?? 'Belirtilmemiş') ?></div>
                </div>
                <div class="info-card">
                    <div class="info-label">Damar Yolu</div>
                    <div class="info-value"><?= h($hasta['damar_yolu'] ?? 'Belirtilmemiş') ?></div>
                </div>
            </div>
            <?php endif; ?>
        </div>

        <?php if ($hasta): ?>
        <form action="islem.php" method="POST" id="seansForm">
            <input type="hidden" name="hasta_id" value="<?= $id ?>">
            <input type="hidden" name="plan_id" value="<?= $plan_id ?>">
            
            <!-- Filtreler -->
            <div class="filter-container">
                <div class="filter-row">
                    <div class="filter-group">
                        <button type="button" class="filter-btn active" data-filter="all">Tümü</button>
                        <button type="button" class="filter-btn" data-filter="empty">Boş</button>
                        <button type="button" class="filter-btn" data-filter="busy">Dolu</button>
                        
                        <?php foreach ($odalar as $oda): ?>
                        <button type="button" class="filter-btn" data-room="<?= $oda ?>">Oda <?= $oda ?></button>
                        <?php endforeach; ?>
                    </div>
                    
                    <div class="search-box">
                        <i class="fas fa-search"></i>
                        <input type="text" id="deviceSearch" placeholder="Cihaz ara...">
                    </div>
                </div>
            </div>

            <!-- Cihaz Listesi -->
            <div class="device-grid" id="deviceGrid">
                <?php foreach ($cihazlar as $c): 
                    $dolu = $c['aktif_seans'] > 0;
                    $checked = $c['id'] == $on_secili_cihaz ? 'checked' : '';
                ?>
                <label class="device-card" 
                       data-status="<?= $dolu ? 'busy' : 'empty' ?>"
                       data-room="<?= $c['oda_no'] ?>"
                       data-search="<?= strtolower('oda ' . $c['oda_no'] . ' cihaz ' . $c['id']) ?>">
                    <input type="radio" name="cihaz_id" value="<?= $c['id'] ?>" <?= $checked ?> required onchange="onDeviceSelect(this)">
                    <div class="device-content">
                        <div class="device-header">
                            <div class="device-room">Oda <?= $c['oda_no'] ?></div>
                            <div class="status-dot <?= $dolu ? 'busy' : '' ?>"></div>
                        </div>
                        <div class="device-body">
                            <div class="device-icon"><i class="fas fa-desktop"></i></div>
                            <div class="device-id">#<?= $c['id'] ?></div>
                            <div class="device-status <?= $dolu ? 'status-busy' : 'status-available' ?>">
                                <?= $dolu ? 'MEŞGUL' : 'MÜSAİT' ?>
                            </div>
                        </div>
                    </div>
                </label>
                <?php endforeach; ?>
            </div>

            <!-- Meşgul Cihaz Uyarısı -->
            <div class="warning-container" id="busyWarning">
                <div class="warning-content">
                    <div class="warning-text">
                        <i class="fas fa-exclamation-triangle"></i>
                        <span>Seçili cihaz meşgul görünüyor. Devam etmek istiyor musunuz?</span>
                    </div>
                    <label class="warning-check">
                        <input type="checkbox" id="busyConfirm" name="dolu_cihaz_onay" value="1">
                        Evet, devam etmek istiyorum
                    </label>
                </div>
            </div>

            <!-- Seans Parametreleri -->
            <div class="form-container">
                <div class="form-grid">
                    <div class="form-group">
                        <label class="form-label">Tansiyon (SYS/DIA)</label>
                        <div class="inline-group">
                            <input type="number" name="tans_sys" class="form-input" placeholder="120" min="50" max="300" required>
                            <input type="number" name="tans_dia" class="form-input" placeholder="80" min="30" max="200" required>
                        </div>
                        <div class="form-hint">Örnek: 120 / 80</div>
                    </div>
                    
                    <div class="form-group">
                        <label class="form-label">Giriş Kilosu (kg)</label>
                        <input type="number" step="0.1" name="giris_kilo" class="form-input" placeholder="75.5" min="20" max="300" required>
                        <div class="form-hint">Ondalıklı giriş desteklenir</div>
                    </div>
                    
                    <div class="form-group">
                        <label class="form-label">Nabız (bpm)</label>
                        <input type="number" name="nabiz" class="form-input" placeholder="72" min="30" max="200" required>
                        <div class="form-hint">Dakikadaki kalp atış sayısı</div>
                    </div>
                    
                    <div class="form-group">
                        <label class="form-label">Hedef UF (L)</label>
                        <input type="number" step="0.1" name="hedef_uf" class="form-input" placeholder="2.5" min="0" max="10" required>
                        <div class="form-hint">Litre bazında hedef ultrafiltrasyon</div>
                    </div>
                    
                    <div class="form-group">
                        <label class="form-label">Diyalizör Modeli</label>
                        <select name="diyalizor" class="form-select">
                            <option value="FX80">FX 80</option>
                            <option value="FX100" selected>FX 100</option>
                            <option value="FX60">FX 60</option>
                        </select>
                        <div class="form-hint">Merkez standartlarına göre seçin</div>
                    </div>
                </div>
                
                <button type="submit" name="seans_baslat_kaydet_yeni" class="submit-btn">
                    <i class="fas fa-play-circle"></i> Seansı Başlat
                </button>
            </div>
        </form>
        
        <?php else: ?>
        <!-- Hasta Seçilmedi -->
        <div class="empty-state">
            <div class="empty-icon"><i class="fas fa-user-injured"></i></div>
            <h2>Hasta Seçilmemiş</h2>
            <p>Seans başlatmak için yukarıdaki listeden bir hasta seçin</p>
        </div>
        <?php endif; ?>
    </div>

    <script>
        // Filtreleme ve Arama
        const filterBtns = document.querySelectorAll('.filter-btn');
        const searchInput = document.getElementById('deviceSearch');
        const deviceCards = document.querySelectorAll('.device-card');
        const warningContainer = document.getElementById('busyWarning');
        const confirmCheckbox = document.getElementById('busyConfirm');

        // Filtre butonları
        filterBtns.forEach(btn => {
            btn.addEventListener('click', function() {
                // Tüm butonlardan active class'ını kaldır
                filterBtns.forEach(b => b.classList.remove('active'));
                // Tıklanan butona active class'ını ekle
                this.classList.add('active');
                filterDevices();
            });
        });

        // Arama
        if (searchInput) {
            searchInput.addEventListener('input', function() {
                filterDevices();
            });
        }

        // Cihaz filtreleme fonksiyonu
        function filterDevices() {
            const activeFilter = document.querySelector('.filter-btn.active');
            const filterType = activeFilter?.getAttribute('data-filter') || 'all';
            const roomFilter = activeFilter?.getAttribute('data-room');
            const searchText = searchInput?.value.toLowerCase() || '';

            deviceCards.forEach(card => {
                const status = card.getAttribute('data-status');
                const room = card.getAttribute('data-room');
                const search = card.getAttribute('data-search') || '';

                let show = true;

                // Durum filtresi
                if (filterType === 'empty' && status !== 'empty') show = false;
                if (filterType === 'busy' && status !== 'busy') show = false;
                
                // Oda filtresi
                if (roomFilter && room !== roomFilter) show = false;
                
                // Arama filtresi
                if (searchText && !search.includes(searchText)) show = false;

                card.style.display = show ? 'block' : 'none';
            });
        }

        // Meşgul cihaz seçimi
        function onDeviceSelect(radio) {
            const card = radio.closest('.device-card');
            const isBusy = card?.getAttribute('data-status') === 'busy';

            if (warningContainer && confirmCheckbox) {
                if (isBusy) {
                    warningContainer.style.display = 'block';
                    confirmCheckbox.required = true;
                } else {
                    warningContainer.style.display = 'none';
                    confirmCheckbox.required = false;
                    confirmCheckbox.checked = false;
                }
            }
        }

        // Sayfa yüklendiğinde meşgul cihaz kontrolü
        document.addEventListener('DOMContentLoaded', function() {
            const selectedDevice = document.querySelector('input[name="cihaz_id"]:checked');
            if (selectedDevice) {
                onDeviceSelect(selectedDevice);
            }
            
            // ESC tuşu ile aramayı temizle
            document.addEventListener('keydown', function(e) {
                if (e.key === 'Escape' && searchInput) {
                    searchInput.value = '';
                    filterDevices();
                }
            });
        });

        // Form gönderim kontrolü
        const seansForm = document.getElementById('seansForm');
        if (seansForm) {
            seansForm.addEventListener('submit', function(e) {
                const selectedDevice = document.querySelector('input[name="cihaz_id"]:checked');
                const isBusy = selectedDevice?.closest('.device-card')?.getAttribute('data-status') === 'busy';
                
                if (isBusy && confirmCheckbox && !confirmCheckbox.checked) {
                    e.preventDefault();
                    alert('Meşgul cihaz için onay vermeniz gerekiyor!');
                    confirmCheckbox.focus();
                }
            });
        }
    </script>
</body>
</html>