<?php
/**
 * Contrôleur frontal / Page partir-en-vacances

 * =========================================================================================================
 *
 * Contrôleur frontal / Page partir-en-vacances
 * 
 * @author          Monagence/Jamal Arbib (jamal.arbib@gmail.com)
 * @started         26/02/2024
 * @updated         
 * @project         Refonte de monagence.com/
 * @namefile        index.php
 * @fileversion     1.0.0
 * @phpversion      8.2.0
 * @htmlversion     HTML5
 * @cssversion      CSS3
 * @jqueryversion   3.6.3
 * @Bootstraap       5.2.3
 * @copyright       2024 Monagence/Jamal Arbib
 * @license         no license - free to use - just link to my github - Thanks :)
 * @mygithub        https://github.com/ 
 */

require __DIR__ .'/../app/config/config.php';

//search products
require __DIR__ .'/../src/searchProducts/SearchProducts.php';
require __DIR__ .'/../src/extractionData/ExtractionData.php';

//get type, theme, country label
require __DIR__ .'/../src/webservice/WebService.php';
// complementaty links
require __DIR__ .'/../src/moteur/SearchEngineField.php';

//filtre
require __DIR__ .'/../src/moteur/SearchEngineFilters.php';

require __DIR__ .'/../src/utils/MyDate.php';
// return $moteurRecherche
require __DIR__ .'/../src/moteur/MoteurRecherche.php';

$month = (empty($_GET['mois'])) ? '01' : $_GET['mois'];
// si le mois est revolu > annnée prochaine
$year  = (  $month >= date('m')) ? date('Y') : date('Y')+1; 

$countryCode = isset($_GET['countryCode']) ? $_GET['countryCode'] : 0; 

$allMonths = [
    '01' =>array('janvier'=>'Janvier'),
    '02' =>array('fevrier'=>'Février'),
    '03' =>array('mars'=>'Mars'),
    '04' =>array('avril'=>'Avril'),
    '05' =>array('mai'=>'Mai'),
    '06' =>array('juin'=>'Juin'),
    '07' =>array('juillet'=>'Juillet'),
    '08' =>array('aout'=>'Août'),
    '09' =>array('septembre'=>'Septembre'),
    '10' =>array('octobre'=>'Octobre'),
    '11' =>array('novembre'=>'Novembre'),
    '12' =>array('decembre'=>'Décembre')
  ];

 $SEOContent='';

// le mois courant
if ($month)
{
  $currentMonthLabel = Null;
  foreach ($allMonths[$month] as $key => $value) {
     $currentMonthLabel = $value;
     $currentMonthCode = $key;
  }

  // Charger le contenu du fichier JSON
   $json = file_get_contents('../src/index/ou-partir-mois.json');
   // Le convertir en tableau PHP
   $data = json_decode($json, true);
   foreach ($data as $mois => $info) {
       if ($mois == $month ){
           $SEOContent = $info['seo_long'];   
      }
   }  
}

//echo  MyDate::translateMonth($now->format('F'));

$depDateMonth=$year.'-'.$month;

$nbrResults   = 21;
$urlPath = 'partir-en-vacances'; // ne pas mettre le '/' au debut et à la fin

// recheche principale : periode par mois
$query = [
 'depDateMonth'=>$depDateMonth,
 'country'		=> $countryCode,
 'firstIndex'	=> $firstIndex,
 'nbrResults'	=> $nbrResults
];

$produits 	 = [];
$xmlResponse = SearchProducts::getProductsByDate($query) ;
$produits    = ExtractionData::getProducts($xmlResponse);

// recheche principale : periode par mois
$query = [];
$query = ['depDateMonth'=>$depDateMonth];

//$prepo='';
//$countryCode='';
$searchFilters = SearchEngineFilters::getFilters($query);

$destination='';

//Gestion de la pagination 
$page 		=  isset($_GET['page']) ? $_GET['page'] : 1;
$paginationPath='/'.$urlPath.'/?page=';
// page canical
$canonicalPage = '/'.$urlPath.'/';


if( isset($_GET['mois']) )
{
    $canonicalPage = '/'.$urlPath.'/mois-'.$currentMonthCode.'/vacances-'.$currentMonthCode.'/'.$month;
    $paginationPath = '/'.$urlPath.'/?mois='.$_GET['mois'].'&page=';

    if( isset($_GET['countryCode']) )
    {
      
      $ws= new WebService();
      $destination    = $ws->getLabel("critValue", $_GET['countryCode'].";dest");
      $canonicalPage = '/'.$urlPath.'/'.$currentMonthCode.'/'.strtolower($destination).'/'.$_GET['countryCode'].'-'.$month;
      $paginationPath = '/'.$urlPath.'/?mois='.$_GET['mois'].'&countryCode='.$_GET['countryCode'].'&page=';

      $SEOContent = "Découvrez nos offres de voyages pour partir en ".$currentMonthLabel." ".$year.' '.$preposition. ' '.$destination." . Réservez vos vacances parmi plus de " .$produits['total']." offres de voyages et profitez des prix pas chers. Vous avez l'embarras du choix, entre les séjours, les circuits, les combinés et les autotours."; 
    }

}

// meta tag pour le SEO
$titlePage = "Où partir en ".$currentMonthLabel.' '.$year.' ? Découvrez nos offres de voyages '.$preposition. ' '.$destination;
$description = "Découvrez nos idées de voyages ". $destination." pour ".$currentMonthLabel." ".$year." : soleil, nature, aventure ou détente. Résevez votre prochaine voyage à partir de ".$produits['0']['price']."€";
$h1 = "Où partir en ".$currentMonthLabel.' '.$year.' ? Découvrez nos offres de voyages '.$preposition. ' '.$destination;

$meta=[   
  "title"      => $titlePage,
  "description"=> $description,       
  "h1"         => $h1,
  "canonical"  => $canonicalPage,
];

// image de fond
$bgpage = "special/voyages-incontournables"; // valeur par défaut

switch ($month) {
    case '12':
    case '01':
    case '02':
        $bgpage = "vacances-scolaires/vacances-hiver";
        break;
    case '03':
    case '04':
    case '05':
        $bgpage = "vacances-scolaires/voyage-ete"; // printemps/avant-été
        break;
    case '06':
    case '07':
    case '08':
        $bgpage = "vacances-scolaires/vacances-ete";
        break;
    case '09':
    case '10':
    case '11':
    $bgpage = "vacances-scolaires/vacances-scolaires";
        break;

}


//  passage de variables au mteur de rendu TWIG
echo $twig->render('listeProduits/listeProduitsParDate.html.twig', 
	[
	'page' 	=> 'mois',
	'meta'   => $meta,
    'bgpage' =>$bgpage ,
    'SEOContent'=>$SEOContent,
	'produits'=>$produits,
    'searchFilters'=>$searchFilters,
    'prepo'=>$preposition,
	'currentMonth'=>$month,
    'allMonths'=>$allMonths,
    'currentMonthLabel'=> $currentMonthLabel,
    'year'=>$year,
	'path'=>$urlPath,
	'countryCode'=>$countryCode,
	'countryLabel'=>$destination,
	'moteurRecherche'=>$moteurRecherche,
    'currentPage'=>$page,
    'paginationPath'=> $paginationPath,
    'lastPage'=> ceil($produits['total']/$nbrResults),
    'showAlwaysFirstAndLast'=> false,
	]
 );

?>