<?php
/**
 * AKTİF SEANS DETAY - NEPHRO ERP
 */
global $pdo;
$seans_id = (int)($_GET['seans_id'] ?? 0);

// Seans verilerini getir
$sql = "SELECT s.*, h.ad, h.soyad, h.tc_no, h.kan_grubu, h.kuru_agirlik, 
               c.marka_model, c.oda_no, c.id as cihaz_id
        FROM seans_programi s 
        JOIN hastalar h ON s.hasta_id = h.id 
        JOIN cihazlar c ON s.cihaz_id = c.id 
        WHERE s.id = ? AND s.bitis_vakti IS NULL";
$stmt = $pdo->prepare($sql);
$stmt->execute([$seans_id]);
$seans = $stmt->fetch();

if (!$seans) {
    echo '<div class="container"><div class="empty-state">
            <div class="empty-icon"><i class="fas fa-search"></i></div>
            <h2>Seans Bulunamadı</h2>
            <p>Aradığınız seans bulunamadı veya sonlandırılmış</p>
            <a href="?sayfa=aktif-seans" class="btn" style="margin-top:20px;">
                <i class="fas fa-arrow-left"></i> Aktif Seanslara Dön
            </a>
          </div></div>';
    return;
}

// Gözlem verilerini getir
$gozlemler = $pdo->prepare("SELECT * FROM seans_gozlemleri 
                           WHERE seans_id = ? 
                           ORDER BY kayit_vakti DESC");
$gozlemler->execute([$seans_id]);
$gozlem_listesi = $gozlemler->fetchAll();

// Hasta adının baş harfleri
$ad = $seans['ad'] ?? '';
$soyad = $seans['soyad'] ?? '';
$initials = mb_strtoupper(
    mb_substr($ad, 0, 1, 'UTF-8') . 
    mb_substr($soyad, 0, 1, 'UTF-8'), 
    'UTF-8'
);

if (empty($initials)) {
    $initials = 'H';
}

// Süre hesaplama
$baslangic = strtotime($seans['baslangic_vakti']);
$sure_fark = time() - $baslangic;
$sure_saat = floor($sure_fark / 3600);
$sure_dakika = floor(($sure_fark % 3600) / 60);
$sure = ($sure_saat > 0 ? $sure_saat . 'sa ' : '') . $sure_dakika . 'dk';
?>
<!DOCTYPE html>
<html lang="tr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Seans Detay - 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İ (Tema 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: 1400px;
            margin: 0 auto;
            padding: 20px;
            min-height: 100vh;
        }

        /* ===== HEADER ===== */
        .page-header {
            display: flex;
            justify-content: space-between;
            align-items: flex-start;
            margin-bottom: 32px;
            flex-wrap: wrap;
            gap: 20px;
        }

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

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

        .header-actions {
            display: flex;
            gap: 12px;
            flex-wrap: wrap;
        }

        /* ===== HASTA BİLGİ KARTI ===== */
        .patient-card {
            background: var(--surface);
            border: 1px solid var(--border);
            border-radius: var(--radius-lg);
            padding: 24px;
            margin-bottom: 24px;
            display: grid;
            grid-template-columns: auto 1fr auto;
            gap: 24px;
            align-items: center;
        }

        @media (max-width: 768px) {
            .patient-card {
                grid-template-columns: 1fr;
                text-align: center;
            }
        }

        .patient-avatar {
            width: 70px;
            height: 70px;
            background: var(--primary-light);
            color: var(--primary);
            border-radius: var(--radius-lg);
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: 700;
            font-size: 24px;
            flex-shrink: 0;
        }

        .patient-info h2 {
            font-size: 22px;
            font-weight: 700;
            color: var(--text);
            margin-bottom: 8px;
        }

        .patient-details {
            display: flex;
            gap: 16px;
            flex-wrap: wrap;
            margin-top: 12px;
        }

        .detail-chip {
            display: inline-flex;
            align-items: center;
            gap: 6px;
            padding: 6px 12px;
            background: var(--background);
            border: 1px solid var(--border);
            border-radius: 20px;
            font-size: 13px;
            font-weight: 500;
        }

        .session-info {
            text-align: right;
        }

        @media (max-width: 768px) {
            .session-info {
                text-align: center;
            }
        }

        .session-time {
            font-size: 24px;
            font-weight: 700;
            color: var(--primary);
            margin-bottom: 4px;
        }

        .session-duration {
            font-size: 14px;
            color: var(--text-light);
        }

        /* ===== ANA İÇERİK LAYOUT ===== */
        .main-layout {
            display: grid;
            grid-template-columns: 350px 1fr;
            gap: 24px;
        }

        @media (max-width: 1024px) {
            .main-layout {
                grid-template-columns: 1fr;
            }
        }

        /* ===== SOL SİDEBAR (Gözlem Formu) ===== */
        .sidebar {
            position: sticky;
            top: 20px;
            height: fit-content;
        }

        .form-card {
            background: var(--surface);
            border: 1px solid var(--border);
            border-radius: var(--radius-lg);
            padding: 24px;
            margin-bottom: 16px;
        }

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

        .form-card p {
            color: var(--text-light);
            font-size: 14px;
            margin-bottom: 20px;
        }

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

        .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 {
            width: 100%;
            padding: 12px 16px;
            border: 1px solid var(--border);
            border-radius: var(--radius);
            font-size: 15px;
            font-weight: 500;
            color: var(--text);
            background: var(--surface);
            transition: var(--transition);
        }

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

        .form-row {
            display: flex;
            gap: 12px;
        }

        .form-row .form-input {
            flex: 1;
        }

        textarea.form-input {
            resize: vertical;
            min-height: 80px;
            font-weight: 400;
        }

        .submit-btn {
            width: 100%;
            padding: 14px;
            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(-1px);
            box-shadow: var(--shadow);
        }

        /* ===== BİTİR BUTONU ===== */
        .end-session-card {
            background: var(--danger-light);
            border: 1px solid rgba(220, 38, 38, 0.2);
            border-radius: var(--radius-lg);
            padding: 24px;
            text-align: center;
        }

        .end-session-card h3 {
            color: var(--danger);
            font-size: 18px;
            font-weight: 600;
            margin-bottom: 12px;
        }

        .end-session-card p {
            color: #991b1b;
            font-size: 14px;
            margin-bottom: 20px;
        }

        .btn-danger {
            width: 100%;
            padding: 14px;
            background: var(--danger);
            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;
        }

        .btn-danger:hover {
            background: #b91c1c;
        }

        /* ===== SAĞ İÇERİK (Gözlem Tablosu) ===== */
        .content {
            background: var(--surface);
            border: 1px solid var(--border);
            border-radius: var(--radius-lg);
            overflow: hidden;
        }

        .content-header {
            padding: 24px;
            border-bottom: 1px solid var(--border);
            display: flex;
            justify-content: space-between;
            align-items: center;
            flex-wrap: wrap;
            gap: 12px;
        }

        .content-header h2 {
            font-size: 20px;
            font-weight: 600;
            color: var(--text);
        }

        .observation-count {
            font-size: 13px;
            color: var(--text-light);
            font-weight: 500;
        }

        /* ===== TABLO ===== */
        .observation-table {
            width: 100%;
            border-collapse: collapse;
        }

        .observation-table th {
            background: var(--background);
            padding: 16px 24px;
            text-align: left;
            font-size: 12px;
            font-weight: 600;
            color: var(--text-light);
            text-transform: uppercase;
            letter-spacing: 0.05em;
            border-bottom: 1px solid var(--border);
        }

        .observation-table td {
            padding: 20px 24px;
            border-bottom: 1px solid var(--border);
            font-size: 14px;
            font-weight: 500;
        }

        .observation-table tr:last-child td {
            border-bottom: none;
        }

        .observation-table tr:hover {
            background: var(--background);
        }

        .time-cell {
            font-weight: 600;
            color: var(--text);
            white-space: nowrap;
        }

        .value-cell {
            font-weight: 700;
            color: var(--text);
        }

        .unit {
            font-size: 12px;
            color: var(--text-muted);
            font-weight: 400;
            margin-left: 4px;
        }

        .notes-cell {
            max-width: 250px;
            word-wrap: break-word;
            font-weight: 400;
            color: var(--text);
        }

        /* ===== BOŞ DURUM ===== */
        .empty-observations {
            text-align: center;
            padding: 60px 20px;
        }

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

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

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

        /* ===== GENEL BUTONLAR ===== */
        .btn {
            display: inline-flex;
            align-items: center;
            gap: 8px;
            padding: 10px 20px;
            border-radius: var(--radius);
            font-size: 14px;
            font-weight: 500;
            text-decoration: none;
            transition: var(--transition);
            border: 1px solid transparent;
            cursor: pointer;
        }

        .btn-outline {
            background: var(--surface);
            color: var(--text);
            border-color: var(--border);
        }

        .btn-outline:hover {
            background: var(--background);
            border-color: var(--primary);
        }

        /* ===== RESPONSIVE ===== */
        @media (max-width: 768px) {
            .container {
                padding: 16px;
            }
            
            .page-header {
                flex-direction: column;
            }
            
            .header-actions {
                width: 100%;
                justify-content: center;
            }
            
            .patient-details {
                justify-content: center;
            }
            
            .form-row {
                flex-direction: column;
            }
            
            .observation-table {
                display: block;
                overflow-x: auto;
            }
            
            .observation-table th,
            .observation-table td {
                padding: 12px 16px;
            }
        }

        @media (max-width: 480px) {
            .patient-card {
                padding: 20px;
            }
            
            .form-card,
            .end-session-card {
                padding: 20px;
            }
            
            .content-header {
                padding: 20px;
                flex-direction: column;
                text-align: center;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <!-- Header -->
        <div class="page-header">
            <div class="page-title">
                <h1>Seans Detay</h1>
                <p>Hasta gözlemleri ve vital bulgular</p>
            </div>
            <div class="header-actions">
                <a href="?sayfa=aktif-seans" class="btn btn-outline">
                    <i class="fas fa-arrow-left"></i> Aktif Seanslara Dön
                </a>
                <a href="?sayfa=hasta-detay&id=<?= $seans['hasta_id'] ?>" class="btn btn-outline">
                    <i class="fas fa-user"></i> Hasta Detayı
                </a>
            </div>
        </div>

        <!-- Hasta Bilgi Kartı -->
        <div class="patient-card">
            <div class="patient-avatar"><?= $initials ?></div>
            <div class="patient-info">
                <h2><?= htmlspecialchars(mb_strtoupper($ad . ' ' . $soyad, 'UTF-8')) ?></h2>
                <div class="patient-details">
                    <span class="detail-chip">
                        <i class="fas fa-hospital"></i> Oda <?= htmlspecialchars($seans['oda_no']) ?>
                    </span>
                    <span class="detail-chip">
                        <i class="fas fa-desktop"></i> <?= htmlspecialchars($seans['marka_model']) ?>
                    </span>
                    <span class="detail-chip">
                        <i class="fas fa-tint"></i> <?= htmlspecialchars($seans['kan_grubu']) ?>
                    </span>
                    <span class="detail-chip">
                        <i class="fas fa-weight"></i> <?= htmlspecialchars($seans['kuru_agirlik']) ?> kg
                    </span>
                </div>
            </div>
            <div class="session-info">
                <div class="session-time">
                    <?= date('H:i', $baslangic) ?>
                </div>
                <div class="session-duration">
                    <i class="fas fa-clock"></i> <?= $sure ?> süredir devam ediyor
                </div>
            </div>
        </div>

        <!-- Ana İçerik -->
        <div class="main-layout">
            <!-- Sol Sidebar - Gözlem Formu -->
            <div class="sidebar">
                <!-- Gözlem Ekleme Formu -->
                <div class="form-card">
                    <h3>Yeni Gözlem Ekle</h3>
                    <p>Hastanın güncel vital bulgularını kaydedin</p>
                    
                    <form action="islem.php" method="POST">
                        <input type="hidden" name="seans_id" value="<?= $seans_id ?>">
                        
                        <div class="form-group">
                            <label class="form-label">Tansiyon (SYS/DIA)</label>
                            <div class="form-row">
                                <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>
                        
                        <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>
                        
                        <div class="form-group">
                            <label class="form-label">Kan Akış Hızı (ml/dk)</label>
                            <input type="number" name="kan_akis" class="form-input" placeholder="300" min="0" max="500">
                        </div>
                        
                        <div class="form-group">
                            <label class="form-label">Gözlem Notları</label>
                            <textarea name="notlar" class="form-input" placeholder="Hasta durumu ile ilgili notlar..."></textarea>
                        </div>
                        
                        <button type="submit" name="gozlem_ekle" class="submit-btn">
                            <i class="fas fa-save"></i> Gözlemi Kaydet
                        </button>
                    </form>
                </div>

                <!-- Seansı Bitirme -->
                <div class="end-session-card">
                    <h3>Seansı Sonlandır</h3>
                    <p>Bu işlem seansı tamamen bitirecek ve hastayı listeden kaldıracaktır.</p>
                    <a href="islem.php?seans_bitir=<?= $seans_id ?>" 
                       class="btn-danger" 
                       onclick="return confirm('Seansı sonlandırmak istediğinize emin misiniz?')">
                        <i class="fas fa-stop-circle"></i> Seansı Sonlandır
                    </a>
                </div>
            </div>

            <!-- Sağ İçerik - Gözlem Tablosu -->
            <div class="content">
                <div class="content-header">
                    <h2>Gözlem Geçmişi</h2>
                    <div class="observation-count">
                        <i class="fas fa-list-check"></i> 
                        Toplam <?= count($gozlem_listesi) ?> kayıt
                    </div>
                </div>
                
                <?php if (empty($gozlem_listesi)): ?>
                    <div class="empty-observations">
                        <i class="fas fa-clipboard-list"></i>
                        <h3>Henüz Gözlem Yok</h3>
                        <p>İlk gözlemi eklemek için sol taraftaki formu kullanın</p>
                    </div>
                <?php else: ?>
                    <table class="observation-table">
                        <thead>
                            <tr>
                                <th>Saat</th>
                                <th>Tansiyon</th>
                                <th>Nabız</th>
                                <th>Kan Akışı</th>
                                <th>Notlar</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($gozlem_listesi as $gozlem): ?>
                                <tr>
                                    <td class="time-cell">
                                        <?= date('H:i', strtotime($gozlem['kayit_vakti'])) ?>
                                    </td>
                                    <td class="value-cell">
                                        <?= htmlspecialchars($gozlem['tansiyon_sys']) ?> 
                                        <span class="unit">/</span>
                                        <?= htmlspecialchars($gozlem['tansiyon_dia']) ?>
                                        <span class="unit">mmHg</span>
                                    </td>
                                    <td class="value-cell" style="color: var(--danger);">
                                        <?= htmlspecialchars($gozlem['nabiz']) ?>
                                        <span class="unit">bpm</span>
                                    </td>
                                    <td class="value-cell">
                                        <?= $gozlem['kan_akis_hizi'] ? htmlspecialchars($gozlem['kan_akis_hizi']) : '-' ?>
                                        <?= $gozlem['kan_akis_hizi'] ? '<span class="unit">ml/dk</span>' : '' ?>
                                    </td>
                                    <td class="notes-cell">
                                        <?= $gozlem['notlar'] ? htmlspecialchars($gozlem['notlar']) : '-' ?>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                <?php endif; ?>
            </div>
        </div>
    </div>

    <script>
        // Form gönderim kontrolü
        const form = document.querySelector('form');
        if (form) {
            form.addEventListener('submit', function(e) {
                const sys = this.querySelector('input[name="tans_sys"]');
                const dia = this.querySelector('input[name="tans_dia"]');
                
                if (parseInt(sys.value) < parseInt(dia.value)) {
                    e.preventDefault();
                    alert('Sistolik (üst) değer diyastolik (alt) değerden küçük olamaz!');
                    sys.focus();
                }
            });
        }

        // Otomatik sayfa yenileme (her 30 saniyede bir)
        let refreshTimer = setTimeout(() => {
            window.location.reload();
        }, 30000);

        // Kullanıcı aktifse yenilemeyi durdur
        document.addEventListener('mousemove', resetTimer);
        document.addEventListener('keydown', resetTimer);

        function resetTimer() {
            clearTimeout(refreshTimer);
            refreshTimer = setTimeout(() => {
                window.location.reload();
            }, 30000);
        }

        // ESC tuşu ile sayfayı yenile
        document.addEventListener('keydown', function(e) {
            if (e.key === 'Escape') {
                window.location.reload();
            }
        });

        // Form alanlarına otomatik odaklanma
        document.addEventListener('DOMContentLoaded', function() {
            const firstInput = document.querySelector('input[name="tans_sys"]');
            if (firstInput && document.activeElement.tagName !== 'INPUT') {
                firstInput.focus();
            }
        });
    </script>
</body>
</html>