<?php
require_once('includes/load.php');
page_require_level(3);

$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;

if ($id > 0) {
    global $db;
    $db->query("UPDATE material_forecasts SET status='completed', completed_at=NOW() WHERE id={$id} AND status='issued'");
    
    if ($db->affected_rows() > 0) {
        $session->msg('s', "Item marked as completed successfully!");
    } else {
        $session->msg('d', "Failed to mark item as completed or item not in issued status.");
    }
}

// Redirect back to the previous page
if (isset($_SERVER['HTTP_REFERER'])) {
    redirect($_SERVER['HTTP_REFERER'], false);
} else {
    redirect('issue_picking_list.php', false);
}
?>