<?php include 'header.php'; ?>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@8"></script>
<script type="text/javascript">
    
function jetonUpdate(user_id){


var url = "api.php";
    var bilgi = {
        type: 'jetonUpdate',
        user_id: user_id,
        kredi: $('#jeton_'+user_id).val()
    }

    $.post(url, bilgi, function(response) {
        var json = JSON.parse(response);
        if (json.result == "1") {

                  Swal.fire({
                                        type: 'success',
                                        title: 'Başarılı!',
                                        html: 'Kullanıcı jetonu güncellendi.',
                                        confirmButtonText: 'Tamam'
                                    }).then((result) => {
                                        if (result.value) {

                                            //location.reload();

                                        }
                                    })

        }
    });


}

function satisIzniToplu(){
    var url = "api.php";
    var bilgi = {
        type: 'satisIzniToplu'
    }

    $.post(url, bilgi, function(response) {
        var json = JSON.parse(response);
        if (json.result == "1") {
            Swal.fire({
                type: 'success',
                title: 'Başarılı!',
                html: json.count + ' kullanıcıya satış izni verildi.',
                confirmButtonText: 'Tamam'
            });
        } else {
            Swal.fire({
                type: 'error',
                title: 'Hata!',
                html: 'Bir hata oluştu.',
                confirmButtonText: 'Tamam'
            });
        }
    });
}

</script>
<style>
.text-muted {
  white-space: nowrap; /* Metnin yan yana kaymasını sağlar */
  overflow: hidden; /* Metnin belirli bir noktada kesilmesini sağlar */
  text-overflow: ellipsis; /* Kesilen kısım yerine ... işareti koyma */
  display: inline-block; /* Elementin genişliği span elementinin genişliği ile sınırlandırılır */
  max-width: 250px; /* Belirli bir maksimum genişlik ayarlanabilir */
}
</style>
    <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">
<?php 
// Satış izni olmayan kullanıcı sayısını al
try {
    $satisIzniStmt = $db->query("SELECT COUNT(*) FROM kullanicilar WHERE user_bot = 0 AND (user_satisizin IS NULL OR user_satisizin = 0 OR user_satisizin = '')");
    $satisIzniOlmayanSayisi = $satisIzniStmt ? $satisIzniStmt->fetchColumn() : 0;
} catch (Exception $e) {
    $satisIzniOlmayanSayisi = 0;
}
?>
<div class="row">



                    <div id="recent-transactions" class="col-12">
                        <div class="card">
                            <div class="card-header">
                                <h4 class="card-title">Kaydolan Son 250 Üye</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">
                                        <?php if($satisIzniOlmayanSayisi > 0): ?>
                                        <li><a class="btn btn-sm btn-success box-shadow-2 round btn-min-width pull-right" href="javascript:void(0);" onclick="satisIzniToplu();" style="margin-right: 5px;">Satış İzni Ver (<?=$satisIzniOlmayanSayisi?> Kişi)</a></li>
                                        <?php endif; ?>
                                        <li><a class="btn btn-sm btn-danger box-shadow-2 round btn-min-width pull-right" href="istatistik.php">Kayıt İstatistikleri</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">Üye ID</th>
                                                <th class="border-top-0">İsim Soyisim</th>
                                                <th class="border-top-0">Bilgi</th>
                                                <th class="border-top-0">Son Aktivite</th>
                                                <th class="border-top-0">Jeton Sayısı</th>
                                                <th class="border-top-0">İşlem</th>
                                            </tr>
                                        </thead>
                                        <tbody>

                                            <?php 
                                            $query = $db->query("SELECT * FROM kullanicilar WHERE user_bot = 0 ORDER BY id DESC LIMIT 250", PDO::FETCH_ASSOC);
                                            ?>
                                             <?php foreach($query as $veri): ?>
                                            <tr>

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

                                                   <div class="d-flex align-items-center">


                                    <div class="avatar m-0 mr-50"><img src="../<?=$veri['user_picture']?>" height="36" width="36" alt="sidebar user image">
                                        <span class="avatar-status-online"></span>
                                    </div>

                                    <div class="chat-sidebar-name">
                                        <h6 class="mb-0"><?=$veri['user_fullname']?> <?=$veri['user_ban'] || $veri['user_deaktif'] ? '<span class="badge badge-danger">Deaktif</span>' : '<span class="badge badge-success">Aktif</span>'?> <?= $veri['user_regtype'] === 'google' ? '<span class="badge badge-warning">Google</span>' : '<span class="badge badge-primary">Mail</span>' ?>   </h6><span class="text-muted"><?=$veri['user_email']?></span>
                                    </div>
                                </div>
                                                </td>

                                                <td class="text-truncate p-2">
                                                    <?php 
                                                    if(isset($veri['user_yas'])){
                                                    ?>
                                                    Yaş: <b><?=$veri['user_yas']?></b>, <?=$veri['user_sehir']?>
                                                <?php }else{
                                                    echo "<i>Bilgi Girilmemiş</i>";
                                                }

                                                ?>
                                                <?= $veri['user_cinsiyet'] === 'erkek' ? '<span class="badge badge-info">E</span>' : '<span class="badge badge-danger">K</span>' ?>
                                                </td>
                                                <td class="text-truncate p-2">
                                                <b>K:</b> <?=date('d.m.Y - H:i:s',$veri['user_kayit'])?>
                                                <br>
                                                <b>A:</b> <?=date('d.m.Y - H:i:s',$veri['user_songiris'])?>
                                                </td>
                                                
                                                <td>
                                                    
                                                    
                                                    <fieldset class="form-group position-relative mb-0">
                                        <input type="text" class="warning input-sm form-control border-warning" id="jeton_<?=$veri['user_id']?>" value="<?=$veri['user_jeton']?>">
                                    </fieldset>




                                                </td>

                                                <td>
                                                   
                        <button class="btn btn-sm btn-info round dropdown-toggle" id="btnGroupDrop1" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"><i class="ft-settings icon-left"></i> İşlemler</button>
                        <div class="dropdown-menu" aria-labelledby="btnGroupDrop1">
                            <a class="dropdown-item" onclick="jetonUpdate(<?=$veri['user_id']?>);" href="javascript:void(0)">Jeton Güncelle</a>
                            <a onclick="tekil_mesaj_bot(<?=$veri['user_id']?>);" data-toggle="modal" data-target="#bootstrap" class="dropdown-item" href="javascript:void(0);">Mesaj Gönder</a>
                            <a class="dropdown-item" onclick="banla(<?=$veri['user_id']?>);" href="javascript:void(0)">Yasakla</a></div>
                 
                                                </td>
                                               
                                            </tr>
                                            <?php endforeach; ?>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                  </div>   </div>   </div>   </div>
                    
                


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