<?php
include 'koneksi.php';
$bulans = array("Jan", "Feb", "Mar", "Apr", "Mei", "Jun", "Jul", "Agu", "Sep", "Okt", "Nov", "Des");
?>
<!DOCTYPE HTML>
<html>
<head>
  <title></title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta name="keywords" content="Modern Responsive web template, Bootstrap Web Templates, Flat Web Templates, Andriod Compatible web template,
  Smartphone Compatible web template, free webdesigns for Nokia, Samsung, LG, SonyErricsson, Motorola web design" />
  <script type="application/x-javascript"> addEventListener("load", function() { setTimeout(hideURLbar, 0); }, false); function hideURLbar(){ window.scrollTo(0,1); } </script>
  <link href="css/bootstrap.min.css" rel='stylesheet' type='text/css' />

</head>
<body style='width:100%; padding:20px;'>
  <div id="wrapper">
    <div id="page-wrapper">
      <div class="graphs" style="font-family:'Tahoma';">
        <?php
        echo "<h3>PT. Berkat Bestari Mandiri</h3>";
        echo "<p>Jl. Lintas Medan - Tebing Tinggi Km. 46,5 Sei Sijenggi</p>";
        echo "Laporan Analisa Kuesioner Periode ".$bulans[$_GET['bulan']-1]." ".$_GET['tahun']."<hr>";
        ?>
        <div class="col-md-12">
          <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">STP</th>
                  <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">STP</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)='".$_GET['bulan']."' AND YEAR(tanggal)='".$_GET['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)='".$_GET['bulan']."' AND YEAR(tanggal)='".$_GET['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)='".$_GET['bulan']."' AND YEAR(tanggal)='".$_GET['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)='".$_GET['bulan']."' AND YEAR(tanggal)='".$_GET['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)='".$_GET['bulan']."' AND YEAR(tanggal)='".$_GET['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)='".$_GET['bulan']."' AND YEAR(tanggal)='".$_GET['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)='".$_GET['bulan']."' AND YEAR(tanggal)='".$_GET['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)='".$_GET['bulan']."' AND YEAR(tanggal)='".$_GET['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)='".$_GET['bulan']."' AND YEAR(tanggal)='".$_GET['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)='".$_GET['bulan']."' AND YEAR(tanggal)='".$_GET['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 $sts_j; ?></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 $sts_h; ?></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>GAP</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");

                $last_d = 1;
                $total_d = 0;
                $total_k = 0;
                $total_h = 0;
                $ave_k = array();
                $ave_h = array();
                $grand_k = 0.0;
                $grand_h = 0.0;

                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)='".$_GET['bulan']."' AND YEAR(tanggal)='".$_GET['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)='".$_GET['bulan']."' AND YEAR(tanggal)='".$_GET['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)='".$_GET['bulan']."' AND YEAR(tanggal)='".$_GET['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)='".$_GET['bulan']."' AND YEAR(tanggal)='".$_GET['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)='".$_GET['bulan']."' AND YEAR(tanggal)='".$_GET['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)='".$_GET['bulan']."' AND YEAR(tanggal)='".$_GET['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)='".$_GET['bulan']."' AND YEAR(tanggal)='".$_GET['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)='".$_GET['bulan']."' AND YEAR(tanggal)='".$_GET['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)='".$_GET['bulan']."' AND YEAR(tanggal)='".$_GET['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)='".$_GET['bulan']."' AND YEAR(tanggal)='".$_GET['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);

                    if($harapan != 0){
                      $kepuasan = $kinerja/$harapan;
                    } else {
                      $kepuasan = 0;
                    }
                    $kepuasan = round($kepuasan, 2);
                    $keterangan = "Tidak Puas";

                    // if($kepuasan >= 1){
                    //   $keterangan = "Puas";
                    //   $puas++;
                    // } else {
                    //   $tidak++;
                    // }

                    if($last_d != $result['id_dimensi']){

                      // $total_k += $kinerja;
                      // $total_h += $harapan;
                      // $total_d++;

                      $avek = $total_k / $total_d;
                      $avek = round($avek, 2);
                      $ave_k[$counter] = $avek;

                      $aveh = $total_h / $total_d;
                      $aveh = round($aveh, 2);
                      $ave_h[$counter] = $aveh;
                      $gap = $avek - $aveh;
                      $gap = round($gap, 4);

                      $grand_k += $avek;
                      $grand_h += $aveh;

                      $last_d = $result['id_dimensi'];
                      echo "<tr class='bg-primary text-white'>";
                        echo "<td colspan=3 class='text-white'>Rata-Rata Tiap Dimensi</td>";
                        echo "<td class='text-white'>".$avek."</td>";
                        echo "<td class='text-white'>".$aveh."</td>";
                        echo "<td class='text-white'>".$gap."</td>";
                      echo "</tr>";

                      $counter++;

                      $total_k = 0;
                      $total_h = 0;
                      $total_d = 0;
                      $total_k += $kinerja;
                      $total_h += $harapan;
                      $total_d++;
                    } else {
                      $total_k += $kinerja;
                      $total_h += $harapan;
                      $total_d++;
                    }

                ?>
                    <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>
                    </tr>
                <?php


                    $no++;
                  }
                  $avek = $total_k / $total_d;
                  $avek = round($avek, 2);
                  $ave_k[$counter] = $avek;

                  $aveh = $total_h / $total_d;
                  $aveh = round($aveh, 2);
                  $ave_h[$counter] = $aveh;
                  $gap = $avek - $aveh;
                  $gap = round($gap, 4);

                  $grand_k += $avek;
                  $grand_h += $aveh;

                  $grand_k /= 5;
                  $grand_h /= 5;

                  $last_gap = $grand_k-$grand_h;
                  $persentase = $last_gap * 100;
                  $kesimpulan = "Sangat Tidak Puas";
                  if($persentase >= 21 && $persentase <= 40){
                    $kesimpulan = "Tidak Puas";
                  } else if($persentase >= 41 && $persentase <= 60){
                    $kesimpulan = "Cukup Puas";
                  } else if($persentase >= 61 && $persentase <= 80){
                    $kesimpulan = "Puas";
                  } else if($persentase >= 81 && $persentase <= 100){
                    $kesimpulan = "Sangat Puas";
                  }

                  $last_d = $result['id_dimensi'];
                  echo "<tr class='bg-primary text-white'>";
                    echo "<td colspan=3 class='text-white'>Rata-Rata Tiap Dimensi</td>";
                    echo "<td class='text-white'>".$avek."</td>";
                    echo "<td class='text-white'>".$aveh."</td>";
                    echo "<td class='text-white'>".$gap."</td>";
                  echo "</tr>";
                  echo "<tr class='bg-danger text-white'>";
                    echo "<td class='text-white' colspan=3>Rata-Rata Akhir</td>";
                    echo "<td class='text-white'>".$grand_k."</td>";
                    echo "<td class='text-white'>".$grand_h."</td>";
                    echo "<td class='text-white'>".$last_gap."</td>";
                  echo "</tr>";
                  echo "<tr class='bg-info text-white fw-bold'>";
                    echo "<td class='text-white' colspan=5>Persentase = GAP AKHIR * 100</td>";
                    echo "<td class='text-white'>".$persentase." %</td>";
                  echo "</tr>";
                } else {
                ?>
                  <tr>
                    <td colspan=3><center>Data Pernyataan Masih Kosong...</center></td>
                  </tr>
                <?php
                }
                // $total = $puas + $tidak;
                // $puas /= $total;
                // $puas *= 100;
                // $tidak /= $total;
                // $tidak *= 100;
                ?>
              </tbody>
            </table>
          </div>
          <h3>Pedoman Nilai Kepuasan</h3>
          <div class="table-responsive">
            <table class="table table-bordered" style="font-size:11px;">
              <thead>
                <tr>
                  <th>No.</th>
                  <th>Persentase</th>
                  <th>Penjelasan</th>
                </tr>
              </thead>
              <tbody>
                <tr>
                  <td>1</td>
                  <td>0 - 20%</td>
                  <td>Sangat Tidak Puas</td>
                </tr>
                <tr>
                  <td>2</td>
                  <td>21 - 40%</td>
                  <td>Tidak Puas</td>
                </tr>
                <tr>
                  <td>3</td>
                  <td>41 - 60%</td>
                  <td>Cukup Puas</td>
                </tr>
                <tr>
                  <td>4</td>
                  <td>61 - 80%</td>
                  <td>Puas</td>
                </tr>
                <tr>
                  <td>5</td>
                  <td>81 - 100%</td>
                  <td>Sangat Puas</td>
                </tr>
              </tbody>
            </table>
          </div>
          <div class="alert alert-success">
            Berdasarkan tabel pedoman diatas maka dapat diambil kesimpulan tingkat kepuasan pelanggan yaitu <?php echo $kesimpulan ?> sebanyak <?php echo $persentase ?> %
          </div>
        </div>
      </div>
    </div>
  </div>
</body>
</html>
<script>
window.print();
</script>
