<?php 
 
namespace App\Controller; 
 
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; 
use Symfony\Component\HttpFoundation\Request; 
use Symfony\Component\HttpFoundation\Response; 
use Symfony\Component\Routing\Annotation\Route; 
use App\Entity\Panel; 
use App\Repository\PanelRepository; 
use App\Entity\TypePanel; 
use App\Repository\TypePanelRepository; 
use App\Entity\HistoryViews; 
use App\Repository\HistoryViewsRepository; 
use Doctrine\Common\Collections\ArrayCollection; 
use Symfony\Component\HttpFoundation\JsonResponse; 
use Doctrine\Common\Collections\Criteria; 
use Doctrine\ORM\EntityManagerInterface; 
use DateTime; 
 
class HomeController extends AbstractController 
{ 
 
    /** 
     */ 
    public function __construct(PanelRepository $panelRepository, TypePanelRepository $typePanelRepository, HistoryViewsRepository $historyViewsRepository) 
    { 
        $this->panelRepository = $panelRepository; 
        $this->typePanelRepository = $typePanelRepository; 
        $this->historyViewsRepository = $historyViewsRepository; 
    } 
 
    public function sendEmail($data) 
    { 
        $curl = curl_init(); 
 
        curl_setopt_array($curl, array( 
            CURLOPT_URL => "https://api.sendgrid.com/v3/mail/send", 
            CURLOPT_RETURNTRANSFER => true, 
            CURLOPT_ENCODING => "", 
            CURLOPT_MAXREDIRS => 10, 
            CURLOPT_TIMEOUT => 0, 
            CURLOPT_FOLLOWLOCATION => true, 
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, 
            CURLOPT_CUSTOMREQUEST => "POST", 
            CURLOPT_POSTFIELDS => json_encode($data), 
            CURLOPT_HTTPHEADER => array( 
                "Authorization: Bearer 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", 
                "Content-Type: application/json" 
            ), 
        )); 
 
        $response = curl_exec($curl); 
 
        curl_close($curl); 
        return true; 
    } 
 
    /** 
     * @Route("/", name="home") 
     */ 
    public function index(Request $request): Response 
    { 
        $panels = $this->panelRepository->findBy(['free' => true]); 
        $images = Array(); 
        $default = "assets/images/panels/default.png"; 
        $type = $request->query->get("type"); 
        foreach($panels as $key=>$panel) 
        { 
            if (!empty($panel->getImages()[0]->getPath()) && !$panel->getImages()[0]->getMain()) 
                $images[$key] = $panel->getImages()[0]->getPath(); 
            else 
                $images[$key] = $default; 
        } 
        $typesPanel = $this->typePanelRepository->findAll(); 
        return $this->render('index.html.twig', [ 
            'panels' => $panels, 
            'typespanel' => $typesPanel, 
            'images' => $images, 
            'defaultimage' => $default 
        ]); 
    } 
 
     /** 
     * @Route("/list-panels/{type}", name="home_list_panels") 
     */ 
/*    public function listPanels(int $type): Response 
    { 
        $typesPanel = $this->typePanelRepository->findAll(); 
        $panels = $type === 0 ? $this->panelRepository->findBy(['free' => true]) : $this->typePanelRepository->find($type)->getPanels(); 
 
        $listPanels = []; 
        $images = []; 
        foreach ($panels as $key => $panel) { 
            $panelFormat['name'] = $panel->getName(); 
            $panelFormat['price'] = $panel->getPrice(); 
            $panelFormat['image'] = $panel->getImages()[0]->getPath(); 
            if (!$panelFormat['image'] || $panel->getImages()[0]->getMain()) 
                $panelFormat['image'] = 'assets/image/panels/default/png'; 
            $listPanels[] = $panelFormat; 
        } 
        return new JsonResponse($listPanels); 
    } */ 
 
     /** 
     * @Route("/contact", name="contact_page") 
     */ 
    public function contact(): Response 
    { 
        return $this->render('page-contact.html.twig'); 
    } 
 
     /** 
     * @Route("/contact/email", name="contact_page_email", methods={"POST"}) 
     */ 
 
    public function contactSendEmail(Request $request): Response 
    {  
        $from = $request->get('email'); 
        $to = $_ENV['MY_EMAIL']; 
        $name = $request->get('name'); 
        $msgSubject = ''; 
        $cmessage = $request->get('message'); 
        $mail = array( 
            "personalizations" => array( 
                array( 
                    "to" => array( 
                        array( 
                            "email" => $to 
                        ) 
                    ), 
                    "dynamic_template_data" => array( 
                        "mail" => $from, 
                        "nom" => $name, 
                        "sujet" => $msgSubject, 
                        "message" => $cmessage 
         
                    ), 
                    "subject" => $msgSubject 
                ) 
            ), 
            "from" => array( 
                "email" => "noreply@wecodx.com" 
            ), 
            "template_id" => "d-2ded55ea65d94ab984ecc72751425b16" 
        ); 
        $this->sendEmail($mail); 
        $this->addFlash( 
            'info', 
            'Votre e-mail a été envoyé !' 
        ); 
        return $this->redirectToRoute('contact_page'); 
    }   
 
 
 
