$(document).ready(function(){
	//init_flot_chart();
		init_sparklines()
		$.ajax({
	        type: "get",
	        url: base_url + '/dashboard/get_events',
	        // data : formData,
	        processData: false,
	        contentType: false,
	        async: false,
	        success: function (data){
	        	init_calendar(data);
		    },
		    error: function(error){
	            var output = $.parseJSON(error.responseText);
	            console.log(output.errors);
	            $('#ipNotifyArea').removeClass('hidden');
	            $('#ipNotifyArea').addClass('alert alert-danger');
	            $.each(output.errors, function (key, value) {
	                //console.log(value[0]);
	                $('#ipNotifyArea').text(value[0]);
	            });
		    }
	    }); 
		
	if ($('#mybarChart').length ){ 
			  
	  	var ctx = document.getElementById("mybarChart");
	  	var mybarChart = new Chart(ctx, {
			type: 'bar',
			data: {
		  		labels: ["January", "February", "March", "April", "May", "June", "July"],
		  		datasets: [{
					label: '# of Votes',
					backgroundColor: "#26B99A",
					data: [51, 30, 40, 28, 92, 50, 45]
				}]
			},

			options: {
			  scales: {
				yAxes: [{
				  ticks: {
					beginAtZero: true
				  }
				}]
			  }
			}
	  });
	  
	} 


	function  init_calendar(theData) {
		//console.log(theData);
		$.each(theData, function (key, value) {
	        	//console.log(value);      
	        });
		if( typeof ($.fn.fullCalendar) === 'undefined'){ return; }
		// console.log('init_calendar');
			
		var date = new Date(),
			d = date.getDate(),
			m = date.getMonth(),
			y = date.getFullYear(),
			started,
			categoryClass;

		var calendar = $('#calendar').fullCalendar({
		  header: {
			left: 'prev,next today',
			center: 'title',
			right: 'month,agendaWeek,agendaDay,listMonth'
		  },
		  selectable: true,
		  selectHelper: true,
		  select: function(start, end, allDay) {
			$('#fc_create').click();

			started = start;
			ended = end;

			$(".antosubmit").on("click", function() {
			  var title = $("#title").val();
			  if (end) {
				ended = end;
			  }

			  categoryClass = $("#event_type").val();

			  if (title) {
				calendar.fullCalendar('renderEvent', {
					title: title,
					start: started,
					end: end,
					allDay: allDay
				  },
				  true // make the event "stick"
				);
			  }

			  $('#title').val('');

			  calendar.fullCalendar('unselect');

			  $('.antoclose').click();

			  return false;
			});
		  },
		  	eventClick: function(calEvent, jsEvent, view) {
			$('#fc_edit').click();
			$('#title2').val(calEvent.title);

			categoryClass = $("#event_type").val();

			$(".antosubmit2").on("click", function() {
			  calEvent.title = $("#title2").val();

			  calendar.fullCalendar('updateEvent', calEvent);
			  $('.antoclose2').click();
			});

			calendar.fullCalendar('unselect');
		  },
		  	editable: true,
		  	events: [
		  		{
					title: 'All Day Event',
					start: new Date(y, m, 1)
				}		
		  	]

		});
		
	};
	function gd(year, month, day) {
		return new Date(year, month - 1, day).getTime();
	}

	function init_flot_chart(){
			
			if( typeof ($.plot) === 'undefined'){ return; }
			
			console.log('init_flot_chart');
			
			var arr_data2 = [
			  [gd(2023, 10, 1), 82],
			  [gd(2023, 10, 2), 23],
			  [gd(2023, 10, 3), 66],
			  [gd(2023, 10, 4), 9],
			  [gd(2023, 10, 5), 119],
			  [gd(2023, 10, 6), 6],
			  [gd(2023, 10, 7), 9]
			];
			
			var chart_plot_02_data = [];
			// console.log(chart_plot_02_data.reverse()[0]);

			for (var i = 0; i < 30; i++) {
			  //console.log([new Date(Date.today().add(i).days()).getTime(), randNum() + i + i + 10]);
			  chart_plot_02_data.push([new Date(Date.today().add(i).days()).getTime(), randNum() + i + i + 10]);
			}
					
			
			
			var chart_plot_02_settings = {
				grid: {
					show: true,
					aboveData: true,
					color: "#3f3f3f",
					labelMargin: 10,
					axisMargin: 0,
					borderWidth: 0,
					borderColor: null,
					minBorderMargin: 5,
					clickable: true,
					hoverable: true,
					autoHighlight: true,
					mouseActiveRadius: 100
				},
				series: {
					lines: {
						show: true,
						fill: true,
						lineWidth: 2,
						steps: false
					},
					points: {
						show: true,
						radius: 4.5,
						symbol: "circle",
						lineWidth: 3.0
					}
				},
				legend: {
					position: "ne",
					margin: [0, -25],
					noColumns: 0,
					labelBoxBorderColor: null,
					labelFormatter: function(label, series) {
						return label + '&nbsp;&nbsp;';
					},
					width: 40,
					height: 1
				},
				colors: ['#96CA59', '#3F97EB', '#72c380', '#6f7a8a', '#f7cb38', '#5a8022', '#2c7282'],
				shadowSize: 0,
				tooltip: true,
				tooltipOpts: {
					content: "%s: %y.0",
					xDateFormat: "%d/%m",
				shifts: {
					x: -30,
					y: -50
				},
				defaultTheme: false
				},
				yaxis: {
					min: 0
				},
				xaxis: {
					mode: "time",
					minTickSize: [1, "day"],
					timeformat: "%d/%m/%y",
					min: chart_plot_02_data[0][0],
					//max: chart_plot_02_data.reverse()[0]
					max: chart_plot_02_data[20][0]
				}
			};	
		
			if ($("#chart_plot_02").length){
				console.log('Plot2');
				
				$.plot( $("#chart_plot_02"), 
				[{ 
					label: "Logins", 
					data: chart_plot_02_data, 
					lines: { 
						fillColor: "rgba(150, 202, 89, 0.12)" 
					}, 
					points: { 
						fillColor: "#fff" } 
				}], chart_plot_02_settings);
				
			}
		  
	}
	function init_sparklines() {
				
		if(typeof (jQuery.fn.sparkline) === 'undefined'){ return; }
		console.log('init_sparklines'); 
		
		
		$(".sparkline_dash").sparkline([2, 4, 3, 4, 5, 4, 5, 4, 3, 4, 5, 6, 4, 5, 6, 3, 5, 4, 5, 4, 5, 4, 3, 4, 5, 6, 7, 5, 4, 3, 5, 6], {
			type: 'bar',
			height: '125',
			barWidth: 13,
			colorMap: {
				'7': '#a1a1a1'
			},
			barSpacing: 2,
			barColor: '#26B99A'
		});
	};

});