<?php
require $_SERVER['DOCUMENT_ROOT'] . '/application/libraries/phpmailer/Exception.php';
require $_SERVER['DOCUMENT_ROOT'] . '/application/libraries/phpmailer/PHPMailer.php';
require $_SERVER['DOCUMENT_ROOT'] . '/application/libraries/phpmailer/SMTP.php';

use PHPMailer\PHPMailer\PHPMailer;

function getEmailTemplate()
{
    $template = new stdClass();

    $date = date('d/m/Y');

    $template->subject = "ARCHLine.XP order: {COUNTRY} {ORDER_NUMBER} {PRODUCT}";
    $template->body = "<html><body style='padding: 10px 30px 10px 30px'>
        <b style='font-size: 200%;'>Thank You for Your Order.</b> <br/><br/>

        We have accepted your online purchase order for the following items. <br/>
        Once processed, invoice will be sent to this e-mail address. <br/>
        Please allow 1 working day for your order to be processed. <br/><br/>

        The following order details were accepted on <b>$date</b> <br/><br/>

        Licensee E-mail: <b>{EMAIL}</b> <br/><br/>

        Licensee name: <b>{NAME}</b> <br/><br/>

        {NONP_INFO}

        Information: <b>{INFO}</b> <br/><br/>

        {MODULE_SERIAL}

        Ordered product(s): <br/><br/>

        Item: <b>{PRODUCT}</b> <br/>
        {SUBSCRIPTION_RENEWAL}
        {SERIAL}</b> <br/>
        {INCL_LIVE_MSG}
        {QUANTITY_MSG}
        Subtotal: <b>{PRODUCT_PRICE} {CURRENCY} {EACH}</b> <br/><br/>

        {LAN_UPGRADE_MSG}
        {LAN_UPGRADE_PRICE}
        {FLOATING_MSG}
        {FLOATING_PRICE}
        {SUBSCRIPTION_MSG}
        {PREV_SOFTWARE_MSG}
        {HARDLOCK_MSG}
        {HARDLOCK_PRICE}
        {LIVE_MSG}
        {LIVE_PRICE}
        {MODULE_MSG}
        {MODULE_PRICE}
        {CREDIT_MSG}
        {NONP_MSG}
        <b>Order Total: {FINAL_PRICE} {CURRENCY}</b> <br/><br/>

        Payment: <b>{PAYMENT}</b> <br/><br/>

        Your order number: {ORDER_NUMBER} <br/><br/>

        {NOTE}

        Thank you for your order, <br/><br/>

        CadLine Ltd. <br/><br/>

        This is an automatically generated email – please do not reply to it. <br/>
        If you have any queries regarding your order please email <a href='mailto:office@cadline.hu'>office@cadline.hu</a>.
    </body></html>";

    return $template;
}

function sendErrorEmail($ctrName, $product, $name, $email, $error, $subject)
{
    $body = "<h3>Cannot generate Non-Profit key</h3>
        <br>
        
        <b>Country:</b> {$ctrName}<br>
        <b>Product:</b> {$product}<br>
        <b>User Name:</b> {$name}<br>
        <b>User Email:</b> {$email}<br>
        <b>Error:</b> {$error}<br>";

    $mail = new PHPMailer();

    $mail->IsSMTP();

    $mail->CharSet  = 'UTF-8';
    $mail->SMTPAuth = true;

    $mail->SMTPOptions = [
        'ssl' => [
            'verify_peer' => false,
            'verify_peer_name' => false,
            'allow_self_signed' => true
        ]
    ];

    $mail->From     = 'info@archlinexp.com';
    $mail->FromName = 'info@archlinexp.com';
    $mail->AddAddress('office@cadline.hu');
    $mail->addCC('mate.nagy@cadline.hu');

    $mail->isHTML(true);
    $mail->Subject  = $subject;
    $mail->Body     = $body;

    $mail->send();
}

function sendEmail($from, $fromName, $to, $subject, $body, $ccEmail = '', $ctrID)
{
    $mail = new PHPMailer();

    $mail->IsSMTP();

    $mail->CharSet  = 'UTF-8';
    $mail->SMTPAuth = true;

    $mail->SMTPOptions = [
        'ssl' => [
            'verify_peer' => false,
            'verify_peer_name' => false,
            'allow_self_signed' => true
        ]
    ];

    $mail->From     = $from;
    $mail->FromName = $fromName;
    $mail->AddAddress($to);

    if (substr($ctrID, 0, 2) != '37') {
        $mail->addCC('office@cadline.hu');
        $mail->addCC('szabados.katalin@cadline.hu');
    }

    if ($ccEmail != '')
        $mail->addCC($ccEmail);

    $mail->isHTML(true);
    $mail->Subject  = $subject;
    $mail->Body     = $body;

    $mail->send();
}
