<?php
include 'koneksi.php';

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $product_id = $_POST['id_produk'];
    $nama_produk = $_POST['nama'];
    $deskripsi = $_POST['deskripsi'];
    $stok = $_POST['stok'];
    $harga = $_POST['harga'];

    if (!empty($_FILES['gambar']['tmp_name'])) {
        $gambar_tmp = $_FILES['gambar']['tmp_name'];
        $gambarData = file_get_contents($gambar_tmp);

        $sql = "UPDATE products SET foto_produk = ?, nama_produk = ?, deskripsi = ?, stok = ?, harga = ? WHERE product_id = ?";
        $stmt = $conn->prepare($sql);
        $stmt->bind_param("sssiii", $gambarData, $nama_produk, $deskripsi, $stok, $harga, $product_id);
    } else {
        $sql = "UPDATE products SET nama_produk = ?, deskripsi = ?, stok = ?, harga = ? WHERE product_id = ?";
        $stmt = $conn->prepare($sql);
        $stmt->bind_param("ssiii", $nama_produk, $deskripsi, $stok, $harga, $product_id);
    }

    if ($stmt->execute()) {
        header("Location: ../Produk/Produk.php");
        exit();
    } else {
        echo "Gagal update data: " . $stmt->error;
    }

    $stmt->close();
    $conn->close();
} else {
    header("Location: ../Produk/produk.php");
    exit();
}
?>
