<?php

$V_idavailability = $_SESSION['idavailability'];
$date_range_evaluations_min = $_SESSION['config'][array_search('DATE_RANGE_EVALUATIONS_MIN', array_column($_SESSION['config'], 'name'))][value];
$date_range_evaluations_max = $_SESSION['config'][array_search('DATE_RANGE_EVALUATIONS_MAX', array_column($_SESSION['config'], 'name'))][value];
// $V_now = strtotime(now());
$date = new DateTime(now());
$V_now = strtotime($date->format('d-m-Y H:i:s')."$date_range_evaluations_min days");
$V_now  = date("Y-m-d",$V_now)."T00:00:01";
$V_tot_day = $date_range_evaluations_max - $date_range_evaluations_min;




if ($_SESSION['language'] == 'Spanish') {
    $language = 'es-es'; // Spanish
		$range_row = 20;
		$formatDate = "dd/MM/yyyy HH:mm"; 
                $formatDatePHP = "d/m/Y H:i"; 
		$startTx = 'Inicio';
		$endTx = 'Fin';
} else {
    $language = 'en-us'; // English
		$range_row = 40;
		$formatDate = "MM/dd/yyyy HH:mm";  
                $formatDatePHP = "m/d/Y H:i"; 
		$startTx = 'Start';
		$endTx = 'End';
}

$strSQL = "SELECT `idavailability`, `media_idmedia`, `startDate`, `endDate` FROM availability where `idavailability` = $V_idavailability
";
$rsSQL = db_query($strSQL,$conn);
$data1 = db_fetch_array($rsSQL);
$V_idmedia = $data1['media_idmedia'];

$strSQL = "SELECT `idmedia`, `type_media_idtype_media`, `code` FROM media where idmedia = $V_idmedia
";
$rsSQL = db_query($strSQL,$conn);
$data2 = db_fetch_array($rsSQL);
$V_idtype_media = $data2['type_media_idtype_media'];

$strSQL = "SELECT `idtype_media`, `code`, `color`, `maxTimeReservation`, `startTimeDay`, `endTimeDay` FROM type_media where idtype_media = $V_idtype_media
";
$rsSQL = db_query($strSQL,$conn);
$data3 = db_fetch_array($rsSQL);
$V_idtype_media = $data2['type_media_idtype_media'];

$V_titleResource = $data3['code'].' -max:'.substr($data3['maxTimeReservation'],0,5).'<br>('.substr($data3['startTimeDay'],0,5).' - '.substr($data3['endTimeDay'],0,5).')';
$V_color = $data3['color'];

$V_event = '';
$strSQL = "SELECT `idavailable_block`, `availability_idavailability`, `media_idmedia`, `startDate`, `endDate` FROM available_block where availability_idavailability = $V_idavailability
";
$rsSQL = db_query($strSQL,$conn);
while ($data4 = db_fetch_array($rsSQL)) {
    $V_start = date($formatDatePHP,strtotime($data4['startDate']));
    $V_end = date($formatDatePHP,strtotime($data4['endDate']));
    $V_event .= "
{
      \"start\": \"".$data4['startDate']."\",
      \"end\": \"".$data4['endDate']."\",
      \"id\": \"".$data4['idavailable_block']."\",
      \"resource\": \"available\",
      \"text\": \"(".$V_start.' - '.$V_end.")\",
      \"barColor\": \"$V_color\"
    },";
}
if (strlen($V_event) <> 0) {
    $V_event = substr($V_event, 0, -1); // rest last comma
}





// ---------------------------------------------- Template of javascript ---------------------------------
$html = <<<EOT

<!-- daypilot libraries -->
<script src="daypilot/js/daypilot-all.min.js?v=2019.4.4160" type="text/javascript"></script>

<div id="dp"></div>

<script>
  var dp = new DayPilot.Scheduler("dp", {
    cellWidthSpec: "Fixed",
    cellWidth: $range_row,
    autoScroll: "Disabled",
    locale: "$language",
    timeHeaders: [{"groupBy":"Day"},{"groupBy":"Hour"}],
    scale: "Hour",
    startDate: "$V_now",
    days: $V_tot_day,
    // days: DayPilot.Date.today().daysInMonth(),
    // startDate: DayPilot.Date.today().firstDayOfMonth(),
    showNonBusiness: false,
    businessBeginsHour: 8,
    businessEndsHour: 21,
    eventHeight: 30,
    eventMovingStartEndEnabled: true,
    timeRangeSelectedHandling: "Disabled",
    eventMoveHandling: "Disabled",
    eventResizeHandling: "Disabled",
    eventDeleteHandling: "Disabled",
    eventClickHandling: "Disabled",
    eventHoverHandling: "Bubble",
    treeEnabled: false,
  });

// bubble, with async loading
dp.bubble = new DayPilot.Bubble({
    cssClassPrefix: "bubble_default",
    
    onLoad: function(args) {
        var ev = args.source;
        args.async = true;  // notify manually using .loaded()
        // simulating slow server-side load
        setTimeout(function() {
            args.html = "<div style='font-weight:bold'>" + ev.text() + "</div><div>$startTx: " + ev.start().toString("$formatDate ") + "</div><div>$endTx: " + ev.end().toString("$formatDate ") + "</div><div>Id: " + ev.id() + "</div>";
            args.loaded();
        }, 50);
    }
});
  
   dp.resources = [ { "name" : "$V_titleResource", "id" : "available" }]; 

   dp.events.list = [$V_event];
  dp.init();
</script>
EOT;

echo $html;

?>