// validation start
// function validateInput(input) {
// 	var regex = /(\b(on\w+)=|javascript:|(<\s*)(\/*)script)/i;
// 	if (input.length > 100 || regex.test(input)) {
// 	  return false;
// 	}
// 	return true;
// }
//validation end
$(document).ready(function () {
    load();
  });
  function load(){
    $("#event_info_datable").load("module/ground_water_ex/event_info_entry_datatble.php");
}  

//upload start
function file_upload(fileInput, callback) {
    var files = fileInput.files;

    if (files.length == 0) {
        alert('Please choose a file to upload!');
        return false;
    }

    var fd = new FormData();
    fd.append('file', files[0]);

    $.ajax({
        url: 'module/common/upload.php',
        data: fd,
        contentType: false,
        processData: false,
        type: 'POST',
        success: function (image_id) {
            // Call the callback function with the image_id
            callback(image_id);
        }
    });
}

function show_project_list(){
  // alert("mm");
var upazila=$("#upazila").val();
var sub_division=$("#sub_division").val();
var dataStr="upazila="+upazila+"&sub_division="+sub_division;
// alert(dataStr);

$.ajax({
  url: "module/ground_water_ex/event_info_entry_datatble.php",
  type: "post",
  data: dataStr,
  cache: false,
  success: function (data) {
  // alert(data);
    $("#event_info_datable").html(data);
  }
});

}
function delete_litho_info(id){
  //alert('delete_litho_info');
  //alert(id)
;
  var operation = "delete";
  var datastr = "id=" + id + "&operation=" + operation;
  //alert(datastr);
  if (confirm("Are you sure you want to delete the selected row?")) {
    $.ajax({
      url: "module/ground_water_ex/event_info_entry_ajax.php",
      type: "post",
      data: datastr,
      success: function (str) {
      //alert(str);
        if (str !== 1) {
          alert("Data has been deleted successfully");
        } else {
          alert("Fail to delete data");
        }
        load(); // Reload or refresh the data
      },
    });
  }
}

function add_geotechncal_info() {
  $("#model_add_geotechnical_body").load("module/ground_water_ex/add_geotechnical_info.php");
}
    //upload end
    // function event_info_save(){
    //   var sub_division         =  $("#sub_division").val(); 
    //  var geotechnical_Id         =  $("#geotechnical_Id").val(); 
    //       var upz_code             =  $("#upz_code").val();

    // alert (upz_code);
    // }
