<?php

require("vendor/phpmailer/phpmailer/class.phpmailer.php");
require("vendor/phpmailer/phpmailer/class.smtp.php"); 
require('config.php');

$linkID = new PDO('mysql:host='.Config::DB_HOST.';dbname='.Config::DB_NAME, Config::DB_USERNAME, Config::DB_PASSWORD);
$linkID->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Setting ERRMODE


$company_name = $linkID->query("SELECT * FROM ea_settings WHERE ea_settings.id=14")->fetchAll(PDO::FETCH_ASSOC);
$company_email = $linkID->query("SELECT * FROM ea_settings WHERE ea_settings.id=15")->fetchAll(PDO::FETCH_ASSOC);
$company_link = $linkID->query("SELECT * FROM ea_settings WHERE ea_settings.id=16")->fetchAll(PDO::FETCH_ASSOC);

$waitlistfirstname = $_POST['waitlist-firstname']; 
$waitlistlastname = $_POST['waitlist-lastname']; 
$waitlistphonenumber = $_POST['waitlist-phonenumber']; 
$waitlistemail = $_POST['waitlist-email']; 
$waitlistnotes = $_POST['waitlist-notes']; 
$waitlistservice = $_POST['waitlist-service']; 

$servicewhich = $linkID->query("SELECT * FROM ea_services WHERE ea_services.id=".$waitlistservice)->fetchAll(PDO::FETCH_ASSOC);
//echo $servicewhich[0]['name'];
$data = [
    'firstname' => $waitlistfirstname,
    'lastname' => $waitlistlastname,
    'phonenumber' => $waitlistphonenumber,
    'email' => $waitlistemail, 
    'notes'=> $waitlistnotes, 
    'service'=> $waitlistservice, 
];


$sql = "INSERT INTO waitlist (firstname, lastname, phonenumber, email, notes, service, status, date_sent, date_expires) VALUES (:firstname, :lastname, :phonenumber, :email, :notes, :service, ' ', null, null)";
//echo $sql; 
$stmt= $linkID->prepare($sql);
$stmt->execute($data);

  $mail = new \PHPMailer;
        $mail->IsSMTP(); // enable SMTP
        $mail->SMTPOptions = array(
            'ssl' => array(
            'verify_peer' => false,
            'verify_peer_name' => false,
            'allow_self_signed' => true
                            )
                                    );
		$mail->SMTPDebug = 0;
		$mail->SMTPAuth = TRUE;
		$mail->SMTPSecure = 'tls'; // tls or ssl
		#$mail->Port     = "SMTP port";
		$mail->Username = 'bradysdrugstorebelleriver@myappts.ca';
		$mail->Password = 'NYu]mGhT){rV';
		#$mail->Host     = "SMTP HOST";
		#$mail->Mailer   = "smtp";
		$mail->SetFrom('bradysdrugstorebelleriver@myappts.ca', $company_name[0]['value']);
		$mail->AddAddress($waitlistemail);
$mail->Subject = $servicewhich[0]['waitlist_confirm_email_header']; 
		$html = file_get_contents('waitlist_email_template.php');
		$tobereplaced = array("companylinkbro", "companynamebro", "servicenamebro", "emaildescriptionbro", "firstnamebro", "lastnamebro", "emailpersonbro", "phonepersonbro");
		$replacements = array($company_link[0]['value'], $company_name[0]['value'], $servicewhich[0]['name'], $servicewhich[0]['waitlist_confirm_email_desc'], $waitlistfirstname, $waitlistlastname, $waitlistemail, $waitlistphonenumber); 
		$htmlone = str_replace($tobereplaced, $replacements, $html); 
		$mail->MsgHTML($htmlone);
		$mail->IsHTML(true);	
	    $mail->Send();


echo 'Your record has been successfully added to the waitlist!'




?>