function go_back(){
	load_ground_water_form();
	load_ground_water_list();
	
	$("#ground_water_datatable").show();
	$("#ground_water_form_data").show();
	//$("#ground_water_form_data").load("module/ground_water/ground_water_form.php");
	//$("#ground_water_datatable").load("module/ground_water/ground_water_datatable.php");
	$("#graph").hide();
	$("#ground_water_graph_datatable").hide();
	$("#go_back_button").css("display","none");
}
function back_button(){
	// load_sw_form();
	// load_sw_list();
	$("#ground_water_datatable").show();
	$("#ground_water_form_data").show();

	$("#graph").hide();
	$("#ground_water_graph_datatable").hide();
	$("#graphLabel").hide();
	$("#go_back_button").css("display","none");
}

function load_ground_water_form(){
	$("#ground_water_form_data").load("module/ground_water/ground_water_form.php");
}
function load_sw_summerry_form(){
	$('#sw_datatable').html("");
	// $('#sw_summery_data').load('module/surface_water_auto/sw_summery_form.php');
}
function load_sw_details_form(){
	$('#sw_datatable_summery').html("");
}

function load_hsub_div_by_hdiv(sel_hdiv_id){
	var datastr = "sel_hdiv_id="+sel_hdiv_id;
	// alert(datastr);
	$("#gws_sub_divs_holder").load("module/ground_water/hydro_sub_div_list_by_hdiv_gw.php",datastr);
}
function load_summary_hsub_div_by_hdiv(sel_hdiv_id){
	var datastr = "sel_hdiv_id="+sel_hdiv_id;
	$("#gws_sub_divs_holder_summary").load("module/ground_water/hydro_sub_div_list_by_hdiv_gw_sum.php",datastr);
}
function enable_days_input(sel_status_num){
	if(sel_status_num==4){
		$("#inactive_days_holder").show();
	}
	else{
		$("#inactive_days_holder").hide();
		$("#inactive_days").val("3");
	}
}
function enable_days_input_summary(sel_status_num){
	if(sel_status_num==4){
		$("#inactive_days_holder_summary").show();
	}
	else{
		$("#inactive_days_holder_summary").hide();
		$("#inactive_days_summary").val("3");
	}
}

function load_gw_details_form(){
	$('#ground_water_datatable_summary').html("");

}
function load_gw_summerry_form(){
	$('#ground_water_datatable').html("");

}
function load_ground_water_list(){
	//alert('hellow');
	$('#cover-spin').show();
	$.ajax({
		url: 'module/ground_water/ground_water_datatable.php',
		//type: 'post',
		//data: datastr,
		cache: false,
		success: function (result) {
			$('#cover-spin').hide();
			$("#ground_water_datatable").html(result);
		}
	});
	//$("#ground_water_datatable").load("module/ground_water/ground_water_datatable.php");
}

$('#division_wise').change(function(){
	$("#division_id").prop("disabled", false);
	$("#sub_division_id").prop("disabled", true);
	$("#district_id").prop("disabled", true);
	$("#sub_division_id").val("");
	$("#district_id").val("");
});

$('#sub_division_wise').change(function(){
	$("#sub_division_id").prop("disabled", false);
	$("#division_id").prop("disabled", true);
	$("#district_id").prop("disabled", true);
	$("#division_id").val("");
	$("#district_id").val("");
});

$('#district_wise').change(function(){
	$("#district_id").prop("disabled", false);
	$("#division_id").prop("disabled", true);
	$("#sub_division_id").prop("disabled", true);
	$("#sub_division_id").val("");
	$("#division_id").val("");
});

function load_edit_form(id){
	var dataStr = "id="+id;
	//alert(dataStr);
	$("#device_form_data").load("module/ground_water/device_form.php",dataStr);
	$("#device_name").focus();
}

