<?php include 'header.php';
include 'detaylar.php';

$urunler = [
    '1paket' => ['adet' => 0, 'fiyat' => 49.99],
    '2paket' => ['adet' => 0, 'fiyat' => 99.99],
    '3paket' => ['adet' => 0, 'fiyat' => 199.99],
    '4paket' => ['adet' => 0, 'fiyat' => 299.99],
    '5paket' => ['adet' => 0, 'fiyat' => 399.99],
    '6paket' => ['adet' => 0, 'fiyat' => 699.99],
    'visitor' => ['adet' => 0, 'fiyat' => 29.99],
    'visitorunlock' => ['adet' => 0, 'fiyat' => 59.99],
    'callunlock' => ['adet' => 0, 'fiyat' => 199.99],
    '100paket' => ['adet' => 0, 'fiyat' => 199.99],
    '100paketindirim' => ['adet' => 0, 'fiyat' => 149.99],
    '100jetonnormal' => ['adet' => 0, 'fiyat' => 149.99],
    '100jetonindirim' => ['adet' => 0, 'fiyat' => 99.99],
    '250paket' => ['adet' => 0, 'fiyat' => 299.99],
    '500paket' => ['adet' => 0, 'fiyat' => 399.99],
    '1000paket' => ['adet' => 0, 'fiyat' => 499.99],
    '2500paket' => ['adet' => 0, 'fiyat' => 799.99],
    '5000paket' => ['adet' => 0, 'fiyat' => 1399.99],
    'sinirsizpaket' => ['adet' => 0, 'fiyat' => 2599.99],
    'sinpaket' => ['adet' => 0, 'fiyat' => 699.99],
    'aramapaket' => ['adet' => 0, 'fiyat' => 249.99],
    'iletisimpaket' => ['adet' => 0, 'fiyat' => 249.99],
    'onaylihesap' => ['adet' => 0, 'fiyat' => 249.99],
    'boostpaket' => ['adet' => 0, 'fiyat' => 499.99],
    'konumpaket' => ['adet' => 0, 'fiyat' => 249.99],
];

// 10.07.2023 tarihinden önceki ödemelerin toplamını al
$queryBefore = $db->query("SELECT urun, COUNT(*) as urun_count FROM odemeler WHERE status = 1 AND tarih < 1688947200 GROUP BY urun", PDO::FETCH_ASSOC);

$toplamKazancBefore = 0;

foreach ($queryBefore as $veri) {
    $urun = $veri['urun'];
    $adet = $veri['urun_count'];

    if (isset($urunler[$urun])) {
        $toplamKazancBefore += ($adet * $urunler[$urun]['fiyat'] * 0.72);
    }
}

// 10.07.2023 tarihinden sonraki ödemelerin toplamını al
$queryAfter = $db->query("SELECT urun, COUNT(*) as urun_count FROM odemeler WHERE status = 1 AND tarih >= 1688947200 GROUP BY urun", PDO::FETCH_ASSOC);

$toplamKazancAfter = 0;

foreach ($queryAfter as $veri) {
    $urun = $veri['urun'];
    $adet = $veri['urun_count'];

    if (isset($urunler[$urun])) {
        $toplamKazancAfter += ($adet * $urunler[$urun]['fiyat'] * 0.7083);
    }
}

// Toplam kazancı göster
$toplamKazanc = $toplamKazancBefore + $toplamKazancAfter;

// ========== BU AY, ÖNCEKİ AY VE TÜM ZAMANLAR ANALİZ VERİLERİ ==========
// Bu ayın başlangıcı ve sonu
$buAyBaslangic = strtotime('first day of this month 00:00:00');
$buAyBitis = strtotime('last day of this month 23:59:59');

// Önceki ayın başlangıcı ve sonu  
$oncekiAyBaslangic = strtotime('first day of last month 00:00:00');
$oncekiAyBitis = strtotime('last day of last month 23:59:59');

