<?php
// ini_set('display_errors', '1');
// ini_set('display_startup_errors', '1');
// error_reporting(E_ALL);
include('db.php');

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

$form_data = serialize($_POST);

$first_name = isset($_POST['first_name']) ? $_POST['first_name'] : '';
$last_name = isset($_POST['last_name']) ? $_POST['last_name'] : '';
$patient_phone_number = isset($_POST['patient_phone_number']) ? $_POST['patient_phone_number'] : '';
$referring_doctor_first_name = isset($_POST['referring_doctor_first_name']) ? $_POST['referring_doctor_first_name'] : '';
$referring_doctor_last_name = isset($_POST['referring_doctor_last_name']) ? $_POST['referring_doctor_last_name'] : '';
$referring_doctor_phone_number = isset($_POST['referring_doctor_phone_number']) ? $_POST['referring_doctor_phone_number'] : '';
$created_at = date('Y-m-d H:i:s');

$fields = 'form_data';
$field_val = "'" . htmlentities(str_replace("'", "\'", $form_data)) . "'";
$insert_suceess = $db->query("INSERT INTO wp_referral_form ($fields) VALUES ($field_val)");
$referral_id = $db->lastInsertId();

if ($insert_suceess) {
    $patient_name = $first_name . ' ' . $last_name;
    $referring_doctor_name = $referring_doctor_first_name . ' ' . $referring_doctor_last_name;

    $to = 'info@niagaraoms.com';
    // $to = 'akash.dvij@gmail.com';
    // $to = 'ritu.theplanetsoft@gmail.com';

    $subject = 'New Referral form for ' . $patient_name;
    $body = '<p><b>Patient Name: </b>' . $patient_name . '</p>
    <p><b>Patient Phone Number: </b>' . $patient_phone_number . '</p>
    <p><b>Referring Doctor Name: </b>' . $referring_doctor_name . '</p>
    <p><b>Referring Doctor Phone Number: </b>' . $referring_doctor_phone_number . '</p>
    <p><a href="https://niagaraoms.mydentalconsult.com/referral_page_pdf.php?referral_id=' . base64_encode($referral_id)  . '" style="padding: 12px 45px; background-color: #0a579e; font-size: 18px; color: #fff; border-radius: 5px; font-weight: 600; cursor: pointer; text-align: center; border: none;display: inline-block; text-decoration: none;">Downlod PDF</a></p>';

    $headers = array('Content-Type: text/html; charset=UTF-8');
    require("../PHPMailer/src/PHPMailer.php");
    require("../PHPMailer/src/SMTP.php");
    require("../PHPMailer/src/Exception.php");
    $mail = new PHPMailer();
    $mail->IsSMTP();
    $mail->SMTPAuth = true;
    $mail->isHTML(true);
    $mail->SMTPDebug = 0;
    $mail->SMTPSecure = 'tls';
    $mail->Host = 'smtp.gmail.com';
    $mail->Port = '587';
    $mail->Username = 'ddsmarketingsmtp@gmail.com';
    $mail->Password = 'omoqjkjchijhqdry';
    $mail->SetFrom('ddsmarketingsmtp@gmail.com', 'Niagara Dental Implant & Oral Surgery');
    $mail->addAddress($to);
    // $mail->addAddress($to1);
    $mail->Subject = $subject;
    $mail->Body = $body;
    $result = $mail->Send();
    $response['status'] = 1;
    $response['referral_id'] = base64_encode($referral_id);
} else {
    $response['status'] = 0;
    $response['referral_id'] = '';
}
echo json_encode($response);
die;