<?php
require_once 'config.php';
require_once 'db.php';

header('Content-Type: application/json');

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $name = sanitize($_POST['name'] ?? '');
    $mobile = sanitize($_POST['mobile'] ?? '');
    $service = sanitize($_POST['service'] ?? '');
    $message = sanitize($_POST['message'] ?? '');
    
    if (empty($name) || empty($mobile) || empty($message)) {
        echo json_encode(['success' => false, 'message' => 'Required fields are empty']);
        exit;
    }
    
    $query = "INSERT INTO contact_messages (name, mobile, service, message) VALUES (?, ?, ?, ?)";
    $result = $db->insert($query, [$name, $mobile, $service, $message]);
    
    if ($result) {
        echo json_encode(['success' => true]);
    } else {
        echo json_encode(['success' => false, 'message' => 'Database error']);
    }
} else {
    echo json_encode(['success' => false, 'message' => 'Invalid request method']);
}
?>