<?php $__env->startSection('panel'); ?>
    <h4 class="fw-bold py-3 mb-3">
        Bonus Rollingan Settings
    </h4>

    
    <?php if(session('success')): ?>
        <div class="alert alert-success alert-dismissible fade show" role="alert">
            <i class="fas fa-check-circle me-1"></i> <?php echo e(session('success')); ?>

            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
    <?php endif; ?>
    
    
    <div class="row mb-4">
        <div class="col-12">
            <div class="card bg-white shadow-sm border-0" style="border-left: 5px solid #ff9800 !important;">
                <div class="card-body d-flex justify-content-between align-items-center flex-wrap">
                    <div>
                        <h5 class="mb-1 fw-bold text-dark"><i class="fas fa-gamepad text-warning me-2"></i> Pusat Kendali Rollingan</h5>
                        <p class="text-muted mb-0" style="font-size: 13px;">Eksekusi penarikan data dan pembagian bonus secara manual untuk menghindari beban server (Overload).</p>
                    </div>
                    <div class="mt-3 mt-md-0 d-flex gap-2">
                        
                        <button type="button" class="btn btn-outline-primary" id="btn-sync-api">
                            <i class="fas fa-cloud-download-alt me-1"></i> 1. Tarik Data (Sync API)
                        </button>
                        
                        
                        <button type="button" class="btn btn-success" id="btn-bagi-bonus">
                            <i class="fas fa-money-bill-wave me-1"></i> 2. Bagikan Rollingan Sekarang
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="row">
        
        <div class="col-lg-4 col-md-12 mb-4">
            <div class="card h-100">
                <div class="card-header bg-primary text-white py-3">
                    <h6 class="mb-0 text-white"><i class="fas fa-plus-circle me-2"></i> Tambah / Update Setting</h6>
                </div>
                <div class="card-body mt-3">
                    <form action="<?php echo e(route('admin.rollingan.store')); ?>" method="POST">
                        <?php echo csrf_field(); ?>
                        
                        <div class="mb-3">
                            <label class="form-label fw-bold">Kategori Game</label>
                            <select name="game_type" class="form-select" required>
                                <option value="">-- Pilih Kategori --</option>
                                <option value="slot">Slot Games</option>
                                <option value="casino">Live Casino</option>
                                <option value="sportsbook">Sportsbook</option>
                                <option value="fish">Tembak Ikan</option>
                                <option value="poker">Poker / P2P</option>
                            </select>
                            <div class="form-text">Data lama akan ditimpa jika kategori sudah ada.</div>
                        </div>

                        <div class="mb-3">
                            <label class="form-label fw-bold">Minimal Turnover (IDR)</label>
                            <div class="input-group">
                                <span class="input-group-text">Rp</span>
                                <input type="number" name="min_turnover" class="form-control" placeholder="1000000" required>
                            </div>
                            <div class="form-text text-danger">*Hanya angka, tanpa titik/koma</div>
                        </div>

                        <div class="mb-4">
                            <label class="form-label fw-bold">Persentase Bonus (%)</label>
                            <div class="input-group">
                                <input type="number" step="0.01" name="persentase" class="form-control" placeholder="0.5" required>
                                <span class="input-group-text">%</span>
                            </div>
                            <div class="form-text">Gunakan titik untuk desimal (Contoh: 0.5).</div>
                        </div>

                        <div class="d-grid">
                            <button type="submit" class="btn btn-primary"><i class="fas fa-save me-1"></i> Simpan Pengaturan</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>

        
        <div class="col-lg-8 col-md-12 mb-4">
            <div class="card h-100">
                <div class="card-header bg-dark text-white py-3">
                    <h6 class="mb-0 text-white"><i class="fas fa-list me-2"></i> Daftar Pengaturan Aktif</h6>
                </div>
                <div class="card-datatable table-responsive pt-2">
                    <table class="table table-bordered table-sm w-100 table-hover">
                        <thead class="table-dark">
                            <tr>
                                <th class="text-center" style="font-size: 12px;">No</th>
                                <th class="text-center" style="font-size: 12px;">Kategori Game</th>
                                <th class="text-center" style="font-size: 12px;">Syarat Min. TO (IDR)</th>
                                <th class="text-center" style="font-size: 12px;">Persentase (%)</th>
                                <th class="text-center" style="font-size: 12px;">Update Terakhir</th>
                                <th class="text-center" style="font-size: 12px;">Aksi</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php $__empty_1 = true; $__currentLoopData = $settings; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $index => $set): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                                <tr>
                                    <td class="text-center align-middle"><?php echo e($index + 1); ?></td>
                                    <td class="text-center align-middle"><strong class="text-uppercase text-primary"><?php echo e($set->game_type); ?></strong></td>
                                    <td class="text-end align-middle pe-3"><?php echo e(number_format($set->min_turnover, 0, ',', '.')); ?></td>
                                    <td class="text-center align-middle">
                                        <span class="badge bg-success" style="font-size: 13px;"><?php echo e($set->persentase); ?> %</span>
                                    </td>
                                    <td class="text-center align-middle" style="font-size: 12px;">
                                        <?php echo e(\Carbon\Carbon::parse($set->updated_at)->format('d M Y H:i')); ?>

                                    </td>
                                    <td class="text-center align-middle">
                                        <form action="<?php echo e(route('admin.rollingan.destroy', $set->id)); ?>" method="POST" class="d-inline" onsubmit="return confirm('Yakin ingin menghapus pengaturan untuk <?php echo e(strtoupper($set->game_type)); ?>?');">
                                            <?php echo csrf_field(); ?>
                                            <?php echo method_field('DELETE'); ?>
                                            <button type="submit" class="btn btn-sm btn-danger" title="Hapus Pengaturan">
                                                <i class="fas fa-trash-alt"></i>
                                            </button>
                                        </form>
                                    </td>
                                </tr>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                                <tr>
                                    <td colspan="6" class="text-center py-4 text-muted">Belum ada pengaturan rollingan yang dibuat.</td>
                                </tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