function search_gwh(){
	// var operation = "search";
	var hydro_div = $("#division_id").val();
	var hydro_sub_div = $("#sub_division_id").val();
	var district = $("#district_id").val();
	var sw_status = $("#inactive_option").val();
	// form = 'groundWaterHourlyForm';
	// var dataStr = $('#'+form).serialize();
	var datastr = "hydro_div="+hydro_div+"&hydro_sub_div="+hydro_sub_div+"&district="+district+"&sw_status="+sw_status;
	if(sw_status==4){
		var inactive_days = $("#inactive_days").val();
		datastr = datastr+"&inactive_days="+inactive_days;
	}
	// var dataStr = "division_id="+division_id+"&sub_division_id="+sub_division_id+"&district_id="+district_id+"&inactive_option="+inactive_option+"&operation="+operation;
	// alert(datastr);
	// exit();
	$('#cover-spin').show();
	$.ajax({
		url: 'module/ground_water/ground_water_datatable.php',
		type: 'get',
		data: datastr,
		cache: false,
		success: function (result) {
			// alert(result);
			$('#cover-spin').hide();
			$("#ground_water_datatable").html(result);
		}
	});
}

function search_gwh_summary(){
	var hydro_div = $("#gw_division_id_summary").val();
	var hydro_sub_div = $("#sub_division_id_sum").val();
	var sw_status = $("#inactive_option_summary").val();
	var datastr = "hydro_div="+hydro_div+"&hydro_sub_div="+hydro_sub_div+"&sw_status="+sw_status;
	if(sw_status==4){
		var inactive_days = $("#inactive_days_summary").val();
		datastr = datastr+"&inactive_days="+inactive_days;
	}
	// alert(datastr);
	$('#cover-spin').show();
	$.ajax({
		url: 'module/ground_water/ground_water_datatable_summary.php',
		type: 'get',
		data: datastr,
		cache: false,
		success: function (result) {
			// alert(result);
			$('#cover-spin').hide();
			$("#ground_water_datatable_summary").html(result);
		}
	});

}

function print_gwh(){
	var hydro_div = $("#division_id").val();
	var hydro_sub_div = $("#sub_division_id").val();
	var district = $("#district_id").val();
	var sw_status = $("#inactive_option").val();
	var datastr = "hydro_div="+hydro_div+"&hydro_sub_div="+hydro_sub_div+"&district="+district+"&sw_status="+sw_status;
	if(sw_status==4){
		var inactive_days = $("#inactive_days").val();
		datastr = datastr+"&inactive_days="+inactive_days;
	}
	// alert(datastr);
	window.open("module/ground_water/gwh_datatable_pdf.php?"+datastr);
}

function download_gwh(){
	var hydro_div = $("#division_id").val();
	var hydro_sub_div = $("#sub_division_id").val();
	var district = $("#district_id").val();
	var sw_status = $("#inactive_option").val();
	var datastr = "hydro_div="+hydro_div+"&hydro_sub_div="+hydro_sub_div+"&district="+district+"&sw_status="+sw_status;
	if(sw_status==4){
		var inactive_days = $("#inactive_days").val();
		datastr = datastr+"&inactive_days="+inactive_days;
	}
	//alert(datastr);
	window.open("module/ground_water/gwh_datatable_excel.php?"+datastr);
}

function print_gwh_summary(){
	var hydro_div = $("#gw_division_id_summary").val();
	var hydro_sub_div = $("#sub_division_id_sum").val();
	var sw_status = $("#inactive_option_summary").val();
	var datastr = "hydro_div="+hydro_div+"&hydro_sub_div="+hydro_sub_div+"&sw_status="+sw_status;
	if(sw_status==4){
		var inactive_days = $("#inactive_days").val();
		datastr = datastr+"&inactive_days="+inactive_days;
	}
	// alert(datastr);
	window.open("module/ground_water/gwh_datatable_summary_pdf.php?"+datastr);
}


