<?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">

    <title></title>

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

    <!-- google fonts -->
    <link href="//fonts.googleapis.com/css?family=Mukta:300,400,600,700,800,900&display=swap" rel="stylesheet">
    <style>
      p {
        font-size: 13px !important;
      }

      h1 {
        font-size: 25px !important;
      }
    </style>
  </head>

  <body style="width:100%;font-size:0.8em !important;background:#f1f2f5;padding:20px;">
    <div class="mt-2" style="padding-bottom:60px;">
      <div>
        <?php
        if (isset($_GET['status'])) {
        ?>
          <h5>Jawaban Kuesioner Pelanggan</h5>
          <form method="post" action="kuesioner.php">
            <label>Pilih Bulan</label>
            <select name="bulan" class="form-control">
              <?php
              for ($i = 0; $i < count($bulans); $i++) {
              ?>
                <option value="<?php echo ($i + 1) ?>"><?php echo $bulans[$i] ?></option>
              <?php
              }
              ?>
            </select>
            <label>Isi Tahun</label>
            <input type="number" name="tahun" class="form-control" value="<?php echo date('Y') ?>" required autocomplete="off" />
            <input type="submit" class="btn btn-success mt-3 col-12" value="Lihat Data" />
          </form>
        <?php
        } else {
        ?>
          <h5>Jawaban Kuesioner Pelanggan Periode <?php echo $bulans[($_POST['bulan'] - 1)] . " " . $_POST['tahun'] ?></h5>
          <div class="table-responsive">
            <table class="table table-bordered" style="font-size:11px;">
              <thead>
                <tr>
                  <?php
                  $query = mysqli_query($koneksi, "SELECT * FROM pernyataan");
                  $row = mysqli_num_rows($query);
                  ?>
                  <th class="align-middle" rowspan="2">Aksi</th>
                  <th class="align-middle" rowspan="2">No.</th>
                  <th class="align-middle" rowspan="2" style="min-width:200px !important;">Tanggal & Responden</th>
                  <th class="bg-warning" colspan="<?php echo $row ?>">Jawaban / Persepsi</th>
                  <th class="bg-success" colspan="<?php echo $row ?>">Harapan</th>
                </tr>
                <tr>
                  <?php
                  $i = 1;
                  $query = mysqli_query($koneksi, "SELECT * FROM pernyataan");
                  while ($result = mysqli_fetch_array($query)) {
                  ?>
                    <th class="bg-warning">P<?php echo $i ?></th>
                  <?php
                    $i++;
                  }

                  $i = 1;
                  $query = mysqli_query($koneksi, "SELECT * FROM pernyataan");
                  while ($result = mysqli_fetch_array($query)) {
                  ?>
                    <th class="bg-success">P<?php echo $i ?></th>
                  <?php
                    $i++;
                  }

                  ?>
                </tr>
              </thead>
              <tbody>
                <?php
                $query = mysqli_query($koneksi, "SELECT * FROM kuesioner k, pengguna p
              WHERE p.id_pengguna=k.id_pengguna AND MONTH(k.tanggal)='" . $_POST['bulan'] . "' AND YEAR(k.tanggal)='" . $_POST['tahun'] . "'
              GROUP BY k.id_pengguna");
                if (mysqli_num_rows($query) > 0) {
                  $no = 1;
                  while ($result = mysqli_fetch_array($query)) {
                    $query2 = mysqli_query($koneksi, "SELECT * FROM kuesioner WHERE id_pengguna='" . $result['id_pengguna'] . "' LIMIT 1");
                    $result2 = mysqli_fetch_array($query2);
                ?>
                    <tr>
                      <td class="align-middle text-center">
                        <div class="dropdown show">
                          <a class="btn btn-danger btn-sm" style="font-size:12px;" href="kuesioner.php?aksi=hapus&id_pengguna=<?php echo $result['id_pengguna'] ?>">
                            Hapus
                          </a>
                        </div>
                      </td>
                      <td class="align-middle"><?php echo $no; ?></td>
                      <td class="align-middle"><?php echo tanggalIndo($result2['tanggal']) . "<br />" . $result['nama_pengguna'] . "<br />" . $result['handphone']; ?></td>

                      <?php
                      $query2 = mysqli_query($koneksi, "SELECT * FROM kuesioner WHERE id_pengguna='" . $result['id_pengguna'] . "' ORDER BY id_kuesioner, id_pernyataan");
                      while ($result2 = mysqli_fetch_array($query2)) {
                      ?>
                        <td class="align-middle"><?php echo $result2['jawaban']; ?></td>
                      <?php
                      }
                      $query2 = mysqli_query($koneksi, "SELECT * FROM kuesioner WHERE id_pengguna='" . $result['id_pengguna'] . "' ORDER BY id_kuesioner, id_pernyataan");
                      while ($result2 = mysqli_fetch_array($query2)) {
                      ?>
                        <td class="align-middle"><?php echo $result2['harapan']; ?></td>
                    <?php
                      }
                      echo "</tr>";
                      $no++;
                    }
                  } else {
                    ?>
                    <tr>
                      <td colspan=3>
                        <center>Data Hasil Kuesioner Masih Kosong...</center>
                      </td>
                    </tr>
                  <?php
                  }
                  ?>
              </tbody>
            </table>
          </div>
        <?php
        }
        ?>
      </div>
    </div>
    <script src="js/jquery.js"></script>
    <script src="js/popper.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
  </body>

  </html>
<?php
}
?>