<?php
session_start();

if (!isset($_SESSION["login"])) {
  header("location:../../login.php");
  exit;
}

include '../../koneksi.php';

$nis         = $_GET['nis'];
$kode_kelas  = $_GET['kode_kelas'];
$kode_mapel  = $_GET['kode_mapel'];
$jenis_ujian = $_GET['jenis_ujian'];

/* =========================
   HITUNG JUMLAH SOAL ESSAY
========================= */
$soal_query = mysqli_query(
    $koneksi,
    "SELECT id_soal 
     FROM soal_essay 
     WHERE kode_kelas='$kode_kelas'
       AND kode_mapel='$kode_mapel'
       AND jenis_ujian='$jenis_ujian'"
);

$jumlah_soal = mysqli_num_rows($soal_query);

if ($jumlah_soal == 0) {
    die("Error: Tidak ada soal essay untuk ujian ini!");
}

/* =========================
   AMBIL NILAI COSINE & LEVENSHTEIN
========================= */
$q = mysqli_query($koneksi, "
    SELECT nilai_cosine, nilai_levenshtein
    FROM nilai_essay
    WHERE nis='$nis'
      AND kode_kelas='$kode_kelas'
      AND kode_mapel='$kode_mapel'
      AND jenis_ujian='$jenis_ujian'
");

if (!$q) {
    die("Error query: " . mysqli_error($koneksi));
}

$data = mysqli_fetch_assoc($q);

if (!$data) {
    die("Error: Data nilai tidak ditemukan untuk siswa ini!");
}

$total_cosine = (float)$data['nilai_cosine'];
$total_levenshtein = (float)$data['nilai_levenshtein'];

/* =========================
   HITUNG NILAI AKHIR (FORMULA BENAR)
========================= */

// Total gabungan
$total_nilai = $total_cosine + $total_levenshtein;

// Nilai akhir: (Cosine + Levenshtein) / Jumlah Soal
$nilai_akhir = $total_nilai / $jumlah_soal;
$nilai_akhir = round($nilai_akhir, 2);


/* =========================
   SIMPAN NILAI AKHIR
========================= */
$update = mysqli_query($koneksi, "
    UPDATE nilai_essay
    SET nilai_akhir='$nilai_akhir'
    WHERE nis='$nis'
      AND kode_kelas='$kode_kelas'
      AND kode_mapel='$kode_mapel'
      AND jenis_ujian='$jenis_ujian'
");

if (!$update) {
    die("Error update nilai akhir: " . mysqli_error($koneksi));
}

/* =========================
   AMBIL DATA SISWA
========================= */
$siswa_query = mysqli_query($koneksi, "SELECT * FROM siswa WHERE nis='$nis'");
$siswa = mysqli_fetch_array($siswa_query);

$kelas_query = mysqli_query($koneksi, "SELECT * FROM kelas WHERE kode_kelas='$kode_kelas'");
$kelas = mysqli_fetch_array($kelas_query);

$mapel_query = mysqli_query($koneksi, "SELECT * FROM mapel WHERE kode_mapel='$kode_mapel'");
$mapel = mysqli_fetch_array($mapel_query);

?>

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <title>Perhitungan Nilai Akhir - Sistem Penilaian Esai</title>
  <link rel="icon" href="../../assets/img/brand/favicon.png" type="image/png">
  <link rel="stylesheet" href="../../https://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700">
  <link rel="stylesheet" href="../../assets/vendor/nucleo/css/nucleo.css" type="text/css">
  <link rel="stylesheet" href="../../assets/vendor/@fortawesome/fontawesome-free/css/all.min.css" type="text/css">
  <link rel="stylesheet" href="../../assets/css/argon.css?v=1.2.0" type="text/css">
  <link rel="stylesheet" href="../../assets/css/style.css">
  <style>
    body {
      background: #f5f7fa;
      font-family: 'Open Sans', sans-serif;
    }
    
    .main-card {
      background: white;
      border-radius: 8px;
      box-shadow: 0 2px 4px rgba(0,0,0,0.1);
      margin-bottom: 20px;
    }
    
    .section-header {
      background: #f8f9fa;
      border-bottom: 2px solid #e9ecef;
      padding: 15px 20px;
      font-weight: 600;
      color: #2c3e50;
    }
    
    .content-box {
      padding: 20px;
    }
    
    .info-row {
      padding: 10px 0;
      border-bottom: 1px solid #e9ecef;
    }
    
    .info-row:last-child {
      border-bottom: none;
    }
    
    .info-label {
      font-weight: 600;
      color: #495057;
      min-width: 150px;
      display: inline-block;
    }
    
    .step-card {
      background: white;
      border: 1px solid #dee2e6;
      border-radius: 6px;
      margin-bottom: 20px;
      overflow: hidden;
    }
    
    .step-header {
      background: #e9ecef;
      padding: 12px 20px;
      border-bottom: 1px solid #dee2e6;
      font-weight: 600;
      color: #2c3e50;
    }
    
    .data-table {
      width: 100%;
      border-collapse: collapse;
      margin: 15px 0;
    }
    
    .data-table th,
    .data-table td {
      padding: 12px;
      border: 1px solid #dee2e6;
      text-align: left;
    }
    
    .data-table th {
      background: #f8f9fa;
      font-weight: 600;
    }
    
    .formula-box {
      background: #f8f9fa;
      border: 1px solid #dee2e6;
      padding: 12px;
      border-radius: 4px;
      margin: 10px 0;
      font-family: monospace;
    }
    
    .calculation-box {
      background: #f8f9fa;
      border: 1px solid #dee2e6;
      padding: 12px;
      border-radius: 4px;
      margin: 10px 0;
      font-family: monospace;
    }
    
    .result-highlight {
      background: #e9ecef;
      padding: 20px;
      border-radius: 6px;
      text-align: center;
      margin: 20px 0;
    }
    
    .result-value {
      font-size: 2.5rem;
      font-weight: 700;
      color: #2c3e50;
      margin: 10px 0;
    }
    
    .summary-table {
      width: 100%;
      border-collapse: collapse;
    }
    
    .summary-table th,
    .summary-table td {
      padding: 12px;
      border: 1px solid #dee2e6;
    }
    
    .summary-table th {
      background: #f8f9fa;
      font-weight: 600;
    }
    
    .summary-table .total-row {
      background: #2c3e50;
      color: white;
      font-weight: 700;
    }
    
    .note-box {
      background: #fff9e6;
      border-left: 3px solid #ffc107;
      padding: 12px;
      border-radius: 4px;
      margin: 15px 0;
    }
    
    .btn-back {
      background: #6c757d;
      color: white;
      padding: 12px 30px;
      border-radius: 4px;
      text-decoration: none;
      display: inline-block;
      margin: 5px;
    }
    
    .btn-back:hover {
      background: #5a6268;
      color: white;
      text-decoration: none;
    }
    
    .btn-print {
      background: #2c3e50;
      color: white;
      padding: 12px 30px;
      border-radius: 4px;
      text-decoration: none;
      display: inline-block;
      margin: 5px;
    }
    
    .btn-print:hover {
      background: #1a252f;
      color: white;
      text-decoration: none;
    }
  </style>
</head>


<body>
  <nav class="sidenav navbar navbar-vertical fixed-left navbar-expand-xs navbar-light bg-white" id="sidenav-main">
    <div class="scrollbar-inner">
      <div class="sidenav-header align-items-center">
        <a class="navbar-brand" href="javascript:void(0)">
          <img src="../../assets/img/logo3.jpg" class="navbar-brand-img" alt="...">
        </a>
      </div>
      <div class="navbar-inner">
        <div class="collapse navbar-collapse" id="sidenav-collapse-main">
          <ul class="navbar-nav">
            <li class="nav-item">
              <a class="nav-link" href="beranda.php">
                <i class="ni ni-tv-2 text-warning"></i>
                <span class="nav-link-text">Dashboard</span>
              </a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="daftar_siswa.php">
                <i class="ni ni-archive-2 text-warning"></i>
                <span class="nav-link-text">Daftar Siswa</span>
              </a>
            </li>
            <li class="nav-item">
              <a class="nav-link active" href="kelas.php">
                <i class="ni ni-paper-diploma text-info"></i>
                <span class="nav-link-text">Room Kelas</span>
              </a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="../../logout.php">
                <i class="ni ni-button-power text-dark"></i>
                <span class="nav-link-text">Log Out</span>
              </a>
            </li>
          </ul>
        </div>
      </div>
    </div>
  </nav>

  <div class="main-content" id="panel">
    <nav class="navbar navbar-top navbar-expand navbar-dark bg-warning border-bottom">
      <div class="container-fluid">
        <div class="collapse navbar-collapse" id="navbarSupportedContent">
          <ul class="navbar-nav align-items-center  ml-md-auto ">
            <li class="nav-item d-xl-none">
              <div class="pr-3 sidenav-toggler sidenav-toggler-dark" data-action="sidenav-pin" data-target="#sidenav-main">
                <div class="sidenav-toggler-inner">
                  <i class="sidenav-toggler-line"></i>
                  <i class="sidenav-toggler-line"></i>
                  <i class="sidenav-toggler-line"></i>
                </div>
              </div>
            </li>
          </ul>
          <ul class="navbar-nav align-items-center ml-auto ml-md-0">
            <li class="nav-item dropdown">
              <a class="nav-link pr-0" href="#" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                <div class="media align-items-center">
                  <span class="avatar avatar-sm rounded-circle">
                    <?php 
                    $profil = mysqli_query($koneksi, "SELECT * FROM guru WHERE nip='{$_SESSION['nip']}'");
                    $tampil_profil = mysqli_fetch_array($profil);
                    ?>
                    <img alt="Image placeholder" src="../../assets/img/profil_guru/<?= $tampil_profil['foto']; ?>">
                  </span>
                  <div class="media-body ml-2 d-none d-lg-block">
                    <span class="mb-0 text-sm font-weight-bold">NIP : <?= $_SESSION['nip']; ?></span>
                  </div>
                </div>
              </a>
              <div class="dropdown-menu dropdown-menu-right">
                <a href="../../logout.php" class="dropdown-item">
                  <i class="ni ni-user-run"></i>
                  <span>Logout</span>
                </a>
              </div>
            </li>
          </ul>
        </div>
      </div>
    </nav>

    <div class="header bg-warning pb-6">
      <div class="container-fluid">
        <div class="header-body">
          <div class="row">
            <div class="col-xl-12">
              
              <div class="main-card">
                <div class="section-header">
                  <h3 class="mb-0">Perhitungan Nilai Akhir Essay</h3>
                </div>
                <div class="content-box">
                  
                  <!-- INFO UJIAN -->
                  <div class="main-card">
                    <div class="section-header">
                      Informasi Ujian
                    </div>
                    <div class="content-box">
                      <div class="row">
                        <div class="col-md-6">
                          <div class="info-row">
                            <span class="info-label">NIS:</span> <?= $nis; ?>
                          </div>
                          <div class="info-row">
                            <span class="info-label">Nama Siswa:</span> <?= $siswa['nama_siswa']; ?>
                          </div>
                          <div class="info-row">
                            <span class="info-label">Kelas:</span> <?= $kelas['nama_kelas']; ?>
                          </div>
                        </div>
                        <div class="col-md-6">
                          <div class="info-row">
                            <span class="info-label">Mata Pelajaran:</span> <?= $mapel['nama_mapel']; ?>
                          </div>
                          <div class="info-row">
                            <span class="info-label">Jenis Ujian:</span> <?= $jenis_ujian; ?>
                          </div>
                          <div class="info-row">
                            <span class="info-label">Jumlah Soal:</span> <?= $jumlah_soal; ?> soal
                          </div>
                        </div>
                      </div>
                    </div>
                  </div>

                  <!-- PROSES PERHITUNGAN -->
                  <h5 style="margin-top: 30px; margin-bottom: 20px; font-weight: 600;">Proses Perhitungan</h5>

                  <!-- STEP 1 -->
                  <div class="step-card">
                    <div class="step-header">
                      Step 1: Data Nilai dari Database
                    </div>
                    <div class="content-box">
                      <table class="data-table">
                        <thead>
                          <tr>
                            <th width="40%">Komponen</th>
                            <th width="30%">Nilai</th>
                            <th width="30%">Keterangan</th>
                          </tr>
                        </thead>
                        <tbody>
                          <tr>
                            <td>Total Cosine Similarity</td>
                            <td style="text-align: center;"><strong><?= number_format($total_cosine, 3); ?></strong></td>
                            <td><small>Nilai semantik (sudah berbobot)</small></td>
                          </tr>
                          <tr>
                            <td>Total Levenshtein Distance</td>
                            <td style="text-align: center;"><strong><?= number_format($total_levenshtein, 3); ?></strong></td>
                            <td><small>Nilai sintaksis (sudah berbobot)</small></td>
                          </tr>
                        </tbody>
                      </table>
                      <div class="note-box">
                        <strong>Catatan:</strong> Nilai Cosine dan Levenshtein sudah mengandung pembobotan masing-masing.
                      </div>
                    </div>
                  </div>

                  <!-- STEP 2 -->
                  <div class="step-card">
                    <div class="step-header">
                      Step 2: Penjumlahan Nilai Cosine + Levenshtein
                    </div>
                    <div class="content-box">
                      <p style="margin-bottom: 10px;"><strong>Formula:</strong></p>
                      <div class="formula-box">
                        Total Nilai = Cosine + Levenshtein
                      </div>
                      <p style="margin-bottom: 10px; margin-top: 15px;"><strong>Perhitungan:</strong></p>
                      <div class="calculation-box">
                        Total Nilai = <?= number_format($total_cosine, 3); ?> + <?= number_format($total_levenshtein, 3); ?>
                      </div>
                      <div class="result-highlight">
                        <h5 style="color: #2c3e50; margin-bottom: 5px;">Total Nilai</h5>
                        <div style="font-size: 2rem; font-weight: 700; color: #2c3e50;">
                          <?= number_format($total_nilai, 3); ?>
                        </div>
                      </div>
                    </div>
                  </div>

                  <!-- STEP 3 -->
                  <div class="step-card">
                    <div class="step-header">
                      Step 3: Rata-rata per Soal (Nilai Akhir)
                    </div>
                    <div class="content-box">
                      <p style="margin-bottom: 10px;"><strong>Formula:</strong></p>
                      <div class="formula-box">
                        Nilai Akhir = (Total Cosine + Total Levenshtein) ÷ Jumlah Soal
                      </div>
                      <p style="margin-bottom: 10px; margin-top: 15px;"><strong>Perhitungan:</strong></p>
                      <div class="calculation-box">
                        Nilai Akhir = (<?= number_format($total_cosine, 3); ?> + <?= number_format($total_levenshtein, 3); ?>) ÷ <?= $jumlah_soal; ?>
                      </div>
                      <div class="calculation-box">
                        Nilai Akhir = <?= number_format($total_nilai, 3); ?> ÷ <?= $jumlah_soal; ?>
                      </div>
                      <div class="result-highlight">
                        <h5 style="color: #2c3e50; margin-bottom: 5px;">Nilai Akhir</h5>
                        <div class="result-value">
                          <?= $nilai_akhir; ?>
                        </div>
                      </div>
                    </div>
                  </div>

                  <!-- RINGKASAN -->
                  <div class="main-card" style="margin-top: 30px;">
                    <div class="section-header">
                      Ringkasan Hasil
                    </div>
                    <div class="content-box" style="padding: 0;">
                      <table class="summary-table">
                        <tbody>
                          <tr>
                            <th width="60%">Komponen</th>
                            <th width="40%" style="text-align: center;">Nilai</th>
                          </tr>
                          <tr>
                            <td>Total Nilai Cosine Similarity</td>
                            <td style="text-align: center;"><?= number_format($total_cosine, 2); ?></td>
                          </tr>
                          <tr>
                            <td>Total Nilai Levenshtein Distance</td>
                            <td style="text-align: center;"><?= number_format($total_levenshtein, 2); ?></td>
                          </tr>
                          <tr style="background: #f8f9fa;">
                            <td><strong>Total Nilai Gabungan (Cosine + Levenshtein)</strong></td>
                            <td style="text-align: center;"><strong><?= number_format($total_nilai, 2); ?></strong></td>
                          </tr>
                          <tr style="background: #f8f9fa;">
                            <td><strong>Jumlah Soal</strong></td>
                            <td style="text-align: center;"><strong><?= $jumlah_soal; ?></strong></td>
                          </tr>
                          <tr class="total-row">
                            <td><strong>NILAI AKHIR</strong></td>
                            <td style="text-align: center; font-size: 1.3rem;"><strong><?= $nilai_akhir; ?></strong></td>
                          </tr>
                        </tbody>
                      </table>
                    </div>
                  </div>

                  <!-- TOMBOL AKSI -->
                  <div class="text-center" style="margin-top: 30px;">
                    <a href="hasil_ujian.php?kode_mapel=<?= $kode_mapel; ?>&jenis_ujian=<?= $jenis_ujian; ?>" class="btn-back">
                      <i class="fas fa-arrow-left"></i> Kembali ke Hasil Ujian
                    </a>
                    <a href="cetak_nilai.php?nis=<?= $nis; ?>&kode_kelas=<?= $kode_kelas; ?>&kode_mapel=<?= $kode_mapel; ?>&jenis_ujian=<?= $jenis_ujian; ?>" class="btn-print" target="_blank">
                      <i class="fas fa-print"></i> Cetak Laporan
                    </a>
                  </div>

                </div>
              </div>

            </div>
          </div>
        </div>
      </div>
    </div>
  </div>

  <script src="../../assets/vendor/jquery/dist/jquery.min.js"></script>
  <script src="../../assets/vendor/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
  <script src="../../assets/vendor/js-cookie/js.cookie.js"></script>
  <script src="../../assets/vendor/jquery.scrollbar/jquery.scrollbar.min.js"></script>
  <script src="../../assets/vendor/jquery-scroll-lock/dist/jquery-scrollLock.min.js"></script>
  <script src="../../assets/js/argon.js?v=1.2.0"></script>
</body>
</html>