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

if (isset($_GET['id']) && !empty($_GET['id'])) {
  $id = $_GET['id'];
  $getBankById = "SELECT id, BankName, BankHolder, BankNumber, PaymentType, status FROM bank_admin WHERE id = :id";
  $stmt = $db->prepare($getBankById);
  $stmt->bindParam(':id', $id);
  $stmt->execute();
  $bank = $stmt->fetch(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">Edit 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="#">Edit Bank</a>
              </li>
              <li class="separator">
                <i class="icon-arrow-right"></i>
              </li>
              <li class="nav-item">
                <a href="#">Bank</a>
              </li>
              <li class="separator">
                <i class="icon-arrow-right"></i>
              </li>
              <li class="nav-item">
                <a href="#">Edit Bank</a>
              </li>
            </ul>
          </div>

          <div class="row">
            <div class="col-md-12">
              <div class="card">
                <div class="card-header">
                  <div class="card-title">Edit Bank</div>
                </div>
                <div class="card-body">
                  <form id="editForm" enctype="multipart/form-data" onsubmit="return false;">
                    <input type="hidden" name="csrf_token" id="csrf_token" value="<?= $csrf_token; ?>">
                    <input type="hidden" name="id" id="id" value="<?= $bank['id']; ?>">
                    <div class="row">
                      <div class="col-md-4" id="qrisUpload" style="display: none;">
                        <div class="form-group">
                          <label for="image">Gambar QRIS</label>
                          <input type="file" class="form-control" id="image" name="image" accept="image/*" onchange="previewImage()" />
                          <div id="imagePreview" class="mt-2"></div>
                        </div>
                      </div>

                      <!-- Bank Type (Read-Only) -->
                      <div class="col-md-4">
                        <div class="form-group">
                          <label for="BankType">Jenis Bank</label>
                          <select class="form-control" id="BankType" name="paymentType" readonly />
                          <option value="BANK" <?= $bank['PaymentType'] == 'BANK' ? 'selected' : ''; ?>>BANK</option>
                          <option value="EWALLET" <?= $bank['PaymentType'] == 'EWALLET' ? 'selected' : ''; ?>>EWALLET</option>
                          <option value="QRIS" <?= $bank['PaymentType'] == 'QRIS' ? 'selected' : ''; ?>>QRIS</option>
                          </select>
                        </div>
                      </div>
                    </div>

                    <!-- Bank Name -->
                    <div class="row">
                      <div class="col-md-4">
                        <div class="form-group">
                          <label for="editBankName">Nama Bank</label>
                          <input type="text" class="form-control" id="bankName" placeholder="Masukan Nama Bank" name="bankName" value="<?= $bank['BankName']; ?>" readonly />
                        </div>
                      </div>

                      <!-- Bank Holder -->
                      <div class="col-md-4">
                        <div class="form-group">
                          <label for="editBankHolder">Nama Pemilik</label>
                          <input type="text" class="form-control" id="bankHolder" placeholder="Masukan Nama Pemilik" name="bankHolder" value="<?= $bank['BankHolder']; ?>" required />
                        </div>
                      </div>

                      <!-- Bank Number -->
                      <div class="col-md-4">
                        <div class="form-group">
                          <label for="editBankNumber">No. Rekening</label>
                          <input type="text" class="form-control" id="bankNumber" placeholder="Masukan No. Rekening" name="bankNumber" value="<?= $bank['BankNumber']; ?>" required />
                        </div>
                      </div>

                      <!-- Status Switch -->
                      <div class="col-md-12">
                        <div class="form-group">
                          <label for="status">Status</label>
                          <div class="form-check form-switch">
                            <input class="form-check-input" type="checkbox" id="status" name="status" value="1" <?= $bank['status'] == 1 ? 'checked' : ''; ?> />
                            <label class="form-check-label" for="status">Aktif</label>
                          </div>
                        </div>
                      </div>
                    </div>

                    <!-- Action Buttons -->
                    <div class="form-group">
                      <button type="button" class="btn btn-secondary" onclick="window.history.back()">
                        <i class="fas fa-arrow-left"></i> Batal
                      </button>
                      <button type="submit" class="btn btn-primary">
                        <i class="fas fa-save"></i> Simpan
                      </button>
                    </div>
                  </form>
                </div>
              </div>
            </div>
          </div>

        </div>
      </div>

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

    </div>
  </div>

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

  <script>
    $(document).ready(function() {
      function toggleQrisUpload() {
        const bankType = $('#BankType').val();
        $('#qrisUpload').toggle(bankType === 'QRIS' || bankType === 'EWALLET');
      }

      $('#BankType').change(toggleQrisUpload);
      toggleQrisUpload();

      $('#editForm').on('submit', function(e) {
        e.preventDefault();

        var formData = new FormData(this);
        formData.append('action', 'edit');

        $.ajax({
          url: 'update-bank',
          type: 'POST',
          data: formData,
          contentType: false,
          processData: false,
          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>