<?php
include "../koneksi.php";

// Ambil data input dan amankan
$id = mysqli_real_escape_string($koneksi, $_POST['id']);
$nama_pasaran = mysqli_real_escape_string($koneksi, $_POST['nama_pasaran']);
$title = mysqli_real_escape_string($koneksi, $_POST['title']);
$meta_desc = mysqli_real_escape_string($koneksi, $_POST['meta_desc']);
$custom_script = mysqli_real_escape_string($koneksi, $_POST['custom_script']);

// Ambil data lama dari database untuk fallback jika file tidak diganti
$query = mysqli_query($koneksi, "SELECT logo, favicon FROM atur_web WHERE id = '$id'");
$data_lama = mysqli_fetch_assoc($query);
$logo = $data_lama['logo'];
$favicon = $data_lama['favicon'];

// Proses upload logo jika ada file baru
if (isset($_FILES['logo']) && $_FILES['logo']['error'] == 0) {
    $ext = pathinfo($_FILES['logo']['name'], PATHINFO_EXTENSION);
    $logo_name = "uploads/logo_" . time() . "." . $ext;
    move_uploaded_file($_FILES['logo']['tmp_name'], "../" . $logo_name);
    $logo = $logo_name;
}

// Proses upload favicon jika ada file baru
if (isset($_FILES['favicon']) && $_FILES['favicon']['error'] == 0) {
    $ext = pathinfo($_FILES['favicon']['name'], PATHINFO_EXTENSION);
    $favicon_name = "uploads/favicon_" . time() . "." . $ext;
    move_uploaded_file($_FILES['favicon']['tmp_name'], "../" . $favicon_name);
    $favicon = $favicon_name;
}

// Update ke database (tanpa background)
$proses = mysqli_query($koneksi, "UPDATE atur_web SET 
    nama_pasaran = '$nama_pasaran',
    title = '$title',
    meta_desc = '$meta_desc',
    logo = '$logo',
    favicon = '$favicon',
    custom_script = '$custom_script'
    WHERE id = '$id'");

if ($proses) {
    header("Location: atur.php?alert=berhasil");
} else {
    echo "Proses Gagal<br>Error: " . mysqli_error($koneksi);
}
?>