<?php
    date_default_timezone_set('Europe/Zagreb');

    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'];
    $user =  ucfirst($user);
    $menu = $_GET['menu'];
    $date = new DateTime($_GET['date']);
    $datum = $date->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');

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

    $emailFrom = 'kriklunch@gmail.com';
    $emailFromName = 'Lunch Notification';
    
    $emailTo = 'mario@krikstudio.com';
    //$emailToName = 'Mario';
    
    $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 ažuriranja: ".$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 = "smtp.gmail.com";
    $mail->Port = 587; // TLS only
    $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($emailTo);
    $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.";
    } else {
        $cookie_name = "mailer";
        $cookie_value = "7958U3S";
        setcookie($cookie_name, $cookie_value, time()+10);
        header('Location: ../');
    }
?>