// Ay isimleri
$aylar = ['Ocak', 'Şubat', 'Mart', 'Nisan', 'Mayıs', 'Haziran', 'Temmuz', 'Ağustos', 'Eylül', 'Ekim', 'Kasım', 'Aralık'];
$buAyIsim = $aylar[date('n') - 1] . ' ' . date('Y');
$oncekiAyIsim = $aylar[date('n', $oncekiAyBaslangic) - 1] . ' ' . date('Y', $oncekiAyBaslangic);

// Ürün isimleri mapping
$urunIsimleri = [
    '1paket' => '50 Jeton (49,99₺)',
    '2paket' => '100 Jeton (99,99₺)',
    '3paket' => '250 Jeton (199,99₺)',
    '4paket' => '500 Jeton (299,99₺)',
    '5paket' => '1000 Jeton (399,99₺)',
    '6paket' => 'Sınırsız Jeton (699,99₺)',
    'visitor' => 'Ziyaretçi Kilidi (29,99₺)',
    'visitorunlock' => 'Ziyaretçi Kilidi (59,99₺)',
    'callunlock' => 'Arama Kilidi (199,99₺)',
    '100paket' => '100 Jeton (199,99₺)',
    '100paketindirim' => '100 Jeton İndirimli (149,99₺)',
    '100jetonnormal' => '100 Jeton (149,99₺)',
    '100jetonindirim' => '100 Jeton İndirimli (99,99₺)',
    '250paket' => '250 Jeton (299,99₺)',
    '500paket' => '500 Jeton (399,99₺)',
    '1000paket' => '1000 Jeton (499,99₺)',
    '2500paket' => '2500 Jeton (799,99₺)',
    '5000paket' => '5000 Jeton (1399,99₺)',
    'sinirsizpaket' => 'Sınırsız Jeton (2599,99₺)',
    'sinpaket' => 'Sınırsız Jeton (699,99₺)',
    'aramapaket' => 'Arama Paketi (249,99₺)',
    'iletisimpaket' => 'İletişim Paketi (249,99₺)',
    'onaylihesap' => 'Onaylı Hesap (249,99₺)',
    'boostpaket' => 'Boost Paketi (499,99₺)',
    'konumpaket' => 'Konum Paketi (249,99₺)'
];

// Kesinti oranı (Google Play komisyonu sonrası - 10.07.2023 sonrası)
$kesintiOrani = 0.7083;

// Bu ay satışlarını çek
$queryBuAy = $db->query("SELECT urun, COUNT(*) as adet FROM odemeler WHERE status = 1 AND tarih >= $buAyBaslangic AND tarih <= $buAyBitis GROUP BY urun ORDER BY adet DESC", PDO::FETCH_ASSOC);

$buAyData = [];
$buAyToplamAdet = 0;
$buAyToplamCiro = 0;

foreach ($queryBuAy as $row) {
    $urunKod = $row['urun'];
    $adet = (int)$row['adet'];
    $fiyat = isset($urunler[$urunKod]) ? $urunler[$urunKod]['fiyat'] : 0;
    $ciro = $adet * $fiyat * $kesintiOrani;
    
    $buAyData[] = [
        'kod' => $urunKod,
        'isim' => isset($urunIsimleri[$urunKod]) ? $urunIsimleri[$urunKod] : $urunKod,
        'adet' => $adet,
        'fiyat' => $fiyat,
        'ciro' => $ciro
    ];
    
    $buAyToplamAdet += $adet;
    $buAyToplamCiro += $ciro;
}

// Önceki ay satışlarını çek
$queryOncekiAy = $db->query("SELECT urun, COUNT(*) as adet FROM odemeler WHERE status = 1 AND tarih >= $oncekiAyBaslangic AND tarih <= $oncekiAyBitis GROUP BY urun ORDER BY adet DESC", PDO::FETCH_ASSOC);

$oncekiAyData = [];
$oncekiAyToplamAdet = 0;
$oncekiAyToplamCiro = 0;

