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

$getAllBanks = "SELECT * FROM bank_admin ORDER BY id ASC";
$stmt = $db->prepare($getAllBanks);
$stmt->execute();
$banks = $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">
            <h4 class="page-title">Bank</h4>
            <ul class="breadcrumbs">
              <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="#">Pengaturan</a>
              </li>
              <li class="separator">
                <i class="icon-arrow-right"></i>
              </li>
              <li class="nav-item">
                <a href="#">Bank</a>
              </li>
            </ul>

            <?php if (isset($_SESSION['admin_id']) && isset($admin['level'])): ?>
            <?php if ($admin['level'] === 'developer' || $admin['level'] === 'admin'): ?>
            <div class="ms-md-auto py-2 py-md-0">
              <a href="add-bank">
                <button class="btn btn-primary btn-round">Tambah Bank</button>
              </a>
            </div>
            <?php endif; ?>
            <?php endif; ?>
          </div>

        <?php if (isset($_SESSION['admin_id']) && isset($admin['level'])): ?>
        <?php if ($admin['level'] === 'developer' || $admin['level'] === 'admin'): ?>
          <div class="row banner">
            <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 Bank</th>
                          <th>Nama Pemilik</th>
                          <th>No. Rekening</th>
                          <th>Status</th>
                          <th>Aksi</th>
                        </tr>
                      </thead>
                      <tbody>
                        <?php $no = 1;
                        foreach ($banks as $bank): ?>
                          <tr>
                            <td><?= $no++; ?></td>
                            <td><?= htmlspecialchars($bank['BankName']); ?></td>
                            <td><?= htmlspecialchars($bank['BankHolder']); ?></td>
                            <td><?= htmlspecialchars($bank['BankNumber']); ?></td>

                            <td>
                              <?php if ($bank['status'] == 1): ?>
                                <span class="badge badge-success">Aktif</span>
                              <?php else: ?>
                                <span class="badge badge-danger">Tidak Aktif</span>
                              <?php endif; ?>
                            </td>
                            <td>
                              <div class="btn-group">
                                <a href="edit-bank?id=<?= $bank["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="<?= $bank["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();

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

        if (confirm('Apakah Kamu yakin ingin menghapus Bank ini?')) {
          $.ajax({
            url: 'update-bank',
            type: 'POST',
            data: {
              action: 'delete',
              id: bankId,
              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>