<?php
include "../config.php";
$my_date = new DateTime();
$mes = date("F");
$year =  date("Y");
$my_date->modify('first day of '.$mes.''.$year);
$firstDay = 
$my_date->modify('last day of '.$mes.''.$year);
$lastDay = $my_date->format('Y-m-d');

$start=  explode('T',  $_GET['start'])[0];
$end=explode('T',  $_GET['end'])[0];;
//$con = mysqli_connect($servername,$username,$password);
$return_arr = array();
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass );

$sql = $conn->prepare("SELECT n_reg AS id ,  Coalesce(NULLIF(fmuncto,''),NULLIF(fnumcto,''),NULLIF(cuarto_seguro,'')) AS resourceId,cancelado,

 CONCAT(fnombre,' ',fapellido) AS a , ffecllegada AS start , ffecsalida AS end ,
  status FROM valeryus19 "
        . "WHERE  ((ffecllegada >= '$start' AND ffecsalida <= '$end')"
        . " or (ffecsalida >= '$start' AND ffecsalida <= '$end') " 
        . "or (ffecllegada <= '$end' AND ffecsalida >= '$start') ) and cancelado=0 and extra1=0");
 $sql->execute();
if (!$sql) {
    echo "\nPDO::errorInfo():\n";
    print_r($sql->errorInfo());
}
$resultado = $sql->fetchAll(PDO::FETCH_ASSOC);
//echo json_encode($resultado); 
foreach ($resultado as $key => $row) {
    $row_array['id'] = $row['id'];
    $row_array['resourceId'] = $row['resourceId'];
    $nom = utf8_encode($row["a"]);
    $row_array['title'] =$row['id']."-".$nom;
    $row_array['start'] = $row['start'];
     $row_array['status'] = $row['status'];
    $row_array['end'] = $row['end'];
    
    if($row["status"] == "HISTORICO"){
        $row_array['color'] = "#58D68D";
    }elseif($row["status"] == "RESERVA GRUPO"){
         $row_array['color'] = "#FFC300";
    }elseif($row["status"] == "RESERVA INDIVIDUAL"){
        $row_array['color'] = "#FF5733";
    }else{
        $row_array['color'] = "#900C3F";
    }
    
    array_push($return_arr, $row_array);
}
echo json_encode($return_arr);
?>
