<?php
require_once 'includes/header.php';

// --- Yardımcı Fonksiyonlar ve Veriler ---
$secili_yil = isset($_GET['yil']) ? (int)$_GET['yil'] : date('Y');
$secili_ay = isset($_GET['ay']) ? str_pad((int)$_GET['ay'], 2, '0', STR_PAD_LEFT) : date('m');
$gun_sayisi = cal_days_in_month(CAL_GREGORIAN, $secili_ay, $secili_yil);

try {
    // "İşe Gidilmedi" ID'sini al (encoding sorunlarından kaçınmak için)
    $iseGidilmediId = null;
    try {
        $stmt_guz = $conn->query("SELECT id FROM guzergahlar WHERE guzergah_adi = 'İşe Gidilmedi' OR guzergah_adi LIKE '%Gidilmedi%'");
        $result_guz = $stmt_guz->fetch(PDO::FETCH_ASSOC);
        if ($result_guz) {
            $iseGidilmediId = $result_guz['id'];
        }
    } catch (PDOException $e) {
        // Hata olursa devam et
    }

    // Güzergahları çek (İşe Gidilmedi HARİÇ)
    if ($iseGidilmediId) {
        $guzergahlar_stmt = $conn->prepare("SELECT id, guzergah_adi FROM guzergahlar WHERE id != ? ORDER BY id ASC");
        $guzergahlar_stmt->execute([$iseGidilmediId]);
    } else {
        // Eğer "İşe Gidilmedi" bulunamazsa isimle dene
        $guzergahlar_stmt = $conn->query("SELECT id, guzergah_adi FROM guzergahlar WHERE guzergah_adi NOT LIKE '%Gidilmedi%' ORDER BY id ASC");
    }
    $guzergahlar = $guzergahlar_stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
    die("Hata: Güzergahlar çekilemedi. " . $e->getMessage());
}

$aylar = ['01'=>'Ocak','02'=>'Şubat','03'=>'Mart','04'=>'Nisan','05'=>'Mayıs','06'=>'Haziran','07'=>'Temmuz','08'=>'Ağustos','09'=>'Eylül','10'=>'Ekim','11'=>'Kasım','12'=>'Aralık'];

function translateDay($day) {
    $en = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
    $tr = ['Pazartesi', 'Salı', 'Çarşamba', 'Perşembe', 'Cuma', 'Cumartesi', 'Pazar'];
    return str_replace($en, $tr, $day);
}

// Resmi tatilleri veritabanından çek
try {
    $tatiller_stmt = $conn->prepare("SELECT tarih, aciklama FROM resmi_tatiller WHERE YEAR(tarih) = ?");
    $tatiller_stmt->execute([$secili_yil]);
    $tatiller_data = $tatiller_stmt->fetchAll(PDO::FETCH_ASSOC);

    $resmiTatiller = [];
    foreach ($tatiller_data as $tatil) {
        $ay_gun = date('m-d', strtotime($tatil['tarih']));
        $resmiTatiller[$ay_gun] = $tatil['aciklama'];
    }
} catch (PDOException $e) {
    $resmiTatiller = [];
}
?>

<div class="d-flex justify-content-between align-items-center mb-3">
    <h1>Veri Girişi (<?php echo $aylar[$secili_ay] . ' ' . $secili_yil; ?>)</h1>
    <div id="save-status" class="text-muted fw-bold"></div>
</div>

<div class="card mb-4">
    <div class="card-body py-2"><form action="veri_giris.php" method="GET" class="row g-3 align-items-end"><div class="col-md-6 col-12"><label for="ay" class="form-label">Ay</label><select name="ay" id="ay" class="form-select"><?php foreach ($aylar as $num => $name): ?><option value="<?php echo $num; ?>" <?php echo ($num == $secili_ay) ? 'selected' : ''; ?>><?php echo $name; ?></option><?php endforeach; ?></select></div><div class="col-md-6 col-12"><label for="yil" class="form-label">Yıl</label><select name="yil" id="yil" class="form-select"><?php for ($y = 2026; $y >= 2024; $y--): ?><option value="<?php echo $y; ?>" <?php echo ($y == $secili_yil) ? 'selected' : ''; ?>><?php echo $y; ?></option><?php endfor; ?></select></div></form></div>
