<?php
include 'koneksi.php';
$bulans = array("Januari", "Februari", "Maret", "April", "Mei", "Juni", "Juli", "Agustus", "September", "Oktober", "November", "Desember");
?>
<!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=Kumbh+Sans: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['aksi'])) {
      ?>
        <h5>Analisa Kepuasan Pelanggan</h5>
        <form method="post" action="analisa.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="Mulai Analisa" />
        </form>
      <?php
      } else {
      ?>
        <h5>Analisa Kepuasan 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>
                <th class="align-middle" rowspan="2">Dimensi</th>
                <th class="align-middle" rowspan="2">No.</th>
                <th class="align-middle" rowspan="2">Pernyataan</th>
                <th class="bg-warning" colspan="5">Jawaban / Kinerja</th>
                <th class="bg-success text-white" colspan="5">Harapan</th>
              </tr>
              <tr>
                <th class="bg-warning">TP</th>
                <th class="bg-warning">CP</th>
                <th class="bg-warning">P</th>
                <th class="bg-warning">SP</th>

                <th class="bg-success text-white">TP</th>
                <th class="bg-success text-white">CP</th>
                <th class="bg-success text-white">P</th>
                <th class="bg-success text-white">SP</th>
              </tr>
            </thead>
            <tbody>
              <?php
              $query = mysqli_query($koneksi, "SELECT * FROM pernyataan p, dimensi d WHERE p.id_dimensi=d.id_dimensi ORDER BY p.id_dimensi");
              if (mysqli_num_rows($query) > 0) {
                $no = 1;
                while ($result = mysqli_fetch_array($query)) {
                  $query2 = mysqli_query($koneksi, "SELECT COUNT(*) AS total FROM kuesioner WHERE MONTH(tanggal)='" . $_POST['bulan'] . "' AND YEAR(tanggal)='" . $_POST['tahun'] . "' AND id_pernyataan='" . $result['id_pernyataan'] . "' AND jawaban='STP'");
                  $result2 = mysqli_fetch_array($query2);
                  $sts_j = $result2['total'];

                  $query2 = mysqli_query($koneksi, "SELECT COUNT(*) AS total FROM kuesioner WHERE MONTH(tanggal)='" . $_POST['bulan'] . "' AND YEAR(tanggal)='" . $_POST['tahun'] . "' AND  id_pernyataan='" . $result['id_pernyataan'] . "' AND jawaban='TP'");
                  $result2 = mysqli_fetch_array($query2);
                  $ts_j = $result2['total'];

                  $query2 = mysqli_query($koneksi, "SELECT COUNT(*) AS total FROM kuesioner WHERE MONTH(tanggal)='" . $_POST['bulan'] . "' AND YEAR(tanggal)='" . $_POST['tahun'] . "' AND  id_pernyataan='" . $result['id_pernyataan'] . "' AND jawaban='CP'");
                  $result2 = mysqli_fetch_array($query2);
                  $n_j = $result2['total'];

                  $query2 = mysqli_query($koneksi, "SELECT COUNT(*) AS total FROM kuesioner WHERE MONTH(tanggal)='" . $_POST['bulan'] . "' AND YEAR(tanggal)='" . $_POST['tahun'] . "' AND  id_pernyataan='" . $result['id_pernyataan'] . "' AND jawaban='P'");
                  $result2 = mysqli_fetch_array($query2);
                  $s_j = $result2['total'];

                  $query2 = mysqli_query($koneksi, "SELECT COUNT(*) AS total FROM kuesioner WHERE MONTH(tanggal)='" . $_POST['bulan'] . "' AND YEAR(tanggal)='" . $_POST['tahun'] . "' AND  id_pernyataan='" . $result['id_pernyataan'] . "' AND jawaban='SP'");
                  $result2 = mysqli_fetch_array($query2);
                  $ss_j = $result2['total'];

                  $query2 = mysqli_query($koneksi, "SELECT COUNT(*) AS total FROM kuesioner WHERE MONTH(tanggal)='" . $_POST['bulan'] . "' AND YEAR(tanggal)='" . $_POST['tahun'] . "' AND  id_pernyataan='" . $result['id_pernyataan'] . "' AND harapan='STP'");
                  $result2 = mysqli_fetch_array($query2);
                  $sts_h = $result2['total'];

                  $query2 = mysqli_query($koneksi, "SELECT COUNT(*) AS total FROM kuesioner WHERE MONTH(tanggal)='" . $_POST['bulan'] . "' AND YEAR(tanggal)='" . $_POST['tahun'] . "' AND  id_pernyataan='" . $result['id_pernyataan'] . "' AND harapan='TP'");
                  $result2 = mysqli_fetch_array($query2);
                  $ts_h = $result2['total'];

                  $query2 = mysqli_query($koneksi, "SELECT COUNT(*) AS total FROM kuesioner WHERE MONTH(tanggal)='" . $_POST['bulan'] . "' AND YEAR(tanggal)='" . $_POST['tahun'] . "' AND  id_pernyataan='" . $result['id_pernyataan'] . "' AND harapan='CP'");
                  $result2 = mysqli_fetch_array($query2);
                  $n_h = $result2['total'];

                  $query2 = mysqli_query($koneksi, "SELECT COUNT(*) AS total FROM kuesioner WHERE MONTH(tanggal)='" . $_POST['bulan'] . "' AND YEAR(tanggal)='" . $_POST['tahun'] . "' AND  id_pernyataan='" . $result['id_pernyataan'] . "' AND harapan='P'");
                  $result2 = mysqli_fetch_array($query2);
                  $s_h = $result2['total'];

                  $query2 = mysqli_query($koneksi, "SELECT COUNT(*) AS total FROM kuesioner WHERE MONTH(tanggal)='" . $_POST['bulan'] . "' AND YEAR(tanggal)='" . $_POST['tahun'] . "' AND  id_pernyataan='" . $result['id_pernyataan'] . "' AND harapan='SP'");
                  $result2 = mysqli_fetch_array($query2);
                  $ss_h = $result2['total'];
              ?>
                  <tr>
                    <td class="align-middle"><?php echo $result['nama_dimensi']; ?></td>
                    <td class="align-middle"><?php echo $no; ?></td>
                    <td class="align-middle"><?php echo $result['nama_pernyataan']; ?></td>


                    <td class="align-middle"><?php echo $ts_j; ?></td>
                    <td class="align-middle"><?php echo $n_j; ?></td>
                    <td class="align-middle"><?php echo $s_j; ?></td>
                    <td class="align-middle"><?php echo $ss_j; ?></td>

                    <td class="align-middle"><?php echo $ts_h; ?></td>
                    <td class="align-middle"><?php echo $n_h; ?></td>
                    <td class="align-middle"><?php echo $s_h; ?></td>
                    <td class="align-middle"><?php echo $ss_h; ?></td>
                  </tr>
                <?php
                  $no++;
                }
              } else {
                ?>
                <tr>
                  <td colspan=3>
                    <center>Data Pernyataan Masih Kosong...</center>
                  </td>
                </tr>
              <?php
              }
              ?>
            </tbody>
          </table>
        </div>
        <h3>Menghitung Nilai Kinerja, Harapan & Kepuasan</h3>
        <div class="table-responsive">
          <table class="table table-bordered" style="font-size:11px;">
            <thead>
              <tr>
                <th>Dimensi</th>
                <th>No.</th>
                <th>Pernyataan</th>
                <th>Jawaban / Kinerja</th>
                <th>Harapan</th>
                <th>Tingkat Kepuasan</th>
                <th>Keterangan</th>
              </tr>
            </thead>
            <tbody>
              <?php
              $puas = 0;
              $tidak = 0;
              $query = mysqli_query($koneksi, "SELECT * FROM kuesioner GROUP BY id_pengguna");
              $n_responden = mysqli_num_rows($query);
              $query = mysqli_query($koneksi, "SELECT * FROM pernyataan p, dimensi d WHERE p.id_dimensi=d.id_dimensi ORDER BY p.id_dimensi");
              if (mysqli_num_rows($query) > 0) {
                $no = 1;
                while ($result = mysqli_fetch_array($query)) {
                  $query2 = mysqli_query($koneksi, "SELECT COUNT(*) AS total FROM kuesioner WHERE MONTH(tanggal)='" . $_POST['bulan'] . "' AND YEAR(tanggal)='" . $_POST['tahun'] . "' AND id_pernyataan='" . $result['id_pernyataan'] . "' AND jawaban='STP'");
                  $result2 = mysqli_fetch_array($query2);
                  $sts_j = $result2['total'];

                  $query2 = mysqli_query($koneksi, "SELECT COUNT(*) AS total FROM kuesioner WHERE MONTH(tanggal)='" . $_POST['bulan'] . "' AND YEAR(tanggal)='" . $_POST['tahun'] . "' AND id_pernyataan='" . $result['id_pernyataan'] . "' AND jawaban='TP'");
                  $result2 = mysqli_fetch_array($query2);
                  $ts_j = $result2['total'];

                  $query2 = mysqli_query($koneksi, "SELECT COUNT(*) AS total FROM kuesioner WHERE MONTH(tanggal)='" . $_POST['bulan'] . "' AND YEAR(tanggal)='" . $_POST['tahun'] . "' AND id_pernyataan='" . $result['id_pernyataan'] . "' AND jawaban='CP'");
                  $result2 = mysqli_fetch_array($query2);
                  $n_j = $result2['total'];

                  $query2 = mysqli_query($koneksi, "SELECT COUNT(*) AS total FROM kuesioner WHERE MONTH(tanggal)='" . $_POST['bulan'] . "' AND YEAR(tanggal)='" . $_POST['tahun'] . "' AND id_pernyataan='" . $result['id_pernyataan'] . "' AND jawaban='P'");
                  $result2 = mysqli_fetch_array($query2);
                  $s_j = $result2['total'];

                  $query2 = mysqli_query($koneksi, "SELECT COUNT(*) AS total FROM kuesioner WHERE MONTH(tanggal)='" . $_POST['bulan'] . "' AND YEAR(tanggal)='" . $_POST['tahun'] . "' AND id_pernyataan='" . $result['id_pernyataan'] . "' AND jawaban='SP'");
                  $result2 = mysqli_fetch_array($query2);
                  $ss_j = $result2['total'];

                  $kinerja = ((1 * $sts_j) + (2 * $ts_j) + (3 * $n_j) + (4 * $s_j) + (5 * $ss_j)) / $n_responden;
                  $kinerja = round($kinerja, 2);

                  $query2 = mysqli_query($koneksi, "SELECT COUNT(*) AS total FROM kuesioner WHERE MONTH(tanggal)='" . $_POST['bulan'] . "' AND YEAR(tanggal)='" . $_POST['tahun'] . "' AND id_pernyataan='" . $result['id_pernyataan'] . "' AND harapan='STP'");
                  $result2 = mysqli_fetch_array($query2);
                  $sts_h = $result2['total'];

                  $query2 = mysqli_query($koneksi, "SELECT COUNT(*) AS total FROM kuesioner WHERE MONTH(tanggal)='" . $_POST['bulan'] . "' AND YEAR(tanggal)='" . $_POST['tahun'] . "' AND id_pernyataan='" . $result['id_pernyataan'] . "' AND harapan='TP'");
                  $result2 = mysqli_fetch_array($query2);
                  $ts_h = $result2['total'];

                  $query2 = mysqli_query($koneksi, "SELECT COUNT(*) AS total FROM kuesioner WHERE MONTH(tanggal)='" . $_POST['bulan'] . "' AND YEAR(tanggal)='" . $_POST['tahun'] . "' AND id_pernyataan='" . $result['id_pernyataan'] . "' AND harapan='CP'");
                  $result2 = mysqli_fetch_array($query2);
                  $n_h = $result2['total'];

                  $query2 = mysqli_query($koneksi, "SELECT COUNT(*) AS total FROM kuesioner WHERE MONTH(tanggal)='" . $_POST['bulan'] . "' AND YEAR(tanggal)='" . $_POST['tahun'] . "' AND id_pernyataan='" . $result['id_pernyataan'] . "' AND harapan='P'");
                  $result2 = mysqli_fetch_array($query2);
                  $s_h = $result2['total'];

                  $query2 = mysqli_query($koneksi, "SELECT COUNT(*) AS total FROM kuesioner WHERE MONTH(tanggal)='" . $_POST['bulan'] . "' AND YEAR(tanggal)='" . $_POST['tahun'] . "' AND id_pernyataan='" . $result['id_pernyataan'] . "' AND harapan='SP'");
                  $result2 = mysqli_fetch_array($query2);
                  $ss_h = $result2['total'];

                  $harapan = ((1 * $sts_h) + (2 * $ts_h) + (3 * $n_h) + (4 * $s_h) + (5 * $ss_h)) / $n_responden;
                  $harapan = round($harapan, 2);
                  $kepuasan = $kinerja / $harapan;
                  $kepuasan = round($kepuasan, 2);
                  $keterangan = "Tidak Puas";
                  if ($kepuasan >= 1) {
                    $keterangan = "Puas";
                    $puas++;
                  } else {
                    $tidak++;
                  }

              ?>
                  <tr>
                    <td class="align-middle"><?php echo $result['nama_dimensi']; ?></td>
                    <td class="align-middle"><?php echo $no; ?></td>
                    <td class="align-middle"><?php echo $result['nama_pernyataan']; ?></td>
                    <td class="align-middle"><?php echo $kinerja; ?></td>
                    <td class="align-middle"><?php echo $harapan; ?></td>
                    <td class="align-middle"><?php echo $kepuasan; ?></td>
                    <td class="align-middle <?php if ($keterangan == "Puas") {
                                              echo "bg-success text-white";
                                            } else {
                                              echo "bg-danger text-white";
                                            } ?>"><?php echo $keterangan; ?></td>
                  </tr>
                <?php
                  $no++;
                }
              } else {
                ?>
                <tr>
                  <td colspan=3>
                    <center>Data Pernyataan Masih Kosong...</center>
                  </td>
                </tr>
              <?php
              }
              $total = $puas + $tidak;
              $puas /= $total;
              $puas *= 100;
              $puas = round($puas, 2);
              $tidak /= $total;
              $tidak *= 100;
              $tidak = round($tidak, 2);
              ?>
            </tbody>
          </table>
        </div>
        <h3 class="mt-4">Grafik Persentase Kepuasan</h3>
        <script src="js/apexcharts.min.js"></script>
        <div id="chart"></div>
        <script>
          var options = {
            series: [{
              data: [<?php echo $puas ?>, <?php echo $tidak ?>]
            }],
            chart: {
              animations: {
                enabled: true,
                easing: 'easeinout',
                speed: 800,
                animateGradually: {
                  enabled: true,
                  delay: 150
                },
                dynamicAnimation: {
                  enabled: true,
                  speed: 350
                }
              },
              fontFamily: 'Kumbh Sans',
              type: 'bar',
              height: 350
            },
            plotOptions: {
              bar: {
                borderRadius: 4,
                horizontal: true,
              }
            },
            dataLabels: {
              enabled: true,
              formatter: function(value, {
                seriesIndex,
                dataPointIndex,
                w
              }) {
                return value + " %"
              }
            },
            xaxis: {
              categories: ['Puas', 'Tidak Puas'],
            },
            yaxis: {
              labels: {
                style: {
                  fontSize: '20px',
                  fontFamily: 'Josefin Sans',
                  fontWeight: 'bold'
                }
              }
            }
          };

          var chart = new ApexCharts(document.querySelector("#chart"), options);
          chart.render();
        </script>

      <?php
      }
      ?>
    </div>
  </div>
</body>

</html>