<?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, maximum-scale=1, user-scalable=no">

  <title>Analisa ServQual</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-success {
      background-color: #b5e88a;
    }

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

    .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: 5px;
    }

    .badge-hasil {
      display: inline-block;
      padding: 5px 10px;
      border: 2px solid #000;
      border-radius: 6px;
      font-weight: 700;
      font-size: 0.8rem;
      text-transform: uppercase;
    }

    .badge-sangat-puas {
      background-color: #8acde8;
    }

    .badge-puas {
      background-color: #b5e88a;
    }

    .badge-cukup-puas {
      background-color: #f7d547;
    }

    .badge-tidak-puas {
      background-color: #E6766E;
    }

    .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;
    }
  </style>
</head>

<body>
  <div style="padding-bottom:60px;">
    <?php
    if (!isset($_POST['bulan'])) {
    ?>
      <div class="neo-card">
        <div class="neo-title">Analisa ServQual</div>
        <div class="neo-subtitle">Pilih periode analisa</div>
        <form method="post" action="analisa.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;">Mulai Analisa</button>
        </form>
      </div>
    <?php
    } else {
    ?>
      <div class="neo-title">Hasil Analisa ServQual</div>
      <div class="neo-subtitle">Periode <?php echo $bulans[($_POST['bulan'] - 1)] . " " . $_POST['tahun'] ?></div>
      <!-- <a href="analisa.php" class="neo-btn" style="margin-bottom: 20px;">Kembali</a> -->

      <?php
      $query = mysqli_query($koneksi, "
        SELECT p.id_pengguna, p.nama_pengguna 
        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
        ORDER BY id_pengguna ASC
      ");

      $total_responden = mysqli_num_rows($query);

      $count_sp = 0;
      $count_p = 0;
      $count_cp = 0;
      $count_tp = 0;

      if ($total_responden > 0) {
        $no = 1;
        while ($result = mysqli_fetch_array($query)) {
          // Hitung rata-rata Persepsi dan Harapan per responden
          $query_ans = mysqli_query($koneksi, "SELECT AVG(jawaban) as mean_p, AVG(harapan) as mean_h FROM kuesioner WHERE id_pengguna='" . $result['id_pengguna'] . "' AND MONTH(tanggal)='" . $_POST['bulan'] . "' AND YEAR(tanggal)='" . $_POST['tahun'] . "'");
          $ans = mysqli_fetch_array($query_ans);

          $mean_p = round($ans['mean_p'], 2);
          $mean_h = round($ans['mean_h'], 2);
          $gap = $mean_p - $mean_h;
          $gap = round($gap, 2);

          $hasil = "";
          $badge_class = "";

          if ($gap > 0.70) {
            $hasil = "Sangat Puas";
            $badge_class = "badge-sangat-puas";
            $count_sp++;
          } elseif ($gap >= 0.60) {
            $hasil = "Puas";
            $badge_class = "badge-puas";
            $count_p++;
          } elseif ($gap >= 0.40) {
            $hasil = "Cukup Puas";
            $badge_class = "badge-cukup-puas";
            $count_cp++;
          } else {
            $hasil = "Tidak Puas";
            $badge_class = "badge-tidak-puas";
            $count_tp++;
          }
      ?>
          <div class="neo-card">
            <div class="respondent-name"><?php echo $no . ". " . $result['nama_pengguna']; ?></div>

            <div style="display:flex; justify-content:space-between; flex-wrap:wrap; margin-top:10px;">
              <div class="respondent-info">Mean Persepsi: <strong><?php echo number_format($mean_p, 2); ?></strong></div>
              <div class="respondent-info">Mean Harapan: <strong><?php echo number_format($mean_h, 2); ?></strong></div>
              <div class="respondent-info">ServQual (Gap): <strong><?php echo number_format($gap, 2); ?></strong></div>
            </div>

            <div style="margin-top: 10px;">
              <span class="badge-hasil <?php echo $badge_class; ?>"><?php echo $hasil; ?></span>
            </div>
          </div>
        <?php
          $no++;
        }
      } else {
        ?>
        <div class="neo-card" style="text-align:center; font-weight:700;">
          Data Hasil Kuesioner Masih Kosong...
        </div>
      <?php
      }

      if ($total_responden > 0) {
        $pct_sp = round(($count_sp / $total_responden) * 100, 2);
        $pct_p = round(($count_p / $total_responden) * 100, 2);
        $pct_cp = round(($count_cp / $total_responden) * 100, 2);
        $pct_tp = round(($count_tp / $total_responden) * 100, 2);
      ?>
        <div class="neo-card">
          <div class="neo-title">Grafik Kepuasan ServQual</div>
          <script src="js/apexcharts.min.js"></script>
          <div id="chart"></div>
          <script>
            var options = {
              series: [{
                name: 'Persentase',
                data: [<?php echo $pct_sp ?>, <?php echo $pct_p ?>, <?php echo $pct_cp ?>, <?php echo $pct_tp ?>]
              }],
              chart: {
                animations: {
                  enabled: true,
                  easing: 'easeinout',
                  speed: 800
                },
                fontFamily: 'Space Grotesk',
                type: 'bar',
                height: 350,
                toolbar: {
                  show: false
                }
              },
              colors: ['#8acde8', '#b5e88a', '#f7d547', '#E6766E'],
              plotOptions: {
                bar: {
                  borderRadius: 4,
                  horizontal: true,
                  distributed: true
                }
              },
              dataLabels: {
                enabled: true,
                formatter: function(val) {
                  return val + "%";
                },
                style: {
                  colors: ['#000']
                }
              },
              stroke: {
                width: 2,
                colors: ['#000']
              },
              xaxis: {
                categories: ['Sangat Puas', 'Puas', 'Cukup Puas', 'Tidak Puas'],
                labels: {
                  style: {
                    fontWeight: 700
                  }
                }
              },
              yaxis: {
                labels: {
                  style: {
                    fontSize: '14px',
                    fontWeight: 700
                  }
                }
              },
              legend: {
                show: false
              }
            };

            var chart = new ApexCharts(document.querySelector("#chart"), options);
            chart.render();
          </script>
        </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>