function  event_info_save(){
       //alert("event_info_save");
       var project_name         =  encodeURIComponent($("#project_name").val());
       var site                 =  encodeURIComponent($("#site").val());
       var ps                   =  encodeURIComponent($("#ps").val());
       var location             =  encodeURIComponent($("#location").val());
       var hole_no              =  encodeURIComponent($("#hole_no").val());
       var drilled_by           =  encodeURIComponent($("#drilled_by").val());
       var upz_code             =  $("#upz_code").val();
       var cilent               =  encodeURIComponent($("#cilent").val());
       var date_started         =  $("#date_started").val();
       var date_completed       =  $("#date_completed").val();
       var unit                 =  encodeURIComponent($("#unit").val());
       var driller              =  encodeURIComponent($("#driller").val());
       var geologist            =  encodeURIComponent($("#geologist").val());
       var sampling_type        =  $("#sampling_type").val();
       var samplenos            =  encodeURIComponent($("#samplenos").val());
       var dimensions           =  encodeURIComponent($("#dimensions").val());
       var notoken              =  encodeURIComponent($("#notoken").val());
       var type                 =  encodeURIComponent($("#type").val());
       var weight_of_hammer     =  encodeURIComponent($("#weight_of_hammer").val());
       var avg_drop             =  encodeURIComponent($("#avg_drop").val());
       var dispatched_on        =  encodeURIComponent($("#dispatched_on").val());
       var how_sampled          =  $("#how_sampled").val();
       var remark               =  encodeURIComponent($("#remark").val());
       var size_cas             =  encodeURIComponent($("#size_cas").val());
       var length_used_cas      =  encodeURIComponent($("#length_used_cas").val());
       var wt_of_hammer_cas     =  encodeURIComponent($("#wt_of_hammer_cas").val());
       var avg_drop_cas         =  encodeURIComponent($("#avg_drop_cas").val());
       var sub_division         =  $("#sub_division_add").val();
       var geotechnical_Id      =  $("#geotechnical_Id").val(); 
       var opearation           =  "insert";
       //
       if (geotechnical_Id == ""){
        alert('Please Enter Geotechnical Id');
        $('#geotechnical_Id').focus();
        return false;
      }
       if (project_name == ""){
        alert('Please Enter Project Name');
        $('#project_name').focus();
        return false;
      } 
      if (sub_division == ""){
        alert('Please select Sub Division');
        $('#sub_division_add').focus();
        return false;
      }
      if (upz_code == ""){
        alert('Please select Upazila');
        $('#upz_code').focus();
        return false;
      }
      
      
      if (date_started == ""){
        alert('Please Enter Start Date');
        $('#date_started').focus();
        return false;
      } 
      if (date_completed == ""){
        alert('Please Enter Completed Date');
        $('#date_completed').focus();
        return false;
      } 

// For the first file input with id 'upload'
var firstFileInput = $('#upload')[0];
// For the second file input with id 'upload_signature'
var secondFileInput = $('#upload_signature')[0];
file_upload(secondFileInput, function(signature_id) {
file_upload(firstFileInput, function(image_id) {
	// file_upload(function (image_id) {
        var datastr              =   "project_name="+project_name+"&site="+site+"&ps="+ps+"&location="+location+
                                     "&hole_no="+hole_no+"&drilled_by="+drilled_by+"&cilent="
				     +cilent+"&sampling_type="+sampling_type+"&date_started="+date_started+"&date_completed="
				     +date_completed+"&unit="+unit+"&samplenos="+samplenos+"&dimensions="+dimensions+
				     "&notoken="+notoken+"&type="+type+"&driller="+driller+"&geologist="+geologist+
                                     "&how_sampled="+how_sampled+"&weight_of_hammer="+weight_of_hammer+"&avg_drop="+avg_drop+
				     "&dispatched_on="+dispatched_on+"&how_sampled="+how_sampled+"&size_cas="+size_cas+
                                     "&length_used_cas="+length_used_cas+"&wt_of_hammer_cas="+wt_of_hammer_cas+"&avg_drop_cas="+
				     avg_drop_cas+"&operation="+opearation+"&remark="+remark+"&image_id="+image_id+"&signature_id="+signature_id+"&sub_division="+sub_division+"&geotechnical_Id="+geotechnical_Id+"&upz_code="+upz_code;
      //  alert(datastr);
      //  exit();

      //  $('#cover-spin').show(0);
        $.ajax({
                url: "module/ground_water_ex/event_info_entry_ajax.php",
                type: "post",
                data: datastr,
                cache: false,
                success: function (data) {
                  //  alert(data);
                   if(data !==1){
                     if(data==0){
                      alert('Geotechnical Id Already Exists');
                      $('#geotechnical_Id').focus();
                      return false;
                     }else{
                      $('#add_geotechnical').modal('hide');
                      $('#cover-spin').hide(0);
                      alert('Data has been inserted successfully');
                      load();
                     }
                   }else{
                    alert("error")
                   }
                  
                }
              });
	});
});

}
// }
//view  modal start
function load_event_info_view_modal(id){
  //alert("load_event_info_view_modal");
  var datastr = "id="+id;
  $('#load_event_info_view_modal').load("module/ground_water_ex/event_info_entry_view.php",datastr);
}
//view modal end
//edit modal start
function load_event_info_edit_modal(id){
  var datastr = "id="+id;
	//alert(datastr);
	$("#model_event_edit_body").load("module/ground_water_ex/event_info_entry_edit.php",datastr);
}
//edit  modal end
//update event start
function update_event_info(){
 // alert(update_event_info);
	var operation                  =  "update";
var update_id                  = $('#update_id').val();
var e_site                     = encodeURIComponent($('#e_site').val());
var e_post_office              = encodeURIComponent($('#e_post_office').val());
var e_district_name            = $('#e_district_name').val();
var e_drilled_by               = encodeURIComponent($('#e_drilled_by').val());
var e_unit                     = encodeURIComponent($('#e_unit').val());
var e_sampling_type            = $('#e_sampling_type').val();
var e_sample_no                = encodeURIComponent($('#e_sample_no').val());
var e_dimension                = encodeURIComponent($('#e_dimension').val());
var e_no_token                 = encodeURIComponent($('#e_no_token').val());
var e_type                     = encodeURIComponent($('#e_type').val());
var e_hammer_weight            = encodeURIComponent($('#e_hammer_weight').val());
var e_avg_drop                 = encodeURIComponent($('#e_avg_drop').val());
var e_how_sampled              = $('#e_how_sampled').val();
var e_despatched_on            = encodeURIComponent($('#e_despatched_on').val());
var e_cashing_size             = encodeURIComponent($('#e_cashing_size').val());
var e_cashing_length_used      = encodeURIComponent($('#e_cashing_length_used').val());
var e_cashing_hammer_weight    = encodeURIComponent($('#e_cashing_hammer_weight').val());
var e_cashing_avg_drop         = encodeURIComponent($('#e_cashing_avg_drop').val());
var e_remarks                  = encodeURIComponent($('#e_remarks').val());
var e_project                  = encodeURIComponent($('#e_project').val());
var e_client                   = encodeURIComponent($('#e_client').val());
var e_driller                  = encodeURIComponent($('#e_driller').val());
var e_geologists               = encodeURIComponent($('#e_geologists').val());
var e_hole_no                  = $('#e_hole_no').val();
var e_location                 = encodeURIComponent($('#e_location').val());
var e_date_started             = $('#e_date_started').val();
var e_date_completed           = $('#e_date_completed').val();
var e_geotechnical_Id          = $('#e_geotechnical_Id').val();
var e_sub_division             = $('#e_sub_division').val();

var firstFile = $('#update_image')[0];
// For the second file input with id 'upload_signature'
var secondFile = $('#update_signature')[0];
  // Get existing image and signature IDs
  var existing_location_sketch_id = $('#existing_location_sketch_id').val();
  var existing_signature_id = $('#existing_signature_id').val();
  function submitData(image_id_update, signature_id_update) {

		var datastr = "operation="+operation+"&update_id="+update_id+"&e_site="+e_site+
		"&e_post_office="+e_post_office+"&e_district_name="+e_district_name+"&e_drilled_by="+e_drilled_by+"&e_unit="+e_unit+"&e_sampling_type="+e_sampling_type+"&e_sample_no="+e_sample_no+
    "&e_dimension="+e_dimension+"&e_no_token="+e_no_token+"&e_type="+e_type+"&e_hammer_weight="+e_hammer_weight+"&e_avg_drop="+ e_avg_drop+
    "& e_how_sampled="+e_how_sampled+"&e_despatched_on="+e_despatched_on+"&e_cashing_size="+e_cashing_size+"&e_cashing_length_used="+e_cashing_length_used+"&e_cashing_hammer_weight="+e_cashing_hammer_weight+"&e_cashing_avg_drop="+e_cashing_avg_drop+
    "&e_remarks="+e_remarks+"&e_project="+e_project+"&e_client="+e_client+"&e_driller="+e_driller+"&e_geologists="+e_geologists+"&e_hole_no="+e_hole_no+"&e_location="+e_location+"&e_date_started="+e_date_started+"&e_date_completed="+e_date_completed+"&signature_id_update="+signature_id_update+"&image_id_uppdate="+image_id_update+"&e_geotechnical_Id="+e_geotechnical_Id+"&e_sub_division="+e_sub_division;
//	alert(datastr);
	// $('#cover-spin').show(0);
  $('#cover-spin').show(0);
  $.ajax({
      url: "module/ground_water_ex/event_info_entry_ajax.php",
      type: "POST",
      data: datastr,
      success: function(data) {
        //alert(data);
          $('#cover-spin').hide();
          if (data == 1) {
              alert('Information has been updated successfully');
              $('#modal_event_edit').modal('hide');
              load(); // Reload or refresh the data
          } else {
              alert('Error occurred');
          }
      }
  });
}
if (secondFile.files.length > 0) {
// Upload the signature
file_upload(secondFile, function(signature_id_update) {
    // Use new signature ID or fallback to existing one
    signature_id_update = signature_id_update || existing_signature_id;
    //console.log("Signature uploaded with IDd:", signature_id_update);

    // Now check for location sketch upload
    if (firstFile.files.length > 0) {
        // Upload the location sketch
        //console.log("Uploading location sketch...");
        file_upload(firstFile, function(image_id_update) {
            // Use new image ID or fallback to existing one
            image_id_update = image_id_update || existing_location_sketch_id;
           // console.log("Location sketch uploaded with IDd:", image_id_update);

            // Proceed to submit the data with both IDs
            submitData(image_id_update, signature_id_update);
        });
    } else {
        // No new location sketch selected, use the existing one
       // console.log("No new location sketch selected. Using existing IDd:",existing_location_sketch_id);
        submitData(existing_location_sketch_id, signature_id_update);
    }
});
} else {
// No new signature selected, use the existing one
//console.log("No new signature selected. Using existing IDD:", existing_signature_id);

// Now check for location sketch upload
if (firstFile.files.length > 0) {
    // Upload the location sketch
    //console.log("Uploading location sketch");
    file_upload(firstFile, function(image_id_update) {
        // Use new image ID or fallback to existing one
        image_id_update = image_id_update || existing_location_sketch_id;
        //console.log("Location sketch uploaded with IDD:", image_id_update);

        // Proceed to submit the data with both IDs
        submitData(image_id_update, existing_signature_id);
    });
} else {
    // No new location  or signature selected, use the existing ones
    //console.log("No new location  or signature selected. Using existing IDs.");
    submitData(existing_location_sketch_id, existing_signature_id);
}
}


}
//update edit end
