<?php
header('Content-Type: text/html; charset=utf-8');
header('Content-Language: he');
echo '<?xml version="1.0" encoding="utf-8"?>'.chr(13);
date_default_timezone_set('Asia/Jerusalem');

// Variables from the form
$lead_full_name = $_POST['lead_full_name'] ?? '';
$lead_phone1 = $_POST['lead_phone1'] ?? '';
$lead_email = $_POST['lead_email'] ?? '';
$service = $_POST['service'] ?? '';
$accept = isset($_POST['acceptCheckbox']) ? $_POST['acceptCheckbox'] : 'לא';
$submission_time = date('Y-m-d H:i:s');
$utm_source = $_POST['utm_source'] ?? '';
$utm_medium = $_POST['utm_medium'] ?? '';
$utm_campaign = $_POST['utm_campaign'] ?? '';
$utm_term = $_POST['utm_term'] ?? '';
$utm_content = $_POST['utm_content'] ?? '';

// Add recipients here
//$to = 'ella.itzhaki@toyota.co.il, koral@hz-adv.co.il, costa@costa.co.il, nofar.lahat@toyota.co.il, meital.ben-shushan@toyota.co.il, Shani.Cohen-Shmuel@Toyota.co.il, liat.fadida@toyota.co.il, ronen.sosnov@toyota.co.il, ofer.zait@toyota.co.il, yaron.gortler@hino.co.il, keren.koren@toyota.co.il, dudy@hz-adv.co.il';
$to = 'costa@costa.co.il';

$subject = "פניה חדשה - " . $lead_full_name;
$subject = mb_encode_mimeheader($subject, 'UTF-8', 'B', "\r\n");

$headers = "From: noreply@costa.agency\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";

// Optional: Add UTM details if available
$utm_message = "";
if (!empty($utm_source)) {
    $utm_message .= "<tr><td>UTM Source</td><td>$utm_source</td></tr>";
}
if (!empty($utm_medium)) {
    $utm_message .= "<tr><td>UTM Medium</td><td>$utm_medium</td></tr>";
}
if (!empty($utm_campaign)) {
    $utm_message .= "<tr><td>UTM Campaign</td><td>$utm_campaign</td></tr>";
}
if (!empty($utm_term)) {
    $utm_message .= "<tr><td>UTM Term</td><td>$utm_term</td></tr>";
}
if (!empty($utm_content)) {
    $utm_message .= "<tr><td>UTM Content</td><td>$utm_content</td></tr>";
}

// Forming the body of the letter and adding the UTM section
$message = "
    <html>
        <body style='direction:rtl;font-family:Arial, Helvetica, sans-serif'>
            <table>
                <tr><td>שם</td><td>$lead_full_name</td></tr>
                <tr><td>טלפון</td><td>$lead_phone1</td></tr>
                <tr><td>דוא''ל</td><td>$lead_email</td></tr>
                <tr><td>בחר שירות</td><td>$service</td></tr>
                <tr><td>אישור קבלת פרסום</td><td>$accept</td></tr>
                $utm_message
            </table>
        </body>
    </html>
    ";

mail($to, $subject, $message, $headers);

// Send confirmation to the user
$user_subject = "פנייתך התקבלה בהצלחה";
$user_subject = mb_encode_mimeheader($user_subject, 'UTF-8', 'B', "\r\n");
$user_message = "
    <html>
        <body style='direction:rtl;font-family:Arial, Helvetica, sans-serif'>
            <p>שלום " . htmlspecialchars($lead_full_name) . ",</p>
            <p>פנייתך התקבלה בהצלחה ונציג מטעמנו יצור איתך קשר בהקדם.</p>
            <p>עד שניצור איתך קשר אנו מזמינים אותך להיכנס לאתר שלנו לפרטים נוספים על המשאית החשמלי </p>
            <p>לכניסה לאתר <a href='https://farizon.co.il/'>לחץ כאן</a>.</p>
        </body>
    </html>
    ";

mail($lead_email, $user_subject, $user_message, $headers);

echo "<script>location.replace('thankyou.html');</script>";
?>
