<?php

include 'koneksi.php';
include 'header1.php';

// Ambil data bobot dari tabel bobot1
$bobot = query("SELECT * FROM bobot1");
if (!empty($bobot)) {
    $bobot = $bobot[0]; // Ambil data pertama jika ada
} else {
    $bobot = null; // Set null jika tidak ada data
}

// Jika tombol submit ditekan, update atau insert data bobot
if (isset($_POST["submit"])) {
    // Ambil nilai dari form
    $c1 = isset($_POST["C1"]) ? floatval($_POST["C1"]) : 0;
    $c2 = isset($_POST["C2"]) ? floatval($_POST["C2"]) : 0;
    $c3 = isset($_POST["C3"]) ? floatval($_POST["C3"]) : 0;
    $c4 = isset($_POST["C4"]) ? floatval($_POST["C4"]) : 0;
    $c5 = isset($_POST["C5"]) ? floatval($_POST["C5"]) : 0;
    $c6 = isset($_POST["C6"]) ? floatval($_POST["C6"]) : 0;
    $c7 = isset($_POST["C7"]) ? floatval($_POST["C7"]) : 0;

    // Gunakan ON DUPLICATE KEY UPDATE untuk insert/update otomatis
    $query = "INSERT INTO bobot1 (c1, c2, c3, c4, c5, c6, c7) 
              VALUES ('$c1', '$c2', '$c3', '$c4', '$c5', '$c6', '$c7') 
              ON DUPLICATE KEY UPDATE 
              c1 = VALUES(c1), 
              c2 = VALUES(c2), 
              c3 = VALUES(c3), 
              c4 = VALUES(c4), 
              c5 = VALUES(c5), 
              c6 = VALUES(c6), 
              c7 = VALUES(c7)";

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


<div id="layoutSidenav_content">
    <main>
        <div class="container-fluid mt-10">
            <h3 class="text-center mb-4 py-4 bg-light rounded" style="font-size: 28px; ">
                <strong> UBAH BOBOT KRITERIA</strong> 
            </h3>


                        <div class="alert alert-info text-center mb-3 bg-primary text-white" style="font-size: 20px;">
                            <strong>Skala Bobot:</strong> 
                            <i class="fas fa-info-circle"></i> 5 = Sangat Penting | 4 = Penting | 3 = Cukup Penting | 2 = Kurang Penting | 1 = Tidak Penting
                        </div>
                <div class="card-body">
                    <form action="" method="post">
                        <div class="table-responsive">
                            <table class="table table-bordered text-center">
                                <thead class="table-primary align-middle  ">
                                    <tr style="height: 100px; align-items: center; text-size-adjust: 30px;">
                                        <th style="width: 15%;">Kode</th>
                                        <th style="width: 45%;">Kriteria</th>
                                        <th style="width: 20%;">Bobot</th>
                                        <th style="width: 20%;">Tipe</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"
                                    ];
                                
                                    foreach ($kriteria as $key => $nama_kriteria) :
                                        $nilai_bobot = isset($bobot[strtolower($key)]) ? $bobot[strtolower($key)] : 0;
                                    ?>
                                        <tr class="align-middle" style="height: 90px;">
                                            <td><?= $key; ?></td>
                                            <td class="text-center align-middle"><?= $nama_kriteria; ?></td>
                                            <td class="align-middle text-center">
                                                <input type="number" step="0.03" name="<?= $key; ?>" class="form-control text-center align-middle" value="<?= $nilai_bobot; ?>" required>
                                            </td> 
                                            <td><?= $tipe[$key]; ?></td>
                                        </tr>
                                    <?php endforeach; ?>
                                </tbody>
                            </table>
                        </div>
                        <div class="text-center mt-4">
                            <button type="submit" name="submit" class="btn btn-success btn-lg px-5 py-3">
                                <i class="fas fa-save"></i>  Simpan Perubahan  
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </main>
</div>