    /** 
     * @Route("/listing-panels/", name="listing_panels_page") 
     */ 
    public function listPanels(Request $request): Response 
    { 
        $data = $request->query; 
        $typePanels = $this->typePanelRepository->findAll(); 
 
        $panels = $this->panelRepository->findAll(); 
        $maxPrice = 0; 
        $maxArea = 0; 
        $area = $data->get("area"); 
        foreach($panels as $panel) 
        { 
            if ($panel->getPrice() > $maxPrice) 
                $maxPrice = $panel->getPrice(); 
            if ($panel->getArea() > $maxArea) 
                $maxArea = $panel->getArea(); 
        } 
        $lowest = $data->get('lowest'); 
        if (!$lowest) 
            $lowest = 0; 
        $highest = $data->get('highest'); 
        if (!$highest) 
            $highest = $maxPrice; 
 
        $panels = $this->panelRepository->findSearch($data); 
        if (!$panels) 
            return $this->render('page-listing-v1.html.twig',[ 
                'error' => "Aucun résultat pour cette recherche", 
                'type_panels' => $typePanels, 
                'panels' => null, 
                'lowest' => $lowest, 
                'highest' => $highest, 
                'most_expensive' => $maxPrice, 
                'max_area' => $maxArea, 
                'area' => $area, 
                'name' => $data->get('name'), 
                'type' => $data->get('type') 
            ]); 
 
        $typePerPanel = Array(); 
        $images = Array(); 
        $default = "../assets/images/panels/default.png"; 
        foreach ($panels as $key => $panel) 
        { 
            if (!empty($panel->getImages()[0]->getPath()) && !$panel->getImages()[0]->getMain()) 
                $images[$key] = "../" . $panel->getImages()[0]->getPath(); 
            else 
                $images[$key] = $default; 
        } 
        return $this->render('page-listing-v1.html.twig',[ 
            'panels' => $panels, 
            'type_per_panel' => $typePerPanel, 
            'images' => $images, 
            'lowest' => $lowest, 
            'highest' => $highest, 
            'most_expensive' => $maxPrice, 
            'max_area' => $maxArea, 
            'area' => $area, 
            'type_panels' => $typePanels, 
            'name' => $data->get('name'), 
            'type' => $data->get('type') 
        ]); 
    } 
    /** 
     * @Route("/listing-panel-single/{id}", name="listing_panel_single_page") 
     */ 
    public function singlePanel(EntityManagerInterface $em, $id): Response 
    { 
        $panel = $this->panelRepository->find($id); 
 
        date_default_timezone_set('Europe/Paris'); 
        $date = new DateTime(); 
        $view = new HistoryViews(); 
        $view->setPanel($panel); 
        $view->setDate($date); 
        $em->persist($view); 
        $em->flush($view); 
 
        if ($panel->getTypePanel()) 
            $typePanel = $panel->getTypePanel()->getName(); 
        else 
            $typePanel = ""; 
 
        $images = []; 
        foreach ($panel->getImages() as $key => $imageInstance) 
        { 
            $images[] = $imageInstance->getPath(); 
            if (!empty($imageInstance->getPath()) && !$imageInstance->getMain()) 
                $images[$key] = "../" . $imageInstance->getPath(); 
            else 
                $images[$key] = "../assets/images/panels/default.png"; 
        } 
        $similars = []; 
        if ($panel->getTypePanel()) 
            $similars = $this->panelRepository->findAllInTypeExcept($panel->getTypePanel()->getId(), $panel->getId()); 
        $similarImages = []; 
        foreach($similars as $key => $similar) 
        { 
            if (!empty($similar->getImages()[0]->getPath()) && !$similar->getImages()[0]->getMain()) 
                $similarImages[$key] = "../" . $similar->getImages()[0]->getPath(); 
            else 
                $similarImages[$key] = "../assets/images/panels/default.png"; 
        } 
 
        return $this->render('page-listing-single-v1.html.twig',[ 
            'panel' => $panel, 
            'type_panel' => $typePanel, 
            'images' => $images, 
            'iframe' => $panel->getLinkGm(), 
            'similars' => $similars, 
            'similars_images' => $similarImages, 
        ]); 
    } 
    /** 
     * @Route("/listing-panel-single/{id}/email", name="listing_panel_single_page_email") 
     */ 
    public function singlePanelEmail($id, Request $request): Response 
    { 
        $panelName = $this->panelRepository->find($id)->getName(); 
        $from = $request->get('email'); 
        $to = $_ENV['MY_EMAIL']; 
        $name = $request->get('name'); 
        $msgSubject = 'Nouvelle demande concernant le panneau ' . $panelName; 
        $cmessage = $request->get('message'); 
        $mail = array( 
            "personalizations" => array( 
                array( 
                    "to" => array( 
                        array( 
                            "email" => $to 
                        ) 
                    ), 
                    "dynamic_template_data" => array( 
                        "mail" => $from, 
                        "nom" => $name, 
                        "sujet" => $msgSubject, 
                        "message" => $cmessage 
         
                    ), 
                    "subject" => $msgSubject 
                ) 
            ), 
            "from" => array( 
                "email" => "noreply@wecodx.com" 
            ), 
            "template_id" => "d-2ded55ea65d94ab984ecc72751425b16" 
        ); 
        $this->sendEmail($mail); 
        $this->addFlash( 
            'info', 
            'Votre e-mail a été envoyé !' 
        ); 
        return $this->redirect('/listing-panel-single/' . $id); 
    } 
 
    /** 
     * @Route("/about-us", name="about_us_page") 
     */ 
    public function aboutUs(): Response 
    { 
        return $this->render('page-agent-list.html.twig'); 
    } 
}