<?php
include('../connection.php');

// Set character set to utf8mb4
$conn->set_charset("utf8mb4");

if (isset($_POST['name'])) {
    // Fetching POST parameters
    $name = $_POST['name'];
    $username = $_POST['username'];
    $address = $_POST['address'];
    $phone = $_POST['phone'];
    $move_charge = $_POST['move_charge'];
    $final_connection_charge = $_POST['final_connection_charge'];
    $cable_length = $_POST['cable_length'];
    $final_cable = $_POST['final_cable'];
    $cable_code = $_POST['cable_code'];
    $final_length = $_POST['final_length'];
    $sender_id = $_POST['sender_id'];
    $id = $_POST['id'];

    // Additional fields
    $expected_time = $_POST['expected_time'] ?? null;
    $remark = $_POST['remark'] ?? null;
    $referby = $_POST['referby'] ?? null;
    $reference = $_POST['reference'] ?? null;

    // Prepare statement
    $stmt = $conn->prepare("UPDATE marketing SET name=?, username=?, address=?, phone=?, move_charge=?, final_connection_charge=?, cable_length=?, final_cable=?, cable_code=?, final_length=?, sender_id=? WHERE id=?");

    if ($stmt) {
        // Bind parameters
        $stmt->bind_param("sssssssssssi", $name, $username, $address, $phone, $move_charge, $final_connection_charge, $cable_length, $final_cable, $cable_code, $final_length, $sender_id, $id);

        // Execute query
        if ($stmt->execute()) {
            $response = [
                'status' => 'success',
                'message' => 'Data updated successfully',
            ];
        } else {
            $response = [
                'status' => 'error',
                'message' => 'Database execution error',
            ];
        }
        $stmt->close(); // Close the statement
    } else {
        $response = [
            'status' => 'error',
            'message' => 'Failed to prepare statement',
        ];
    }
} else {
    $response = [
        'status' => 'error',
        'message' => 'Required data not found',
    ];
}

// Set content type to JSON and echo the response
header("Content-Type: application/json");
echo json_encode($response);

?>
