<?php
require_once '../config/connect.php';

use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
require '../vendor/autoload.php';

$mail            = new PHPMailer(true);
$dateTimeNow     = date('Y-m-d H:i:s');
$data['name']    = ($_POST['name']) ? MRES_FILTER($_POST['name']) : "";
$data['email']   = ($_POST['email']) ? MRES_FILTER($_POST['email']) : "";
$data['subject'] = ($_POST['subject']) ? MRES_FILTER($_POST['subject']) : "";
$data['phone'] = ($_POST['phone']) ? MRES_FILTER($_POST['phone']) : "";
$data['comment'] = ($_POST['comment']) ? MRES_FILTER($_POST['comment']) : "";
$subject         = "";
$subject         = "New contact from " . $data['name'] . " -  ".$data['email'];
$full_logo = $siteurl."/".$logo_url;
$email_txt = "<html>
    <head>
        <title></title>
        <style type='text/css'>
            body {
                width: 100%;
                margin: 0;
                padding: 0;
            }
            table tbody tr td {
                padding: 10px 5px;
            }
        </style>
    </head>
    <div style='border:1px solid'>
        <div style='background:#fff; padding:10px; text-align:center;'><img src='" . $full_logo . "' alt='" . $emailTitle . "'  title='" . $emailTitle . "'  /></div>
        <div style=' padding:10px;'>
            <table>
                <tbody>
                    <tr>
                        <td><strong>Name</strong></td>
                        <td>:</td>
                        <td>" . $data['name'] . "</td>
                    </tr>
                     <tr>
                        <td><strong>Email Address</strong></td>
                        <td>:</td>
                        <td>" . $data['email'] . "</td>
                    </tr>
                     <tr>
                        <td><strong>Subjects</strong></td>
                        <td>:</td>
                        <td>" . $data['subject'] . "</td>
                    </tr>
                     <tr>
                        <td><strong>Phone Number</strong></td>
                        <td>:</td>
                        <td>" . $data['phone'] . "</td>
                    </tr>
                    <tr>
                        <td><strong>Your Comments</strong></td>
                        <td>:</td>
                        <td>" . $data['comment'] . "</td>
                    </tr>
                </tbody>
            </table>
            <p><strong>Thank You !</strong><br></p>
        </div>
    </div>
</html>";
$mail              = new PHPMailer(true);
$contact_from_name = $emailTitle;
try {
    $mail->SMTPDebug = SMTP::DEBUG_OFF;
    $mail->isSMTP();
    $mail->Host       = $email_host;
    $mail->SMTPAuth   = true;
    $mail->Username   = $email_username;
    $mail->Password   = $email_userpassword;
    $mail->SMTPSecure = 'tls';                // 'ssl' or 'tls'
    $mail->Port       = 587;                  // 465 for SSL, 587 for TLS
    $mail->setFrom($contact_from_email, $emailTitle);
    $mail->addAddress($data['email'], $data['name']);
    $mail->addBCC('praveen0123@yopmail.com', $emailTitle);
    $mail->isHTML(true);
    $mail->Subject = $subject;
    $mail->Body    = $email_txt;
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
    $mail->send();
    $data['created_at'] = $dateTimeNow;
    $insertedid         = jRInsert("contact_form", $data);
    if ($insertedid > 0) {
        $_SESSION['success'] = 'Your message has been submitted successfully!';
        echo json_encode([
            'status'  => 'success',
            'message' => 'Message submitted successfully!',
        ]);
    } else {
        $_SESSION['error'] = 'Something went wrong, please try again later.';
        echo json_encode([
            'status'  => 'error',
            'message' => 'Error in submitting your message.',
        ]);
    }
}
 catch (Exception $e) {
    $_SESSION['error'] = 'Message could not be sent. Mailer Error: ' . $mail->ErrorInfo;
    echo json_encode([
        'status'  => 'error',
        'message' => 'Mailer Error: ' . $mail->ErrorInfo,
    ]);
}
