<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {

    // ============================
    //  RECEIVE FORM VALUES
    // ============================
    $name       = $_POST['name'];
    $email      = $_POST['email'];
    $phone      = $_POST['phone'];
    $city       = $_POST['city'];
    $profileref = $_POST['profileref'];

    // ============================
    //   HANDLE RESUME FILE
    // ============================
    $resume_name = $_FILES['resume']['name'];
    $resume_tmp  = $_FILES['resume']['tmp_name'];

    $upload_path = "uploads/" . $resume_name;

    // Create folder if not exists
    if (!is_dir("uploads")) {
        mkdir("uploads", 0777, true);
    }

    move_uploaded_file($resume_tmp, $upload_path);

    // ============================
    //   SEND EMAIL WITH ATTACHMENT
    // ============================

    $to = "info@rbvroyalsbiltmorevacation.com";
    $subject = "New Job Application Received";

    $message_body = "
        <h2>New Job Application Details</h2>
        <p><strong>Name:</strong> $name</p>
        <p><strong>Email:</strong> $email</p>
        <p><strong>Phone:</strong> $phone</p>
        <p><strong>City:</strong> $city</p>
        <p><strong>Reference:</strong> $profileref</p>
        <p><strong>Resume:</strong> Attached Below</p>
    ";

    // Email Boundary
    $boundary = md5(time());

    // Headers
    $headers  = "MIME-Version: 1.0\r\n";
    $headers .= "From: Website Job Form <no-reply@yourdomain.com>\r\n";
    $headers .= "Content-Type: multipart/mixed; boundary=\"$boundary\"\r\n";

    // Body
    $body  = "--$boundary\r\n";
    $body .= "Content-Type: text/html; charset=UTF-8\r\n";
    $body .= "Content-Transfer-Encoding: base64\r\n\r\n";
    $body .= chunk_split(base64_encode($message_body));

    // Resume Attachment
    if (file_exists($upload_path)) {
        $resume_content = chunk_split(base64_encode(file_get_contents($upload_path)));
        $body .= "--$boundary\r\n";
        $body .= "Content-Type: application/octet-stream; name=\"$resume_name\"\r\n";
        $body .= "Content-Disposition: attachment; filename=\"$resume_name\"\r\n";
        $body .= "Content-Transfer-Encoding: base64\r\n\r\n";
        $body .= $resume_content . "\r\n\r\n";
    }

    $body .= "--$boundary--";

    // SEND EMAIL
    mail($to, $subject, $body, $headers);

    // ============================
    //     SEND WHATSAPP MESSAGE
    // ============================

    $whatsapp_number = "919911101669"; // India code + No.

    // Message Encode
    $msg = "New Job Application%0A%0A"
        . "Name: $name%0A"
        . "Email: $email%0A"
        . "Phone: $phone%0A"
        . "City: $city%0A"
        . "Reference: $profileref%0A"
        . "Resume: Uploaded ($resume_name)";

    // WhatsApp URL
    $wa_url = "https://wa.me/$whatsapp_number?text=$msg";

    // Redirect to WhatsApp
    echo "<script>window.location.href='$wa_url';</script>";
    exit();
}
?>
