<?php
include 'koneksi.php';
$bulans = array("Januari", "Februari", "Maret", "April", "Mei", "Juni", "Juli", "Agustus", "September", "Oktober", "November", "Desember");
if (isset($_GET['aksi'])) {
  $query = "DELETE FROM kuesioner WHERE id_pengguna='" . $_GET['id_pengguna'] . "'";
  $result = mysqli_query($koneksi, $query);
  if ($result) {
    $_SESSION['success'] = 1;
    $_SESSION['message'] = "Data Jawaban Kuesioner Berhasil Dihapus";
    header('location:kuesioner.php');
  } else {
    $_SESSION['success'] = 0;
    $_SESSION['message'] = "Gagal Menghapus Data Jawaban Kuesioner";
    header('location:kuesioner.php');
  }
} else {
?>
  <!doctype html>
  <html lang="en">

  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no, maximum-scale=1, user-scalable=no">

    <title>Hasil Kuesioner</title>

    <!-- Template CSS -->
    <link rel="stylesheet" href="css/bootstrap.min.css">

    <!-- Google Fonts -->
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Space+Grotesk:wght@300..700&display=swap" rel="stylesheet">

    <style>
      body {
        font-family: 'Space Grotesk', sans-serif;
        background-color: #f4f4f0;
        padding: 15px;
        min-height: 100vh;
        color: #000;
      }

      .neo-card {
        background-color: #ffffff;
        border: 3px solid #000;
        border-radius: 12px;
        box-shadow: 4px 4px 0px #000;
        padding: 20px;
        margin-bottom: 25px;
      }

      .neo-title {
        font-weight: 800;
        font-size: 1.4rem;
        margin-bottom: 10px;
        line-height: 1.2;
      }

      .neo-subtitle {
        font-weight: 600;
        font-size: 0.95rem;
        margin-bottom: 20px;
        color: #333;
      }

      .neo-btn {
        display: inline-block;
        background-color: #8acde8;
        border: 2px solid #000;
        border-radius: 8px;
        box-shadow: 2px 2px 0px #000;
        font-weight: 700;
        color: #000;
        text-align: center;
        padding: 10px 15px;
        font-size: 0.9rem;
        cursor: pointer;
        transition: all 0.1s ease;
        text-transform: uppercase;
        letter-spacing: 0.5px;
        text-decoration: none;
      }

      .neo-btn:active {
        transform: translate(2px, 2px);
        box-shadow: 0px 0px 0px #000;
      }

      .neo-btn-danger {
        background-color: #E6766E;
      }

      .neo-btn-success {
        background-color: #b5e88a;
      }

      .neo-input {
        width: 100%;
        border: 2px solid #000;
        border-radius: 8px;
        padding: 10px;
        margin-bottom: 15px;
        font-family: 'Space Grotesk', sans-serif;
        font-weight: 600;
      }

      .neo-input:focus {
        outline: none;
        box-shadow: 2px 2px 0px #000;
      }

      .respondent-name {
        font-weight: 800;
        font-size: 1.1rem;
        margin-bottom: 5px;
        border-bottom: 2px solid #000;
        padding-bottom: 5px;
      }

      .respondent-info {
        font-weight: 600;
        font-size: 0.85rem;
        margin-bottom: 15px;
      }

      .grid-container {
        overflow-x: auto;
        border: 2px solid #000;
        border-radius: 8px;
        margin-bottom: 15px;
        background-color: #fff;
      }

      .grid-table {
        width: 100%;
        min-width: 500px;
        border-collapse: collapse;
      }

      .grid-table th,
      .grid-table td {
        border: 1px solid #000;
        text-align: center;
        padding: 6px;
        font-size: 0.85rem;
        font-weight: 600;
      }

      .grid-table th {
        background-color: #000;
        color: #fff;
      }

      .grid-table .row-persepsi td:first-child {
        background-color: #8acde8;
        font-weight: 800;
        text-align: left;
      }

      .grid-table .row-harapan td:first-child {
        background-color: #E6766E;
        font-weight: 800;
        text-align: left;
      }
    </style>
  </head>

  <body>
    <div style="padding-bottom:60px;">
      <?php
      if (!isset($_POST['bulan'])) {
      ?>
        <div class="neo-card">
          <div class="neo-title">Jawaban Kuesioner Pelanggan</div>
          <div class="neo-subtitle">Pilih periode</div>
          <form method="post" action="kuesioner.php">
            <label style="font-weight:700;">Bulan</label>
            <select name="bulan" class="neo-input">
              <?php
              for ($i = 0; $i < count($bulans); $i++) {
              ?>
                <option value="<?php echo ($i + 1) ?>" <?php echo (date('m') == ($i + 1)) ? 'selected' : ''; ?>><?php echo $bulans[$i] ?></option>
              <?php
              }
              ?>
            </select>
            <label style="font-weight:700;">Tahun</label>
            <input type="number" name="tahun" class="neo-input" value="<?php echo date('Y') ?>" required autocomplete="off" />
            <button type="submit" class="neo-btn neo-btn-success" style="width:100%; font-size:1rem; padding:12px;">Lihat Data</button>
          </form>
        </div>
      <?php
      } else {
      ?>
        <div class="neo-title">Hasil Kuesioner</div>
        <div class="neo-subtitle">Periode <?php echo $bulans[($_POST['bulan'] - 1)] . " " . $_POST['tahun'] ?></div>

        <!-- Button Kembali -->
        <!-- <a href="kuesioner.php" class="neo-btn" style="margin-bottom: 20px;">Kembali</a> -->

        <?php
        // Fixed Query to avoid only_full_group_by error
        $query = mysqli_query($koneksi, "
          SELECT p.id_pengguna, p.nama_pengguna, p.handphone, MAX(k.tanggal) as tanggal
          FROM kuesioner k 
          JOIN pengguna p ON p.id_pengguna = k.id_pengguna 
          WHERE MONTH(k.tanggal)='" . $_POST['bulan'] . "' AND YEAR(k.tanggal)='" . $_POST['tahun'] . "'
          GROUP BY p.id_pengguna, p.nama_pengguna, p.handphone
          ORDER BY id_pengguna ASC
        ");

        $total_responden = mysqli_num_rows($query);

        if ($total_responden > 0) {
          $no = 1;
          while ($result = mysqli_fetch_array($query)) {
        ?>
            <div class="neo-card">
              <div class="respondent-name"><?php echo $no . ". " . $result['nama_pengguna']; ?></div>
              <div class="respondent-info">
                No. HP: <?php echo $result['handphone']; ?><br>
                Tanggal: <?php echo tanggalIndo($result['tanggal']); ?>
              </div>

              <div class="grid-container">
                <table class="grid-table">
                  <thead>
                    <tr>
                      <th>Kategori</th>
                      <?php
                      $query_p = mysqli_query($koneksi, "SELECT * FROM pernyataan");
                      $i = 1;
                      while ($p = mysqli_fetch_array($query_p)) {
                        echo "<th>P$i</th>";
                        $i++;
                      }
                      ?>
                    </tr>
                  </thead>
                  <tbody>
                    <tr class="row-persepsi">
                      <td>Persepsi</td>
                      <?php
                      $query_ans = mysqli_query($koneksi, "SELECT jawaban FROM kuesioner WHERE id_pengguna='" . $result['id_pengguna'] . "' AND MONTH(tanggal)='" . $_POST['bulan'] . "' AND YEAR(tanggal)='" . $_POST['tahun'] . "' ORDER BY id_pernyataan ASC");
                      while ($ans = mysqli_fetch_array($query_ans)) {
                        echo "<td>" . $ans['jawaban'] . "</td>";
                      }
                      ?>
                    </tr>
                    <tr class="row-harapan">
                      <td>Harapan</td>
                      <?php
                      $query_ans = mysqli_query($koneksi, "SELECT harapan FROM kuesioner WHERE id_pengguna='" . $result['id_pengguna'] . "' AND MONTH(tanggal)='" . $_POST['bulan'] . "' AND YEAR(tanggal)='" . $_POST['tahun'] . "' ORDER BY id_pernyataan ASC");
                      while ($ans = mysqli_fetch_array($query_ans)) {
                        echo "<td>" . $ans['harapan'] . "</td>";
                      }
                      ?>
                    </tr>
                  </tbody>
                </table>
              </div>

              <a class="neo-btn neo-btn-danger" href="kuesioner.php?aksi=hapus&id_pengguna=<?php echo $result['id_pengguna'] ?>" onclick="return confirm('Apakah Anda yakin ingin menghapus data kuesioner ini?');">
                Hapus Data
              </a>
            </div>
          <?php
            $no++;
          }
        } else {
          ?>
          <div class="neo-card" style="text-align:center; font-weight:700;">
            Data Hasil Kuesioner Masih Kosong...
          </div>
        <?php
        }
        ?>
      <?php
      }
      ?>
    </div>
    <script src="js/jquery.js"></script>
    <script src="js/popper.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
  </body>

  </html>
<?php
}
?>