<div class="page-breadcrumb">
    <div class="row">
        <div class="col-5 align-self-center">
            <h4 class="page-title">User</h4>
        </div>
        <div class="col-7 align-self-center">
            <div class="d-flex align-items-center justify-content-end">
                <nav aria-label="breadcrumb">
                    <ol class="breadcrumb">
                        <li class="breadcrumb-item">
                            <a href="#">Daftar User</a>
                        </li>
                        <li class="breadcrumb-item active" aria-current="page">Menu User</li>
                    </ol>
                </nav>
            </div>
        </div>
    </div>
</div>

<div class="container-fluid">

    <!-- Tabel Daftar User mulai dari sini -->
    <div class="row">
        <div class="col-lg-12">
            <div class="card">
                <div class="card-body d-flex justify-content-between align-items-center">
                    <h4 class="card-title mb-0">Daftar User</h4>
                    <button type="button" onclick="exportNohpDariTabel()" class="btn btn-info btn-sm">
                        <i class="fas fa-phone-alt mr-1"></i> Export No HP (.txt)
                    </button>
                </div>
                <div class="table-responsive">
                    <table id="zero_config" class="table table-striped table-bordered ">
                        <thead>
                        <tr>
                            <th scope="col">#</th>
                            <th scope="col">Username</th>
                            <th scope="col">Full Name</th>
                            <th scope="col">Email</th>
                            <th scope="col">No Handphone</th>
                            <th scope="col">Rekening Bank</th>
                            <th scope="col">Nomor Rekening</th>
                            <th scope="col">Status Game</th>
                            <th scope="col">Status</th>
                            <th scope="col">Total Deposit</th>
                            <th scope="col">Total Withdraw</th>
                            <th scope="col">Aksi</th>
                        </tr>
                        </thead>
                        <tbody>
                            <?php
                        $query = mysqli_query($koneksi, "
                            SELECT 
                                u.*,
                                b.nama_bank,
                                b.nomor_rekening,
                                IFNULL(t.total_dp,0) AS total_dp,
                                IFNULL(t.total_wd,0) AS total_wd
                            FROM tb_user u
                            LEFT JOIN tb_bank b ON b.id_user = u.extplayer
                            LEFT JOIN (
                                SELECT 
                                    id_user,
                                    SUM(CASE WHEN transaksi='Top Up' THEN total ELSE 0 END) AS total_dp,
                                    SUM(CASE WHEN transaksi='Withdraw' THEN total ELSE 0 END) AS total_wd
                                FROM tb_transaksi
                                GROUP BY id_user
                            ) t ON t.id_user = u.extplayer
                            WHERE u.level = 'user'
                            AND u.refferal IS NOT NULL
                            AND u.refferal <> ''
                        ");
                        $no = 1;
                        while ($data = mysqli_fetch_assoc($query)) {
                            ?>
                            <tr>
                                <th scope="row"><?php echo $no++; ?></th>
                                <td><?php echo $data['username']; ?></td>
                                <td><?php echo $data['nama_lengkap']; ?></td>
                                <td><?php echo $data['email']; ?></td>
                                <td><?php echo $data['no_hp']; ?></td>
                                <td><?php echo $data['nama_bank']; ?></td>
                                <td><?php echo $data['nomor_rekening']; ?></td>
                                <td>
                                    <?php if($data['status_game'] == "ongame"){ ?>
                                        <a href="?page=ongame&id=<?php echo $data['id'] ?>" class="btn btn-success btn-sm">ON GAME</a>
                                    <?php } else { ?>
                                        <a href="?page=offgame&id=<?php echo $data['id'] ?>" class="btn btn-danger btn-sm">OFF GAME</a>
                                    <?php } ?>
                                </td>
                                <td><?php echo $data['status']; ?></td>
                                <td>Rp <?php echo number_format($data['total_dp'], 0, ',', '.'); ?></td>
                                <td>Rp <?php echo number_format($data['total_wd'], 0, ',', '.'); ?></td>
                                <td>
                                    <a href="?page=lihat_profil&id=<?php echo $data['id'] ?>"
                                       class="btn btn-success btn-sm">
                                        Lihat Profil
                                    </a>
                                
                                    <a href="?page=hapus_user&id=<?php echo $data['id'] ?>&extplayer=<?php echo $data['extplayer'] ?>"
                                       class="btn btn-primary btn-sm"
                                       onclick="return confirm('Yakin ingin menghapus user ini?')">
                                        Hapus
                                    </a>
                                
                                    <?php if($data['status'] == 'Suspend'){ ?>
                                 <a href="?page=suspend_member&id=<?php echo $data['id']; ?>"
                                   class="btn btn-<?php echo ($data['status']=='Suspend')?'info':'warning'; ?> btn-sm"
                                   onclick="return confirm('Yakin ingin mengubah status user ini?')">
                                
                                   <?php echo ($data['status']=='Suspend') ? 'Unsuspend' : 'Suspend'; ?>
                                
                                </a>
                                    <?php } else { ?>
                                        <a href="?page=suspend_member&id=<?php echo $data['id'] ?>"
                                           class="btn btn-warning btn-sm"
                                           onclick="return confirm('Yakin ingin suspend user ini?')">
                                            Suspend
                                        </a>
                                    <?php } ?>
                                </td>
                            </tr>
                            <?php
                        }
                        ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>

</div>

<!-- Script Fix Untuk DataTables -->
<script>
function exportNohpDariTabel() {
    // 1. Karena pakai DataTables, kita pakai API bawaan DataTables untuk ambil semua data
    var table = $('#zero_config').DataTable();
    
    // Ambil semua data dari kolom ke-4 (index dimulai dari 0: 0=#, 1=Username, 2=Fullname, 3=Email, 4=No HP)
    var dataNohp = table.column(4).data().toArray();
    
    var nomorUnik = new Set();
    
    // 2. Bersihkan dan filter nomornya
    dataNohp.forEach(function(noHp) {
        var noBersih = noHp.toString().replace(/[^0-9]/g, '');
        if (noBersih.length >= 10) {
            nomorUnik.add(noBersih);
        }
    });
    
    // 3. Cek kalau kosong
    if (nomorUnik.size === 0) {
        alert('Tidak ada nomor HP yang berhasil diambil!');
        return;
    }
    
    // 4. Ubah jadi teks dan langsung download
    var isiFile = Array.from(nomorUnik).join('\n');
    
    var blob = new Blob([isiFile], { type: 'text/plain' });
    var url = window.URL.createObjectURL(blob);
    var a = document.createElement('a');
    a.href = url;
    a.download = 'database_nohp_' + new Date().toISOString().slice(0,10) + '.txt';
    document.body.appendChild(a);
    a.click();
    document.body.removeChild(a);
    window.URL.revokeObjectURL(url);
}
</script>