<?php
 	include_once(__DIR__."/../../core.php");
	
	//Impostazione filtri di default a tutte le selezioni la prima volta
	if( !isset($_SESSION['dashboard']['idtecnici']) ){
		$rs = $dbo->fetchArray("SELECT an_anagrafiche.idanagrafica AS id FROM an_anagrafiche INNER JOIN (an_tipianagrafiche_anagrafiche INNER JOIN an_tipianagrafiche ON an_tipianagrafiche_anagrafiche.idtipoanagrafica=an_tipianagrafiche.idtipoanagrafica) ON an_anagrafiche.idanagrafica=an_tipianagrafiche_anagrafiche.idanagrafica WHERE deleted=0 AND descrizione='Tecnico'");
		
		$_SESSION['dashboard']['idtecnici'] = array("'-1'");
		
		for( $i=0; $i<sizeof($rs); $i++ ){
			array_push( $_SESSION['dashboard']['idtecnici'], "'".$rs[$i]['id']."'" );
		}
	}
	
	
	if( !isset($_SESSION['dashboard']['idstatiintervento']) ){
		$rs = $dbo->fetchArray("SELECT idstatointervento AS id, descrizione FROM in_statiintervento");
		
		$_SESSION['dashboard']['idstatiintervento'] = array("'-1'");
		
		for( $i=0; $i<sizeof($rs); $i++ ){
			array_push( $_SESSION['dashboard']['idstatiintervento'], "'".$rs[$i]['id']."'" );
		}
	}
	
	
	if( !isset($_SESSION['dashboard']['idtipiintervento']) ){
		$rs = $dbo->fetchArray("SELECT idtipointervento AS id, descrizione FROM in_tipiintervento");
		
		$_SESSION['dashboard']['idtipiintervento'] = array("'-1'");
		
		for( $i=0; $i<sizeof($rs); $i++ ){
			array_push( $_SESSION['dashboard']['idtipiintervento'], "'".$rs[$i]['id']."'" );
		}
	}
	
	
	if( !isset($_SESSION['dashboard']['idzone']) ){
		$rs = $dbo->fetchArray("SELECT id, descrizione FROM an_zone");
		
		$_SESSION['dashboard']['idzone'] = array("'-1'");
		
		for( $i=0; $i<sizeof($rs); $i++ ){
			array_push( $_SESSION['dashboard']['idzone'], "'".$rs[$i]['id']."'" );
		}
	}
?>
<link rel="stylesheet" href="<?php echo $css ?>/fullcalendar.min.css">
<script src="<?php echo $js ?>/fullcalendar.min.js"></script>
<script type="text/javascript">
	

$(document).ready(function() {

// comandi seleziona tutti
	$('#selectallstati').click(function(event) {  
		$(this).parent().parent().find('li input[type=checkbox]').each(function() { //loop through each checkbox
			this.checked = true; 
		});

		update_counter( 'idstati_count', $('#idstati_ul').find('input:checked').length );
		$('#calendar').fullCalendar('refetchEvents');
	});

	$('#selectalltipi').click(function(event) { 
		$(this).parent().parent().find('li input[type=checkbox]').each(function() { //loop through each checkbox
			this.checked = true; 
		});

		update_counter( 'idtipi_count', $('#idtipi_ul').find('input:checked').length );
		$('#calendar').fullCalendar('refetchEvents');
	});

	$('#selectalltecnici').click(function(event) { 
		$(this).parent().parent().find('li input[type=checkbox]').each(function() { //loop through each checkbox
			this.checked = true; 
		});

		update_counter( 'idtecnici_count', $('#idtecnici_ul').find('input:checked').length );
		$('#calendar').fullCalendar('refetchEvents');
	});

	$('#selectallzone').click(function(event) { 
		$(this).parent().parent().find('li input[type=checkbox]').each(function() { //loop through each checkbox
			this.checked = true; 
		});

		update_counter( 'idzone_count', $('#idzone_ul').find('input:checked').length );
		$('#calendar').fullCalendar('refetchEvents');
	});

// comandi deseleziona tutti
	$('#deselectallstati').click(function(event) { 
		$(this).parent().parent().find('li input[type=checkbox]').each(function() { //loop through each checkbox
			if( this.checked == true ) session_set_array( 'dashboard,idstatiintervento', this.value, 1 ); 
			this.checked = false; 
		});

		update_counter( 'idstati_count', $('#idstati_ul').find('input:checked').length );
		$('#calendar').fullCalendar('refetchEvents');
	});

	$('#deselectalltipi').click(function(event) { 
		$(this).parent().parent().find('li input[type=checkbox]').each(function() { //loop through each checkbox
			if( this.checked == true ) session_set_array( 'dashboard,idtipiintervento', this.value, 1 ); 
			this.checked = false; 
		});

		update_counter( 'idtipi_count', $('#idtipi_ul').find('input:checked').length );
		$('#calendar').fullCalendar('refetchEvents');
	});

	$('#deselectalltecnici').click(function(event) { 
		$(this).parent().parent().find('li input[type=checkbox]').each(function() { //loop through each checkbox
			if( this.checked == true ) session_set_array( 'dashboard,idtecnici', this.value, 1 ); 
			this.checked = false; 
		});

		update_counter( 'idtecnici_count', $('#idtecnici_ul').find('input:checked').length );
		$('#calendar').fullCalendar('refetchEvents');
	});

	$('#deselectallzone').click(function(event) { 
		$(this).parent().parent().find('li input[type=checkbox]').each(function() { //loop through each checkbox
			if( this.checked == true ) session_set_array( 'dashboard,idzone', this.value, 1 ); 
			this.checked = false;  
		});

		update_counter( 'idzone_count', $('#idzone_ul').find('input:checked').length );
		$('#calendar').fullCalendar('refetchEvents');
	});
});

