<?php
include 'header.php';

// Son 30 gün verisi
$month_ago_timestamp = time() - 2592000;
$stmt = $db->prepare("SELECT DATE(FROM_UNIXTIME(user_kayit)) AS tarih, COUNT(*) AS toplam_kayit, SUM(user_cinsiyet='erkek') AS erkek_kayit, SUM(user_cinsiyet='kadin') AS kadin_kayit, SUM(user_regtype='google') AS google_kayit, SUM(user_deaktif!=0) AS deaktif_kayit FROM kullanicilar WHERE user_kayit >= :month_ago_timestamp AND user_bot = 0 GROUP BY tarih ORDER BY tarih DESC");
$stmt->bindParam(':month_ago_timestamp', $month_ago_timestamp, PDO::PARAM_INT);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);

// Toplam istatistikler
$total_users = array_sum(array_column($result, 'toplam_kayit'));
$total_erkek = array_sum(array_column($result, 'erkek_kayit'));
$total_kadin = array_sum(array_column($result, 'kadin_kayit'));
$total_google = array_sum(array_column($result, 'google_kayit'));
$total_deaktif = array_sum(array_column($result, 'deaktif_kayit'));
$total_aktif = $total_users - $total_deaktif;

// Yüzdelik hesaplamalar
$erkek_yuzde = $total_users > 0 ? round(($total_erkek / $total_users) * 100) : 0;
$kadin_yuzde = $total_users > 0 ? round(($total_kadin / $total_users) * 100) : 0;
$google_yuzde = $total_users > 0 ? round(($total_google / $total_users) * 100) : 0;
$aktif_yuzde = $total_users > 0 ? round(($total_aktif / $total_users) * 100) : 0;

// Türkçe gün isimleri
$gunler = [
    'Monday' => 'Pazartesi',
    'Tuesday' => 'Salı',
    'Wednesday' => 'Çarşamba',
    'Thursday' => 'Perşembe',
    'Friday' => 'Cuma',
    'Saturday' => 'Cumartesi',
    'Sunday' => 'Pazar'
];
?>

<style>
.stat-card {
    border-radius: 12px;
    padding: 1.5rem;
    margin-bottom: 1.5rem;
    box-shadow: 0 2px 8px rgba(0,0,0,0.08);
    transition: all 0.3s ease;
    border: none;
}
.stat-card:hover {
    transform: translateY(-5px);
    box-shadow: 0 8px 20px rgba(0,0,0,0.12);
}
.stat-card .icon-wrapper {
    width: 50px;
    height: 50px;
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 24px;
    margin-bottom: 1rem;
}
.stat-card h2 {
    font-size: 32px;
    font-weight: 700;
    margin: 0;
    color: #2c3e50;
}
.stat-card p {
    margin: 0;
    color: #7f8c8d;
    font-size: 14px;
    font-weight: 500;
}
.stat-card .trend {
    font-size: 12px;
    font-weight: 600;
    margin-top: 0.5rem;
}
.progress-modern {
    height: 8px;
    border-radius: 10px;
    background: #ecf0f1;
    overflow: hidden;
    margin-top: 0.5rem;
}
.progress-modern .progress-bar {
    border-radius: 10px;
    transition: width 0.6s ease;
}
.table-modern {
    background: white;
    border-radius: 12px;
    overflow: hidden;
    box-shadow: 0 2px 8px rgba(0,0,0,0.08);
}
.table-modern thead {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
}
.table-modern thead th {
    border: none;
    padding: 1rem;
    font-weight: 600;
    font-size: 13px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}
.table-modern tbody tr {
    transition: all 0.2s ease;
    border-bottom: 1px solid #f1f3f5;
}
.table-modern tbody tr:hover {
    background: #f8f9fa;
    transform: scale(1.01);
}
.table-modern tbody td {
    padding: 1rem;
    vertical-align: middle;
}
.badge-modern {
    padding: 6px 12px;
    border-radius: 6px;
    font-weight: 600;
    font-size: 12px;
}
</style>

