<?php
/**
 * NEPHRO ERP - AKILLI SEANS TAKVİMİ (v33 - FULL ENTEGRE)
 */
global $pdo;

// 1. TARİH VE NAVİGASYON YÖNETİMİ
$secili_tarih = $_GET['tarih'] ?? date('Y-m-d');
$baslangic_gunu = date('Y-m-d', strtotime('monday this week', strtotime($secili_tarih)));
$bitis_gunu = date('Y-m-d', strtotime('sunday this week', strtotime($secili_tarih)));

// 2. DİNAMİK İSTATİSTİKLER (Sadece seçili haftaya göre)
// Haftalık Toplam Planlanan
$st_hafta = $pdo->prepare("SELECT COUNT(*) FROM seans_programi WHERE plan_tarihi BETWEEN ? AND ?");
$st_hafta->execute([$baslangic_gunu, $bitis_gunu]);
$haftalik_toplam = $st_hafta->fetchColumn();

// Haftalık Tamamlanan (Bitti durumundakiler)
$st_bitti = $pdo->prepare("SELECT COUNT(*) FROM seans_programi WHERE durum = 'Bitti' AND plan_tarihi BETWEEN ? AND ?");
$st_bitti->execute([$baslangic_gunu, $bitis_gunu]);
$tamamlanan = $st_bitti->fetchColumn();

// Haftalık Gelmeyenler
$st_gelmedi = $pdo->prepare("SELECT COUNT(*) FROM seans_programi WHERE durum = 'Gelmedi' AND plan_tarihi BETWEEN ? AND ?");
$st_gelmedi->execute([$baslangic_gunu, $bitis_gunu]);
$gelmeyen_sayi = $st_gelmedi->fetchColumn();

// 3. TAKVİM VERİLERİ (Sadece bu hafta)
$plan_sql = "SELECT s.*, h.ad, h.soyad, c.oda_no 
             FROM seans_programi s 
             JOIN hastalar h ON s.hasta_id = h.id 
             JOIN cihazlar c ON s.cihaz_id = c.id 
             WHERE s.plan_tarihi BETWEEN ? AND ?
             ORDER BY s.planlanan_saat ASC";
$stmt = $pdo->prepare($plan_sql);
$stmt->execute([$baslangic_gunu, $bitis_gunu]);
$planlar = $stmt->fetchAll();

// 4. GRID YAPILANDIRMASI
$gun_isimleri = ['Pazartesi', 'Sali', 'Carsamba', 'Persembe', 'Cuma', 'Cumartesi', 'Pazar'];
$gun_etiketleri = ['PAZARTESİ', 'SALI', 'ÇARŞAMBA', 'PERŞEMBE', 'CUMA', 'CUMARTESİ', 'PAZAR'];

$matris = [];
$saat_ekseni = [];
foreach ($planlar as $p) {
    $p_saat = !empty($p['planlanan_saat']) ? substr($p['planlanan_saat'], 0, 5) : '08:00';
    $matris[$p['gun']][$p_saat][] = $p;
    if (!in_array($p_saat, $saat_ekseni)) $saat_ekseni[] = $p_saat;
}
sort($saat_ekseni);
if(empty($saat_ekseni)) $saat_ekseni = ['08:00', '13:00', '18:00'];
?>

