<?php

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

function randHash($len=32)
{
	return substr(md5(openssl_random_pseudo_bytes(20)),-$len);
}

$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, 
    'hash'=> randHash(),
];


$sql = "INSERT INTO waitlist (firstname, lastname, phonenumber, email, notes, pharmacynotes, service, status, date_sent, date_expires, hash) VALUES (:firstname, :lastname, :phonenumber, :email, :notes, ' ', :service, ' ', null, null, :hash)";
//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 = Config::EMAIL;
		$mail->Password = Config::EMAIL_PASSWORD;
		#$mail->Host     = "SMTP HOST";
		#$mail->Mailer   = "smtp";
		$mail->SetFrom(Config::EMAIL, $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", "frontendcolor");
		$replacements = array($company_link[0]['value'], $company_name[0]['value'], $servicewhich[0]['name'], $servicewhich[0]['waitlist_confirm_email_desc'], $waitlistfirstname, $waitlistlastname, $waitlistemail, $waitlistphonenumber, Config::FRONTEND_COLOR); 
		$htmlone = str_replace($tobereplaced, $replacements, $html); 
		$mail->MsgHTML($htmlone);
		$mail->IsHTML(true);	
	    $mail->Send();
	    $mail->ClearAddresses();
		    $mail->ClearAllRecipients();
  $db='myappts_accountofemailsandtexts'; 

   $linkID1 = new PDO('mysql:host=myappts.ca;dbname='.$db, "myappts_admin", "Taals123!@#");
$linkID1->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Setting ERRMODE
$today = date("Y-m-d h:i:sa"); 
$dataone = [
                'datesent' => $today, 
                'pharmacy' => Config::COMPANY_FIRST_NAME.' '.Config::COMPANY_LAST_NAME,
                'apptid' => 'na',
                ];
        $sqlone = "INSERT INTO `emails_sent`(`date_sent`, `pharmacy`, `apptid`, `reason`) VALUES (:datesent, :pharmacy, :apptid, 'Waitlist Confirmation')";
        $stmtone= $linkID1->prepare($sqlone);
        $stmtone->execute($dataone);

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




?>