<?php

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

// Get weight data from bobot1 table
$bobot = query("SELECT * FROM bobot1");
if (!empty($bobot)) {
    $bobot = $bobot[0]; // Get first row if available
} else {
    $bobot = null; // Set null if no data available
}

// If the submit button is pressed, update or insert the weight data
if (isset($_POST["submit"])) {
    // Get values from the 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;

    // Use ON DUPLICATE KEY UPDATE for automatic insert/update
    $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('Weights updated successfully!');
                window.location = 'bobot1.php';
              </script>";
    } else {
        echo "<script>alert('Failed to update weights!');</script>";
    }
}
?>


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

            <div class="alert alert-info text-center mb-3 bg-primary text-white" style="font-size: 20px;">
            <i class="fas fa-info-circle"></i>  <strong>Level of Importance = </strong> 
            5 : Very Important | 4 : Important | 3 : Important Enough | 2 : Less Important | 1 : Not Important
            </div>
                <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%;">Code</th>
                                    <th style="width: 45%;">Criteria</th>
                                    <th style="width: 20%;">Weight</th>
                                    <th style="width: 20%;">Type</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"
                                ];
                            
                                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>  Save Changes  
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </main>
</div>  
