<?php 
include 'koneksi.php';
include 'header1.php'; 


// Ambil ID dari URL
$id = isset($_GET["id"]) ? $_GET["id"] : 0;

// Ambil data nilai berdasarkan ID
$nilai_query = query("SELECT * FROM mahasiswa WHERE id = $id");

// Periksa apakah data ditemukan
if (empty($nilai_query)) {
    echo "<script>
            alert('Data tidak ditemukan!');
            window.location = 'penilaian1.php';
          </script>";
    exit();
}

// Data nilai yang ditemukan
$nilai = $nilai_query[0];

// Ambil data mahasiswa untuk dropdown alternatif
$mahasiswa = query("SELECT * FROM mahasiswa");

// Jika tombol simpan ditekan
if (isset($_POST["simpan"])) {
    $id_nilai = $_POST["id"];
    $alternatif = $_POST["alternatif"];
    $nama = $_POST["nama"];
    $c1 = $_POST["C1"];
    $c2 = $_POST["C2"];
    $c3 = $_POST["C3"];
    $c4 = $_POST["C4"];
    $c5 = $_POST["C5"];
    $c6 = $_POST["C6"];
    $c7 = $_POST["C7"];

        $query = "UPDATE mahasiswa SET 
        alternatif = '$alternatif', 
        nama = '$nama', 
        c1 = '$c1', 
        c2 = '$c2', 
        c3 = '$c3', 
        c4 = '$c4', 
        c5 = '$c5', 
        c6 = '$c6', 
        c7 = '$c7' 
        WHERE id = '$id_nilai'";


    if (mysqli_query($conn, $query)) {
        echo "<script>
                alert('Data berhasil diperbarui!');
                window.location = 'penilaian1.php';
              </script>";
    } else {
        echo "<script>alert('Gagal memperbarui data!');</script>";
    }
}
?>

<div id="layoutSidenav_content">
    <main>
    <div class="container-fluid mt-3" style="min-height: 75vh;">
            <div class="card shadow-lg p-4 w-100">
                <div class="card-header bg-primary text-white text-center">
                    <h4><i class="fas fa-edit"></i> Ubah Penilaian Mahasiswa</h4>
                </div>
                <div class="card-body">
                    <form action="" method="post">
                    <input type="hidden" name="id" value="<?= $nilai['id']; ?>">


                        <div class="row mb-3">
                            <div class="col-md-6">
                                <label class="form-label" style="font-size: 18px;">Pilih Alternatif</label>
                                <select class="form-control form-control-lg" name="alternatif" id="alternatif" onchange="updateNama()" required>
                                    <?php foreach ($mahasiswa as $mhs) : ?>
                                        <option value="<?= $mhs['alternatif']; ?>" data-nama="<?= $mhs['nama']; ?>" <?= ($nilai['alternatif'] == $mhs['alternatif']) ? 'selected' : ''; ?>>
                                            <?= $mhs['alternatif']; ?>
                                        </option>
                                    <?php endforeach; ?>
                                </select>
                            </div>

                            <div class="col-md-6">
                                <label class="form-label" style="font-size: 18px;">Nama Mahasiswa</label>
                                <input type="text" class="form-control form-control-lg" id="nama" name="nama" value="<?= $nilai["nama"]; ?>" readonly>
                            </div>
                        </div>

                        <div class="table-responsive">
                            <table class="table table-bordered text-center" style="font-size: 18px;">
                                <thead class="table-primary align-middle">
                                    <tr style="height: 100px;">
                                        <th style="width: 15%;">Kode</th>
                                        <th style="width: 40%;">Kriteria</th>
                                        <th style="width: 15%;">Tipe</th>
                                        <th style="width: 30%;">Nilai Baru</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    $kriteria = [
                                        "C1" => "Relevansi Konten",
                                        "C2" => "Kualitas Penyampaian",
                                        "C3" => "Kualitas Audiovisual",
                                        "C4" => "Interaktivitas & Engagement",
                                        "C5" => "Frekuensi Penggunaan",
                                        "C6" => "Hambatan Akses",
                                        "C7" => "Dampak terhadap Pemahaman"
                                    ];

                                    $tipe = [
                                        "C1" => "Benefit",
                                        "C2" => "Benefit",
                                        "C3" => "Benefit",
                                        "C4" => "Benefit",
                                        "C5" => "Benefit",
                                        "C6" => "Cost",
                                        "C7" => "Benefit"
                                    ];

                                    $pilihan_nilai = [95, 85, 75, 40, 25];

                                    foreach ($kriteria as $key => $nama_kriteria) : 
                                        $nilai_kriteria = isset($nilai[$key]) ? $nilai[$key] : "";
                                    ?>
                                        <tr class="align-middle" style="height: 90px;">
                                            <td><strong><?= $key; ?></strong></td>
                                            <td><?= $nama_kriteria; ?></td>
                                            <td><?= $tipe[$key]; ?></td>
                                            <td class="align-midlle"> 
                                                <select class="form-control align-midlle" style="align-items: center;"  name="<?= $key; ?>" required>
                                                    <option value="" disabled selected>Tentukan nilai baru</option>
                                                    <?php foreach ($pilihan_nilai as $pn) : ?>
                                                        <option value="<?= $pn; ?>" <?= ($nilai_kriteria == $pn) ? "selected" : ""; ?>>
                                                            <?= $pn; ?>
                                                        </option>
                                                    <?php endforeach; ?>
                                                </select>
                                            </td>
                                        </tr>
                                    <?php endforeach; ?>
                                </tbody>
                            </table>
                        </div>

                        <div class="text-center mt-4">
                            <button type="submit" name="simpan" class="btn btn-success btn-lg">
                                <i class="fas fa-save"></i> Simpan Perubahan
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </main>
</div>

<script>
    function updateNama() {
        const selectedOption = document.getElementById("alternatif").selectedOptions[0];
        document.getElementById("nama").value = selectedOption.getAttribute("data-nama");
    }
</script>
