<?php
require_once '../config/database.php';
session_start();

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $id = $_POST['id'];
    $name = trim($_POST['name']); // Menangkap Nama Baru
    $stock = $_POST['stock_qty'];
    $price = $_POST['price_per_unit'];
    $min = $_POST['min_stock_level'];

    try {
        // Query UPDATE sekarang memasukkan field `name`
        $stmt = $pdo->prepare("UPDATE ingredients SET name = ?, stock_qty = ?, price_per_unit = ?, min_stock_level = ? WHERE id = ?");
        $stmt->execute([$name, $stock, $price, $min, $id]);

        header("Location: ../modules/admin/ingredients_manage.php?status=updated");
        exit();
    } catch (PDOException $e) {
        die("Gagal update bahan: " . $e->getMessage());
    }
}