<?php
if (session_status() == PHP_SESSION_NONE) {
    session_start();
}

if (!isset($_SESSION['nik'])) {
    header("Location: index.php");
    exit;
}

require 'koneksi.php'; // $conn akan terdefinisi dari sini
?>

<!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>Lihat Pengaduan</title>

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

<body id="page-top">

  <div class="card shadow mb-4">
    <div class="card-header py-3">
      <h6 class="m-0 font-weight-bold text-primary">Data Pengaduan</h6>
    </div>
    <div class="card-body">
      <div class="table-responsive">
        <table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
          <thead>
            <tr>
              <th>ID</th>
              <th>Tanggal</th>
              <th>NIK</th>
              <th>Isi Laporan</th>
              <th>Foto</th>
              <th>Status</th>
              <th>Aksi</th>
            </tr>
          </thead>
          <tbody>
          <?php
          $stmt = $conn->prepare("SELECT * FROM pengaduan WHERE nik = ?");
          $stmt->bind_param("s", $_SESSION['nik']);
          $stmt->execute();
          $result = $stmt->get_result();
          while ($data = $result->fetch_assoc()) {
          ?>
            <tr>
              <td><?= htmlspecialchars($data['id_pengaduan']) ?></td>
              <td><?= htmlspecialchars($data['tgl_pengaduan']) ?></td>
              <td><?= htmlspecialchars($data['nik']) ?></td>
              <td><?= htmlspecialchars($data['isi_laporan']) ?></td>
              <td>
                <?php if (!empty($data['foto'])): ?>
                  <img src="foto/<?= htmlspecialchars($data['foto']) ?>" width="100">
                <?php else: ?>
                  Tidak ada foto
                <?php endif; ?>
              </td>
              <td><?= htmlspecialchars($data['status']) ?></td>
              <td>
                <a href="?url=detail_pengaduan&id=<?= $data['id_pengaduan'] ?>" class="btn btn-info btn-icon-split mb-1">
                  <span class="icon text-white-50"><i class="fas fa-info"></i></span>
                  <span class="text">Detail</span>
                </a>
                <a href="?url=lihat_tanggapan&id=<?= $data['id_pengaduan'] ?>" class="btn btn-primary btn-icon-split">
                  <span class="icon text-white-50"><i class="fas fa-eye"></i></span>
                  <span class="text">Lihat Tanggapan</span>
                </a>
              </td>
            </tr>
          <?php } ?>
          </tbody>
        </table>
      </div>
    </div>
  </div>

  <!-- Scroll to Top Button-->
  <a class="scroll-to-top rounded" href="#page-top">
    <i class="fas fa-angle-up"></i>
  </a>

  <!-- Logout Modal-->
  <div class="modal fade" id="logoutModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title" id="exampleModalLabel">Ready to Leave?</h5>
          <button class="close" type="button" data-dismiss="modal" aria-label="Close"><span>×</span></button>
        </div>
        <div class="modal-body">Pilih "Logout" jika Anda yakin untuk keluar dari sesi ini.</div>
        <div class="modal-footer">
          <button class="btn btn-secondary" type="button" data-dismiss="modal">Batal</button>
          <a class="btn btn-primary" href="logout.php">Logout</a>
        </div>
      </div>
    </div>
  </div>

  <!-- Bootstrap core JavaScript-->
  <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>
  <script src="vendor/datatables/jquery.dataTables.min.js"></script>
  <script src="vendor/datatables/dataTables.bootstrap4.min.js"></script>
  <script src="js/demo/datatables-demo.js"></script>

</body>
</html>
