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

$getDepositTransactions = "
    SELECT u.id, u.fullname, u.username, t.trxid, t.Amount, t.FromAccount, t.ToAccount, t.TransactionReceipt, t.status, t.created_at, t.id,
           b.BankName, b.BankHolder, b.BankNumber
    FROM user u
    LEFT JOIN transaction t ON u.id = t.user_id
    LEFT JOIN bank_user b ON u.id = b.user_id
    WHERE t.status = 0 AND t.type = 'deposit'
    ORDER BY t.created_at DESC
";
$stmt = $db->prepare($getDepositTransactions);
$stmt->execute();
$transaction = $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">Setoran</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="#">Transaksi</a>
              </li>
              <li class="separator">
                <i class="icon-arrow-right"></i>
              </li>
              <li class="nav-item">
                <a href="#">Setoran</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>Invoice</th>
                          <th>Nama Lengkap</th>
                          <th>Nama Pengguna</th>
                          <th>Asal Bank</th>
                          <th>Jumlah</th>
                          <th>Tujuan Bank</th>
                          <th>Bukti</th>
                          <th>Status</th>
                          <th>Waktu Transaksi</th>
                          <th>Aksi</th>
                        </tr>
                      </thead>
                      <tbody>
                        <?php $no = 1;
                        foreach ($transaction as $trx): ?>
                          <tr>
                            <td><?= $no++; ?></td>
                            <td><?= htmlspecialchars($trx['trxid']); ?></td>
                            <td><?= htmlspecialchars($trx['fullname']); ?></td>
                            <td><?= htmlspecialchars($trx['username']); ?></td>
                            <td><?= htmlspecialchars($trx['FromAccount']); ?></td>
                            <td><?= htmlspecialchars(number_format($trx['Amount'], 0, ',', '.')); ?></td>
                            <td><?= htmlspecialchars($trx['ToAccount']); ?></td>
                            <td>
                              <div class="btn-group">
                                <a href="<?= htmlspecialchars($trx['TransactionReceipt']); ?>">
                                  <i class="fas fa-eye"></i>
                                </a>
                              </div>
                            </td>
                            <td>
                              <div class="btn-group">
                                <?php if ($trx['status'] == 0): ?>
                                  <span class="badge badge-warning">Tertunda</span>
                                <?php endif; ?>
                              </div>
                            </td>
                            <td><?= htmlspecialchars((new DateTime($trx['created_at']))->setTimezone(new DateTimeZone('Asia/Jakarta'))->format('d-M-Y H:i:s T')); ?></td>
                            <td>
                              <div class="btn-group">
                                <button class="btn btn-icon btn-round btn-primary approve-btn" data-id="<?= $trx['trxid'] ?>">
                                  <i class="fas fa-check"></i>
                                </button>
                                <button class="btn btn-icon btn-round btn-danger reject-btn" data-id="<?= $trx['trxid'] ?>">
                                  <i class="fas fa-times"></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() {

      var table = $('#basic-datatables').DataTable();

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

        if (confirm('Apakah Kamu yakin ingin terima Transaksi ini?')) {
          $.ajax({
            url: 'update-deposit',
            type: 'POST',
            data: {
              action: 'approve',
              trxid: trxId,
              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!');
            }
          });
        }
      });

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

        if (confirm('Apakah Kamu yakin ingin menolak Transaksi ini?')) {
          $.ajax({
            url: 'update-deposit',
            type: 'POST',
            data: {
              action: 'reject',
              trxid: trxId,
              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>