</script>


<!-- Filtri -->
<div class="row">
	<?php
	$checks = '';
	$count = 0;
	$total = 0;
	
	$rs = $dbo->fetchArray("SELECT idstatointervento AS id, descrizione, colore FROM in_statiintervento ORDER BY descrizione ASC");
	$total = sizeof($rs);

	$allchecksstati = "";
	for( $i=0; $i<sizeof($rs); $i++ ){
		$attr = '';
		
		foreach( $_SESSION['dashboard']['idstatiintervento'] as $idx => $val ){
			if( $val == "'".$rs[$i]['id']."'" ){
				$attr = 'checked="checked"';
				$count++;
			}
		}

		$checks .= "<li><input type='checkbox' id='idstato_".$rs[$i]['id']."' value=\"".$rs[$i]['id']."\" ".$attr." onclick=\"session_set_array( 'dashboard,idstatiintervento', '".$rs[$i]['id']."' ); $('#calendar').fullCalendar('refetchEvents'); update_counter( 'idstati_count', $('#idstati_ul').find('input:checked').length );\"> <label for='idstato_".$rs[$i]['id']."'> <span class='badge' style=\"color:#333; background:".$rs[$i]['colore'].";\">".$rs[$i]['descrizione']."</span></label></li>\n";
		
		$allchecksstati .= "session_set_array( 'dashboard,idstatiintervento', '".$rs[$i]['id']."', 0 ); ";
	}
	
	if( $count == $total ){
		$class = "btn-success";
	}
	
	else if( $count == 0 ){
		$class = "btn-danger";
	}
	
	else{
		$class = "btn-warning";
	}
	
	if ($total==0)
	$class = "btn-primary disabled";
	?>
	
	<!-- STATI INTERVENTO -->
	<div class="dropdown col-md-3">
		<a class="btn <?php echo $class ?> btn-block" data-toggle="dropdown" href="javascript:;" id="idstati_count"><i class="fa fa-filter"></i> Stati intervento (<?php echo $count."/".$total ?>) <i class="caret"></i></a>
	
		<ul class="dropdown-menu" role="menu" id="idstati_ul">
			<?php echo $checks ?>
			<div class="btn-group pull-right">
				<button  id="selectallstati" onclick="<?php echo $allchecksstati; ?>" class="btn btn-primary btn-xs" type="button"><?php echo _("Tutti"); ?></button>
				<button id="deselectallstati" class="btn btn-danger btn-xs" type="button"><?php echo _("&times;"); ?></button>
			</div>
			
		</ul>
	</div>
	
	
	<!-- TIPI DI INTERVENTO -->
	<?php
	$checks = '';
	$count = 0;
	$total = 0;
	
	$rs = $dbo->fetchArray("SELECT idtipointervento AS id, descrizione FROM in_tipiintervento ORDER BY descrizione ASC");
	$total = sizeof($rs);

	$allcheckstipi = "";
	for( $i=0; $i<sizeof($rs); $i++ ){
		$attr = '';
		
		foreach( $_SESSION['dashboard']['idtipiintervento'] as $idx => $val ){
			if( $val == "'".$rs[$i]['id']."'" ){
				$attr = 'checked="checked"';
				$count++;
			}
		}

		$checks .= "<li><input type='checkbox' id='idtipo_".$rs[$i]['id']."' value=\"".$rs[$i]['id']."\" ".$attr." onclick=\"session_set_array( 'dashboard,idtipiintervento', '".$rs[$i]['id']."' ); $('#calendar').fullCalendar('refetchEvents'); update_counter( 'idtipi_count', $('#idtipi_ul').find('input:checked').length );\"> <label for='idtipo_".$rs[$i]['id']."'> ".$rs[$i]['descrizione']."</label></li>\n";
				
		$allcheckstipi .= "session_set_array( 'dashboard,idtipiintervento', '".$rs[$i]['id']."', 0 ); ";
		
	}
	
	if( $count == $total ){
		$class = "btn-success";
	}
	
	else if( $count == 0 ){
		$class = "btn-danger";
	}
	
	else{
		$class = "btn-warning";
	}
	
	if ($total==0)
		$class = "btn-primary disabled";
	?>
	
	<div class="dropdown col-md-3">
		<a class="btn <?php echo $class ?> btn-block" data-toggle="dropdown" href="javascript:;" id="idtipi_count"><i class="fa fa-filter"></i> Tipi intervento (<?php echo $count."/".$total ?>) <i class="caret"></i></a>
		
		<ul class="dropdown-menu" role="menu" id="idtipi_ul">
			<?php echo $checks ?>
			<div class="btn-group pull-right">
				<button  id="selectalltipi" onclick="<?php echo $allcheckstipi; ?>" class="btn btn-primary btn-xs" type="button"><?php echo _("Tutti"); ?></button>
				<button id="deselectalltipi" class="btn btn-danger btn-xs" type="button"><?php echo _("&times;"); ?></button>
			</div>
			
		</ul>
		
	</div>
	
	
	
	<!-- TECNICI -->
	<?php
	$checks = '';
	$count = 0;
	$total = 0;
	$totale_tecnici = 0; // conteggia tecnici eliminati e non
	
	$rs = $dbo->fetchArray("SELECT an_anagrafiche.idanagrafica AS id, ragione_sociale FROM an_anagrafiche INNER JOIN (an_tipianagrafiche_anagrafiche INNER JOIN an_tipianagrafiche ON an_tipianagrafiche_anagrafiche.idtipoanagrafica=an_tipianagrafiche.idtipoanagrafica) ON an_anagrafiche.idanagrafica=an_tipianagrafiche_anagrafiche.idanagrafica WHERE deleted=0 AND descrizione='Tecnico' ORDER BY ragione_sociale ASC");
	$total = sizeof($rs);

	$totale_tecnici += $total;

	$allchecktecnici = "";
	for( $i=0; $i<sizeof($rs); $i++ ){
		$attr = '';
		
		foreach( $_SESSION['dashboard']['idtecnici'] as $idx => $val ){
			if( $val == "'".$rs[$i]['id']."'" ){
				$attr = 'checked="checked"';
				$count++;
			}
		}

		$checks .= "<li><input type='checkbox' id='tech_".$rs[$i]['id']."' value=\"".$rs[$i]['id']."\" ".$attr." onclick=\"session_set_array( 'dashboard,idtecnici', '".$rs[$i]['id']."' ); $('#calendar').fullCalendar('refetchEvents'); update_counter( 'idtecnici_count', $('#idtecnici_ul').find('input:checked').length );\"> <label for='tech_".$rs[$i]['id']."'> ".$rs[$i]['ragione_sociale']."</label></li>\n";
		
		$allchecktecnici .= "session_set_array( 'dashboard,idtecnici', '".$rs[$i]['id']."', 0 ); ";
		
	}
	
	// TECNICI ELIMINATI
	$rs = $dbo->fetchArray("SELECT an_anagrafiche.idanagrafica AS id, ragione_sociale FROM an_anagrafiche INNER JOIN (an_tipianagrafiche_anagrafiche INNER JOIN an_tipianagrafiche ON an_tipianagrafiche_anagrafiche.idtipoanagrafica=an_tipianagrafiche.idtipoanagrafica) ON an_anagrafiche.idanagrafica=an_tipianagrafiche_anagrafiche.idanagrafica WHERE deleted=1 AND descrizione='Tecnico' ORDER BY ragione_sociale ASC");
	$total = sizeof($rs);

	$totale_tecnici += $total;

	if( $total>0 ){
		$checks .= "<li><hr>Tecnici eliminati:</li>\n";
		for( $i=0; $i<sizeof($rs); $i++ ){
			$attr = '';
			
			foreach( $_SESSION['dashboard']['idtecnici'] as $idx => $val ){
				if( $val == "'".$rs[$i]['id']."'" ){
					$attr = 'checked="checked"';
					$count++;
				}
			}

			$checks .= "<li><input type='checkbox' id='tech_".$rs[$i]['id']."' value=\"".$rs[$i]['id']."\" ".$attr." onclick=\"session_set_array( 'dashboard,idtecnici', '".$rs[$i]['id']."' ); $('#calendar').fullCalendar('refetchEvents'); update_counter( 'idtecnici_count', $('#idtecnici_ul').find('input:checked').length );\"> <label for='tech_".$rs[$i]['id']."'> ".$rs[$i]['ragione_sociale']."</label></li>\n";

			$allchecktecnici .= "session_set_array( 'dashboard,idtecnici', '".$rs[$i]['id']."', 0 ); ";
			
		} // end for
	} // end if


	if( $count == $totale_tecnici ){
		$class = "btn-success";
	}
	
	else if( $count == 0 ){
		$class = "btn-danger";
	}
	else{
		$class = "btn-warning";
	}
	
	if ($totale_tecnici==0)
		$class = "btn-primary disabled";
		
	?>
	<div class="dropdown col-md-3">
		<a class="btn <?php echo $class ?> btn-block" data-toggle="dropdown" href="javascript:;" id="idtecnici_count"><i class="fa fa-filter"></i> Operatore (<?php echo $count."/".$totale_tecnici ?>) <i class="caret"></i></a>
		
		<ul class="dropdown-menu" role="menu" id="idtecnici_ul">
			<?php echo $checks ?>
			<div class="btn-group pull-right">
				<button id="selectalltecnici" onclick="<?php echo $allchecktecnici; ?>" class="btn btn-primary btn-xs" type="button"><?php echo _("Tutti"); ?></button>
				<button id="deselectalltecnici" class="btn btn-danger btn-xs" type="button"><?php echo _("&times;"); ?></button>
			</div>
		</ul>
	</div>
	
	
	
