<?php

require_once 'databaseConnection.php';

if ($_SERVER["REQUEST_METHOD"] == "POST") {

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

    if (
        !empty($_POST['jobNumber']) && !empty($_POST['jobDate']) && !empty($_POST['registerNumber']) &&
        !empty($_POST['year']) && !empty($_POST['millage']) && !empty($_POST['vehicleBrand']) &&
        !empty($_POST['vehicleModel']) && !empty($_POST['vehicleColour']) &&
        !empty($_POST['vinNumber']) && !empty($_POST['customerName']) && !empty($_POST['contactNumber']) &&
        !empty($_POST['jobCardStatus']) && isset($_POST['dateTimeIn'])
    ) {

        // Assigning values to variables
        $job_date = $_POST['jobDate'];
        $register_number = $_POST['registerNumber'];
        $year = (int)$_POST['year'];
        $millage = $_POST['millage'];
        $vehicle_brand = $_POST['vehicleBrand'];
        $vehicle_model = $_POST['vehicleModel'];
        $vehicle_colour = $_POST['vehicleColour'];
        $vin_number = $_POST['vinNumber'];
        $customer_name = $_POST['customerName'];
        $contact_number = $_POST['contactNumber'];
        $dateTimeIn = $_POST['dateTimeIn'];
        $jobCardStatus = $_POST['jobCardStatus'];

        // Optional Fields (set NULL if empty)
        $customer_address = !empty($_POST['customerAddress']) ? $_POST['customerAddress'] : null;
        $reported_defects = !empty($_POST['reportedDefects']) ? $_POST['reportedDefects'] : null;
        $completed_action = !empty($_POST['completedAction']) ? $_POST['completedAction'] : null;
        $pending_defects = !empty($_POST['pendingDefects']) ? $_POST['pendingDefects'] : null;
        $inspection_report = !empty($_POST['inspectionReport']) ? $_POST['inspectionReport'] : null;
        $dateTimeOut = !empty($_POST['dateTimeOut']) ? $_POST['dateTimeOut'] : null;
        $contactPerson = !empty($_POST['contactPerson']) ? $_POST['contactPerson'] : null;
        $personContactNumber = !empty($_POST['personContactNumber']) ? $_POST['personContactNumber'] : null;
        $technicianName = !empty($_POST['technicianName']) ? $_POST['technicianName'] : null;
        $supervisor = !empty($_POST['supervisor']) ? $_POST['supervisor'] : null;
        $engine_number = !empty($_POST['engineNumber']) ? $_POST['engineNumber'] : null;

        // Database connection
        $mysqli = db_connect();
        if (!$mysqli) {
            echo json_encode(["status" => "error", "message" => "Database connection failed: " . $mysqli->connect_error]);
            exit;
        }

        // Prepared SQL statement for UPDATE
        $stmt = $mysqli->prepare("UPDATE `customerField`
       SET `job_date` = ?, `register_number` = ?, `year` = ?, `millage` = ?, `vehicle_brand` = ?, `vehicle_model` = ?, `vehicle_colour` = ?, `engine_number` = ?, `vin_number` = ?,
           `customer_name` = ?, `customer_address` = ?, `contact_number` = ?, `reported_defects` = ?, `completed_action` = ?, `jobCardStatus` = ?, `dateTimeIn` = ?, `dateTimeOut` = ?, `contactPerson` = ?, `personContactNumber` = ?, `technicianName` = ?, `supervisor` = ?, `pending_defects` = ?, `inspection_report` = ?
       WHERE `job_number` = ?");

        if (!$stmt) {
            echo "Error preparing statement: " . $mysqli->error;
            exit;
        }

        // Bind parameters and execute statement
        $stmt->bind_param(
            "ssisssssssssssssssssssss",
            $job_date,
            $register_number,
            $year,
            $millage,
            $vehicle_brand,
            $vehicle_model,
            $vehicle_colour,
            $engine_number,
            $vin_number,
            $customer_name,
            $customer_address,
            $contact_number,
            $reported_defects,
            $completed_action,
            $jobCardStatus,
            $dateTimeIn,
            $dateTimeOut,
            $contactPerson,
            $personContactNumber,
            $technicianName,
            $supervisor,
            $pending_defects,
            $inspection_report,
            $jobNumber
        );

        if ($stmt->execute()) {
            header("Location: ../customer.php");
            exit;
        } else {
            echo "Error inserting record: " . $stmt->error;
        }

        // Close statement and database connection
        $stmt->close();
        $mysqli->close();
    } else {
        echo "All required fields must be filled.";
    }
} else {
    echo "Invalid request method.";
}