</div>

<div class="mobile-scroll-hint">
    Tabloyu sağa-sola kaydırarak tüm kolonları görebilirsiniz
</div>

<div class="card">
    <div class="card-body p-0"><div class="table-responsive"><table class="table table-bordered table-hover" id="data-table"><thead class="table-light text-center" style="position: sticky; top: 0; z-index: 10;"><tr><th style="width: 5%;">Gün</th><th style="width: 12%;">Tarih</th><th style="width: 7%;">İzinli</th><?php foreach ($guzergahlar as $guzergah): ?><th><?php echo htmlspecialchars($guzergah['guzergah_adi']); ?></th><?php endforeach; ?><th style="width: 20%;">Durum / Açıklama</th><th style="width: 10%;">Günlük Toplam</th></tr></thead><tbody>
    <?php for ($gun = 1; $gun <= $gun_sayisi; $gun++): ?>
        <?php
            $tam_tarih = $secili_yil . '-' . str_pad($secili_ay, 2, '0', STR_PAD_LEFT) . '-' . str_pad($gun, 2, '0', STR_PAD_LEFT);
            $ay_gun = date('m-d', strtotime($tam_tarih)); $gun_no = date('N', strtotime($tam_tarih)); $gun_adi = date('l', strtotime($tam_tarih));
            $row_class = ''; $is_disabled = false; $durum_text = '';
            if ($gun_no >= 6) { $row_class = 'weekend-row'; $is_disabled = true; $durum_text = 'Hafta Sonu Tatili'; } 
            elseif (isset($resmiTatiller[$ay_gun])) { $row_class = 'holiday-row'; $is_disabled = true; $durum_text = $resmiTatiller[$ay_gun]; }
        ?>
        <tr id="row-<?php echo $gun; ?>" class="<?php echo $row_class; ?>"><td class="text-center fw-bold align-middle"><?php echo $gun; ?></td><td class="text-center align-middle"><?php echo date("d.m.Y", strtotime($tam_tarih)) . '<br><small>' . translateDay($gun_adi) . '</small>'; ?></td><td class="text-center align-middle"><div class="form-check form-switch d-flex justify-content-center"><input class="form-check-input izin-check" type="checkbox" role="switch" data-date="<?php echo $tam_tarih; ?>" <?php if($is_disabled) echo 'disabled'; ?>></div></td>
        <?php foreach ($guzergahlar as $guzergah): ?><td><input type="number" class="form-control form-control-sm daily-cost" data-date="<?php echo $tam_tarih; ?>" data-guzergah-id="<?php echo $guzergah['id']; ?>" step="0.01" min="0" <?php if($is_disabled) echo 'disabled'; ?>></td><?php endforeach; ?>
        <td><input type="text" class="form-control form-control-sm status-description" data-date="<?php echo $tam_tarih; ?>" value="<?php echo $durum_text; ?>" <?php if($is_disabled) echo 'disabled'; ?>></td>
        <td class="text-center fw-bold align-middle"><input type="text" class="form-control form-control-sm daily-total bg-light" data-date="<?php echo $tam_tarih; ?>" readonly disabled></td></tr>
    <?php endfor; ?>
    </tbody><tfoot><tr class="table-dark"><td colspan="3" class="text-end fw-bold">AYLIK TOPLAM:</td><?php foreach ($guzergahlar as $guzergah): ?><td class="text-center fw-bold" id="total-col-<?php echo $guzergah['id']; ?>">0,00 TL</td><?php endforeach; ?><td></td><td class="text-center fw-bold" id="grand-total">0,00 TL</td></tr></tfoot></table></div></div>
</div>
<?php require_once 'includes/footer.php'; ?>
