<?php
    date_default_timezone_set('Europe/Berlin');
    
    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\Exception;
    use PHPMailer\PHPMailer\SMTP;

    require '../mailer/Exception.php';
    require '../mailer/PHPMailer.php';
    require '../mailer/SMTP.php';

    $user = $_GET['name'];
    $menu = $_GET['menu'];
    $change = $_GET['change'];
    $datum = new DateTime($_GET['date']);
    $datum = $datum->format("d.m.Y.");
    $now = new DateTime(date("d.m.Y. H:i:s"));
    $date = $now->format('d.m.Y.');
    $time = $now->format('H:i:s');
    
    if($user == '' || $menu == '' || $change == '') {
        echo '<script>';
        echo 'window.location.href="../"';
        echo '</script>';
    }

    $smtpUsername = 'kriklunch@gmail.com';
    $smtpPassword = 'mryjvkxepolbfymp';

    $emailFrom = 'kriklunch@gmail.com';
    $emailFromName = 'Krik Lunch';
    
    //$emailTo = 'mario@krikstudio.com';
    //$emailToName = 'Mario';
    
    if($change == 'yes') {
        $email_body = "
            <head>
                <meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
            </head>
            <div>
                <div>
                    <p>Narudžba je ažurirana.</p>
                </div>
                <div></div>
                <div>
                    <p><b>".$user."</b> je ažurirao/ažurirala na menu ".$menu." za ".$datum."</p>
                </div>
                <div><br/></div>
                <div>
                    <p style='font-size:13px'><i>Vrijeme dodavanje: ".$date." u ".$time."</i></p>
                </div>
                <div></div>
            </div>
        "; 
    } else {
        $email_body = "
            <head>
                <meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
            </head>
            <div>
                <div>
                    <p>Dodana je nova narudžba.</p>
                </div>
                <div></div>
                <div>
                    <p><b>".$user."</b> je naručio/naručila menu ".$menu." za ".$datum."</p>
                </div>
                <div><br/></div>
                <div>
                    <p style='font-size:13px'><i>Vrijeme dodavanje: ".$date." u ".$time."</i></p>
                </div>
                <div></div>
            </div>
        ";
    }
    
    $mail = new PHPMailer;
    $mail->CharSet = 'UTF-8';
    $mail->Encoding = 'base64';
    $mail->isSMTP(); 
    $mail->SMTPDebug = 0; // 0 = off (for production use) - 1 = client messages - 2 = client and server messages
    $mail->Host = "ssl://smtp.gmail.com";
    $mail->Port = 587; // TLS only
    $mail->SMTPKeepAlive = true;   
    $mail->Mailer = “smtp”;
    $mail->SMTPSecure = 'tls';
    $mail->SMTPAuth = true;
    $mail->SMTPOptions = array(
        'ssl' => array(
            'verify_peer' => false,
            'verify_peer_name' => false,
            'allow_self_signed' => true
        )
    );
    $mail->Username = $smtpUsername;
    $mail->Password = $smtpPassword;
    $mail->setFrom($emailFrom, $emailFromName);
    $mail->addAddress('mario@krikstudio.com');
    $mail->Subject = 'Lunch narudžba';
    $mail->msgHTML($email_body);
    $mail->AltBody = 'HTML messaging not supported';
    
    if(!$mail->send()){
        echo "We are sorry, but the email did not go through.";
        echo 'Mailer Error: ' . $mail->ErrorInfo;
    } else {
        echo '<script>';
        echo 'window.location.href="../"';
        echo '</script>';
    }
?>