<?php 
error_reporting(E_ALL);
ini_set("display_errors", 1);?>
<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">Withdraw</div>
    </div>
    <div class="col-md-6">
      <div class="text-center text-md-end">
        <span><?php echo ucapan().', '.tanggalIndonesia(date('Y-m-d'), true).', '; ?></span>
        <span id="jam_sekarang">Jam </span>
      </div>
    </div>
  </div>

  <div class="card p-3">
    <?php
    // Ambil data withdraw terbaru
    $withdraw = mysqli_query($koneksi, "SELECT * FROM tb_withdraw ORDER BY created_at DESC");
    ?>
    <div class="table-responsive">

      <table class="table" id="example">
        <thead>
          <tr>
            <th class="text-center">#</th>
            <th class="text-center">Ref</th>
            <th class="text-center">Username</th>
            <th class="text-center">ExtPlayer</th>
            <th class="text-center">Bank Asal</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
          $no = 1;
          if (mysqli_num_rows($withdraw) > 0) {
            while ($row = mysqli_fetch_assoc($withdraw)) {
              $id         = $row['id'];
              $extplayer  = $row['extplayer'];
              $username   = $row['username'];
              $ref        = $row['ref'];
              $jumlah     = $row['jumlah'];
              $bank_asal  = $row['bank_asal'];
              $status     = $row['status'];
              $created_at = $row['created_at'];

              // Tentukan tampilan status
              switch ($status) {
                case 'Pending':
                  $status_text = 'Diproses';
                  $status_class = 'text-warning';
                  break;
                case 'Sukses':
                  $status_text = 'Disetujui';
                  $status_class = 'text-success';
                  break;
                case 'Rejected':
                  $status_text = 'Dibatalkan';
                  $status_class = 'text-danger';
                  break;
                default:
                  $status_text = 'Tidak Diketahui';
                  $status_class = 'text-muted';
                  break;
              }
          ?>
              <tr>
                <td class="text-center"><?php echo $no++; ?></td>
                <td class="text-center"><?php echo htmlspecialchars($ref); ?></td>
                <td class="text-center"><?php echo htmlspecialchars($username); ?></td>
                <td class="text-center"><?php echo htmlspecialchars($extplayer); ?></td>
                <td class="text-center"><?php echo htmlspecialchars($bank_asal); ?></td>
                <td class="text-center"><?php echo 'Rp ' . number_format($jumlah, 0, ',', '.'); ?></td>
                <td class="text-center"><?php echo jamTanggalIndonesia($created_at); ?></td>
                <td class="text-center <?php echo $status_class; ?>"><?php echo $status_text; ?></td>
                <td class="text-center">
                  <a href="<?php echo $alamat_admin.'ubah_withdraw/'.$id; ?>" 
                     class="btn btn-sm btn-primary waves-effect waves-light">
                    <span class="tf-icons mdi mdi-pencil me-1"></span> Ubah
                  </a>
                </td>
              </tr>
          <?php
            }
          }
          ?>
        </tbody>
      </table>
    </div>
  </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>
