function load_pending_list_holder(){
	//alert('load_pending_list_holder');
	var from_date = $("#from_date").val();
	var to_date = $("#to_date").val();
	var data_src = $("#data_src").val();
	var datastr = "from_date="+from_date+"&to_date="+to_date+"&data_src="+data_src;
	//alert(datastr);
	$('#cover-spin').show(0);
	$.ajax({
		type:'get' ,
		url:'module/mobiledatalog/ffwc_faulty_auto_data_holder.php',					 
		data:datastr,
		success:function(res){
		//	alert(res);
			$('#cover-spin').hide();
		    $('#pending_list_holder').html(res);
		}					
	});
}
function sms_list(){
	//alert(data_log);
	var from_date = $("#from_date").val();
	var to_date = $("#to_date").val();
	var mobile = $("#mobile").val();
	var data_src = $("#data_src").val();
	// alert(mobile);
	var datastr = "from_date="+from_date+"&to_date="+to_date+"&mobile="+mobile+"&data_src="+data_src;
 
	$('#cover-spin').show(0);
	$.ajax({
		type:"get",
		url:"module/mobiledatalog/ffwc_sms_list_datatable.php",
		data:datastr,
		success:function(st){
			//alert(st);
			$('#cover-spin').hide();
			$("#wrong_auto_sms_holder").html(st);
		}
	});
}
function load_auto_wrong_data(){
	//alert(data_log);
	var from_date = $("#from_date").val();
	var to_date = $("#to_date").val();
	var data_src = $("#data_src").val();
	var datastr = "from_date="+from_date+"&to_date="+to_date+"&data_src="+data_src;
 
	$('#cover-spin').show(0);
	$.ajax({
		type:"get",
		url:"module/mobiledatalog/hourly_water_level_datatable.php",
		data:datastr,
		success:function(st){
			//alert(st);
			$('#cover-spin').hide();
			$("#wrong_auto_data_holder").html(st);
		}
	});
}


function sms_list_load(){
	//alert('inside data load');
	$('#cover-spin').show(0);
	$.ajax({
		type:"get",
		url:"module/mobiledatalog/ffwc_sms_list_datatable.php",
		data: "",
		success:function(st){
			$('#cover-spin').hide();
			$("#sms_list").html(st);
		}
	});
}

function multiple_save() {
	//alert('multiple_save');
	var data_count = $("#data_count").val();
	//var vwl = $("#v_w_l").val();
	var chk_box = $("#chk_box").val();
	
	var data_list = [];
	for(let i=1; i<=data_count; i++) {
		var select_up = $("#chk_box_"+i+"").is(':checked') ? 1 : 0;
		var sel_id = $("#row_id_holder_"+i+"").val();
		var sel_vs = $("#vs_"+i+"").val();

		//alert(sel_info);
		if(select_up === 1) {
			var sel_info = sel_id+"#"+sel_vs;
			data_list.push(sel_info);
		}
	}
	//alert(data_list);
	if(data_list.length == 0) {
		//alert("Error");
		alert('Save Failed, Please select at least one checkbox for save.');
	} else {
		var operation = "update_sms_content";
		var datastr = "data_list="+data_list+"&operation="+operation; 
		//alert(datastr);
		$.ajax({
			url: 'module/mobiledatalog/ffwc_mobile_data_log_ajax.php',
			type: 'post',
			data: datastr,
			cache: false,
			success: function(result) {
				//alert(result);
				var alrt_msg = result+" data has been updated";
				alert(alrt_msg);
				jQuery('input[type=checkbox]').each(function() {
					if(jQuery('#'+this.id).prop("checked")) {
						jQuery('#box'+this.id).remove();
					}
				});
				load_auto_wrong_data();
				$('#wrong_auto_sms_tab').html("Incorrect SMS List(0)");
				$('.nav-tabs a[href="#wrong_auto_data"]').tab('show');
			}
		});
	}
}