<div class="app-content container center-layout mt-2">
   <div class="content-overlay"></div>
   <div class="content-wrapper">
      <div class="content-body">
         <!-- Özet Kartları -->
         <div class="row">
            <div class="col-xl-3 col-lg-6 col-12">
               <div class="stat-card" style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);">
                   <h2 style="color: white;"><?= number_format($total_users) ?></h2>
                   <p style="color: rgba(255,255,255,0.9);">Toplam Kayıt<br><small style="font-size: 12px; opacity: 0.8;">Son 30 günde kayıt olan</small></p>
                   <div class="progress-modern">
                      <div class="progress-bar" style="width: 100%; background: white; opacity: 0.3;"></div>
                   </div>
                   <div class="trend" style="color: white; opacity: 0.8;">Toplam Kayıt</div>
                </div>
            </div>
            
            <div class="col-xl-3 col-lg-6 col-12">
               <div class="stat-card" style="background: linear-gradient(135deg, #28d094 0%, #10b981 100%);">
                   <h2 style="color: white;"><?= number_format($total_aktif) ?></h2>
                   <p style="color: rgba(255,255,255,0.9);">Aktif Kullanıcı<br><small style="font-size: 12px; opacity: 0.8;">Deaktif olmayan (30 gün)</small></p>
                   <div class="progress-modern">
                      <div class="progress-bar" style="width: <?= $aktif_yuzde ?>%; background: white;"></div>
                   </div>
                   <div class="trend" style="color: white;">%<?= $aktif_yuzde ?> Aktif Oran</div>
                </div>
            </div>
            
            <div class="col-xl-3 col-lg-6 col-12">
               <div class="stat-card" style="background: white; border: 2px solid #3b82f6;">
                   <h2><?= number_format($total_erkek) ?> <span style="font-size: 16px; color: #7f8c8d;">/</span> <span style="font-size: 20px; color: #e74c3c;"><?= number_format($total_kadin) ?></span></h2>
                   <p>Erkek / Kadın<br><small style="font-size: 12px; color: #95a5a6;">Son 30 günde kayıt olan</small></p>
                   <div class="progress-modern">
                      <div class="progress-bar" style="width: <?= $erkek_yuzde ?>%; background: #3b82f6;"></div>
                      <div class="progress-bar" style="width: <?= $kadin_yuzde ?>%; background: #e74c3c;"></div>
                   </div>
                   <div class="trend" style="color: #3b82f6;">
                      %<?= $erkek_yuzde ?> E
                      <span style="color: #e74c3c; margin-left: 10px;">%<?= $kadin_yuzde ?> K</span>
                   </div>
                </div>
            </div>
            
            <div class="col-xl-3 col-lg-6 col-12">
               <div class="stat-card" style="background: white; border: 2px solid #f59e0b;">
                   <h2><?= number_format($total_google) ?></h2>
                   <p>Google Kayıt<br><small style="font-size: 12px; color: #95a5a6;">Son 30 günde kayıt olan</small></p>
                   <div class="progress-modern">
                      <div class="progress-bar" style="width: <?= $google_yuzde ?>%; background: #f59e0b;"></div>
                   </div>
                   <div class="trend" style="color: #f59e0b;">%<?= $google_yuzde ?> Google ile</div>
                </div>
            </div>
         </div>

         <!-- Günlük Detay Tablosu -->
         <div class="row">
            <div class="col-12">
               <div class="card table-modern">
                  <div class="card-header" style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); border: none;">
                     <h4 class="card-title" style="color: white; margin: 0;">
                        <i class="ft-calendar mr-2"></i>Günlük Kayıt Detayları (Son 30 Gün)
                     </h4>
                  </div>
                  <div class="card-content">
                     <div class="table-responsive">
                        <table class="table mb-0">
                           <thead>
                              <tr>
                                 <th><i class="ft-calendar mr-1"></i>Tarih</th>
                                 <th class="text-center"><i class="ft-users mr-1"></i>Toplam</th>
                                 <th class="text-center"><i class="ft-user mr-1"></i>Erkek</th>
                                 <th class="text-center"><i class="ft-user mr-1"></i>Kadın</th>
                                 <th class="text-center"><i class="ft-mail mr-1"></i>Google</th>
                                 <th class="text-center"><i class="ft-x-circle mr-1"></i>Deaktif</th>
                              </tr>
                           </thead>
                           <tbody>
                           <?php foreach ($result as $row): 
                              $row_total = $row['toplam_kayit'];
                              $row_aktif = $row_total - $row['deaktif_kayit'];
                           ?>
                              <tr>
                                 <td>
                                    <strong style="color: #2c3e50;">
                                       <?php echo date('d.m.Y', strtotime($row['tarih'])); ?>
                                    </strong>
                                    <br>
                                    <small style="color: #95a5a6;">
                                       <?php 
                                       $gun_adi = date('l', strtotime($row['tarih']));
                                       echo isset($gunler[$gun_adi]) ? $gunler[$gun_adi] : $gun_adi;
                                       ?>
                                    </small>
                                 </td>
                                 <td class="text-center">
                                    <span class="badge badge-modern" style="background: #667eea; color: white;">
                                       <?= $row['toplam_kayit'] ?> Kayıt
                                    </span>
                                 </td>
                                 <td class="text-center">
                                    <span class="badge badge-modern" style="background: #dbeafe; color: #3b82f6;">
                                       <i class="ft-user"></i> <?= $row['erkek_kayit'] ?>
                                    </span>
                                 </td>
                                 <td class="text-center">
                                    <span class="badge badge-modern" style="background: #fee2e2; color: #e74c3c;">
                                       <i class="ft-user"></i> <?= $row['kadin_kayit'] ?>
                                    </span>
                                 </td>
                                 <td class="text-center">
                                    <span class="badge badge-modern" style="background: #fef3c7; color: #f59e0b;">
                                       <i class="ft-mail"></i> <?= $row['google_kayit'] ?>
                                    </span>
                                 </td>
                                 <td class="text-center">
                                    <?php if($row['deaktif_kayit'] > 0): ?>
                                       <span class="badge badge-modern" style="background: #fee2e2; color: #ef4444;">
                                          <i class="ft-x-circle"></i> <?= $row['deaktif_kayit'] ?>
                                       </span>
                                    <?php else: ?>
                                       <span class="badge badge-modern" style="background: #d1fae5; color: #10b981;">
                                          <i class="ft-check"></i> 0
                                       </span>
                                    <?php endif; ?>
                                 </td>
                              </tr>
                           <?php endforeach; ?>
                           </tbody>
                        </table>
                     </div>
                  </div>
               </div>
            </div>
         </div>
      </div>
   </div>
</div>

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