foreach ($queryOncekiAy as $row) {
    $urunKod = $row['urun'];
    $adet = (int)$row['adet'];
    $fiyat = isset($urunler[$urunKod]) ? $urunler[$urunKod]['fiyat'] : 0;
    $ciro = $adet * $fiyat * $kesintiOrani;
    
    $oncekiAyData[] = [
        'kod' => $urunKod,
        'isim' => isset($urunIsimleri[$urunKod]) ? $urunIsimleri[$urunKod] : $urunKod,
        'adet' => $adet,
        'fiyat' => $fiyat,
        'ciro' => $ciro
    ];
    
    $oncekiAyToplamAdet += $adet;
    $oncekiAyToplamCiro += $ciro;
}

// Tüm zamanlar satışlarını çek - tarih bazlı kesinti hesaplama
$queryTumZamanlar = $db->query("SELECT urun, tarih FROM odemeler WHERE status = 1 ORDER BY urun", PDO::FETCH_ASSOC);

$tumZamanlarTemp = [];
$tumZamanlarToplamAdet = 0;
$tumZamanlarToplamCiro = 0;

foreach ($queryTumZamanlar as $row) {
    $urunKod = $row['urun'];
    $tarih = (int)$row['tarih'];
    $fiyat = isset($urunler[$urunKod]) ? $urunler[$urunKod]['fiyat'] : 0;
    
    // 10.07.2023 öncesi %72, sonrası %70.83
    $oran = ($tarih < 1688947200) ? 0.72 : 0.7083;
    $ciro = $fiyat * $oran;
    
    if (!isset($tumZamanlarTemp[$urunKod])) {
        $tumZamanlarTemp[$urunKod] = [
            'kod' => $urunKod,
            'isim' => isset($urunIsimleri[$urunKod]) ? $urunIsimleri[$urunKod] : $urunKod,
            'adet' => 0,
            'fiyat' => $fiyat,
            'ciro' => 0
        ];
    }
    
    $tumZamanlarTemp[$urunKod]['adet']++;
    $tumZamanlarTemp[$urunKod]['ciro'] += $ciro;
    $tumZamanlarToplamAdet++;
    $tumZamanlarToplamCiro += $ciro;
}

// Adete göre sırala
usort($tumZamanlarTemp, function($a, $b) {
    return $b['adet'] - $a['adet'];
});

$tumZamanlarData = array_values($tumZamanlarTemp);

?>

