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

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $ing_id = $_POST['ingredient_id'];
    $qty = $_POST['waste_qty'];
    $reason = $_POST['reason'];

    $pdo->beginTransaction();
    // 1. Catat Log
    $stmt = $pdo->prepare("INSERT INTO waste_logs (ingredient_id, waste_qty, reason) VALUES (?, ?, ?)");
    $stmt->execute([$ing_id, $qty, $reason]);
    
    // 2. Potong Stok
    $upd = $pdo->prepare("UPDATE ingredients SET stock_qty = stock_qty - ? WHERE id = ?");
    $upd->execute([$qty, $ing_id]);
    
    $pdo->commit();
    header("Location: ../modules/staff/production_input.php?waste=success");
}