<?php

namespace App\Controller;

use App\Form\ContactType;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

class HomeController extends AbstractController
{
    #[Route('/', name: 'app_home')]
    public function index(MailerInterface $mailer, Request $request ): Response
    {
        $form = $this->createForm(ContactType::class );

        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            // $form->getData() holds the submitted values
            // but, the original `$task` variable has also been updated
          
            $entreprise = $form->get('entreprise')->getData();
            $objet = $form->get('objet')->getData();
            
            $email = (new TemplatedEmail())
            ->from('contact@renfortimmo.com')
            ->to('contact@renfortimmo.com')
            //->cc('cc@example.com')
            ->bcc('psanachy@renfortimmo.com')
            ->bcc('cecilephilippe31@gmail.com')
            //->replyTo('fabien@example.com')
            //->priority(Email::PRIORITY_HIGH)
            ->subject('Mail reçu depuis le site web de Renfort Immo Partenaire : '. $objet )
            ->htmlTemplate('mail/template.html.twig')
            ->context([
                'entreprise' => $form->get('entreprise')->getData(),
                'contact' => $form->get('contact')->getData(),
                'telephone' => $form->get('tel')->getData(),
                'mail'=>$form->get('email')->getData(),
                'objet'=>$form->get('objet')->getData(),
                'message'=>$form->get('message')->getData(),
            
            ]);
    
            try {
    
                $mailer->send($email);
                $this->addFlash(
                    'notice',
                    'Votre message a bien été envoyé'
                );
    
              } catch (\Throwable $th) {
                  dd($th);
              }
            // ... perform some action, such as saving the task to the database

            return $this->redirectToRoute('app_home');
        }


        /*
        */
        return $this->renderForm('home/index.html.twig', [  
            'form'=>$form,
            'title'=>'Renfort Immo'
        ]);
    }
}
