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

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

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

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

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

// Ambil daftar nama channel untuk dropdown
$channels = query("SELECT * FROM channel");

// Definisi Nama Kriteria
$nama_kriteria = [
    "C1" => "Jumlah Views",
    "C2" => "Jumlah Subscribers",
    "C3" => "Frekuensi Unggah Video",
    "C4" => "Jumlah Video yang Diunggah",
    "C5" => "Latar Belakang Pengajar/Konten Kreator"
];

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

    $query = "UPDATE channel SET 
                c1 = '$c1', 
                c2 = '$c2', 
                c3 = '$c3', 
                c4 = '$c4', 
                c5 = '$c5' 
              WHERE id = '$id_nilai'";

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

// Jika tombol reset ditekan
if (isset($_POST["reset"])) {
    $query = "UPDATE channel SET c1 = 0, c2 = 0, c3 = 0, c4 = 0, c5 = 0 WHERE id = '$id'";

    if (mysqli_query($conn, $query)) {
        echo "<script>
                alert('Nilai berhasil direset ke 0!');
                window.location = 'penilaian2.php';
              </script>";
    } else {
        echo "<script>alert('Gagal mereset nilai!');</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-success text-white text-center">
                    <h4><i class="fas fa-edit"></i> Ubah Penilaian Channel</h4>
                </div>
                <div class="card-body">
                    <form action="" method="post">
                        <input type="hidden" name="id" value="<?= $nilai["id"] ?>">

                        <!-- Pilihan Nama Channel -->
                        <div class="text-size: 18px mb-3 height: 100px;">
                            <label for="nama" class="text-size: 18px; form-label height: 100px;"><b>Pilih Alternatif berdasarkan Nama Channel</b></label>
                            <select class="text-size: 18px; form-control height: 100px;" name="nama" id="nama" required>
                                <?php foreach ($channels as $channel) : ?>
                                    <option value="<?= $channel['nama']; ?>" <?= ($nilai['nama'] == $channel['nama']) ? 'selected' : ''; ?>>
                                        <?= $channel['nama']; ?>
                                    </option>
                                <?php endforeach; ?>
                            </select>
                        </div>

                        <!-- Tabel Nilai Awal dan Perubahan -->
                        <div class="mb-3">
                            <label class="form-label"><b>Nilai Alternatif</b></label>
                            <div class="table-responsive">
                                <table class="table table-bordered text-center w-100 text-center align-middle">
                                    <thead class="table-success text-size: 18px">
                                        <tr class="align-middle" style="height: 100px;">
                                            <th style="width: 15%;">Kode</th>
                                            <th style="width: 35%;">Kriteria</th>
                                            <th style="width: 20%;">Tipe</th>
                                            <th style="width: 30%;">Nilai Baru</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php 
    
                                        $pilihan_nilai = [95, 85, 75, 40, 25];
                                        $tipe_kriteria = "Benefit";
    
                                        foreach ($nama_kriteria as $kode => $nama) :
                                            $nilai_kriteria = isset($nilai[$kode]) ? $nilai[$kode] : 0;
                                        ?>
                                            <tr class="align-middle text-size: 100px" style="height: 90px;">
                                                <td><?= $kode; ?></td>
                                                <td><?= $nama; ?></td>
                                                <td><?= $tipe_kriteria; ?></td>
                                                <td class="align-middle"> 
                                                    <select class="form-control align-middle" style="align-items: center;"  name="<?= $kode; ?>" 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>

                                    <!-- Tombol Aksi -->
                        <div class="d-flex justify-content-center">
                            <button type="submit" name="simpan" class="btn btn-success">
                                <i class="fas fa-save"></i> Simpan Perubahan
                            </button>
                        </div>

                    </form>
                </div>
            </div>
        </div>
    </main>
</div>
