<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <title>Edit Data</title>

  <!-- Custom fonts -->
  <link href="../vendor/fontawesome-free/css/all.min.css" rel="stylesheet" type="text/css">
  <link href="https://fonts.googleapis.com/css?family=Nunito:200,300,400,600,700,800,900" rel="stylesheet">
  <link href="../css/sb-admin-2.min.css" rel="stylesheet">
</head>

<body id="page-top">
  <div class="card shadow">
    <div class="card-header">Edit Data Petugas</div>
    <div class="card-body">

      <a href="admin.php?url=lihat_petugas" class="btn btn-primary btn-icon-split">
        <span class="icon text-white-50"><i class="fas fa-angle-left"></i></span>
        <span class="text">Kembali</span>
      </a>
      <br><br>

      <?php
      require '../koneksi.php';

      if (isset($_GET['id'])) {
          $id = mysqli_real_escape_string($conn, $_GET['id']);
          $query = "SELECT * FROM petugas WHERE id_petugas = ?";
          $stmt = mysqli_prepare($conn, $query);
          mysqli_stmt_bind_param($stmt, "s", $id);
          mysqli_stmt_execute($stmt);
          $result = mysqli_stmt_get_result($stmt);

          if ($data = mysqli_fetch_assoc($result)) {
      ?>
          <form action="admin.php?url=update_petugas" method="post" class="form-horizontal" enctype="multipart/form-data">
            <div class="form-group cols-sm-5">
              <label>ID Petugas</label>
              <input type="text" name="id_petugas" value="<?= htmlspecialchars($data['id_petugas']) ?>" class="form-control" readonly>
            </div>
            <div class="form-group cols-sm-5">
              <label>Nama Petugas</label>
              <input type="text" name="nama_petugas" value="<?= htmlspecialchars($data['nama_petugas']) ?>" class="form-control">
            </div>
            <div class="form-group cols-sm-5">
              <label>Username</label>
              <input type="text" name="username" value="<?= htmlspecialchars($data['username']) ?>" class="form-control">
            </div>
            <div class="form-group cols-sm-5">
              <label>Password</label>
              <input type="text" name="password" value="<?= htmlspecialchars($data['password']) ?>" class="form-control">
            </div>
            <div class="form-group cols-sm-5">
              <label>Telepon</label>
              <input type="text" name="telp" value="<?= htmlspecialchars($data['telp']) ?>" class="form-control">
            </div>
            <div class="form-group cols-sm-5">
              <label>Level</label>
              <select class="form-control" name="level">
                <option value="<?= $data['level'] ?>"><?= ucfirst($data['level']) ?></option>
                <?php if ($data['level'] !== 'admin') : ?>
                  <option value="admin">Admin</option>
                <?php endif; ?>
                <?php if ($data['level'] !== 'petugas') : ?>
                  <option value="petugas">Petugas</option>
                <?php endif; ?>
              </select>
            </div>

            <div class="form-group col-sm-5">
              <input type="submit" value="Edit Data" class="btn btn-primary">
              <input type="reset" value="Kosongkan" class="btn btn-warning">
            </div>
          </form>
      <?php
          } else {
              echo "<div class='alert alert-danger'>Data tidak ditemukan.</div>";
          }
          mysqli_stmt_close($stmt);
      } else {
          echo "<div class='alert alert-warning'>ID tidak tersedia.</div>";
      }
      ?>
    </div>
  </div>

  <!-- Scripts -->
  <script src="../vendor/jquery/jquery.min.js"></script>
  <script src="../vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
  <script src="../vendor/jquery-easing/jquery.easing.min.js"></script>
  <script src="../js/sb-admin-2.min.js"></script>
</body>
</html>
