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

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

// Get score data based on ID
$nilai_query = query("SELECT * FROM mahasiswa WHERE id = $id");

// Check if data is found
if (empty($nilai_query)) {
    echo "<script>
            alert('Data not found!');
            window.location = 'penilaian1.php';
          </script>";
    exit();
}

// Data found
$nilai = $nilai_query[0];

// Get student data for alternative dropdown
$mahasiswa = query("SELECT * FROM mahasiswa");

// If save button is pressed
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 successfully updated!');
                window.location = 'penilaian1.php';
              </script>";
    } else {
        echo "<script>alert('Failed to update 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> Edit Student Evaluation</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;">Select Alternative</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;">Student Name</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%;">Code</th>
                                        <th style="width: 40%;">Criteria</th>
                                        <th style="width: 15%;">Type</th>
                                        <th style="width: 30%;">New Score</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    $kriteria = [
                                        "C1" => "Content Relevance",
                                        "C2" => "Delivery Quality",
                                        "C3" => "Audiovisual Quality",
                                        "C4" => "Interactivity & Engagement",
                                        "C5" => "Usage Frequency",
                                        "C6" => "Access Barriers",
                                        "C7" => "Impact on Understanding"
                                    ];

                                    $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>Choose new score</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> Save Changes
                            </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>
