<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json");

require_once "koneksi.php";

$id = $_POST['id'] ?? 0;
$judul = $_POST['judul'] ?? '';
$keterangan = $_POST['keterangan'] ?? '';

if (empty($judul)) {
    echo json_encode(["success" => false, "msg" => "Judul tidak boleh kosong"]);
    exit;
}

// Ambil data lama untuk hapus file
$query = $conn->prepare("SELECT foto, video FROM informasi WHERE id = ?");
$query->bind_param("i", $id);
$query->execute();
$result = $query->get_result();
$dataLama = $result->fetch_assoc();

$uploadDir = "../uploads/";
$fotoPath = $dataLama['foto'] ?? null;
$videoPath = $dataLama['video'] ?? null;

// Cek apakah ada permintaan hapus media
$hapusLama = isset($_POST['hapus_lama']) && $_POST['hapus_lama'] == '1';

// Proses upload gambar baru
if (isset($_FILES['gambar']) && $_FILES['gambar']['error'] === UPLOAD_ERR_OK) {
    // Hapus file lama jika ada
    if ($fotoPath && $hapusLama) {
        $fileLama = str_replace("https://netrapay.xyz/Informasi/uploads/", $uploadDir, $fotoPath);
        if (file_exists($fileLama)) {
            unlink($fileLama);
        }
    }
    
    $ext = pathinfo($_FILES['gambar']['name'], PATHINFO_EXTENSION);
    $namaFile = time() . "_img." . $ext;
    $target = $uploadDir . $namaFile;
    if (move_uploaded_file($_FILES['gambar']['tmp_name'], $target)) {
        $fotoPath = "https://netrapay.xyz/Informasi/uploads/" . $namaFile;
    }
}

// Proses upload video baru
if (isset($_FILES['video']) && $_FILES['video']['error'] === UPLOAD_ERR_OK) {
    // Hapus file lama jika ada
    if ($videoPath && $hapusLama) {
        $fileLama = str_replace("https://netrapay.xyz/Informasi/uploads/", $uploadDir, $videoPath);
        if (file_exists($fileLama)) {
            unlink($fileLama);
        }
    }
    
    $ext = pathinfo($_FILES['video']['name'], PATHINFO_EXTENSION);
    $namaFile = time() . "_vid." . $ext;
    $target = $uploadDir . $namaFile;
    if (move_uploaded_file($_FILES['video']['tmp_name'], $target)) {
        $videoPath = "https://netrapay.xyz/Informasi/uploads/" . $namaFile;
    }
}

// Jika ada permintaan hapus media tanpa upload baru
if ($hapusLama && !isset($_FILES['gambar']) && !isset($_FILES['video'])) {
    // Hapus file foto lama
    if ($fotoPath) {
        $fileLama = str_replace("https://netrapay.xyz/Informasi/uploads/", $uploadDir, $fotoPath);
        if (file_exists($fileLama)) {
            unlink($fileLama);
        }
        $fotoPath = null;
    }
    // Hapus file video lama
    if ($videoPath) {
        $fileLama = str_replace("https://netrapay.xyz/Informasi/uploads/", $uploadDir, $videoPath);
        if (file_exists($fileLama)) {
            unlink($fileLama);
        }
        $videoPath = null;
    }
}

// Update database
$stmt = $conn->prepare("UPDATE informasi SET judul = ?, keterangan = ?, foto = ?, video = ? WHERE id = ?");
$stmt->bind_param("ssssi", $judul, $keterangan, $fotoPath, $videoPath, $id);

if ($stmt->execute()) {
    echo json_encode(["success" => true, "msg" => "Informasi berhasil diupdate"]);
} else {
    echo json_encode(["success" => false, "msg" => "Gagal mengupdate: " . $conn->error]);
}

$stmt->close();
$conn->close();
?>