</div>
<br>
	
	
<div id="calendar"></div>



<script type="text/javascript">
	$(document).ready(function() {
		create_calendar();
	});
	
	function create_calendar(){
		var calendar = $('#calendar').fullCalendar({
			monthNames: ['<?php echo _("Gennaio") ?>','<?php echo _("Febbraio") ?>','<?php echo _("Marzo") ?>','<?php echo _("Aprile") ?>','<?php echo _("Maggio") ?>','<?php echo _("Giugno") ?>','<?php echo _("Luglio") ?>','<?php echo _("Agosto") ?>','<?php echo _("Settembre") ?>','<?php echo _("Ottobre") ?>','<?php echo _("Novembre") ?>','<?php echo _("Dicembre") ?>'],
			monthNamesShort: ['<?php echo _("gen") ?>','<?php echo _("feb") ?>','<?php echo _("mar") ?>','<?php echo _("apr") ?>','<?php echo _("mag") ?>','<?php echo _("giu") ?>','<?php echo _("lug") ?>','<?php echo _("ago") ?>','<?php echo _("set") ?>','<?php echo _("ott") ?>','<?php echo _("nov") ?>','<?php echo _("dic") ?>'],
			dayNames: ['<?php echo _("Domenica") ?>','<?php echo _("Lunedì") ?>','<?php echo _("Martedì") ?>','<?php echo _("Mercoledì") ?>','<?php echo _("Giovedì") ?>','<?php echo _("Venerdì") ?>','<?php echo _("Sabato") ?>'],
			dayNamesShort: ['<?php echo _("Dom") ?>','<?php echo _("Lun") ?>','<?php echo _("Mar") ?>','<?php echo _("Mer") ?>','<?php echo _("Gio") ?>','<?php echo _("Ven") ?>','<?php echo _("Sab") ?>'],
			firstDay: 1,
			<?php if( get_var("Visualizzare la domenica sul calendario") == "0" ){ ?>
			hiddenDays: [ 0 ],
			<?php } ?>
			buttonText: {
				today: '<?php echo _("Oggi") ?>',
				day: '<?php echo _("Giorno") ?>',
				week:'<?php echo _("Settimana") ?>',
				month:'<?php echo _("Mese") ?>'
			},
			titleFormat: {
				month: "MMMM YYYY",
				week: "D MMMM  YYYY",
				day: "D MMMM YYYY"
			},
			columnFormat: {
				month: "ddd",
				week: "ddd D/M",
				day: "dddd D MMMM"
			},
			header: {
				left: 'prev,next today',
				center: 'title',
				right: 'month,agendaWeek,agendaDay'
			},
			timeFormat: 'H:mm\n',
			slotDuration: '00:15:00',
			<?php if( get_var("Vista dashboard") == "mese" ){  ?>
			defaultView: 'month',
			<?php }elseif( get_var("Vista dashboard") == "giorno" ){  ?>
			defaultView: 'agendaDay',
			<?php }else{ ?>
			defaultView: 'agendaWeek',
			<?php } ?>
			<?php if( get_var("Abilitare orario lavorativo") == "1" ){ ?>
			minTime: '08:00:00',
			maxTime: '20:00:00',
			<?php } ?>
			axisFormat: "H:mm",
			selectable: true,
			selectHelper: true,
			editable: true,
			eventLimit: false, // allow "more" link when too many events
			allDaySlot: false,
			select: function(start, end, allDay) {
				data = moment(start).format("YYYY-MM-DD");
				ora_dal = moment(start).format("HH:mm");
				ora_al = moment(end).format("HH:mm");
				
			//	<?php
			//	if( $modules_info[$module_name]["permessi"] == 'rw' ){
			//	?>
			//		launch_modal( 'Aggiungi intervento', '<?php echo $rootdir ?>/add.php?id_module=<?php echo $modules_info['Interventi']['id'] ?>&ref=dashboard&data='+data+'&orario_inizio='+ora_dal+'&orario_fine='+ora_al, 1 );
			//	<?php
			//	}
			//	?>
				
				$('#calendar').fullCalendar('unselect');
			},
			events: {
				url: "<?php echo $rootdir ?>/modules/dashboard/ajaxreq.php?op=get_current_month", 
				type: 'GET',
				error: function() {
					alert('Errore durante la creazione degli eventi.');
				}
			},
			eventResize: function(event,dayDelta,minuteDelta,revertFunc) {
				$.get("<?php echo $rootdir ?>/modules/dashboard/ajaxreq.php?op=update_intervento&id="+event.id+"&timeStart="+moment(event.start).format("YYYY-MM-DD HH:mm")+"&timeEnd="+moment(event.end).format("YYYY-MM-DD HH:mm"), function(data,response){
					if( response=="success" ){
						data = $.trim(data);
						if( data!="ok" ){
							alert( data );
							$('#calendar').fullCalendar('refetchEvents');
							revertFunc();
						}
						else{
							//$('#calendar').fullCalendar('refetchEvents');
							return false;
						}
					}
				});
			},
			eventDrop: function(event,dayDelta,minuteDelta,revertFunc) {
				$.get("<?php echo $rootdir ?>/modules/dashboard/ajaxreq.php?op=update_intervento&id="+event.id+"&timeStart="+moment(event.start).format("YYYY-MM-DD HH:mm")+"&timeEnd="+moment(event.end).format("YYYY-MM-DD HH:mm"), function(data,response){
					if( response=="success" ){
						data = $.trim(data);
						if( data!="ok" ){
							alert( data );
							$('#calendar').fullCalendar('refetchEvents');
							revertFunc();
						}
						else{
							//$('#calendar').fullCalendar('refetchEvents');
							return false;
						}
					}
				});
			},
			eventAfterRender : function(event, element) {
				//alert(element.find('.fc-event-title').html();
				element.find('.fc-title').html(event.title);
				<?php if( get_var("Utilizzare i tooltip sul calendario") == "1" ){ ?>
				$.get("<?php echo $rootdir ?>/modules/dashboard/ajaxreq.php?op=get_more_info&id="+event.id+"&timeStart="+moment(event.start).format("YYYY-MM-DD HH:mm")+"&timeEnd="+moment(event.end).format("YYYY-MM-DD HH:mm"), function(data,response){
					if( response=="success" ){
						data = $.trim(data);
						if( data!="ok" ){
							element.tooltipster({
								content: $(''+data+''),
								animation: 'grow',
								contentAsHTML: true,
								hideOnClick: true,
								onlyOne: true,
								speed: 200,
								delay: 100,
								maxWidth: 400,
								theme: 'tooltipster-shadow',
								touchDevices: true,
								trigger: 'hover',
								position: 'left'
							});
							
						}
						else{
							return false;
						}
					}
				});
				<?php } ?>
			}
		});
	}
</script>