<?php
if (empty($_POST["form_name"])) {
    die("Invalid");
}
ini_set("display_errors", "0");

$subject = "Quick Chat Inquiry";
if (!empty($subject)) {
    foreach ($_POST["fields"] as $key => $value) {
        if (is_array($value)) {
            $value = implode(" ,", $value);
        }
        $subject = str_replace("__" . $key . "__", $value, $subject);
    }
}

$form_fields = [
    "name" => "text",
    "phone" => "text",
    "email" => "text",
    "upload" => "file_upload",
];

$body = "Name : __name__
Number : __phone__
Email : __email__";
$data = array();

$body = '<table style="border-collapse: collapse; width: 100%; margin: 0 auto;">';

foreach ($_POST["fields"] as $key => $value) {
    if (is_array($value)) {
        $value = implode(", ", $value);
    }
    $data[$key] = $value;
    
    $body .= '<tr>
                <td style="border: 1px solid #ddd; padding: 8px; text-align: left;">' . ucfirst($key) . '</td>
                <td style="border: 1px solid #ddd; padding: 8px; text-align: left;">' . htmlspecialchars($value) . '</td>
              </tr>';
}

$body .= '</table>';

$body = str_replace("__SENDER_IP__", $_SERVER["REMOTE_ADDR"], $body);
$body = str_replace("__DATE_TIME__", date("Y-m-d H:i:s"), $body);

// Check if attachments are present
if (!empty($_POST["attachments"])) {
    $attachments = explode(",", $_POST["attachments"]);
    
    // Create a boundary for the email
    $boundary = md5(time());

    // Set up email headers with the boundary
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-Type: multipart/mixed; boundary=\"$boundary\"" . "\r\n";
    $headers .= "From: info@quikchat.store" . "\r\n";

    // Compose the email body with text
    $email_body = "--$boundary\r\n";
    $email_body .= "Content-Type: text/html; charset=UTF-8\r\n";
    $email_body .= "\r\n" . $body . "\r\n";

    foreach ($attachments as $attachment) {
        $attachment_path = dirname(__DIR__) . "/uploads/" .  $attachment;
        if (file_exists($attachment_path)) {
            $file_contents = file_get_contents($attachment_path);
            $file_encoded = chunk_split(base64_encode($file_contents));
            
            // Add the attachment to the email body
            $email_body .= "--$boundary\r\n";
            $email_body .= "Content-Type: application/octet-stream; name=\"$attachment\"\r\n";
            $email_body .= "Content-Transfer-Encoding: base64\r\n";
            $email_body .= "Content-Disposition: attachment; filename=\"$attachment\"\r\n";
            $email_body .= "\r\n" . $file_encoded . "\r\n";
        }
    }

    // Close the boundary
    $email_body .= "--$boundary--\r\n";

    // Send the email
    $email_sent = mail("quikchat.in@gmail.com", $subject, $email_body, $headers);
} else {
    // Set up email headers for a plain text email if no attachments
    $headers = "From: info@quikchat.store" . "\r\n";

    // Send the email without attachments
    $email_sent = mail("quikchat.in@gmail.com", $subject, $body, $headers);
}

if ($email_sent) {
    $output = array(
        "success" => 1,
        "redirect" => 0,
        "msg" => "Email sent successfully.",
        "data" => $data
    );
    echo json_encode($output);
    exit;
} else {
    $output = array(
        "success" => 0,
        "error" => "Email error",
        "msg" => "Something went wrong, please try again.",
        "data" => $data,
        // You can include additional error information here if needed.
    );
    echo json_encode($output);
    exit;
}
?>
