<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;

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

$error = '';
$name = '';
$surname = '';
$phone = '';
$email = '';
$subject = '';
$message = '';

function clean_text($string)
{
    $string = trim($string);
    $string = stripslashes($string);
    $string = htmlspecialchars($string);
    return $string;
}

if (isset($_POST["submit"])) {
    if (empty($_POST["name"])) {
        $error .= '<p class="py-3 message"><label class="text-danger">Inserisci il tuo nome</label></p>';
    }
    if (empty($_POST["surname"])) {
        $error .= '<p class="py-3 message"><label class="text-danger">Inserisci il tuo cognome</label></p>';
    }
    if (empty($_POST["email"])) {
        $error .= '<p class="py-3 message" ><label class="text-danger">Inserisci la tua email</label></p>';
    } else {
        $email = clean_text($_POST["email"]);
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $error .= '<p class="py-3"><label class="text-danger">Email non valida</label></p>';
        }
    }
    if ($error == '') {
        $file_open = fopen("contact_data.csv", "a");
        $no_rows = count(file("contact_data.csv"));
        if ($no_rows > 1) {
            $no_rows = ($no_rows - 1) + 1;
        }
        $date = date('m/d/Y h:i:s a', time());

        $name = $_POST["name"];
        $surname = $_POST["surname"];
        $phone = $_POST["phone"];

        $form_data = array(
            'sr_no'  => $no_rows,
            'name'  => $name,
            'surname' => $surname,
            'phone' => $phone,
            'email'  => $email,
            'date' => $date,
        );
        fputcsv($file_open, $form_data);

        $to      = $email;
        $subject = 'Prenotazione Heliossea XX';
        $message = file_get_contents('./email.html');;
        $headers = 'From: noreply@prodigysgroup.it'       . "\r\n" .
                    'Reply-To: noreply@prodigysgroup.it' . "\r\n" .
                    'MIME-Version: 1.0' . "\r\n" .
                    'Content-type: text/html; charset=iso-8859-1' . "\r\n" .
                    'X-Mailer: PHP/' . phpversion();
        $altbody = 'Heliossea XX, Domenica 3 settembre 2023 alle ore 5:33, Parco del Castello di Miramare - Trieste';
        $rows = 0;
        $cells = 0;
        $fp = fopen("contact_data.csv","r");
        while($content = fgetcsv($fp)){
            $rows++;
        }
        fclose($fp);
        if ($rows > 210){
            $subject = ' Heliossea XX';
            $altbody = 'Attenzione - i biglietti sono esauriti! Heliossea XX, Domenica 3 settembre 2023 alle ore 5:33, Parco del Castello di Miramare - Trieste';
        }

        //mail($to, $subject, $message, $headers);
        $mail = new PHPMailer(true);
        try {
            
            //Server settings
            //$mail->SMTPDebug = SMTP::DEBUG_SERVER;                      //Enable verbose debug output
            $mail->isSMTP();                                            //Send using SMTP
            $mail->Host       = 'smtps.aruba.it';                     //Set the SMTP server to send through
            $mail->SMTPAuth   = true;                                   //Enable SMTP authentication
            $mail->Username   = 'noreply@prodigysgroup.it';                     //SMTP username
            $mail->Password   = 'ZYg4b6Dt';                               //SMTP password
            $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;            //Enable implicit TLS encryption
            $mail->Port       = 465;                                    //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`

            //Recipients
            $mail->setFrom('noreply@prodigysgroup.it', 'noreply');
            $mail->addAddress($to, $name);     //Add a recipient
            $mail->addReplyTo('noreply@prodigysgroup.it', 'noreply');

            //Content
            $mail->isHTML(true);                                  //Set email format to HTML
            $mail->Subject = $subject;
            $mail->Body    = $message;
            $mail->AltBody = $altbody;
            $mail->send();
            /*echo 'Message has been sent';*/
        } catch (Exception $e) {
            /*echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";*/
        }

        $error = '<p class="py-3 message"><label class="text-success">Grazie per esserti iscritto!</label></p>';
        $name = '';
        $surname = '';
        $phone = '';
        $email = '';
        $subject = '';
        $message = '';
    }
}


?>
<?php
include 'include/header.php';
?>

<div class="main py-5">
    <div class="container">
        <div class="row">
            <div class="col-md-8" style="margin:0 auto; float:none;">
                <form method="post" action="index.php#prenotazione" id="prenotazione">
                    
                    <h1 class="text-center">Domenica 3 settembre 2023 alle ore 5:33</h1>
                    <h3 class="text-center">Parco del Castello di Miramare - Trieste</h3>

                    <h2 class="text-gold2 text-center">Prenota il tuo posto</h2>

                    <?php

                    $rows = 0;
$cells = 0;
$fp = fopen("contact_data.csv","r");
while($content = fgetcsv($fp)){
    $rows++;
}
fclose($fp);
if ($rows > 210){
    echo '<h2 class="text-gold2 text-center text-danger">Attenzione! Abbiamo raggiunto il massimo della capienza prevista. Ti ricontatteremo se dovessero liberarsi dei posti.</h2>';
}
?>

                    <?php echo $error; ?>


                    <div class="text-center pt-3"><h3>Seguirà prima colazione</h3></div>
                    <div class="text-center pt-3 text-gold2"><h3>Ingresso gratuito con prenotazione obbligatoria</h3></div>

                </form>
            </div>
        </div>
    </div>
</div>

<?php
include 'include/footer.php';
?>