<?php
include('../connection.php');
header("Content-Type: application/json");
mysqli_set_charset($conn, 'utf8mb4');

if (isset($_POST['name'])) {

    $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'];
    $expected_time = $_POST['expected_time'];
    $remark = $_POST['remark'];
    $referby = $_POST['referby'];
    $reference = $_POST['reference'];
    $status = isset($_POST['status']) ? $_POST['status'] : 'pending';

    $stmt = $conn->prepare("INSERT INTO marketing (name, username, address, phone, move_charge, final_connection_charge, cable_length, final_cable, cable_code, final_length, sender_id, expected_time, remark, referby, reference, status) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
    $stmt->bind_param("ssssssssssssssss", $name, $username, $address, $phone, $move_charge, $final_connection_charge, $cable_length, $final_cable, $cable_code, $final_length, $sender_id, $expected_time, $remark, $referby, $reference, $status);

    if ($stmt->execute()) {
        $response = [
            'status' => 'success',
            'message' => 'Data inserted',
        ];
    } else {
        $response = [
            'status' => 'error',
            'message' => 'Database error: ' . $stmt->error,
        ];
    }

    $stmt->close();
} else {
    $response = [
        'status' => 'error',
        'message' => 'Required data not found',
    ];
}

echo json_encode($response);
$conn->close();
?>
