<?php

namespace App\Controller;

use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;

class FindUsController extends AbstractController
{
    /**
     * @Route("/findus", name="find_us")
     */
    public function index()
    {
        return $this->render('find_us/index.html.twig', [
            'controller_name' => 'Where to Find US',
        ]);
    }


    /**
     * @Route("/send_email_contact", name="send_email_contact")
     */
    public function send_email(Request $request):JsonResponse
    {

        $name = $request->get('name');
        $email = $request->get('email');
        $message = $request->get('message');


        $mail = new PHPMailer(true);

        try {
            //Server settings
            $mail->SMTPDebug = SMTP::DEBUG_SERVER;                      // Enable verbose debug output
            $mail->isSMTP();                                            // Send using SMTP
            $mail->Host       = 'mail.telheirodoinfante.com';                    // Set the SMTP server to send through
            $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
            $mail->Username   = 'gerencia@telheirodoinfante.com';                     // SMTP username
            $mail->Password   = '_G,SWRi!N)A)';                               // SMTP password
            $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;         // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
            $mail->Port       = 587;                                    // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above

            //Recipients
            $mail->setFrom('gerencia@telheirodoinfante.com', $name);
            $mail->addAddress('gerencia@telheirodoinfante.com');               // Name is optional

            // Content
            $mail->isHTML(true);                                  // Set email format to HTML
            $mail->Subject = 'Formulario de Contacto Website';
            $mail->Body    = '<p style="font-size: 20px;">Nova mensagem do site com os seguintes dados:</p>
            <table style=" width: 100%;font-weight: 600;font-size: 14px;">  
            <tr style=" background-color: #2e578b; color: #fff;">
            <td>Nome</td><td>Email</td><td>Mensagem</td>
            </tr>
             <tr>
            <td>'.$name.'</td><td>'.$email.'</td><<td>'.$message.'</td>
            </tr>
            </table>
            
            ';

            $mail->send();
            echo 'Message has been sent';
            return new JsonResponse(['state' => '1']);


        } catch (Exception $e) {
            echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
            return new JsonResponse(['state' => '2']);
        }

    }


    /**
     * @Route("/send_booking", name="send_booking")
     */
    public function send_booking(Request $request):JsonResponse
    {

        $name_booking = $request->get('name_booking');
        $email_booking = $request->get('email_booking');
        $phone_booking = $request->get('phone_booking');
        $date_booking = $request->get('date_booking');
        $time_booking = $request->get('time_booking');
        $seats_booking = $request->get('seats_booking');
        $message_booking = $request->get('message_booking');

        $mail = new PHPMailer(true);

        try {
            //Server settings
            $mail->SMTPDebug = SMTP::DEBUG_SERVER;                      // Enable verbose debug output
            $mail->isSMTP();                                            // Send using SMTP
            $mail->Host       = 'mail.telheirodoinfante.com';                    // Set the SMTP server to send through
            $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
            $mail->Username   = 'reservas@telheirodoinfante.com';                     // SMTP username
            $mail->Password   = 'reservasmesa';                               // SMTP password
            $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;         // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
            $mail->Port       = 587;                                    // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above

            //Recipients
            $mail->setFrom('reservas@telheirodoinfante.com', $name_booking);
            $mail->addAddress('reservas@telheirodoinfante.com');               // Name is optional

            // Content
            $mail->isHTML(true);                                  // Set email format to HTML
            $mail->Subject = ' Nova Reserva de Mesa';
            $mail->Body    = '<p style="font-size: 20px;">Nova Reserva de mesa com os seguintes dados:</p>
            <table style=" width: 100%;font-weight: 600;font-size: 14px;">  
            <tr style=" background-color: #2e578b; color: #fff;">
            <td>Nome</td><td>Email</td><td>Telemovel</td><td>Data</td><td>Hora</td><td>Lugares</td><td>Mensagem</td>
            </tr>
             <tr>
            <td>'.$name_booking.'</td><td>'.$email_booking.'</td><td>'.$phone_booking.'</td><td>'.$date_booking.'</td><td>'.$time_booking.'</td><td>'.$seats_booking.'</td><td>'.$message_booking.'</td>
            </tr>
            </table>
            
            ';

            $mail->send();
            echo 'Message has been sent';
            return new JsonResponse(['state' => '1']);


        } catch (Exception $e) {
            echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
            return new JsonResponse(['state' => '2']);
        }

    }

}
