<?php
require 'databaseConnection.php'; // Ensure the database connection is established

$conn = db_connect(); // Now we have the connection

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (!isset($_GET['jobNumber'])) {  // Ensure jobNumber is provided
        echo "jobNumber is missing.";
        exit;
    } else {
        $jobNumber = $_GET['jobNumber']; // Get jobNumber from URL parameter
    }

    // Retrieve values from POST request
    $TotalBillAmount = $_POST['TotalBillAmount'];
    $vatAmount = $_POST['vatAmount'];
    $discountAmount = $_POST['discountAmount'];
    $netTotal = $_POST['netTotal'];
    $billRemarks = $_POST['billRemarks'];
    $totalPaid = $_POST['totalPaid'];
    $remainingAmount = $_POST['remainingAmount'];
    $paymentStatus = $_POST['paymentStatus'];
    $dateTimeOut = $_POST['dateTimeOut'];


    // Prepare the update statement using jobNumber instead of billNumber
    $sql = "UPDATE jobCard 
            SET TotalBillAmount = ?, 
                vatAmount = ?, 
                discountAmount = ?, 
                netTotal = ?, 
                billRemarks = ?, 
                totalPaid = ?, 
                remainingAmount = ?, 
                paymentStatus = ?,
                dateTimeOut = ?
            WHERE jobNumber = ?";

    $stmt = mysqli_prepare($conn, $sql);
    if ($stmt) {
        mysqli_stmt_bind_param($stmt, "ddddssdsss", $TotalBillAmount, $vatAmount, $discountAmount, $netTotal, $billRemarks, $totalPaid, $remainingAmount, $paymentStatus, $dateTimeOut, $jobNumber);

        if (mysqli_stmt_execute($stmt)) {
            // Redirect to product-list.php after successful update
            header("Location: ../product-list.php");
            exit;
        } else {
            echo "Error updating record: " . mysqli_error($conn);
        }

        mysqli_stmt_close($stmt);
    } else {
        echo "Error preparing statement: " . mysqli_error($conn);
    }

    mysqli_close($conn); // Close the database connection
}
