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

if (isset($_GET['id']) && !empty($_GET['id'])) {
  $id = (int)$_GET['id'];
  $getUserById = "
        SELECT u.id, u.fullname, u.username, u.email, u.phone, u.gameplay, u.kode_unik, u.status, 
               ba.BankName, ba.BankHolder, ba.BankNumber
        FROM user u
        LEFT JOIN bank_user ba ON u.id = ba.user_id
        WHERE u.id = :id";

  $stmt = $db->prepare($getUserById);
  $stmt->bindParam(':id', $id);
  $stmt->execute();
  $user = $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">
            <h3 class="fw-bold mb-3">Edit 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="#">Edit Pengguna</a>
              </li>
            </ul>
          </div>

          <?php if ($user): ?>
            <div class="row">
              <div class="col-md-12">
                <div class="card">
                  <div class="card-header">
                    <div class="card-title">Edit Pengguna</div>
                  </div>
                  <div class="card-body">
                    <!-- Form starts here -->
                    <form id="editForm" method="POST" onsubmit="return false;">
                      <input type="hidden" name="csrf_token" id="csrf_token" value="<?= $csrf_token; ?>">
                      <input type="hidden" name="id" id="id" value="<?= $user['id']; ?>">
                      <div class="row">
                        <!-- Fullname -->
                        <div class="col-md-6">
                          <div class="form-group">
                            <label for="fullname">Nama Lengkap</label>
                            <input type="text" class="form-control" id="fullname" name="fullname" value="<?= $user['fullname']; ?>" placeholder="Masukan Nama Lengkap" />
                          </div>
                        </div>
                        <!-- Username -->
                        <div class="col-md-6">
                          <div class="form-group">
                            <label for="username">Nama Pengguna</label>
                            <input type="text" class="form-control" id="username" name="username" value="<?= $user['username']; ?>" placeholder="Masukan Nama Pengguna" />
                          </div>
                        </div>
                        <!-- Email -->
                        <div class="col-md-6">
                          <div class="form-group">
                            <label for="email">Email</label>
                            <input type="email" class="form-control" id="email" name="email" value="<?= $user['email']; ?>" placeholder="Masukan Alamat Email" />
                          </div>
                        </div>
                        <!-- Password -->
                        <div class="col-md-6">
                          <div class="form-group">
                            <label for="password">Kata Sandi</label>
                            <input type="password" class="form-control" id="password" name="password" placeholder="Masukan Kata Sandi" />
                          </div>
                        </div>
                        <!-- Bank Name -->
                        <div class="col-md-4">
                          <div class="form-group">
                            <label for="bankName">Nama Bank</label>
                            <input type="text" class="form-control" id="bankName" name="bankName" value="<?= $user['BankName']; ?>" placeholder="Masukan Nama Bank" />
                          </div>
                        </div>
                        <!-- Bank Holder -->
                        <div class="col-md-4">
                          <div class="form-group">
                            <label for="bankHolder">Nama Pemilik</label>
                            <input type="text" class="form-control" id="bankHolder" name="bankHolder" value="<?= $user['BankHolder']; ?>" placeholder="Masukan Nama Pemilik" />
                          </div>
                        </div>
                        <!-- Bank Number -->
                        <div class="col-md-4">
                          <div class="form-group">
                            <label for="bankNumber">No. Rekening</label>
                            <input type="text" class="form-control" id="bankNumber" name="bankNumber" value="<?= $user['BankNumber']; ?>" placeholder="Masukan No. Rekening" />
                          </div>
                        </div>
                        <!-- Status Switch -->
                        <div class="col-md-4">
                          <div class="form-group">
                            <label for="gameplay">Akses Permainan</label>
                            <div class="form-check form-switch">
                              <input class="form-check-input" type="checkbox" id="gameplay" name="gameplay" value="1" <?= $user['gameplay'] == 1 ? 'checked' : ''; ?> />
                              <label class="form-check-label" for="gameplay">Aktif</label>
                            </div>
                          </div>
                        </div>
                        <!-- Status Switch -->
                        <div class="col-md-4">
                          <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" <?= $user['status'] == 1 ? 'checked' : ''; ?> />
                              <label class="form-check-label" for="status">Aktif</label>
                            </div>
                          </div>
                        </div>
                      </div>
                      <!-- Status Switch -->
                      <div class="col-md-4">
                        <div class="form-group">
                          <label for="status">Kode Unik</label>
                          <div class="form-check form-switch">
                            <input class="form-check-input" type="checkbox" id="kode_unik" name="kode_unik" value="1" <?= $user['kode_unik'] == 1 ? 'checked' : ''; ?> />
                            <label class="form-check-label" for="status">Aktif</label>
                          </div>
                        </div>
                      </div>
                  </div>
                  <!-- Action Buttons -->
                  <div class="form-group">
                    <a href="window.history.back()" class="btn btn-secondary">
                      <i class="fas fa-arrow-left"></i> Kembali
                    </a>
                    <button type="submit" class="btn btn-primary">
                      <i class="fas fa-save"></i> Simpan
                    </button>
                  </div>
                  </form>
                  <!-- Form ends here -->
                </div>
              </div>
            </div>
        </div>
      <?php endif; ?>

      </div>
    </div>

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

  </div>
  </div>

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

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

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

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

        $.ajax({
          url: 'update-user',
          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>