<style>
    .tc-wrapper { padding: 15px; animation: fadeIn 0.4s ease; font-family: 'Inter', sans-serif; }
    
    /* İSTATİSTİK KARTLARI */
    .tc-stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 20px; margin-bottom: 25px; }
    .s-card { background: #fff; padding: 20px; border-radius: 20px; border: 1px solid #f1f5f9; display: flex; align-items: center; gap: 15px; box-shadow: 0 4px 6px -1px rgba(0,0,0,0.02); }
    .s-card i { width: 45px; height: 45px; border-radius: 12px; display: flex; align-items: center; justify-content: center; font-size: 1.2rem; }
    .s-val { display: block; font-size: 1.5rem; font-weight: 900; color: #1e293b; line-height: 1; }
    .s-tit { font-size: 0.6rem; font-weight: 800; color: #94a3b8; text-transform: uppercase; letter-spacing: 0.5px; }

    /* NAVİGASYON */
    .tc-nav { background: #fff; border-radius: 20px; padding: 15px 25px; border: 1px solid #e2e8f0; display: flex; justify-content: space-between; align-items: center; margin-bottom: 25px; }
    .tc-btn { background: #f8fafc; border: 1px solid #e2e8f0; padding: 10px 18px; border-radius: 12px; cursor: pointer; font-weight: 700; color: #64748b; text-decoration: none; font-size: 0.8rem; }
    .tc-btn:hover { background: #2563eb; color: #fff; border-color: #2563eb; }

    /* GRID SİSTEMİ */
    .tc-grid-box { background: #fff; border-radius: 25px; border: 1px solid #e2e8f0; overflow-x: auto; box-shadow: 0 10px 30px rgba(0,0,0,0.03); }
    .tc-grid { display: grid; grid-template-columns: 100px repeat(7, 1fr); min-width: 1100px; }
    .tc-h { background: #f8fafc; padding: 15px; text-align: center; font-weight: 800; font-size: 0.7rem; color: #94a3b8; border-bottom: 2px solid #f1f5f9; border-right: 1px solid #f1f5f9; }
    .tc-time { background: #f8fafc; border-right: 2px solid #f1f5f9; border-bottom: 1px solid #f1f5f9; display: flex; align-items: center; justify-content: center; font-weight: 900; color: #1e293b; font-size: 1.1rem; }
    .tc-cell { padding: 10px; border-right: 1px solid #f1f5f9; border-bottom: 1px solid #f1f5f9; min-height: 110px; }

    /* HASTA KARTI */
    .p-card { background: #fff; border: 1px solid #e2e8f0; border-radius: 14px; padding: 10px; margin-bottom: 6px; border-left: 5px solid #cbd5e1; position: relative; transition: 0.2s; }
    .p-card b { display: block; font-size: 0.8rem; color: #1e293b; line-height: 1.2; }
    .p-card small { font-size: 0.65rem; color: #94a3b8; font-weight: 700; display: block; margin-top: 2px; }
    
    /* DURUMLAR */
    .p-card.Basladi { border-left-color: #10b981; background: #f0fdf4; }
    .p-card.Gelmedi { border-left-color: #ef4444; background: #fef2f2; opacity: 0.7; }
    .p-card.Bitti { border-left-color: #64748b; background: #f8fafc; }
    
    .btn-play { background: #10b981; color: #fff; border: none; border-radius: 8px; padding: 5px 10px; cursor: pointer; font-size: 0.7rem; font-weight: 800; margin-top: 8px; text-decoration: none; display: inline-block; }
    .status-tag { font-size: 0.55rem; font-weight: 900; padding: 2px 6px; border-radius: 4px; margin-top: 5px; display: inline-block; }
</style>

<div class="tc-wrapper">
    <div class="tc-stats">
        <div class="s-card">
            <i class="fas fa-users" style="background:#eff6ff; color:#2563eb;"></i>
            <div><span class="s-val"><?php echo $haftalik_toplam; ?></span><span class="s-tit">HAFTALIK PLAN</span></div>
        </div>
        <div class="s-card">
            <i class="fas fa-check-double" style="background:#f0fdf4; color:#10b981;"></i>
            <div><span class="s-val"><?php echo $tamamlanan; ?></span><span class="s-tit">TAMAMLANAN</span></div>
        </div>
        <div class="s-card">
            <i class="fas fa-user-times" style="background:#fef2f2; color:#ef4444;"></i>
            <div><span class="s-val"><?php echo $gelmeyen_sayi; ?></span><span class="s-tit">GELMEYEN</span></div>
        </div>
        <div class="s-card">
            <i class="fas fa-bed" style="background:#fff7ed; color:#ea580c;"></i>
            <div><span class="s-val">40</span><span class="s-tit">CİHAZ KAPASİTE</span></div>
        </div>
    </div>

    <div class="tc-nav">
        <div style="display:flex; gap:10px;">
            <a href="?sayfa=seans-takvimi&tarih=<?php echo date('Y-m-d', strtotime($baslangic_gunu . ' -1 week')); ?>" class="tc-btn"><i class="fas fa-chevron-left"></i> ÖNCEKİ HAFTA</a>
            <a href="?sayfa=seans-takvimi&tarih=<?php echo date('Y-m-d'); ?>" class="tc-btn">BU HAFTA</a>
            <a href="?sayfa=seans-takvimi&tarih=<?php echo date('Y-m-d', strtotime($baslangic_gunu . ' +1 week')); ?>" class="tc-btn">SONRAKİ HAFTA <i class="fas fa-chevron-right"></i></a>
        </div>
        <div style="text-align:right;">
            <div style="font-weight: 900; color: #1e293b; font-size:1.1rem;">
                <?php echo date('d M', strtotime($baslangic_gunu)); ?> - <?php echo date('d M Y', strtotime($bitis_gunu)); ?>
            </div>
            <span style="font-size:0.6rem; font-weight:800; color:#94a3b8; text-transform:uppercase;">Seçili Hafta Görünümü</span>
        </div>
        <button onclick="openPModal()" class="tc-btn" style="background:#2563eb; color:#fff; border:none; padding:10px 20px;">+ YENİ RANDEVU</button>
    </div>

    <div class="tc-grid-box">
        <div class="tc-grid">
            <div class="tc-h">SAAT</div>
            <?php foreach($gun_etiketleri as $label): ?> <div class="tc-h"><?php echo $label; ?></div> <?php endforeach; ?>

            <?php foreach($saat_ekseni as $saat): ?>
                <div class="tc-time"><?php echo $saat; ?></div>
                <?php foreach($gun_isimleri as $eng_gun): ?>
                    <div class="tc-cell">
                        <?php if(isset($matris[$eng_gun][$saat])): ?>
                            <?php foreach($matris[$eng_gun][$saat] as $p): ?>
                                <div class="p-card <?php echo $p['durum']; ?>">
                                    <b><?php echo mb_strtoupper($p['ad'].' '.$p['soyad'], 'UTF-8'); ?></b>
                                    <small><i class="fas fa-bed"></i> Oda: <?php echo $p['oda_no']; ?></small>
                                    <small style="color:#2563eb; margin-top:3px;"><i class="fas fa-calendar-alt"></i> <?php echo date('d.m.Y', strtotime($p['plan_tarihi'])); ?></small>
                                    
                                    <?php if($p['durum'] == 'Randevu'): ?>
                                        <a href="?sayfa=seans-baslat&id=<?php echo $p['hasta_id']; ?>&plan_id=<?php echo $p['id']; ?>" class="btn-play">
                                            <i class="fas fa-play"></i> BAŞLAT
                                        </a>
                                    <?php else: ?>
                                        <span class="status-tag" style="background:<?php echo ($p['durum']=='Basladi'?'#10b981':'#64748b'); ?>; color:#fff;">
                                            <?php echo ($p['durum']=='Basladi'?'BAĞLI':$p['durum']); ?>
                                        </span>
                                    <?php endif; ?>
                                </div>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </div>
                <?php endforeach; ?>
            <?php endforeach; ?>
        </div>
    </div>
</div>