<?php $__env->stopSection(); ?>

<?php $__env->startPush('script'); ?>
<script>
    // Opsional: Jika Anda ingin tabel ini menggunakan DataTable seperti halaman sebelumnya
    // Hapus tanda komentar di bawah ini jika ingin mengaktifkannya
    /*
    $(function () {
        $('table').DataTable({
            "paging": false,
            "lengthChange": false,
            "searching": false,
            "ordering": true,
            "info": false,
            "autoWidth": false,
            "responsive": true,
            columnDefs: [
                { orderable: false, targets: 5 } // Matikan sorting di kolom Aksi
            ]
        });
    });
    */
</script>
<script type="text/javascript">
$(document).ready(function() {
    
    // SETUP CSRF TOKEN UNTUK AJAX LARAVEL
    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });

    // 1. TOMBOL SYNC API (TARIK DATA)
    $('#btn-sync-api').on('click', function() {
        let $btn = $(this);
        let originalText = $btn.html();

        // Menggunakan confirm() bawaan browser, 100% Anti-Error
        if (confirm('Tarik data taruhan terbaru dari server API Seamless sekarang? Proses ini memakan waktu beberapa saat.')) {
            
            $btn.prop('disabled', true).html('<i class="fas fa-spinner fa-spin me-1"></i> Menarik Data...');
            
            $.ajax({
                url: "<?php echo e(route('admin.rollingan.manual_sync')); ?>",
                type: "POST",
                success: function(res) {
                    $btn.prop('disabled', false).html(originalText);
                    // Menggunakan alert() bawaan browser
                    alert(res.message); 
                },
                error: function(xhr) {
                    $btn.prop('disabled', false).html(originalText);
                    let errMsg = xhr.responseJSON && xhr.responseJSON.message ? xhr.responseJSON.message : 'Terjadi kesalahan koneksi atau server sibuk (Timeout).';
                    alert('GAGAL: ' + errMsg);
                }
            });
        }
    });

    // 2. TOMBOL BAGIKAN BONUS
    $('#btn-bagi-bonus').on('click', function() {
        let $btn = $(this);
        let originalText = $btn.html();

        if (confirm('Bagikan bonus rollingan ke semua player yang memenuhi syarat SEKARANG? Pastikan Anda sudah menarik data (Sync API) terlebih dahulu.')) {
            
            $btn.prop('disabled', true).html('<i class="fas fa-spinner fa-spin me-1"></i> Sedang Menghitung & Membagikan...');
            
            $.ajax({
                url: "<?php echo e(route('admin.rollingan.manual_distribute')); ?>",
                type: "POST",
                success: function(res) {
                    $btn.prop('disabled', false).html(originalText);
                    
                    alert(res.message);
                    
                    if(res.success) {
                        location.reload(); // Refresh halaman agar tabel terupdate
                    }
                },
                error: function(xhr) {
                    $btn.prop('disabled', false).html(originalText);
                    let errMsg = xhr.responseJSON && xhr.responseJSON.message ? xhr.responseJSON.message : 'Terjadi kesalahan koneksi saat membagikan bonus.';
                    alert('GAGAL: ' + errMsg);
                }
            });
        }
    });

});
</script>
<?php $__env->stopPush(); ?>
<?php echo $__env->make('backend.layouts.main', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH /www/wwwroot/website/sewa/butuhjp/resources/views/backend/rollingan/index.blade.php ENDPATH**/ ?>