<?php

include 'koneksi.php';
include 'header2.php';

// Retrieve weight data from bobot2 table
$bobot = query("SELECT * FROM bobot2");
$bobot = !empty($bobot) ? $bobot[0] : null; // Take the first data if available

// If the submit button is pressed, update or insert weight data
if (isset($_POST["submit"])) {
    // Get values from form (not using decimal at this stage)
    $c1 = isset($_POST["C1"]) ? intval($_POST["C1"]) : 0;
    $c2 = isset($_POST["C2"]) ? intval($_POST["C2"]) : 0;
    $c3 = isset($_POST["C3"]) ? intval($_POST["C3"]) : 0;
    $c4 = isset($_POST["C4"]) ? intval($_POST["C4"]) : 0;
    $c5 = isset($_POST["C5"]) ? intval($_POST["C5"]) : 0;

    // If data exists, perform UPDATE, if not, perform INSERT
    if ($bobot) {
        $query = "UPDATE bobot2 SET 
                    c1 = '$c1', 
                    c2 = '$c2', 
                    c3 = '$c3', 
                    c4 = '$c4', 
                    c5 = '$c5'";
    } else {
        $query = "INSERT INTO bobot2 (c1, c2, c3, c4, c5) 
                  VALUES ('$c1', '$c2', '$c3', '$c4', '$c5')";
    }

    if (mysqli_query($conn, $query)) {
        echo "<script>
                alert('Weight data successfully updated!');
                window.location = 'bobot2.php';
              </script>";
    } else {
        echo "<script>alert('Failed to update weight data!');</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> EDIT CRITERIA WEIGHTS</strong> 
                </h3>
                    <div class="alert alert-info text-center mb-3 bg-success 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-success 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" => "Total Channel Views",
                                        "C2" => "Number of Subscribers",
                                        "C3" => "Upload Frequency",
                                        "C4" => "Total Uploaded Videos",
                                        "C5" => "Educator/Content Creator Background"
                                    ];
                                    $tipe = [
                                        "C1" => "Benefit",
                                        "C2" => "Benefit",
                                        "C3" => "Benefit",
                                        "C4" => "Benefit",
                                        "C5" => "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 class="text-center align-middle"><strong><?= $key; ?></strong></td>
                                            <td><?= $nama_kriteria; ?></td>
                                            <td class="align-middle text-center">
                                                <input type="number" name="<?= $key; ?>" class="form-control text-center" value="<?= $nilai_bobot; ?>" required style="font-size: 18px; width: 100%;">
                                            </td>
                                            <td><?= $tipe[$key]; ?></td>
                                        </tr>
                                    <?php endforeach; ?>
                                </tbody>
                            </table>
                        </div>
                        <div class="text-center mt-3">
                            <button type="submit" name="submit" class="btn btn-success btn-lg">
                                <i class="fas fa-save"></i>  Save Changes  
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </main>
</div>