function delete_multiple_data() {
	var data_count = $("#data_count").val();
	var chk_box = $("#chk_box").val();
	if(confirm('Deleting the selected SMS')){
		var data_list = [];
		for(let i=1; i<=data_count; i++) {
			var select_up = $("#chk_box_"+i+"").is(':checked') ? 1 : 0;
			var sel_id = $("#row_id_holder_"+i+"").val();
			
			if(select_up === 1) {
				data_list.push(sel_id);
			}
		}
		//alert(data_list);
		if(data_list.length == 0) {
			toastr.error('Save Failed, Please select at least one checkbox for save.');
		} else {
			var operation = "delete_raw_sms";
			var datastr = "data_list="+data_list+"&operation="+operation; 
			//alert(datastr);
			$.ajax({
				url: 'module/mobiledatalog/ffwc_mobile_data_log_ajax.php',
				type: 'post',
				data: datastr,
				cache: false,
				success: function(result) {
				//	alert(result);
					var alrt_msg = result+" data has been updated";
					toastr.success(alrt_msg);
					jQuery('input[type=checkbox]').each(function() {
						if(jQuery('#'+this.id).prop("checked")) {
							jQuery('#box'+this.id).remove();
						}
					});
					sms_list(); 
				}
			});
		}
	}
}

function show_raw_data(sel_raw_id){
	var datastr = "sel_raw_id="+sel_raw_id;
	//alert(datastr);
	$("#modal_body_show_raw_data").load("module/mobiledatalog/ffwc_show_raw_data.php",datastr);
}


function multiple_data_save() {
	var chk_box = $(".data_chk_box").map(function () {
        return $(this).is(':checked') ? 1 : 0;
    }).get().join("#");
	
	var row_id = $(".row_id_holder").map(function () {
        return $(this).val();
    }).get().join("#");
	
	/*var status_holder = $(".status_holder").map(function () {
        return $(this).val();
    }).get().join("#");*/
	
	var well_id = $(".well_id").map(function () {
        return $(this).val();
    }).get().join("#");
	
	var v_date = $(".v_date").map(function () {
        return $(this).val();
    }).get().join("#");
	
	var vwl_hr6 = $(".vwl_hr6").map(function () {
        return $(this).val();
    }).get().join("#");
	
	var vwl_hr9 = $(".vwl_hr9").map(function () {
        return $(this).val();
    }).get().join("#");
	
	var vwl_hr12 = $(".vwl_hr12").map(function () {
        return $(this).val();
    }).get().join("#");
	
	var vwl_hr15 = $(".vwl_hr15").map(function () {
        return $(this).val();
    }).get().join("#");
	
	var vwl_hr18 = $(".vwl_hr18").map(function () {
        return $(this).val();
    }).get().join("#");
	
	var vwl_rf = $(".vwl_rf").map(function () {
        return $(this).val();
    }).get().join("#");
	
	var selected = "1";
	var is_selected = chk_box.indexOf(selected);
	//alert(is_selected);
	if(is_selected<0){
		alert('Save Failed, Please select at least one checkbox for save.');
	}
	else{
		var operation = "approve_multiple_three_hourly_water_level";
		var datastr = "chk_box="+chk_box+"&row_id="+row_id+
			"&well_id="+well_id+"&v_date="+v_date+
			"&vwl_hr6="+vwl_hr6+"&vwl_hr9="+vwl_hr9+"&vwl_hr12="+vwl_hr12+
			"&vwl_hr15="+vwl_hr15+"&vwl_hr18="+vwl_hr18+"&vwl_rf="+vwl_rf+
			"&operation="+operation;
		//alert(datastr);
		$('#cover-spin').show(0);
		$.ajax({
			url: 'module/mobiledatalog/hourly_water_level_log_ajax.php',
			type: 'post',
			data: datastr,
			cache: false,
			success: function(result) {
				//alert(result);
				var alrt_msg = "";
				if(result==1){
					alrt_msg = "Data has been approved!";
					alert(alrt_msg);
					//alert(alrt_msg);
				}
				else{
					console.log(result);
					alrt_msg = "error occurred during data verification";
					alert(alrt_msg);
					//alert(alrt_msg);
				}
				$('#cover-spin').hide();
				jQuery('input[type=checkbox]').each(function() {
					if(jQuery('#'+this.id).prop("checked")) {
						jQuery('#data_chk_box'+this.id).remove();
					}
				});
				load_auto_wrong_data();
			}
		});
	}
}


