<?php
include_once '../db/koneksi.php';

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

// Ambil semua anggota
$anggota_query = mysqli_query($koneksi, "
    SELECT *
    FROM tb_user where referral = ''
");
$all= mysqli_query($koneksi, "select * from tb_user")

?>

<div class="container-xxl flex-grow-1 container-p-y">
    <div class="row gy-4 mb-4">
        <div class="col-md-6">
            <div class="fw-bold fs-4 text-center text-md-start">Referral</div>
        </div>
    </div>
    <div class="card table-responsive p-3">
        <table class="table" id="example">
            <thead>
                <tr>
                    <th>#</th>
                    <th>ID Anggota</th>
                    <th>Nama Pengguna</th>
                    <!-- <th>Deposit Reff</th> -->
                    <th>Turnover Reff</th>
                    <th>Total Depo Sukses</th>
                    <th>Total Referral</th>
                    <th>Aksi</th>
                </tr>
            </thead>
            <tbody>
                <?php
                if (mysqli_num_rows($anggota_query) > 0) {
                    $no = 1;
                    while ($data_anggota = mysqli_fetch_array($anggota_query)) {
                        $id_anggota   = $data_anggota['id'];
                        $nama_pengguna = $data_anggota['username'];
                        $nm = $data_anggota['username'];
                        $upline = $data_anggota['username'];

                        // Total deposit reff
                        $deposit_query = mysqli_query($koneksi, "
                            SELECT SUM(d.jumlah) AS total_deposit 
                            FROM tb_deposit d
                            JOIN tb_user a ON d.username = a.username 
                            WHERE a.referral = '$nm' AND d.status = 'Sukses'
                        ");
                        $deposit_data   = mysqli_fetch_array($deposit_query);
                        $total_deposit  = $deposit_data['total_deposit'] ? $deposit_data['total_deposit'] : 0;

                        // Total turnover = 10% dari deposit reff
                        $total_turnover = $total_deposit * 0.10;

                        // Total referral (jumlah downline langsung)
                        $referral_query = mysqli_query($koneksi, "
                            SELECT COUNT(*) AS total_referral 
                            FROM tb_user 
                            WHERE referral = '$nama_pengguna'
                        ");
                        $referral_data  = mysqli_fetch_array($referral_query);
                        $total_referral = $referral_data['total_referral'];
                        
                      // $depo_sukses_query= mysqli_query($koneksi, "select count(*) as total_sukses from deposit where status_deposit='disetujui'");
                      $depo_sukses_query = mysqli_query($koneksi, "
                            SELECT COUNT(*) AS total_sukses 
                            FROM tb_deposit d
                            JOIN tb_user a ON d.username = a.username
                            WHERE d.status = 'Sukses'
                            AND a.referral = '$nama_pengguna'
                        ");

                        $sukses_data = mysqli_fetch_array($depo_sukses_query);
                        $total_sukses = $sukses_data['total_sukses'];
            ?>
                <tr>
                    <td><?php echo $no++; ?></td>
                    <td><?php echo $id_anggota; ?></td>
                    <td><?php echo $nm; ?></td>
                    <!--<td><?php echo number_format($total_deposit, 2, ',', '.'); ?></td>-->
                    <td><?php echo number_format($total_turnover, 2, ',', '.'); ?></td>
                    <td>
                        <span class="badge bg-primary">
                            <?php echo $total_sukses;?> ID
                        </span>
                    </td>
                    <td>
                        <span class="badge bg-secondary">
                            <?php echo $total_referral; ?> ID
                        </span>
                    </td>
                    <td>
                        <a href="downline/<?php echo urlencode($upline); ?>" 
                           class="btn btn-sm btn-info">
                           Lihat Downline
                        </a>
                    </td>
                </tr>
                <?php
                    }
                }?>
            </tbody>
        </table>
    </div>
</div>

<script>
$(document).ready(function () {
    $('#example').DataTable({
        "pageLength": 25,
        "autoWidth": false,
        "ordering": true
    });
});
</script>

<script>
$(document).ready(function(){
    var table = $('#example').DataTable();
    $('#searchTable').on('keyup', function(){
        table.search(this.value).draw();
    });
});
</script>
