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

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

// Get data based on ID from the channel table
$nilai_query = query("SELECT * FROM channel WHERE id = $id");

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

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

// Get channel names for dropdown
$channels = query("SELECT * FROM channel");

// Criteria Definitions
$nama_kriteria = [
    "C1" => "Number of Views",
    "C2" => "Number of Subscribers",
    "C3" => "Video Upload Frequency",
    "C4" => "Number of Uploaded Videos",
    "C5" => "Background of Teacher/Content Creator"
];

// If save button is pressed (Update value)
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 has been successfully updated!');
                window.location = 'penilaian2.php';
              </script>";
    } else {
        echo "<script>alert('Failed to update data!');</script>";
    }
}

// If reset button is pressed
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('Values have been reset to 0!');
                window.location = 'penilaian2.php';
              </script>";
    } else {
        echo "<script>alert('Failed to reset values!');</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> Edit Channel Rating</h4>
                </div>
                <div class="card-body">
                    <form action="" method="post">
                        <input type="hidden" name="id" value="<?= $nilai["id"] ?>">

                        <!-- Channel Name Selection -->
                        <div class="text-size: 18px mb-3 height: 100px;">
                            <label for="nama" class="text-size: 18px; form-label height: 100px;"><b>Select Alternative based on Channel Name</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>

                        <!-- Initial Values and Changes Table -->
                        <div class="mb-3">
                            <label class="form-label"><b>Alternative Values</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%;">Code</th>
                                            <th style="width: 35%;">Criteria</th>
                                            <th style="width: 20%;">Type</th>
                                            <th style="width: 30%;">New Value</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>Select a new value</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>

                                    <!-- Action Button -->
                        <div class="d-flex justify-content-center">
                            <button type="submit" name="simpan" class="btn btn-success">
                                <i class="fas fa-save"></i> Save Changes
                            </button>
                        </div>

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