<?php
if (isset($_POST['update_invoice_btn'])) {
    // Collect and sanitize inputs
    $invoiceID    = trim($_POST['invoice_id']);
    $receiverName = trim($_POST['receiver_name']);
    $subTotal     = trim($_POST['sub_total']);
    $totalPaid    = trim($_POST['total_amount_paid']);
    $totalDue     = trim($_POST['total_amount_due']);

    // Validate input
    if (empty($invoiceID) || empty($receiverName)) {
        $_SESSION['msg'] = "⚠️ Invoice ID and Receiver Name are required.";
        $_SESSION['type'] = "warning";
    } else {
        // Prepared statement
        $stmt = $connection->prepare("UPDATE invoice_order SET 
            receiver_name = ?, 
            sub_total = ?, 
            total_amount_paid = ?, 
            total_amount_due = ?
            WHERE invoice_id = ?");

        $stmt->bind_param("sssss", $receiverName, $subTotal, $totalPaid, $totalDue, $invoiceID);

        if ($stmt->execute()) {
            $_SESSION['msg'] = "✅ Invoice updated successfully!";
            $_SESSION['type'] = "success";
        } else {
            $_SESSION['msg'] = "❌ Error updating invoice: " . $stmt->error;
            $_SESSION['type'] = "danger";
        }
        $stmt->close();
    }
}
?>

