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

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

<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">Anggota</div>
    </div>
    <div class="col-md-6 text-center text-md-end">
      <a href="<?php echo $alamat_admin.'tambah_anggota'; ?>" class="btn btn-sm btn-primary waves-effect waves-light">
        <span class="tf-icons mdi mdi-plus me-1"></span> Tambah Data
      </a>
    </div>
  </div>

  <div class="card table-responsive p-3">
    <table class="table" id="example">
      <thead>
        <tr>
          <th class="text-center">#</th>
          <th class="text-center">ExtPlayer</th>
          <th class="text-center">Referral</th>
          <th class="text-center">Ip Address</th>
          <th class="text-center">Username</th>
          <th class="text-center">Email</th>
          <th class="text-center">Nomor</th>
          <th class="text-center">Bank</th>
          <th class="text-center">Nama Rekening</th>
          <th class="text-center">Nomor Rekening</th>
          <th class="text-center">Dibuat</th>
          <th class="text-center">Diperbarui</th>
          <th class="text-center">Aksi</th>
        </tr>
      </thead>
      <tbody>
        <?php
        $nomor = 1;
        $anggota = mysqli_query($koneksi, "SELECT * FROM tb_user ORDER BY id DESC");

        if (mysqli_num_rows($anggota) > 0) {
          while ($data = mysqli_fetch_assoc($anggota)) {
            $id = $data['id'];
            $extplayer = $data['extplayer'];
            $username = $data['username'];
            $email = $data['email'];
            $nomor_hp = $data['nomor'];
            $bank = $data['bank'];
            $nama_rekening = $data['nama_rekening'];
            $nomor_rekening = $data['nomor_rekening'];
            $referral = $data['referral'];
            $ip = $data['ip'];
            $created_at = $data['created_at'];
            $updated_at = $data['updated_at'];
        ?>
        <tr>
          <td class="text-center"><?php echo $nomor++; ?></td>
          <td class="text-center"><?php echo $extplayer ?: '-'; ?></td>
          <td class="text-center"><?php echo $referral ?: 'Tidak ada'; ?></td>
          <td class="text-center"><span class="badge bg-primary"><?php echo $ip ?: '-'; ?></span></td>
          <td class="text-center"><?php echo $username; ?></td>
          <td class="text-center"><?php echo $email; ?></td>
          <td class="text-center"><?php echo $nomor_hp; ?></td>
          <td class="text-center"><?php echo $bank ?: 'Belum Daftar'; ?></td>
          <td class="text-center"><?php echo $nama_rekening ?: 'Belum Daftar'; ?></td>
          <td class="text-center"><?php echo $nomor_rekening ?: 'Belum Daftar'; ?></td>
          <td class="text-center"><?php echo $created_at; ?></td>
          <td class="text-center"><?php echo $updated_at; ?></td>
          <td class="text-center">
            <a href="<?php echo $alamat_admin.'ubah_anggota/'.$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
          }
        } else {
        ?>
        <tr>
          <td colspan="13" class="text-center">Tidak Ada Data</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>
