<?php
require_once __DIR__ . '/../includes/api-helpers.php';
api_require_post();
if (api_is_spam()) api_json(true, 'Thank you! Your valuation request has been received.');

$name = api_str('name', 120);
$email = api_str('email', 160);
$phone = api_str('phone', 40);
$address = api_str('address', 300);
$propertyType = api_str('property_type', 100);
$notes = api_str('notes', 1500);

if ($name === '' || !api_valid_email($email) || $address === '') {
    api_json(false, 'Please fill in your name, a valid email and the property address.');
}

$message = "Property type: $propertyType\nAddress: $address\nNotes: $notes";

$stmt = db()->prepare('INSERT INTO leads (type, name, email, phone, message) VALUES (?, ?, ?, ?, ?)');
$stmt->execute(['valuation', $name, $email, $phone, $message]);

api_json(true, 'Thank you! An advisor will contact you within 48 hours with your free valuation.');
