<?php
require_once("/home/elitehom/elitesandsoil.com.au/wp-load.php");
header("Content-Type: application/json");
if(($_SERVER["REQUEST_METHOD"]??"")!=="POST"){http_response_code(405);echo json_encode(["ok"=>false]);exit;}
if(!empty($_POST["website"])){echo json_encode(["ok"=>true]);exit;}
$name=sanitize_text_field($_POST["name"]??"");$email=sanitize_email($_POST["email"]??"");
$phone=sanitize_text_field($_POST["phone"]??"");$job=sanitize_text_field($_POST["job"]??"");
$product=sanitize_text_field($_POST["product"]??"");$purl=esc_url_raw($_POST["purl"]??"");
$order=sanitize_text_field($_POST["order"]??"");$pickup=sanitize_text_field($_POST["pickup"]??"");
$delivery=sanitize_text_field($_POST["delivery"]??"");$bags=sanitize_text_field($_POST["bags"]??"");
if(!is_email($email)||$order===""){http_response_code(422);echo json_encode(["ok"=>false,"err"=>"invalid"]);exit;}
$ip=$_SERVER["REMOTE_ADDR"]??"0";$rk="aib_calc_rl_".md5($ip);$rn=(int)get_transient($rk);
if($rn>=6){http_response_code(429);echo json_encode(["ok"=>false,"err"=>"rate"]);exit;}
set_transient($rk,$rn+1,HOUR_IN_SECONDS);
$logo="https://elitesandsoil.com.au/wp-content/uploads/2025/07/ESS-Logo-New-2021-600x187.png";
$plink=$purl?'<a href="'.esc_url($purl).'" style="color:#7a5b14">'.esc_html($product).'</a>':esc_html($product);
$rows="";
foreach(["Job"=>$job,"Product"=>$product,"You&#39;ll need"=>$order,"Pickup"=>$pickup,"Delivery"=>$delivery,"Bags"=>$bags] as $k=>$v){ if($v!=="") $rows.='<tr><td style="padding:4px 12px 4px 0;color:#777">'.$k.'</td><td style="padding:4px 0;font-weight:bold">'.esc_html($v).'</td></tr>'; }
$cust='<div style="font-family:Arial,sans-serif;max-width:520px;margin:auto;color:#222;border:1px solid #e6e2d8;border-radius:12px;overflow:hidden">'
.'<div style="background:#c8a24a;text-align:center;padding:16px"><img src="'.$logo.'" alt="Elite Sand &amp; Soil" style="height:38px"></div>'
.'<div style="padding:20px">'
.'<p>Hi '.($name?esc_html($name):"there").',</p>'
.'<p>Thanks for using our material calculator — here&#39;s your estimate:</p>'
.'<table style="font-size:15px;margin:12px 0">'.$rows.'</table>'
.'<p>Recommended product: '.$plink.'</p>'
.'<p style="background:#f7f4ee;border-radius:8px;padding:12px;font-size:14px">This is an estimate only — real amounts vary with compaction and moisture. For an exact quantity and a <strong>delivered price to your postcode</strong>, just reply to this email or call us on <strong>1300 935 483</strong>.</p>'
.'<p style="color:#777;font-size:12px">Elite Sand &amp; Soil · 4 Harford St, Jamisontown NSW 2750 · 1300 935 483 · <a href="https://elitesandsoil.com.au">elitesandsoil.com.au</a> · Open 6 days</p>'
.'</div></div>';
$hdr=array("Content-Type: text/html; charset=UTF-8","From: Elite Sand & Soil <sales@elitegroup.sydney>","Reply-To: sales@elitegroup.sydney");
$sent1=wp_mail($email,"Your Elite Sand & Soil estimate",$cust,$hdr);
$note='<div style="font-family:Arial,sans-serif"><h3>New calculator enquiry</h3><table style="font-size:15px">'
.'<tr><td style="color:#777;padding-right:12px">Name</td><td>'.esc_html($name).'</td></tr>'
.'<tr><td style="color:#777">Email</td><td>'.esc_html($email).'</td></tr>'
.'<tr><td style="color:#777">Phone</td><td>'.esc_html($phone).'</td></tr>'
.$rows.'</table><p style="color:#777;font-size:12px">Sent from the website material calculator.</p></div>';
$sent2=wp_mail("sales@elitegroup.sydney","New calculator lead — ".($name?:$email)." (".$order.")",$note,array("Content-Type: text/html; charset=UTF-8","Reply-To: ".$email));
echo json_encode(["ok"=>($sent1||$sent2),"customer_sent"=>$sent1,"notify_sent"=>$sent2]);