function show_graph(well_id,rl_id,last_rec_date){
	//alert('show_graph');
	$("#graph").show();
	$("#graphLabel").show();
	$("#ground_water_graph_datatable").show();
	$("#ground_water_datatable").hide();
	$("#ground_water_form_data").hide();
	$("#graphLabel").html("Ground Water Table Hydro Graph for Well "+well_id+"<button onclick='back_button()' class='btn btn-small btn-warning' style='float: right; margin-left: 10px; padding: 3px 10px;' >" + 
	"<i class='fas fa-arrow-left'></i> Back</button>");
	//alert(1);
	var dataA = "well_id="+well_id+"&rl_id="+rl_id+"&last_rec_date="+last_rec_date;
	// alert(dataA);
	$.ajax({
		type:"post",
		url:"module/ground_water/ground_water_hourly_graph.php",
		data:dataA,
		success:function(str){
			alert(str);
			// exit();
			$('#cover-spin').hide();
			//alert('356');
			//alert(str);
			var data_list_avalability = $.parseJSON(str);
		
			// alert(data_list_avalability);
			var date_input = data_list_avalability['date'];
			//alert(date_input);
			var waterlabel_data_input = data_list_avalability['waterlabel'];
			// alert(waterlabel_data_input);
			// console.log(waterlabel_data_input);
			// // var manu_data_input = data_list['manual'];
			// //alert(manu_data_input);
			var timeSeries =  date_input;
			//alert(timeSeries);
			//alert('546567');
			// Define individual datasets for each station
			var datasets = [
				{
					label: 'Water Table(m)',
					data: waterlabel_data_input,
					//borderColor: 'rgba(255, 99, 132, 1)',
					backgroundColor: 'rgba(0, 153, 255, 1)',
					borderColor: 'rgba(0, 153, 255, 1)',
					fill: false,
				},
			];

			var data = {
				labels: timeSeries,
				datasets: datasets,
			};

			var ctx = document.getElementById('lineChart').getContext('2d');
			var lineChart = new Chart(ctx, {
				type: 'line',
				data: data,
				options: {
					scales: {
							x: [{
								type: 'time',
								time: {
									unit: 'hour',
									displayFormats: {
										hour: 'MMM D, HH:mm',
									},
								},
								scaleLabel: {
									display: true,
									labelString: 'Date',
								},
								title: {
								  display: true,
								  text: 'Date-Time'
								},
							},
							
							],
							y: {
								scaleLabel: {
									display: true,
									labelString: 'Value',
								},
							},
						},
					},
					plugins: {
						title: {
							display: true,
							text: 'Multi-Line Chart with Common Time Series',
						},
					},
	
				});
			
			}
	});
	$("#ground_water_graph_datatable").load("module/ground_water/ground_water_graph_data_table.php",dataA);
}

//print graph strat

				// Function to download the chart as a JPEG image with a white background
				function download_gwa_availability_graph_chart() {
				   //alert('download_gwa_availability_graph_chart');
					var chartContainer = document.getElementById('lineChart');
					var canvas = document.createElement('canvas');
					canvas.width = chartContainer.offsetWidth;
					canvas.height = chartContainer.offsetHeight;
					//alert(chartContainer.offsetWidth);
					var ctx = canvas.getContext('2d');
					ctx.fillStyle = 'white';
					ctx.fillRect(0, 0, canvas.width, canvas.height);
					html2canvas(chartContainer).then(function(chartCanvas) {
						ctx.drawImage(chartCanvas, 0, 0);
						canvas.toBlob(function(blob) {
							var link = document.createElement('a');
							link.href = URL.createObjectURL(blob);
							link.download = 'Ground Water Availability Graph';
							link.click();
						});
					});
				}
				//print graph end






function show_graph_on_modal(well_id){
	//alert('show_graph_on_modal');
	$("#ground_water_datatable").hide();
	$("#ground_water_form_data").hide();
	//alert(1);
	var dataA = "well_id="+well_id;
	//alert(dataA);
	$.ajax({
		type:"post",
		url:"module/ground_water/ground_water_hourly_graph.php",
		data:dataA,
		success:function(str){
			//alert(str);
			if($.trim(str)){
				var day_data = $.parseJSON($.trim(str));
				var day_data = $.parseJSON($.trim(str));
				//alert(day_data[0]["period"]);
				if(day_data[0]["period"]==''){
					toastr.error ('No Data Available. Please Try Again Other Option.');
					$("#gwh_show_graph_body").html("");
				} else {
					$("#go_back_button").css("display","block");
					if($.trim(str)!=""){
						//var day_data = $.parseJSON($.trim(str));
						$("#gwh_show_graph_body").html("");
						Morris.Line({
							element: 'graph',
							data: day_data,
							xkey: 'period',
							ykeys: ['value'],
							labels: [ 'Value']
						});
						prettyPrint();
					}
				}		
			}	
		}
	});
	$("#ground_water_graph_datatable").load("module/ground_water/ground_water_graph_data_table.php",dataA);
}