<?php
session_start();

// Security Guard: Batasi akses jika session login tidak valid
if (empty($_SESSION['namauser']) && empty($_SESSION['passuser'])) {
    echo "<link href='../../css/style_login.css' rel='stylesheet' type='text/css' />
          <div id='login'>
              <h1 class='fail'>Untuk mengakses modul, Anda harus login dulu.</h1>
              <p class='fail'><a href='../../index.php'>LOGIN</a></p>
          </div>";
    exit();
}

include "../../../config/koneksi.php";
include "../../../config/fungsi_seo.php";

$module = $_GET['module'] ?? '';
$act    = $_GET['act'] ?? '';

// =========================================================================
// 1. HAPUS FAQ
// =========================================================================
if ($module == 'faq' && $act == 'hapus') {
    $id = mysqli_real_escape_string($konek, $_GET['id'] ?? '');

    if (!empty($id)) {
        $queryHapus = "DELETE FROM faq WHERE id_faq = '$id'";
        mysqli_query($konek, $queryHapus);
    }

    header("Location: ../../media.php?module=" . $module);
    exit();
}

// =========================================================================
// 2. INPUT / TAMBAH FAQ
// =========================================================================
elseif ($module == 'faq' && $act == 'input') {
    $pertanyaan = mysqli_real_escape_string($konek, $_POST['pertanyaan'] ?? '');
    $jawaban    = mysqli_real_escape_string($konek, $_POST['jawaban'] ?? '');

    $queryInsert = "INSERT INTO faq (pertanyaan, jawaban) 
                    VALUES ('$pertanyaan', '$jawaban')";
    
    mysqli_query($konek, $queryInsert);

    header("Location: ../../media.php?module=" . $module);
    exit();
}

// =========================================================================
// 3. UPDATE / EDIT FAQ
// =========================================================================
elseif ($module == 'faq' && $act == 'update') {
    $id         = mysqli_real_escape_string($konek, $_POST['id'] ?? '');
    $pertanyaan = mysqli_real_escape_string($konek, $_POST['pertanyaan'] ?? '');
    $jawaban    = mysqli_real_escape_string($konek, $_POST['jawaban'] ?? '');

    $queryUpdate = "UPDATE faq SET 
                        pertanyaan = '$pertanyaan', 
                        jawaban    = '$jawaban' 
                    WHERE id_faq   = '$id'";

    mysqli_query($konek, $queryUpdate);

    header("Location: ../../media.php?module=" . $module);
    exit();
}
?>