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

if (isset($_GET['id']) && !empty($_GET['id'])) {
  $id = $_GET['id'];
  $getAdminById = "SELECT id, fullname, username, level, status FROM admin WHERE id = :id";
  $stmt = $db->prepare($getAdminById);
  $stmt->bindParam(':id', $id);
  $stmt->execute();
  $admin = $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 Admin</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="#">Pengaturan</a>
              </li>
              <li class="separator">
                <i class="icon-arrow-right"></i>
              </li>
              <li class="nav-item">
                <a href="#">Admin</a>
              </li>
              <li class="separator">
                <i class="icon-arrow-right"></i>
              </li>
              <li class="nav-item">
                <a href="#">Edit Admin</a>
              </li>
            </ul>
          </div>

          <div class="row">
            <div class="col-md-12">
              <div class="card">
                <div class="card-header">
                  <div class="card-title">Edit Admin</div>
                </div>
                <div class="card-body">
                  <!-- Form starts here -->
                  <form id="editForm" enctype="multipart/form-data">
                    <input type="hidden" name="csrf_token" id="csrf_token" value="<?= $csrf_token; ?>">
                    <input type="hidden" name="id" id="id" value="<?= $admin['id']; ?>">

                    <div class="row">
                      <!-- Fullname -->
                      <div class="col-md-4">
                        <div class="form-group">
                          <label for="fullname">Nama Lengka</label>
                          <input type="text" class="form-control" id="fullname" name="fullname" placeholder="Masukan Nama Lengkap" value="<?= htmlspecialchars($admin['fullname']); ?>" required />
                        </div>
                      </div>

                      <!-- Username -->
                      <div class="col-md-4">
                        <div class="form-group">
                          <label for="username">Nma Pengguna</label>
                          <input type="text" class="form-control" id="username" name="username" placeholder="Masukan Nama Pengguna" value="<?= htmlspecialchars($admin['username']); ?>" required />
                        </div>
                      </div>

                      <!-- Password -->
                      <div class="col-md-4">
                        <div class="form-group">
                          <label for="password">Kata Sandi</label>
                          <input type="password" class="form-control" id="password" name="password" placeholder="Masukan Password Baru" />
                        </div>
                      </div>

                      <!-- Level -->
                      <div class="col-md-4">
                        <div class="form-group">
                          <label for="level">Level</label>
                          <select class="form-control" id="level" name="level" required>
                            <option value="admin" <?= $admin['level'] == 'admin' ? 'selected' : ''; ?>>Admin</option>
                            <option value="staff" <?= $admin['level'] == 'staff' ? 'selected' : ''; ?>>Staff</option>
                          </select>
                        </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" <?= $admin['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>
                  <!-- Form ends here -->
                </div>
              </div>
            </div>
          </div>


        </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-admin',
          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>