<?php
header('Content-Type: application/json');
include("config/db.php");

$response = ["status"=>"error","message"=>"Invalid request"];

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

    $name = trim($_POST['popupName'] ?? '');
    $phone = trim($_POST['popupPhone'] ?? '');
    $boat = trim($_POST['popupBoat'] ?? '');

    if(empty($name) || empty($phone) || empty($boat)){
        echo json_encode(["status"=>"error","message"=>"All fields are required."]);
        exit;
    }

    // HACK: Store boat type in the email field since 'subject' column doesn't exist.
    $email_with_subject = "Interested in: " . htmlspecialchars($boat);

    $stmt = $conn->prepare("INSERT INTO contact_messages(name, mobile, email) VALUES (?, ?, ?)");
    $stmt->bind_param("sss", $name, $phone, $email_with_subject);

    if($stmt->execute()){
        $response = ["status"=>"success","message"=>"Message sent successfully. Our team will contact you soon."];
    }else{
        $response = ["status"=>"error","message"=>"Database error: " . $stmt->error];
    }
    $stmt->close();
}

echo json_encode($response);
?>