<?php
if ($c['level'] == "superadmin") {
?>

<div class="page-breadcrumb">
    <div class="row">
        <div class="col-5 align-self-center">
            <h4 class="page-title">Pengaturan Roda Hadiah 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="#">User</a></li>
                        <li class="breadcrumb-item active" aria-current="page">Roda Hadiah</li>
                    </ol>
                </nav>
            </div>
        </div>
    </div>
</div>

<div class="container-fluid">

    <div class="row mb-3">
        <div class="col">
            <button type="button" class="btn btn-danger" data-toggle="modal" data-target="#updateAllModal">
                Update All Roda Hadiah
            </button>
        </div>
    </div>

    <div class="row">
        <div class="col-lg-12">
            <div class="card">
                <div class="card-body">
                    <h4 class="card-title">Daftar User dan Roda Hadiah</h4>
                </div>
                <div class="table-responsive">
                    <table id="zero_config" class="table table-striped table-bordered">
                        <thead>
                            <tr>
                                <th>#</th>
                                <th>Username</th>
                                <th>Nama Lengkap</th>
                                <th>Roda Hadiah</th>
                                <th>Total Kemenangan</th>
                                <th>Aksi</th>
                            </tr>
                        </thead>
                        <tbody>
                        <?php
                        $query = mysqli_query($koneksi, "
                        SELECT 
                            u.username,
                            u.nama_lengkap,
                            u.roda_hadiah,
                            u.extplayer,
                            COALESCE(SUM(b.nominal), 0) AS total_kemenangan
                        FROM tb_user u
                        LEFT JOIN tb_bonus b 
                            ON b.id_user = u.extplayer 
                            AND b.status = 'Selesai'
                            WHERE u.refferal IS NOT NULL
  AND u.refferal <> ''
  AND u.refferal <> '0'
                        GROUP BY 
                            u.extplayer,
                            u.username,
                            u.nama_lengkap,
                            u.roda_hadiah
                        ORDER BY u.username ASC
                    ");

                        $no = 1;
                        while ($row = mysqli_fetch_assoc($query)) {
                        ?>
                            <tr>
                                <td><?= $no++; ?></td>
                                <td><?= htmlspecialchars($row['username']); ?></td>
                                <td><?= htmlspecialchars($row['nama_lengkap']); ?></td>
                                <td><?= number_format($row['roda_hadiah']); ?></td>
                                <td>Rp. <?= number_format($row['total_kemenangan']); ?></td>
                                <td>
                                    <a href="?page=edit_roda_hadiah&extplayer=<?= urlencode($row['extplayer']); ?>" 
                                       class="btn btn-warning btn-sm">
                                        Edit
                                    </a>
                                </td>
                            </tr>
                        <?php } ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>

</div>

<!-- Modal Update All -->
<div class="modal fade" id="updateAllModal" tabindex="-1">
  <div class="modal-dialog">
    <form action="?page=proses_update_all_roda_hadiah" method="POST">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title">Update Roda Hadiah Semua User</h5>
          <button type="button" class="close" data-dismiss="modal">&times;</button>
        </div>

        <div class="modal-body">
            <p>Anda yakin ingin update <strong>roda_hadiah</strong> semua user?</p>
            <div class="form-group">
                <label>Pilih Nilai Roda Hadiah</label>
                <select name="roda_hadiah" class="form-control" required>
                    <?php
                    $options = [1000,5000,10000,20000,25000,50000,100000,277000,900000];
                    foreach ($options as $val) {
                        echo '<option value="'.$val.'">'.number_format($val).'</option>';
                    }
                    ?>
                </select>
            </div>
        </div>

        <div class="modal-footer">
          <button type="submit" class="btn btn-danger">Ya, Update Semua</button>
          <button type="button" class="btn btn-secondary" data-dismiss="modal">Batal</button>
        </div>
      </div>
    </form>
  </div>
</div>

<?php
} else {
    echo "<script>window.location='?page=dashboard';</script>";
}
?>