<div class="app-content container center-layout mt-2">
    <div class="content-overlay"></div>
    <div class="content-wrapper">
        <div class="content-header row">
        </div>
        <div class="content-body">
            <!-- AYLIK ANALİZ KARTI - BU AY / GEÇen AY / TÜM ZAMANLAR -->
            <div class="row mb-1 d-none d-md-flex">
                <div class="col-12">
                    <div class="card">
                        <div class="card-header" style="cursor: pointer; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);" onclick="$('#analizContent').slideToggle(300); $(this).find('.toggle-icon').toggleClass('ft-chevron-down ft-chevron-up');">
                            <h4 class="card-title" style="color: white;">
                                <i class="ft-bar-chart-2 mr-1"></i> Paket Satış Analizi
                                <span id="headerBadgeSatis" class="badge ml-2" style="background: rgba(255,255,255,0.25); color: white;"><?= $buAyToplamAdet ?> Satış</span>
                                <span id="headerBadgeCiro" class="badge badge-warning ml-1"><?= number_format($buAyToplamCiro, 2, ',', '.') ?>₺</span>
                                <span id="vergiToggle" class="badge ml-1" style="background: rgba(255,255,255,0.15); color: white; cursor: pointer; font-size: 10px;" onclick="event.stopPropagation(); toggleVergi();">
                                    <i class="ft-toggle-left" id="vergiIcon"></i> Vergisiz
                                </span>
                            </h4>
                            <a class="heading-elements-toggle"><i class="la la-ellipsis-v font-medium-3" style="color: white;"></i></a>
                            <div class="heading-elements">
                                <ul class="list-inline mb-0">
                                    <li><i class="toggle-icon ft-chevron-down font-medium-3" style="color: white;"></i></li>
                                </ul>
                            </div>
                        </div>
                        <div class="card-content" id="analizContent" style="display: none;">
                            <div class="card-body">
                                <!-- Tab Butonları -->
                                <div class="btn-group mb-1" role="group" style="width: 100%; display: flex;">
                                    <button type="button" id="btnBuAy" class="btn btn-primary" onclick="showTab('buAy')" style="flex: 1;">
                                        <i class="ft-calendar mr-1"></i> Bu Ay
                                        <span class="badge ml-1" style="background: white; color: #333; border: 1px solid #ddd;"><?= $buAyToplamAdet ?></span>
                                    </button>
                                    <button type="button" id="btnOncekiAy" class="btn btn-outline-primary" onclick="showTab('oncekiAy')" style="flex: 1;">
                                        <i class="ft-clock mr-1"></i> Geçen Ay
                                        <span class="badge ml-1" style="background: white; color: #333; border: 1px solid #ddd;"><?= $oncekiAyToplamAdet ?></span>
                                    </button>
                                    <button type="button" id="btnTumZamanlar" class="btn btn-outline-primary" onclick="showTab('tumZamanlar')" style="flex: 1;">
                                        <i class="ft-globe mr-1"></i> Tüm Zamanlar
                                        <span class="badge ml-1" style="background: white; color: #333; border: 1px solid #ddd;"><?= $tumZamanlarToplamAdet ?></span>
                                    </button>
                                </div>

                                <!-- BU AY TABLOSU -->
                                <div id="tabBuAy">
                                    <?php if (count($buAyData) > 0): ?>
                                    <div class="table-responsive">
                                        <table class="table table-hover mb-0">
                                            <thead style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);">
                                                <tr>
                                                    <th style="color: white; border: none;">#</th>
                                                    <th style="color: white; border: none;">Paket Adı</th>
                                                    <th style="color: white; border: none;" class="text-center">Adet</th>
                                                    <th style="color: white; border: none;" class="text-center">Ciro</th>
                                                    <th style="color: white; border: none; min-width: 200px;">Oran</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php 
                                                $sira = 1;
                                                foreach ($buAyData as $item): 
                                                    $oran = $buAyToplamAdet > 0 ? round(($item['adet'] / $buAyToplamAdet) * 100, 1) : 0;
                                                    $renk = $oran >= 25 ? '#28d094' : ($oran >= 10 ? '#667eea' : ($oran >= 5 ? '#ff9149' : '#6c757d'));
                                                ?>
                                                <tr>
                                                    <td><strong><?= $sira++ ?></strong></td>
                                                    <td>
                                                        <strong><?= htmlspecialchars($item['isim']) ?></strong>
                                                    </td>
                                                    <td class="text-center">
                                                        <span class="badge" style="background: <?= $renk ?>; color: white; font-size: 14px; padding: 8px 15px;"><?= $item['adet'] ?></span>
                                                    </td>
                                                    <td class="text-center">
                                                        <strong style="color: #28d094;"><?= number_format($item['ciro'], 2, ',', '.') ?>₺</strong>
                                                    </td>
                                                    <td>
                                                        <div class="d-flex align-items-center">
                                                            <div class="progress" style="flex: 1; height: 22px; border-radius: 11px; background: #eee;">
                                                                <div class="progress-bar" role="progressbar" style="width: <?= $oran ?>%; background: <?= $renk ?>; border-radius: 11px; font-weight: bold; color: <?= $oran >= 15 ? 'white' : '#333' ?>;" aria-valuenow="<?= $oran ?>" aria-valuemin="0" aria-valuemax="100">
                                                                    <?= $oran ?>%
                                                                </div>
                                                            </div>
                                                        </div>
                                                    </td>
                                                </tr>
                                                <?php endforeach; ?>
                                            </tbody>
                                            <tfoot style="background: linear-gradient(135deg, #28d094 0%, #20c997 100%);">
                                                <tr>
                                                    <th style="color: white; border: none;" colspan="2"><i class="ft-award mr-1"></i> TOPLAM</th>
                                                    <th style="color: white; border: none;" class="text-center"><span style="font-size: 16px;"><?= $buAyToplamAdet ?></span></th>
                                                    <th style="color: white; border: none;" class="text-center"><span style="font-size: 16px;"><?= number_format($buAyToplamCiro, 2, ',', '.') ?>₺</span></th>
                                                    <th style="color: white; border: none;"><span style="font-size: 16px;">100%</span></th>
                                                </tr>
                                            </tfoot>
                                        </table>
                                    </div>
                                    <?php else: ?>
                                    <div class="alert alert-info text-center mb-0">
                                        <i class="ft-info mr-1"></i> <?= $buAyIsim ?> içinde satış bulunmuyor.
                                    </div>
                                    <?php endif; ?>
                                </div>

                                <!-- GEÇen AY TABLOSU -->
                                <div id="tabOncekiAy" style="display: none;">
                                    <?php if (count($oncekiAyData) > 0): ?>
                                    <div class="table-responsive">
                                        <table class="table table-hover mb-0">
                                            <thead style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);">
                                                <tr>
                                                    <th style="color: white; border: none;">#</th>
                                                    <th style="color: white; border: none;">Paket Adı</th>
                                                    <th style="color: white; border: none;" class="text-center">Adet</th>
                                                    <th style="color: white; border: none;" class="text-center">Ciro</th>
                                                    <th style="color: white; border: none; min-width: 200px;">Oran</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php 
                                                $sira = 1;
                                                foreach ($oncekiAyData as $item): 
                                                    $oran = $oncekiAyToplamAdet > 0 ? round(($item['adet'] / $oncekiAyToplamAdet) * 100, 1) : 0;
                                                    $renk = $oran >= 25 ? '#28d094' : ($oran >= 10 ? '#667eea' : ($oran >= 5 ? '#ff9149' : '#6c757d'));
                                                ?>
                                                <tr>
                                                    <td><strong><?= $sira++ ?></strong></td>
                                                    <td>
                                                        <strong><?= htmlspecialchars($item['isim']) ?></strong>
                                                    </td>
                                                    <td class="text-center">
                                                        <span class="badge" style="background: <?= $renk ?>; color: white; font-size: 14px; padding: 8px 15px;"><?= $item['adet'] ?></span>
                                                    </td>
                                                    <td class="text-center">
                                                        <strong style="color: #28d094;"><?= number_format($item['ciro'], 2, ',', '.') ?>₺</strong>
                                                    </td>
                                                    <td>
                                                        <div class="d-flex align-items-center">
                                                            <div class="progress" style="flex: 1; height: 22px; border-radius: 11px; background: #eee;">
                                                                <div class="progress-bar" role="progressbar" style="width: <?= $oran ?>%; background: <?= $renk ?>; border-radius: 11px; font-weight: bold; color: <?= $oran >= 15 ? 'white' : '#333' ?>;" aria-valuenow="<?= $oran ?>" aria-valuemin="0" aria-valuemax="100">
                                                                    <?= $oran ?>%
                                                                </div>
                                                            </div>
                                                        </div>
                                                    </td>
                                                </tr>
                                                <?php endforeach; ?>
                                            </tbody>
                                            <tfoot style="background: linear-gradient(135deg, #28d094 0%, #20c997 100%);">
                                                <tr>
                                                    <th style="color: white; border: none;" colspan="2"><i class="ft-award mr-1"></i> TOPLAM</th>
                                                    <th style="color: white; border: none;" class="text-center"><span style="font-size: 16px;"><?= $oncekiAyToplamAdet ?></span></th>
                                                    <th style="color: white; border: none;" class="text-center"><span style="font-size: 16px;"><?= number_format($oncekiAyToplamCiro, 2, ',', '.') ?>₺</span></th>
                                                    <th style="color: white; border: none;"><span style="font-size: 16px;">100%</span></th>
                                                </tr>
                                            </tfoot>
                                        </table>
                                    </div>
                                    <?php else: ?>
                                    <div class="alert alert-info text-center mb-0">
                                        <i class="ft-info mr-1"></i> <?= $oncekiAyIsim ?> içinde satış bulunmuyor.
                                    </div>
                                    <?php endif; ?>
                                </div>

                                <!-- TÜM ZAMANLAR TABLOSU -->
                                <div id="tabTumZamanlar" style="display: none;">
                                    <?php if (count($tumZamanlarData) > 0): ?>
                                    <div class="table-responsive">
                                        <table class="table table-hover mb-0">
                                            <thead style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);">
                                                <tr>
                                                    <th style="color: white; border: none;">#</th>
                                                    <th style="color: white; border: none;">Paket Adı</th>
                                                    <th style="color: white; border: none;" class="text-center">Adet</th>
                                                    <th style="color: white; border: none;" class="text-center">Ciro</th>
                                                    <th style="color: white; border: none; min-width: 200px;">Oran</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php 
                                                $sira = 1;
                                                foreach ($tumZamanlarData as $item): 
                                                    $oran = $tumZamanlarToplamAdet > 0 ? round(($item['adet'] / $tumZamanlarToplamAdet) * 100, 1) : 0;
                                                    $renk = $oran >= 25 ? '#28d094' : ($oran >= 10 ? '#667eea' : ($oran >= 5 ? '#ff9149' : '#6c757d'));
                                                ?>
                                                <tr>
                                                    <td><strong><?= $sira++ ?></strong></td>
                                                    <td>
                                                        <strong><?= htmlspecialchars($item['isim']) ?></strong>
                                                    </td>
                                                    <td class="text-center">
                                                        <span class="badge" style="background: <?= $renk ?>; color: white; font-size: 14px; padding: 8px 15px;"><?= $item['adet'] ?></span>
                                                    </td>
                                                    <td class="text-center">
                                                        <strong style="color: #28d094;"><?= number_format($item['ciro'], 2, ',', '.') ?>₺</strong>
                                                    </td>
                                                    <td>
                                                        <div class="d-flex align-items-center">
                                                            <div class="progress" style="flex: 1; height: 22px; border-radius: 11px; background: #eee;">
                                                                <div class="progress-bar" role="progressbar" style="width: <?= $oran ?>%; background: <?= $renk ?>; border-radius: 11px; font-weight: bold; color: <?= $oran >= 15 ? 'white' : '#333' ?>;" aria-valuenow="<?= $oran ?>" aria-valuemin="0" aria-valuemax="100">
                                                                    <?= $oran ?>%
                                                                </div>
                                                            </div>
                                                        </div>
                                                    </td>
                                                </tr>
                                                <?php endforeach; ?>
                                            </tbody>
                                            <tfoot style="background: linear-gradient(135deg, #28d094 0%, #20c997 100%);">
                                                <tr>
                                                    <th style="color: white; border: none;" colspan="2"><i class="ft-award mr-1"></i> TOPLAM</th>
                                                    <th style="color: white; border: none;" class="text-center"><span style="font-size: 16px;"><?= $tumZamanlarToplamAdet ?></span></th>
                                                    <th style="color: white; border: none;" class="text-center"><span style="font-size: 16px;"><?= number_format($tumZamanlarToplamCiro, 2, ',', '.') ?>₺</span></th>
                                                    <th style="color: white; border: none;"><span style="font-size: 16px;">100%</span></th>
                                                </tr>
                                            </tfoot>
                                        </table>
                                    </div>
                                    <?php else: ?>
                                    <div class="alert alert-info text-center mb-0">
                                        <i class="ft-info mr-1"></i> Hiç satış bulunmuyor.
                                    </div>
                                    <?php endif; ?>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            
            <script>
            var vergiAktif = false;
            var aktifTab = 'buAy';
            
            // Vergisiz cirolar
            var cirolar = {
                buAy: <?= $buAyToplamCiro ?>,
                oncekiAy: <?= $oncekiAyToplamCiro ?>,
                tumZamanlar: <?= $tumZamanlarToplamCiro ?>
            };
            
            function formatNumber(num) {
                return num.toFixed(2).replace('.', ',').replace(/\B(?=(\d{3})+(?!\d))/g, '.');
            }
            
            function toggleVergi() {
                vergiAktif = !vergiAktif;
                
                if (vergiAktif) {
                    $('#vergiToggle').html('<i class="ft-toggle-right"></i> Vergili');
                    $('#vergiToggle').css('background', 'rgba(40,208,148,0.5)');
                } else {
                    $('#vergiToggle').html('<i class="ft-toggle-left"></i> Vergisiz');
                    $('#vergiToggle').css('background', 'rgba(255,255,255,0.15)');
                }
                
                updateCiro();
            }
            
            function updateCiro() {
                var ciro = cirolar[aktifTab];
                if (vergiAktif) {
                    ciro = ciro * 0.85; // %15 vergi kesintisi
                }
                $('#headerBadgeCiro').text(formatNumber(ciro) + '₺');
            }
            
            function showTab(tab) {
                aktifTab = tab;
                
                // Tüm tabları gizle
                $('#tabBuAy, #tabOncekiAy, #tabTumZamanlar').hide();
                // Tüm butonları outline yap
                $('#btnBuAy, #btnOncekiAy, #btnTumZamanlar').removeClass('btn-primary').addClass('btn-outline-primary');
                
                if (tab === 'buAy') {
                    $('#tabBuAy').show();
                    $('#btnBuAy').removeClass('btn-outline-primary').addClass('btn-primary');
                    $('#headerBadgeSatis').text('<?= $buAyToplamAdet ?> Satış');
                } else if (tab === 'oncekiAy') {
                    $('#tabOncekiAy').show();
                    $('#btnOncekiAy').removeClass('btn-outline-primary').addClass('btn-primary');
                    $('#headerBadgeSatis').text('<?= $oncekiAyToplamAdet ?> Satış');
                } else {
                    $('#tabTumZamanlar').show();
                    $('#btnTumZamanlar').removeClass('btn-outline-primary').addClass('btn-primary');
                    $('#headerBadgeSatis').text('<?= $tumZamanlarToplamAdet ?> Satış');
                }
                
                updateCiro();
            }
            </script>
            <!-- / AYLIK ANALİZ KARTI -->

            <div class="row">
                <div id="recent-transactions" class="col-12">
                    <div class="card">
                        <div class="card-header">
                            <h4 class="card-title">Satışlar</h4>
                            <a class="heading-elements-toggle"><i class="la la-ellipsis-v font-medium-3"></i></a>
                            <div class="heading-elements">
                                <ul class="list-inline mb-0">
                                    <li><a class="btn btn-sm btn-primary box-shadow-2 round btn-min-width pull-right" href="javascript:void(0);">Bu Ay: <?= number_format($buAyToplamCiro, 2, ',', '.') ?>₺</a></li>
                                     <li><a class="btn btn-sm btn-warning box-shadow-2 round btn-min-width pull-right" href="javascript:void(0);">Geçen Ay: <?= number_format($oncekiAyToplamCiro, 2, ',', '.') ?>₺</a></li>
                                    <li><a class="btn btn-sm btn-danger box-shadow-2 round btn-min-width pull-right" href="javascript:void(0);">Toplam Kazanç: <?= number_format($toplamKazanc, 2, ',', '.') ?>₺</a></li>
                                </ul>
                            </div>
                        </div>
                        <div class="card-content">
                            <div class="table-responsive">
                                <table id="recent-orders" class="table table-hover table-xl mb-0">
                                    <thead>
                                        <tr>
                                            <th class="border-top-0">ID</th>
                                            <th class="border-top-0">İsim Soyisim</th>
                                            <th class="border-top-0">Paket</th>
                                            <th class="border-top-0">GPA</th>
                                            <th class="border-top-0">Tarih</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php
                                        $query = $db->query("SELECT * FROM odemeler ORDER BY id DESC LIMIT 500", PDO::FETCH_ASSOC);

                                        foreach ($query as $veri) {
                                            // İzin kontrolü (varsayılan 1)
                                            $izin = isset($veri['izin']) ? $veri['izin'] : 1;
                                            
                                            $urun = '';
                                            switch ($veri['urun']) {
                                                case '1paket':
                                                    $urun = '50 Jeton - 49,99TL';
                                                    break;
                                                case '2paket':
                                                    $urun = '100 Jeton - 99,99TL';
                                                    break;
                                                case '3paket':
                                                    $urun = '250 Jeton - 199,99TL';
                                                    break;
                                                case '4paket':
                                                    $urun = '500 Jeton - 299,99TL';
                                                    break;
                                                case '5paket':
                                                    $urun = '1000 Jeton - 399,99TL';
                                                    break;
                                                case '6paket':
                                                    $urun = 'Sınırsız Jeton - 699,99TL';
                                                    break;
                                                case 'visitor':
                                                    $urun = 'Ziyaretçi Kilidi - 29,99TL';
                                                    break;
                                                case 'visitorunlock':
                                                    $urun = 'Ziyaretçi Kilidi - 59,99TL';
                                                    break;
                                                case 'callunlock':
                                                    $urun = 'Arama Kilidi - 199,99TL';
                                                    break;
                                                case '100paket':
                                                    $urun = '100 Jeton - 199,99TL';
                                                    break;
                                                case '100paketindirim':
                                                    $urun = '100 Jeton - 149,99TL (İndirimli)';
                                                    break;
                                                case '100jetonnormal':
                                                    $urun = '100 Jeton - 149,99TL';
                                                    break;
                                                case '100jetonindirim':
                                                    $urun = '100 Jeton - 99,99TL (İndirimli)';
                                                    break;
                                                case '250paket':
                                                    $urun = '250 Jeton - 299,99TL';
                                                    break;
                                                case '500paket':
                                                    $urun = '500 Jeton - 399,99TL';
                                                    break;
                                                case '1000paket':
                                                    $urun = '1000 Jeton - 499,99TL';
                                                    break;
                                                case '2500paket':
                                                    $urun = '2500 Jeton - 799,99TL';
                                                    break;
                                                case '5000paket':
                                                    $urun = '5000 Jeton - 1399,99TL';
                                                    break;
                                                case 'sinirsizpaket':
                                                    $urun = 'Sınırsız Jeton - 2599,99TL';
                                                    break;
                                                case 'sinpaket':
                                                    $urun = 'Sınırsız Jeton - 699,99TL';
                                                    break;
                                                case 'aramapaket':
                                                    $urun = 'Arama Kilidi - 249,99TL';
                                                    break;
                                                case 'iletisimpaket':
                                                    $urun = 'İletişim Paketi - 249,99TL';
                                                    break;
                                                case 'onaylihesap':
                                                    $urun = 'Onaylı Hesap - 249,99TL';
                                                    break;
                                                case 'boostpaket':
                                                    $urun = 'Boost Paketi - 499,99TL';
                                                    break;
                                                case 'konumpaket':
                                                    $urun = 'Konum Paketi - 249,99TL';
                                                    break;
                                            }
                                        ?>

                                            <tr>
                                                <td class="text-truncate p-2">
                                                    <span><i class="la la-dot-circle-o success font-medium-1"></i> <b><?= $veri['user_id'] ?></b></span>
                                                </td>

                                                <td class="text-truncate p-1">
                                                    <div class="d-flex align-items-center">
                                                        <div class="chat-sidebar-name">
                                                            <h6 class="mb-0"><?= $veri['user_fullname'] ?></h6><span class="text-muted"><?= $veri['user_email'] ?></span>
                                                        </div>
                                                    </div>
                                                </td>

                                                <td class="text-truncate p-2">
                                                    <?php if ($izin == 0): ?>
                                                        <span style="font-style: italic; color: #6c757d; font-weight: 600; text-decoration: underline;"><?= $urun ?></span>
                                                    <?php else: ?>
                                                        <?= $urun ?>
                                                    <?php endif; ?>
                                                </td>
                                                <td class="text-truncate p-2"><?= $veri['gpa'] ?></td>
                                                <td class="text-truncate p-2"><?= date('d.m.Y - H:i:s', $veri['tarih']) ?></td>
                                            </tr>
                                        <?php } ?>

                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<?php include 'footer.php'; ?>
