<?php
include 'koneksi.php';
ob_start();
session_start();
$bulans = array("Januari", "Februari", "Maret", "April", "Mei", "Juni", "Juli", "Agustus", "September", "Oktober", "November", "Desember");
if (isset($_GET['aksi'])) {
  $id_pengguna = $_POST['id_pengguna'];
  $query2 = mysqli_query($koneksi, "SELECT * FROM pernyataan ORDER BY id_pernyataan");
  while ($result2 = mysqli_fetch_array($query2)) {
    $id = $result2['id_pernyataan'];
    $jawaban = $_POST["kinerja" . $id];
    $harapan = $_POST["harapan" . $id];
    $query = "INSERT INTO kuesioner(tanggal, id_pengguna, id_pernyataan, jawaban, harapan)
		VALUES(NOW(), '" . $id_pengguna . "', '" . $id . "', '" . $jawaban . "', '" . $harapan . "')";
    $result = mysqli_query($koneksi, $query);
  }
  if ($result) {
    $_SESSION['success'] = 1;
    $_SESSION['message'] = "Jawaban kuesioner anda telah kami terima, Terima Kasih";
    header('location:isi_kuesioner.php?id_pengguna=' . $id_pengguna);
  } else {
    $_SESSION['success'] = 0;
    $_SESSION['message'] = "Maaf, gagal mengirim jawaban kuesioner, coba lagi nanti";
    header('location:isi_kuesioner.php?id_pengguna=' . $id_pengguna);
  }
} 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;min-height:100vh">
    <div class="mt-2" style="padding-bottom:60px;">
      <div>
        <h5>Isi Kuesioner Pelanggan Periode <?php echo $bulans[(date('m') - 1)] . " " . date('Y') ?></h5>
        <?php
        $query = mysqli_query($koneksi, "SELECT * FROM kuesioner WHERE MONTH(tanggal)=MONTH(CURDATE()) AND YEAR(tanggal)=YEAR(CURDATE()) AND id_pengguna='" . $_GET['id_pengguna'] . "'");
        $rows = mysqli_num_rows($query);
        if ($rows == 0) {
        ?>
          <form action="isi_kuesioner.php?aksi=simpan" method="post" class="contact-wthree-do" enctype="multipart/form-data">
            <input type="hidden" name="id_pengguna" value="<?php echo $_GET['id_pengguna'] ?>" />
            <div class="table-responsive">
              <div class="row mb-4 text-dark">
                <div class="col">
                  <label>STP : Sangat Tidak Puas</label>
                </div>
                <div class="col">
                  <label>TP : Tidak Puas</label>
                </div>
                <div class="col">
                  <label>CP : Cukup Puas</label>
                </div>
                <div class="col">
                  <label>P : Puas</label>
                </div>
                <div class="col">
                  <label>SP : Sangat Puas</label>
                </div>
              </div>
              <table class="table table-bordered">
                <thead>
                  <tr>
                    <th class="align-middle">No.</th>
                    <th class="align-middle">Pernyataan</th>
                    <th class="align-middle bg-danger text-white">Kinerja</th>
                    <th class="align-middle bg-success text-white">Harapan</th>
                  </tr>
                </thead>
                <tbody>
                  <?php
                  $query = mysqli_query($koneksi, "SELECT * FROM pernyataan ORDER BY id_pernyataan");
                  $row = mysqli_num_rows($query);
                  if ($row > 0) {
                    $no = 1;
                    while ($result = mysqli_fetch_array($query)) {
                      $id = $result['id_pernyataan'];
                      $pernyataan = $result['nama_pernyataan'];
                      echo "<tr>";
                      echo "<td style='vertical-align:middle;color:#000 !important;' class='small'>" . $no . "</td>";
                      echo "<td style='vertical-align:middle;color:#000 !important;' class='small'>" . $pernyataan . "</td>";
                  ?>
                      <td style="white-space:nowrap !important" class="align-middle">
                        <div class="btn-group" role="group" aria-label="Basic radio toggle button group">
                          <input type="radio" class="btn-check" value="STP" required name="kinerja<?php echo $id ?>" id="option1<?php echo $id ?>" autocomplete="off">
                          <label class="btn btn-outline-danger btn-sm" for="option1<?php echo $id ?>">STP</label>

                          <input type="radio" class="btn-check" value="TP" required name="kinerja<?php echo $id ?>" id="option2<?php echo $id ?>" autocomplete="off">
                          <label class="btn btn-outline-danger btn-sm" for="option2<?php echo $id ?>">TP</label>

                          <input type="radio" class="btn-check" value="CP" required name="kinerja<?php echo $id ?>" id="option3<?php echo $id ?>" autocomplete="off">
                          <label class="btn btn-outline-danger btn-sm" for="option3<?php echo $id ?>">CP</label>

                          <input type="radio" class="btn-check" value="P" required name="kinerja<?php echo $id ?>" id="option4<?php echo $id ?>" autocomplete="off">
                          <label class="btn btn-outline-danger btn-sm" for="option4<?php echo $id ?>">P</label>

                          <input type="radio" class="btn-check" value="SP" required name="kinerja<?php echo $id ?>" id="option5<?php echo $id ?>" autocomplete="off">
                          <label class="btn btn-outline-danger btn-sm" for="option5<?php echo $id ?>">SP</label>


                        </div>
                      </td>
                      <td style="white-space:nowrap !important" class="align-middle">
                        <div class="btn-group btn-group-toggle" data-toggle="buttons">
                          <div class="btn-group" role="group" aria-label="Basic radio toggle button group">
                            <input type="radio" class="btn-check" value="STP" required name="harapan<?php echo $id ?>" id="option6<?php echo $id ?>" autocomplete="off">
                            <label class="btn btn-outline-success btn-sm" for="option6<?php echo $id ?>">STP</label>

                            <input type="radio" class="btn-check" value="TP" required name="harapan<?php echo $id ?>" id="option7<?php echo $id ?>" autocomplete="off">
                            <label class="btn btn-outline-success btn-sm" for="option7<?php echo $id ?>">TP</label>

                            <input type="radio" class="btn-check" value="CP" required name="harapan<?php echo $id ?>" id="option8<?php echo $id ?>" autocomplete="off">
                            <label class="btn btn-outline-success btn-sm" for="option8<?php echo $id ?>">CP</label>

                            <input type="radio" class="btn-check" value="P" required name="harapan<?php echo $id ?>" id="option9<?php echo $id ?>" autocomplete="off">
                            <label class="btn btn-outline-success btn-sm" for="option9<?php echo $id ?>">P</label>

                            <input type="radio" class="btn-check" value="SP" required name="harapan<?php echo $id ?>" id="option10<?php echo $id ?>" autocomplete="off">
                            <label class="btn btn-outline-success btn-sm" for="option10<?php echo $id ?>">SP</label>
                          </div>
                        </div>
                      </td>
                  <?php

                      // echo "<td style='vertical-align:middle;'><center><input type=radio class='form-control' name=".$id." value='S' required> S</center></td>";
                      // echo "<td style='vertical-align:middle;'><center><input type=radio class='form-control' name=".$id." value='N' required> N</center></td>";
                      // echo "<td style='vertical-align:middle;'><center><input type=radio class='form-control' name=".$id." value='TS' required> TS</center></td>";
                      // echo "<td style='vertical-align:middle;'><center><input type=radio class='form-control' name=".$id." value='STS' required> STS</center></td>";
                      echo "</tr>";
                      $no++;
                    }
                  } else {
                    echo "<tr>";
                    echo "<td colspan='5' style='vertical-align:middle;'><center>Kuesioner masih kosong...</center></td>";
                    echo "</tr>";
                  }
                  ?>
                </tbody>
              </table>
            </div>
            <div class="col-md-12">
              <button type="submit" class="btn btn-success" style="width:100%;">Selesai</button>
            </div>
          </form>
          <?php
        } else {
          if (isset($_SESSION['success'])) {
          ?>
            <div class="alert alert-primary mt-4"><?php echo $_SESSION['message'] ?></div>
          <?php
          } else {
          ?>
            <div class="alert alert-primary mt-4">Kamu sudah mengisi kuesioner Periode <?php echo $bulans[(date('m') - 1)] . " " . date('Y') ?> sebelumnya, terima kasih...</div>
          <?php
          }
          ?>

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