<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
include_once '../koneksi.php';
include_once '../classes/class.exa.php';
include_once '../classes/class.nexusggr.php';
include_once '../classes/connectAPI.php';

// Pastikan session admin ada, jika tidak, alihkan ke halaman keluar
if (!isset($_SESSION['kode_admin'])) {
    echo '
        <script>
            alert("Terjadi kesalahan, harap masuk kembali!");
            window.location.replace("' . $alamat_admin . 'keluar.php");
        </script>
    ';
    exit();
}

// ======================================================================
// AMBIL DATA DARI DATABASE
// ======================================================================

$deposit = mysqli_query($koneksi, "
    SELECT COALESCE(SUM(CAST(jumlah_deposit AS DECIMAL(15,2))), 0) AS total_jumlah_deposit 
    FROM deposit 
    WHERE status_deposit = 'disetujui'
    AND tanggal_deposit >= CURDATE()
    AND tanggal_deposit < CURDATE() + INTERVAL 1 DAY
");

$data_deposit = mysqli_fetch_assoc($deposit);
$total_jumlah_deposit = $data_deposit['total_jumlah_deposit'] ?? 0;

// Query untuk mendapatkan total withdraw HARI INI dengan status 'disetujui'
$withdraw = mysqli_query($koneksi, "
    SELECT COALESCE(SUM(jumlah_withdraw), 0) AS total_jumlah_withdraw
    FROM withdraw
    WHERE status_withdraw = 'disetujui'
    AND tanggal_withdraw >= CURDATE()
    AND tanggal_withdraw < CURDATE() + INTERVAL 1 DAY
");

$data_withdraw = mysqli_fetch_assoc($withdraw);
$total_jumlah_withdraw = $data_withdraw['total_jumlah_withdraw'] ?? 0;

// Query untuk mendapatkan TOTAL DEPOSIT SEMUA WAKTU dengan status 'disetujui' (untuk Net Revenue)
$deposit_all_time = mysqli_query($koneksi, "
    SELECT COALESCE(SUM(jumlah_deposit), 0) AS total_deposit_all 
    FROM deposit 
    WHERE status_deposit = 'disetujui'
");
$data_deposit_all = mysqli_fetch_assoc($deposit_all_time);
$total_deposit_all_time = $data_deposit_all['total_deposit_all'] ?? 0;

// Query untuk mendapatkan TOTAL WITHDRAW SEMUA WAKTU dengan status 'disetujui' (untuk Net Revenue)
$withdraw_all_time = mysqli_query($koneksi, "
    SELECT COALESCE(SUM(jumlah_withdraw), 0) AS total_withdraw_all 
    FROM withdraw 
    WHERE status_withdraw = 'disetujui'
");
$data_withdraw_all = mysqli_fetch_assoc($withdraw_all_time);
$total_withdraw_all_time = $data_withdraw_all['total_withdraw_all'] ?? 0;

// Query untuk mendapatkan jumlah anggota (semua waktu)
$anggota = mysqli_query($koneksi, "SELECT * FROM anggota");
$jumlah_anggota = mysqli_num_rows($anggota);

// Query untuk mendapatkan jumlah anggota HARI INI dengan status 'aktif'
$anggota_hari_ini = mysqli_query($koneksi, "
    SELECT COALESCE(COUNT(*), 0) AS total_anggota_hari_ini
    FROM anggota
    WHERE status_anggota = 'aktif'
    AND tanggal_bergabung >= CURDATE()
    AND tanggal_bergabung < CURDATE() + INTERVAL 1 DAY
");

$data_anggota_hari_ini = mysqli_fetch_assoc($anggota_hari_ini);
$jumlah_anggota_hari_ini = $data_anggota_hari_ini['total_anggota_hari_ini'] ?? 0;

// Query untuk mendapatkan jumlah promosi
$promosi = mysqli_query($koneksi, "SELECT * FROM promosi");
$jumlah_promosi = mysqli_num_rows($promosi);

// --- Menghitung Net Revenue (SEMUA WAKTU) ---
$total_net_revenue = $total_deposit_all_time - $total_withdraw_all_time;
$formatted_net_revenue = number_format($total_net_revenue, 0, ',', '.');
$net_revenue_class = ($total_net_revenue >= 0) ? 'text-success' : 'text-danger';

// Simpan jumlah saat ini ke dalam session
$_SESSION['lastDepositCount'] = $total_jumlah_deposit;
$_SESSION['lastWithdrawCount'] = $total_jumlah_withdraw;
$_SESSION['lastAnggotaCount'] = $jumlah_anggota;
$_SESSION['lastPromosiCount'] = $jumlah_promosi;
$_SESSION['lastStaffCount'] = 0;

// ======================================================================
// AMBIL DATA DARI API EXA & NEXUS
// ======================================================================

// --- Ambil Saldo Agent dan Pemain dari EXA API ---
$formatted_balance_exa = 'Gagal memuat saldo.';
$formatted_player_balance_exa = 'Gagal memuat saldo.';
try {
    $exaAPI = new GameXaAPI();
    
    // Ambil saldo Agent
    $responseDataExa = $exaAPI->getCurrentAgentInfo();
    if (isset($responseDataExa['success']) && $responseDataExa['success'] === true && isset($responseDataExa['data']['agent']['balance'])) {
        $balance = $responseDataExa['data']['agent']['balance'];
        $formatted_balance_exa = number_format($balance, 0, ',', '.');
    } else {
        $apiMessage = $responseDataExa['message'] ?? 'Respon API Exa tidak valid.';
        $formatted_balance_exa = 'Permintaan API Exa gagal. (' . $apiMessage . ')';
    }
    
    // Ambil saldo Pemain
    $exaPlayers = $exaAPI->getPlayers();
    $total_player_balance_exa = 0;
    if ($exaPlayers['success'] && isset($exaPlayers['data']['players'])) {
        foreach ($exaPlayers['data']['players'] as $player) {
            $total_player_balance_exa += (float)$player['balance'];
        }
        $formatted_player_balance_exa = number_format($total_player_balance_exa, 0, ',', '.');
    } else {
        $formatted_player_balance_exa = 'Gagal memuat saldo pemain.';
    }
} catch (Exception $e) {
    $formatted_balance_exa = 'Error saat memanggil API Exa.';
    $formatted_player_balance_exa = 'Error saat memuat saldo pemain.';
}

// --- Ambil Saldo Agent dan Pemain dari NEXUS API ---
$formatted_balance_nexus = 'Gagal memuat saldo.';
$formatted_player_balance_nexus = 'Gagal memuat saldo.';
try {
    $nexusAPI = new API($user_agent, $signature);
    
    // Ambil saldo Agent
    $responseDataNexus = $nexusAPI->money_info();
    if (isset($responseDataNexus['status']) && $responseDataNexus['status'] == 1 && isset($responseDataNexus['agent']['balance'])) {
        $balance = $responseDataNexus['agent']['balance'];
        $formatted_balance_nexus = number_format($balance, 0, ',', '.');
    } else {
        $apiMessage = $responseDataNexus['msg'] ?? 'Respon API Nexus tidak valid.';
        $formatted_balance_nexus = 'Permintaan API Nexus gagal. (' . $apiMessage . ')';
    }
    
    // Ambil saldo Pemain
    $nexusPlayers = $nexusAPI->money_info_all();
    $total_player_balance_nexus = 0;
    if (isset($nexusPlayers['status']) && $nexusPlayers['status'] == 1 && isset($nexusPlayers['user_list'])) {
        foreach ($nexusPlayers['user_list'] as $user) {
            $total_player_balance_nexus += (float)$user['balance'];
        }
        $formatted_player_balance_nexus = number_format($total_player_balance_nexus, 0, ',', '.');
    } else {
        $formatted_player_balance_nexus = 'Gagal memuat saldo pemain.';
    }
} catch (Exception $e) {
    $formatted_balance_nexus = 'Error saat memanggil API Nexus.';
    $formatted_player_balance_nexus = 'Error saat memuat saldo pemain.';
}

// ======================================================================
// AMBIL DATA TRANSAKSI DIPROSES UNTUK TABEL & KARTU
// ======================================================================

// Query untuk total jumlah deposit yang diproses
$deposit_diproses_count = mysqli_query($koneksi, "SELECT COUNT(*) AS total_diproses FROM deposit WHERE status_deposit = 'diproses'");
$data_deposit_diproses_count = mysqli_fetch_assoc($deposit_diproses_count);
$total_deposit_diproses = $data_deposit_diproses_count['total_diproses'] ?? 0;

// Query untuk total jumlah withdraw yang diproses
$withdraw_diproses_count = mysqli_query($koneksi, "SELECT COUNT(*) AS total_diproses FROM withdraw WHERE status_withdraw = 'diproses'");
$data_withdraw_diproses_count = mysqli_fetch_assoc($withdraw_diproses_count);
$total_withdraw_diproses = $data_withdraw_diproses_count['total_diproses'] ?? 0;

// Query untuk menampilkan detail deposit yang diproses
$query_deposit_diproses_data = mysqli_query($koneksi, "SELECT * FROM deposit WHERE status_deposit = 'diproses' ORDER BY tanggal_deposit DESC");

// Query untuk mendapatkan data queued withdraw
$query_withdraw_diproses = mysqli_query($koneksi, "SELECT * FROM withdraw WHERE status_withdraw = 'diproses' ORDER BY tanggal_withdraw DESC");

// ======================================================================
// AMBIL DATA DEPOSIT HARIAN UNTUK TABEL
// ======================================================================

$query_deposit_harian = mysqli_query($koneksi, "
    SELECT * 
    FROM deposit 
    WHERE status_deposit = 'disetujui'
    AND tanggal_deposit >= CURDATE()
    AND tanggal_deposit < CURDATE() + INTERVAL 1 DAY
    ORDER BY tanggal_deposit DESC
");


if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    if (isset($_POST['dateSearch']) && !empty($_POST['startDate']) && !empty($_POST['endDate'])) {
        $startDate = strtotime($_POST['startDate']);
        $endDate = strtotime($_POST['endDate']);
        $startDateFormat = date("Y-m-d", $startDate);
        $endDateFormat = date("Y-m-d", $endDate);
        $sql_today = mysqli_query($koneksi,"SELECT * FROM `anggota` WHERE tanggal_bergabung BETWEEN '$startDateFormat' AND '$endDateFormat'") or die(mysqli_error());
        $registerToday = mysqli_num_rows($sql_today);
        $dpTotalNya = mysqli_query($koneksi,"SELECT * FROM deposit WHERE status_deposit = 'disetujui' AND waktu_mulai BETWEEN '$startDateFormat' AND '$endDateFormat'") or die(mysqli_error());
        $depoRows = mysqli_num_rows($dpTotalNya);
        $wdTotalNya = mysqli_query($koneksi,"SELECT * FROM withdraw WHERE status_withdraw = 'disetujui' AND tanggal_withdraw BETWEEN '$startDateFormat' AND '$endDateFormat'") or die(mysqli_error());
        $wedeRows = mysqli_num_rows($wdTotalNya);
        $sql_depo_today = mysqli_query($koneksi,"SELECT SUM(jumlah_deposit) as totaldeposit FROM deposit WHERE status_deposit = 'disetujui' AND waktu_mulai BETWEEN '$startDateFormat' AND '$endDateFormat'") or die(mysqli_error());
        $depositToday = mysqli_fetch_array($sql_depo_today);
        if ($depositToday['totaldeposit'] > 0) {
            $depoTot = $depositToday['totaldeposit'];
        } else {
            $depoTot = 0;
        }
        $sql_wede_today = mysqli_query($koneksi,"SELECT SUM(jumlah_withdraw) as totalwithdraw FROM withdraw WHERE status_withdraw = 'disetujui' AND tanggal_withdraw BETWEEN '$startDateFormat' AND '$endDateFormat'") or die(mysqli_error());
        $withdrawToday = mysqli_fetch_array($sql_wede_today);
        if ($withdrawToday['totalwithdraw'] > 0) {
            $wedeTot = $withdrawToday['totalwithdraw'];
        } else {
            $wedeTot = 0;
        }
    } else {
        echo '
          <script>
            alert("Pastikan semua form terisi.");
            window.location.replace("'.$alamat_admin.'dasbor");
          </script>
        ';
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <style>
        .card-stat {
            transition: transform 0.2s;
            cursor: pointer;
        }
        .card-stat:hover {
            transform: translateY(-5px);
        }
        .status-badge {
            font-size: 0.75rem;
            padding: 0.25rem 0.75rem;
            border-radius: 9999px;
            font-weight: bold;
            display: inline-block;
        }
        .status-badge.pending {
            background-color: #fbd38d;
            color: #9c5c16;
        }
        .status-badge.in_progress {
            background-color: #f7e3af;
            color: #a08433;
        }
        .status-badge.resolved {
            background-color: #9ae6b4;
            color: #276749;
        }
        .status-badge.diproses {
            background-color: #ffc107;
            color: #000000;
        }
        .status-badge.dibatalkan {
            background-color: #dc3545;
            color: #ffffff;
        }
        .status-badge.disetujui {
            background-color: #198754;
            color: #ffffff;
        }
    </style>
</head>
<body>
    <audio id="notificationSound" preload="auto"></audio>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        // Kode JavaScript untuk notifikasi dan pembaruan otomatis telah dihapus.
        // Data akan diperbarui setiap kali halaman di-refresh secara manual.
        
        // Fungsi jam_sekarang tetap berjalan secara otomatis
        function updateClock() {
            var now = new Date();
            var hours = now.getHours();
            var minutes = now.getMinutes();
            var seconds = now.getSeconds();
            minutes = minutes < 10 ? '0' + minutes : minutes;
            seconds = seconds < 10 ? '0' + seconds : seconds;
            document.getElementById('jam_sekarang').textContent = 'Jam ' + hours + ':' + minutes + ':' + seconds;
        }
        setInterval(updateClock, 1000);
        updateClock(); // Panggil saat pertama kali dimuat
    </script>
    
        
        <div class="row g-4 mb-4">
            <div class="col-md-6">
                <div class="fw-bold fs-4 text-center text-md-start">Dasbor</div>
            </div>
            <div class="col-md-6 text-center text-md-end">
                <span class="d-block d-md-inline-block"><?php echo ucapan() . ', '; ?></span>
                <span class="d-block d-md-inline-block"><?php echo tanggalIndonesia(date('Y-m-d'), true) . ', '; ?></span>
                <span id="jam_sekarang" class="d-block d-md-inline-block"></span>
            </div>
        </div>
        
        <div class="row g-4 mb-4">
            <div class="col-md-6">
                <div class="text-center text-md-end">
                    <a href="#" data-bs-toggle="modal" data-bs-target="#modalTanggal" class="btn btn-sm btn-primary waves-effect waves-light">
                        <span class="tf-icons mdi mdi-plus me-1"></span>
                        Cek Transaksi
                    </a>
                </div>
            </div>
        </div>
        <?php
        if (empty($registerToday)) {
            
        } else {
        ?>
        <div class="row g-4 mb-4">
            <div class="col-lg-3 col-md-6 col-sm-6">
                <div class="card card-stat h-100">
                    <div class="card-body">
                        <div class="d-flex align-items-center mb-3">
                            <div class="avatar avatar-lg flex-shrink-0 me-3">
                                <div class="avatar-initial bg-label-primary rounded">
                                    <i class="mdi mdi-account-plus mdi-36px"></i>
                                </div>
                            </div>
                            <div class="d-flex flex-column">
                                <span class="text-uppercase text-muted">Total Cek Anggota</span>
                                <h4 class="mb-0 text-white"><?php echo formatAngkaSingkat($registerToday); ?></h4>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            
            <div class="col-lg-3 col-md-6 col-sm-6">
                <div class="card card-stat h-100">
                    <div class="card-body">
                        <div class="d-flex align-items-center mb-3">
                            <div class="avatar avatar-lg flex-shrink-0 me-3">
                                <div class="avatar-initial bg-label-secondary rounded">
                                    <i class="mdi mdi-star-box mdi-36px"></i>
                                </div>
                            </div>
                            <div class="d-flex flex-column">
                                <span class="text-uppercase text-muted">Total Deposit Success</span>
                                <h4 class="mb-0 text-white"><?php echo formatAngkaSingkat($depoTot); ?></h4>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            
            <div class="col-lg-3 col-md-6 col-sm-6">
                <div class="card card-stat h-100">
                    <div class="card-body">
                        <div class="d-flex align-items-center mb-3">
                            <div class="avatar avatar-lg flex-shrink-0 me-3">
                                <div class="avatar-initial bg-label-secondary rounded">
                                    <i class="mdi mdi-star-box mdi-36px"></i>
                                </div>
                            </div>
                            <div class="d-flex flex-column">
                                <span class="text-uppercase text-muted">Total Withdraw Success</span>
                                <h4 class="mb-0 text-white"><?php echo formatAngkaSingkat($wedeTot); ?></h4>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            
            <div class="col-lg-3 col-md-6 col-sm-6">
                <div class="card card-stat h-100">
                    <div class="card-body">
                        <div class="d-flex align-items-center mb-3">
                            <div class="avatar avatar-lg flex-shrink-0 me-3">
                                <div class="avatar-initial bg-label-secondary rounded">
                                    <i class="mdi mdi-star-box mdi-36px"></i>
                                </div>
                            </div>
                            <div class="d-flex flex-column">
                                <span class="text-uppercase text-muted">Jumlah Anggota Deposit Success</span>
                                <h4 class="mb-0 text-white"><?php echo formatAngkaSingkat($depoRows); ?></h4>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            
            <div class="col-lg-3 col-md-6 col-sm-6">
                <div class="card card-stat h-100">
                    <div class="card-body">
                        <div class="d-flex align-items-center mb-3">
                            <div class="avatar avatar-lg flex-shrink-0 me-3">
                                <div class="avatar-initial bg-label-secondary rounded">
                                    <i class="mdi mdi-star-box mdi-36px"></i>
                                </div>
                            </div>
                            <div class="d-flex flex-column">
                                <span class="text-uppercase text-muted">Jumlah Anggota Withdraw Success</span>
                                <h4 class="mb-0 text-white"><?php echo formatAngkaSingkat($wedeRows); ?></h4>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <?php } ?>
        
        <div class="row g-4 mb-4">
            <div class="col-lg-3 col-md-6 col-sm-6">
                <div class="card card-stat h-100">
                    <div class="card-body pb-0">
                        <h5 class="mb-0">EXA Balance</h5>
                    </div>
                    <div class="card-body pt-0">
                        <div class="d-flex align-items-center mb-3">
                            <div class="avatar avatar-lg flex-shrink-0 me-3">
                                <div class="avatar-initial bg-label-success rounded">
                                    <i class="mdi mdi-chart-bar mdi-36px"></i>
                                </div>
                            </div>
                            <div class="row g-0 flex-grow-1">
                                <div class="col-6">
                                    <span class="text-uppercase text-muted">Agent</span>
                                    <h5 class="mb-0 text-white"><?php echo 'Rp.' . $formatted_balance_exa; ?></h5>
                                </div>
                                <div class="col-6">
                                    <span class="text-uppercase text-muted">Player</span>
                                    <h5 class="mb-0 text-white"><?php echo 'Rp.' . $formatted_player_balance_exa; ?></h5>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            
            <div class="col-lg-3 col-md-6 col-sm-6">
                <div class="card card-stat h-100">
                    <div class="card-body pb-0">
                        <h5 class="mb-0">Nexus Balance</h5>
                    </div>
                    <div class="card-body pt-0">
                        <div class="d-flex align-items-center mb-3">
                            <div class="avatar avatar-lg flex-shrink-0 me-3">
                                <div class="avatar-initial bg-label-info rounded">
                                    <i class="mdi mdi-hexagon-slice-5 mdi-36px"></i>
                                </div>
                            </div>
                            <div class="row g-0 flex-grow-1">
                                <div class="col-6">
                                    <span class="text-uppercase text-muted">Agent</span>
                                    <h5 class="mb-0 text-white"><?php echo 'Rp.' . $formatted_balance_nexus; ?></h5>
                                </div>
                                <div class="col-6">
                                    <span class="text-uppercase text-muted">Player</span>
                                    <h5 class="mb-0 text-white"><?php echo 'Rp.' . $formatted_player_balance_nexus; ?></h5>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        
        <div class="row g-4 mb-4">
            <div class="col-lg-3 col-md-6 col-sm-6">
                <div class="card card-stat h-100">
                    <div class="card-body pb-0">
                        <h5 class="mb-0">Deposit Hari Ini</h5>
                    </div>
                    <div class="card-body pt-0">
                        <div class="d-flex align-items-center mb-3">
                            <div class="avatar avatar-lg flex-shrink-0 me-3">
                                <div class="avatar-initial bg-label-success rounded">
                                    <i class="mdi mdi-cash-plus mdi-36px"></i>
                                </div>
                            </div>
                            <div class="row g-0 flex-grow-1">
                                <div class="col-6">
                                    <span class="text-uppercase text-muted">Total</span>
                                    <h5 class="mb-0 text-white"><?php echo 'Rp.' . number_format($total_jumlah_deposit, 0, ',', '.'); ?></h5>
                                </div>
                                <div class="col-6">
                                    <span class="text-uppercase text-muted">Queued</span>
                                    <h5 class="mb-0 text-white"><?php echo $total_deposit_diproses; ?></h5>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            
            <div class="col-lg-3 col-md-6 col-sm-6">
                <div class="card card-stat h-100">
                    <div class="card-body pb-0">
                        <h5 class="mb-0">Withdraw Hari Ini</h5>
                    </div>
                    <div class="card-body pt-0">
                        <div class="d-flex align-items-center mb-3">
                            <div class="avatar avatar-lg flex-shrink-0 me-3">
                                <div class="avatar-initial bg-label-danger rounded">
                                    <i class="mdi mdi-cash-minus mdi-36px"></i>
                                </div>
                            </div>
                            <div class="row g-0 flex-grow-1">
                                <div class="col-6">
                                    <span class="text-uppercase text-muted">Total</span>
                                    <h5 class="mb-0 text-white"><?php echo 'Rp.' . formatAngkaSingkat($total_jumlah_withdraw); ?></h5>
                                </div>
                                <div class="col-6">
                                    <span class="text-uppercase text-muted">Queued</span>
                                    <h5 class="mb-0 text-white"><?php echo $total_withdraw_diproses; ?></h5>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            
            <div class="col-lg-3 col-md-6 col-sm-6">
                <div class="card card-stat h-100">
                    <div class="card-body">
                        <div class="d-flex align-items-center mb-3">
                            <div class="avatar avatar-lg flex-shrink-0 me-3">
                                <div class="avatar-initial bg-label-warning rounded">
                                    <i class="mdi mdi-wallet-bifold mdi-36px"></i>
                                </div>
                            </div>
                            <div class="d-flex flex-column">
                                <span class="text-uppercase text-muted">Net Revenue</span>
                                <h4 class="mb-0 <?php echo $net_revenue_class; ?>"><?php echo 'Rp.' . $formatted_net_revenue; ?></h4>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        
        <div class="row g-4 mb-4">
            <div class="col-lg-3 col-md-6 col-sm-6">
                <div class="card card-stat h-100">
                    <div class="card-body">
                        <div class="d-flex align-items-center mb-3">
                            <div class="avatar avatar-lg flex-shrink-0 me-3">
                                <div class="avatar-initial bg-label-info rounded">
                                    <i class="mdi mdi-account-multiple mdi-36px"></i>
                                </div>
                            </div>
                            <div class="d-flex flex-column">
                                <span class="text-uppercase text-muted">Total Anggota</span>
                                <h4 class="mb-0 text-white"><?php echo formatAngkaSingkat($jumlah_anggota); ?></h4>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            
            <div class="col-lg-3 col-md-6 col-sm-6">
                <div class="card card-stat h-100">
                    <div class="card-body">
                        <div class="d-flex align-items-center mb-3">
                            <div class="avatar avatar-lg flex-shrink-0 me-3">
                                <div class="avatar-initial bg-label-primary rounded">
                                    <i class="mdi mdi-account-plus mdi-36px"></i>
                                </div>
                            </div>
                            <div class="d-flex flex-column">
                                <span class="text-uppercase text-muted">Anggota Hari Ini</span>
                                <h4 class="mb-0 text-white"><?php echo formatAngkaSingkat($jumlah_anggota_hari_ini); ?></h4>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            
            <div class="col-lg-3 col-md-6 col-sm-6">
                <div class="card card-stat h-100">
                    <div class="card-body">
                        <div class="d-flex align-items-center mb-3">
                            <div class="avatar avatar-lg flex-shrink-0 me-3">
                                <div class="avatar-initial bg-label-secondary rounded">
                                    <i class="mdi mdi-star-box mdi-36px"></i>
                                </div>
                            </div>
                            <div class="d-flex flex-column">
                                <span class="text-uppercase text-muted">Total Promosi</span>
                                <h4 class="mb-0 text-white"><?php echo formatAngkaSingkat($jumlah_promosi); ?></h4>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        
        <div class="row g-4 mb-4">
            <div class="col-12">
                <div class="card">
                    <div class="card-header">
                        <h5 class="card-title mb-0">Antrean Transaksi</h5>
                        <small class="text-muted">Daftar deposit dan withdraw yang perlu diproses</small>
                    </div>
                    <div class="card-body p-0">
                        <div class="table-responsive text-nowrap">
                            <table class="table table-hover">
                                <thead>
                                    <tr>
                                        <th class="text-center">No.</th>
                                        <th class="text-center">Tipe</th>
                                        <th class="text-center">Kode</th>
                                        <th class="text-center">Username</th>
                                        <th class="text-center">Jumlah</th>
                                        <th class="text-center">Tanggal</th>
                                        <th class="text-center">Status</th>
                                        <th class="text-center">Aksi</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    $combined_transactions = [];
                                    
                                    // Fetch all queued deposits
                                    while ($row = mysqli_fetch_assoc($query_deposit_diproses_data)) {
                                        $row['type'] = 'Deposit';
                                        $combined_transactions[] = $row;
                                    }
                                    
                                    // Fetch all queued withdrawals
                                    while ($row = mysqli_fetch_assoc($query_withdraw_diproses)) {
                                        $row['type'] = 'Withdraw';
                                        $combined_transactions[] = $row;
                                    }
                                    
                                    // Sort by date (newest first)
                                    usort($combined_transactions, function($a, $b) {
                                        $date_a = new DateTime($a['tanggal_deposit'] ?? $a['tanggal_withdraw']);
                                        $date_b = new DateTime($b['tanggal_deposit'] ?? $b['tanggal_withdraw']);
                                        return $date_b <=> $date_a;
                                    });
                                    
                                    $nomor = 1;
                                    if (empty($combined_transactions)) {
                                        echo '<tr><td colspan="8" class="text-center text-muted py-4">Tidak ada transaksi yang diantrekan saat ini.</td></tr>';
                                    } else {
                                        foreach ($combined_transactions as $data_transaction) {
                                            $type = $data_transaction['type'];
                                            if ($type === 'Deposit') {
                                                $kode = $data_transaction['kode_deposit'];
                                                $id_anggota = $data_transaction['id_anggota_deposit'];
                                                $jumlah = $data_transaction['jumlah_deposit'];
                                                $tanggal = $data_transaction['tanggal_deposit'];
                                                $status = $data_transaction['status_deposit'];
                                                $link_aksi = $alamat_admin . 'deposit';
                                            } else { // Withdraw
                                                $kode = $data_transaction['kode_withdraw'];
                                                $id_anggota = $data_transaction['id_anggota_withdraw'];
                                                $jumlah = $data_transaction['jumlah_withdraw'];
                                                $tanggal = $data_transaction['tanggal_withdraw'];
                                                $status = $data_transaction['status_withdraw'];
                                                $link_aksi = $alamat_admin . 'withdraw';
                                            }
                                            
                                            // Ambil nama pengguna
                                            $anggota_query = mysqli_query($koneksi, "SELECT nama_pengguna_anggota FROM anggota WHERE id_anggota = '$id_anggota'");
                                            $nama_pengguna = mysqli_num_rows($anggota_query) > 0 ? mysqli_fetch_assoc($anggota_query)['nama_pengguna_anggota'] : 'Anggota Dihapus';
                                            
                                            // Status badge
                                            $badge_class = '';
                                            $display_status = '';
                                            switch ($status) {
                                                case 'diproses':
                                                    $badge_class = 'status-badge diproses';
                                                    $display_status = 'Diproses';
                                                    break;
                                                case 'dibatalkan':
                                                    $badge_class = 'status-badge dibatalkan';
                                                    $display_status = 'Dibatalkan';
                                                    break;
                                                case 'disetujui':
                                                    $badge_class = 'status-badge disetujui';
                                                    $display_status = 'Disetujui';
                                                    break;
                                                default:
                                                    $badge_class = 'status-badge';
                                                    $display_status = ucfirst($status);
                                                    break;
                                            }
                                            ?>
                                            <tr>
                                                <td class="text-center"><?php echo $nomor++; ?></td>
                                                <td class="text-center"><?php echo $type; ?></td>
                                                <td class="text-center"><?php echo $kode; ?></td>
                                                <td class="text-center"><?php echo $nama_pengguna; ?></td>
                                                <td class="text-center">Rp.<?php echo number_format($jumlah, 0, ',', '.'); ?></td>
                                                <td class="text-center"><?php echo jamTanggalIndonesia($tanggal); ?></td>
                                                <td class="text-center">
                                                    <span class="<?php echo $badge_class; ?>">
                                                        <?php echo $display_status; ?>
                                                    </span>
                                                </td>
                                                <td class="text-center">
                                                    <a href="<?php echo $link_aksi; ?>" class="btn btn-sm btn-primary waves-effect waves-light" aria-label="Ubah">
                                                        <span class="tf-icons mdi mdi-cog me-1"></span>
                                                    </a>
                                                </td>
                                            </tr>
                                        <?php }
                                    }
                                    ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        
        <div class="container-xxl flex-grow-1 container-p-y">
            <div class="row g-4 mb-4">
                <div class="col-12">
                    <div class="card">
                        <div class="card-header">
                            <h5 class="card-title mb-0">Deposit Harian</h5>
                            <small class="text-muted">Daftar deposit yang disetujui hari ini</small>
                        </div>
                        <div class="card-body p-0">
                            <div class="table-responsive text-nowrap">
                                <table class="table table-hover">
                                    <thead>
                                        <tr>
                                            <th class="text-center">No.</th>
                                            <th class="text-center">Kode Deposit</th>
                                            <th class="text-center">Username</th>
                                            <th class="text-center">Jumlah</th>
                                            <th class="text-center">Tanggal</th>
                                            <th class="text-center">Status</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php
                                        $nomor_deposit = 1;
                                        
                                        if (mysqli_num_rows($query_deposit_harian) == 0) {
                                            echo '<tr><td colspan="6" class="text-center text-muted py-4">Belum ada deposit yang disetujui hari ini.</td></tr>';
                                        } else {
                                            while ($data_deposit_harian = mysqli_fetch_assoc($query_deposit_harian)) {
                                                $kode_deposit = $data_deposit_harian['kode_deposit'];
                                                $id_anggota_deposit = $data_deposit_harian['id_anggota_deposit'];
                                                $jumlah_deposit = $data_deposit_harian['jumlah_deposit'];
                                                $tanggal_deposit = $data_deposit_harian['tanggal_deposit'];
                                                $status_deposit = $data_deposit_harian['status_deposit'];
                                                
                                                // Ambil nama pengguna
                                                $anggota_deposit_query = mysqli_query($koneksi, "SELECT nama_pengguna_anggota FROM anggota WHERE id_anggota = '$id_anggota_deposit'");
                                                $nama_pengguna_deposit = mysqli_num_rows($anggota_deposit_query) > 0 ? mysqli_fetch_assoc($anggota_deposit_query)['nama_pengguna_anggota'] : 'Anggota Dihapus';
                                                
                                                // Cast untuk tampilan (ringan)
                                                $jumlah_numeric = floatval($jumlah_deposit);
                                                
                                                // Status badge
                                                $badge_class_deposit = 'status-badge disetujui';
                                                $display_status_deposit = 'Disetujui';
                                                ?>
                                                <tr>
                                                    <td class="text-center"><?php echo $nomor_deposit++; ?></td>
                                                    <td class="text-center"><?php echo $kode_deposit; ?></td>
                                                    <td class="text-center"><?php echo $nama_pengguna_deposit; ?></td>
                                                    <td class="text-center">Rp.<?php echo number_format($jumlah_numeric, 0, ',', '.'); ?></td>
                                                    <td class="text-center"><?php echo jamTanggalIndonesia($tanggal_deposit); ?></td>
                                                    <td class="text-center">
                                                        <span class="<?php echo $badge_class_deposit; ?>">
                                                            <?php echo $display_status_deposit; ?>
                                                        </span>
                                                    </td>
                                                </tr>
                                            <?php }
                                            ?>
                                            <tr class="table-active">
                                                <td colspan="3" class="text-end fw-bold">Total Deposit Hari Ini:</td>
                                                <td class="text-center fw-bold">Rp.<?php echo number_format($total_jumlah_deposit, 0, ',', '.'); ?></td>
                                                <td colspan="2"></td>
                                            </tr>
                                            <?php
                                        }
                                        ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="modal fade" id="modalTanggal" tabindex="-1" aria-labelledby="modalTanggalLabel" aria-hidden="true">
                    <div class="modal-dialog modal-dialog-centered">
                        <div class="modal-content">
                            <div class="modal-header">
                                <h5 class="modal-title" id="modalTanggalLabel">Cek Transaksi</h5>
                                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                            </div>
                            <div class="modal-body">
                                <div class="card p-4">
                                    <form method="POST" action="">
                                        <div class="mb-3">
                                            <label for="startDate" class="form-label">Dari Tanggal:</label>
                                            <input type="date" class="form-control" id="startDate" name="startDate" value="" required>
                                        </div>
                                        <div class="mb-3">
                                            <label for="endDate" class="form-label">Sampai Tanggal:</label>
                                            <input type="date" class="form-control" id="endDate" name="endDate" value="" required>
                                        </div>
                                        <div class="text-end">
                                            <button type="submit" name="dateSearch" class="btn btn-primary waves-effect waves-light">Simpan</button>
                                        </div>
                                    </form>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
</body>
</html>