function reject_log_data() {
	var chk_box = $(".data_chk_box").map(function () {
        return $(this).is(':checked') ? 1 : 0;
    }).get().join("#");
	
	var row_id = $(".row_id_holder").map(function () {
        return $(this).val();
    }).get().join("#");
	
	var selected = "1";
	var is_selected = chk_box.indexOf(selected);
	//alert(is_selected);
	if(is_selected<0){
		alert('Save Failed, Please select at least one checkbox for save.');
	}
	else {
		var operation = "reject_multiple_three_hourly_water_level";
		var datastr = "chk_box="+chk_box+"&row_id="+row_id+"&operation="+operation; 
		//alert(datastr);
		$('#cover-spin').show(0);
		$.ajax({
			type:"post" ,
			url:"module/mobiledatalog/hourly_water_level_log_ajax.php" ,
			data:datastr ,
			cache:false ,
			success:function(st){
				$('#cover-spin').hide();
				//alert(st);
				var alrt_msg = "";
				if(st==1){
					alrt_msg = "data has been rejected";
					alert(alrt_msg);
				}
				else{
					alrt_msg = "error occurred during data rejection";
					alert(alrt_msg);
				}
				jQuery('input[type=checkbox]').each(function() {
					if(jQuery('#'+this.id).prop("checked")) {
						jQuery('#chk_box_'+this.id).remove();
					}
				});
				load_auto_wrong_data();
			}
		});
	}
}

function show_raw_data_ffwc(sel_row_id){
	//alert('show_raw_data_ffwc');
	var datastr = "sel_row_id="+sel_row_id;
	//alert(datastr);
	$("#modal_body_show_raw_data_ffwc").load("module/mobiledatalog/show_raw_data_ffwc.php",datastr);
}

function load_ffwc_wl_graph(id){
	//alert('load_ffwc_wl_graph');
	var well_id = $("#well_id_"+id+"").val();
	var from_date = $("#grph_ffwc_from_date").val();
	var to_date = $("#grph_ffwc_to_date").val();
	var data_status = $("#grph_ffwc_status").val();
	
	var datastr = "well_id="+well_id+"&from_date="+from_date+"&to_date="+to_date+"&data_status="+data_status;
//	alert(datastr);
	$('#ffwc_wl_graph_holder').load('module/mobiledatalog/ffwc_sms_wl_graph_holder.php',datastr);
}

function load_ffwc_rf_graph(id){
//	alert('load_ffwc_rf_graph');
	var well_id = $("#well_id_"+id+"").val();
	var from_date = $("#grph_ffwc_from_date").val();
	var to_date = $("#grph_ffwc_to_date").val();
	var data_status = $("#grph_ffwc_status").val();
	
	var datastr = "well_id="+well_id+"&from_date="+from_date+"&to_date="+to_date+"&data_status="+data_status;
	//alert(datastr);
	$('#ffwc_rf_graph_holder').load('module/mobiledatalog/ffwc_sms_rf_graph_holder.php',datastr);
}
function show_graph_ffwc(id){
    var well_id = $("#well_id_"+id+"").val();
	var from_date = $("#ffwc_wl_from_date").val();
	var to_date = $("#ffwc_wl_to_date").val();


	var datastr = "well_id="+well_id+"&from_date="+from_date+"&to_date="+to_date;
//	alert(datastr);
	$('#cover-spin').show(0);
	$.ajax({
		type:"post",
		url:"module/mobiledatalog/show_graph_ffwc.php",
		data:datastr,
		success: function (str){
            //alert(str);
			if($.trim(str)){
				$('#cover-spin').hide();
				var mdl_data = $.parseJSON(str);
				//mdl_data;
				google.charts.load('current',{packages:['corechart']});
				google.charts.setOnLoadCallback(drawChart);
				console.log(mdl_data);
				function drawChart(){
                    //alert("Hlw");
					var mobile_data = new google.visualization.DataTable();
					mobile_data.addColumn('string','Date');
					mobile_data.addColumn('number','Water Label');
					
					var k = 0;
					var l = 1;
					for(i=0;i<mdl_data.length;i++){
                    //alert("first for") ;  
					 k=0;
                     l=1;
                        for(j=0; j<5; j++){
                            mobile_data.addRow([mdl_data[i][k],parseFloat(mdl_data[i][l])]);
                            k = k+2;
                            l = l+2;
                        }
                    }
               
                
					
					var options = {
						title: 'FFWC Mobile Log Data Graph',
						width: 1700,
						height: 500,
						pointSize: 2.5,
						pointShape: 'circle',
						hAxis: {
						title: 'Date'
						},
						vAxis: {
							title: 'Water Level',
							format: '0.00',
							gridlines: {
								count: 20
							}
						},
						colors: ['#0039e6', '#ff3300'],
					/* crosshair: {
						color: '#0340',
						trigger: 'selection'
						}*/
						
					};
					var chart= new google.visualization.LineChart(document.getElementById('ffwc_mobile_data_chart'));
					chart.draw(mobile_data,options);
				}
			}
		}
	});
}