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

// Pastikan session aktif
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

// Cek apakah admin sudah login
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">Saldo Anggota</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">Username</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">Saldo</th>
            <th class="text-center">Aksi</th>
          </tr>
        </thead>
        <tbody>
          <?php
          $no = 1;
          $query = mysqli_query($koneksi, "SELECT * FROM tb_user ORDER BY id DESC");

          if (mysqli_num_rows($query) > 0) {
              while ($row = mysqli_fetch_assoc($query)) {
                  $id               = $row['id'];
                  $username         = htmlspecialchars($row['username']);
                  $bank             = htmlspecialchars($row['bank']);
                  $nama_rekening    = htmlspecialchars($row['nama_rekening']);
                  $nomor_rekening   = htmlspecialchars($row['nomor_rekening']);
                  $saldo            = (int)$row['saldo'];
                  $status           = htmlspecialchars($row['status']);
          ?>
              <tr>
                <td class="text-center"><?= $no++; ?></td>
                <td class="text-center"><?= $username; ?></td>
                <td class="text-center"><?= $bank; ?></td>
                <td class="text-center"><?= $nama_rekening; ?></td>
                <td class="text-center"><?= $nomor_rekening; ?></td>
                <td class="text-center fw-bold text-success">Rp <?= number_format($saldo, 0, ',', '.'); ?></td>
                <td class="text-center">
                  <a href="<?= htmlspecialchars($alamat_admin . 'ubah_saldo/' . $id); ?>" class="btn btn-sm btn-primary">
                    <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>
