<?php
include(__DIR__ . '/../../session.php');

$getAllUsers = "
    SELECT u.id, u.fullname, u.username, u.phone, u.gameplay, u.kode_unik, u.status, u.created_at, u.updated_at,
           ba.BankName, ba.BankHolder, ba.BankNumber
    FROM user u
    LEFT JOIN bank_user ba ON u.id = ba.user_id
    ORDER BY u.id ASC
";
$stmt = $db->prepare($getAllUsers);
$stmt->execute();
$users = $stmt->fetchAll(PDO::FETCH_ASSOC);

?>

<!DOCTYPE html>
<html lang="en">

<?php include(__DIR__ . '/../../layouts/header.php'); ?>

<body>
  <div class="wrapper">
    <?php include(__DIR__ . '/../../layouts/sidebar.php'); ?>

    <div class="main-panel">
      <?php include(__DIR__ . '/../../layouts/navbar.php'); ?>

      <div class="container">
        <div class="page-inner">

          <div class="page-header">
            <h3 class="fw-bold mb-3">Info Pengguna</h3>
            <ul class="breadcrumbs mb-3">
              <li class="nav-home">
                <a href="#">
                  <i class="icon-home"></i>
                </a>
              </li>
              <li class="separator">
                <i class="icon-arrow-right"></i>
              </li>
              <li class="nav-item">
                <a href="#">Pengguna</a>
              </li>
              <li class="separator">
                <i class="icon-arrow-right"></i>
              </li>
              <li class="nav-item">
                <a href="#">Info Pengguna</a>
              </li>
            </ul>
          </div>

            <?php if (isset($_SESSION['admin_id']) && isset($admin['level'])): ?>
            <?php if ($admin['level'] === 'developer' || $admin['level'] === 'admin'): ?>
          <div class="row">
            <div class="col-md-12">
              <div class="card">
                <div class="card-body">
                  <div class="table-responsive">
                    <table id="basic-datatables" class="display table table-striped table-hover">
                      <thead>
                        <tr>
                          <th>No</th>
                          <th>Nama Lengkap</th>
                          <th>Nama Pengguna</th>
                          <th>Email</th>
                          <th>Phone</th>
                          <th>Nama Bank</th>
                          <th>No. Rekening</th>
                          <th>Askes Permainan</th>
                          <th>Kode Unik</th>
                          <th>Status</th>
                          <th>Waktu Terdaftar</th>
                          <th>Aksi</th>
                        </tr>
                      </thead>
                      <tbody>
                        <?php $no = 1;
                        foreach ($users as $user): ?>
                          <tr>
                            <td><?= $no++; ?></td>
                            <td><?= htmlspecialchars($user['fullname']); ?></td>
                            <td><?= htmlspecialchars($user['username']); ?></td>
                            <td>-</td>
                            <td><?= htmlspecialchars($user['phone']); ?></td>
                            <td><?= htmlspecialchars($user['BankName']); ?></td>
                            <td><?= htmlspecialchars($user['BankNumber']); ?></td>
                            <td>
                              <div class="btn-group">
                                <button class="btn <?= $user['gameplay'] ? 'btn-success' : 'btn-danger' ?> btn-xs" id="btn-editAccess" data-id="<?= $user['id']; ?>">
                                  <?= $user['gameplay'] ? 'Aktif' : 'Tidak Aktif'; ?>
                                </button>
                              </div>
                            </td>
                            <td>
                              <div class="btn-group">
                                <button class="btn <?= $user['kode_unik'] ? 'btn-success' : 'btn-danger' ?> btn-xs" id="btn-editKodeUnik" data-id="<?= $user['id']; ?>">
                                  <?= $user['kode_unik'] ? 'Aktif' : 'Tidak Aktif'; ?>
                                </button>
                              </div>
                            </td>
                            <td>
                              <div class="btn-group">
                                <button class="btn <?= $user['status'] ? 'btn-success' : 'btn-danger' ?> btn-xs" id="btn-editStatus" data-id="<?= $user['id']; ?>">
                                  <?= $user['status'] ? 'Aktif' : 'Tidak Aktif'; ?>
                                </button>
                              </div>
                            </td>
                            <td><?= htmlspecialchars((new DateTime($user['created_at']))->setTimezone(new DateTimeZone('Asia/Jakarta'))->format('d-M-Y H:i:s T')); ?>
                            </td>
                            <td>
                              <div class="btn-group">
                                <a href="edit-user?id=<?= $user['id']; ?>">
                                  <button class="btn btn-icon btn-round btn-warning">
                                    <i class="fas fa-edit"></i>
                                  </button>
                                </a>
                                <button class="btn btn-icon btn-round btn-danger delete-btn" data-id="<?= $user["id"] ?>">
                                  <i class="fas fa-trash"></i>
                                </button>
                              </div>
                            </td>
                          </tr>
                        <?php endforeach; ?>
                      </tbody>
                    </table>
                  </div>
                </div>
              </div>
            </div>
          </div>
            <?php else: ?>
                <span>Askes Dilarang</span>
            <?php endif; ?>
            <?php endif; ?>

        </div>
      </div>

      <?php include(__DIR__ . '/../../layouts/footer.php'); ?>

    </div>
  </div>

  <?php include(__DIR__ . '/../../layouts/script.php'); ?>

  <script>
    $(document).ready(function() {

      $('#basic-datatables').DataTable();

      $(document).on('click', '.delete-btn', function() {
        var userId = $(this).data('id');
        var csrfToken = '<?= $csrf_token; ?>';

        if (confirm('Apakah Kamu yakin ingin menghapus Pengguna ini?')) {
          $.ajax({
            url: 'update-user',
            type: 'POST',
            data: {
              action: 'delete',
              id: userId,
              csrf_token: csrfToken
            },
            success: function(response) {
              var res = JSON.parse(response);
              alert(res.message);

              if (res.status === 'success' || res.status === 'error') {
                location.reload();
              }
            },
            error: function() {
              alert('Terjadi kesalahan, coba lagi!');
            }
          });
        }
      });
    });
  </script>

</body>

</html>