$(document).ready(function(){
  $(document).ajaxStart(function(){
    $("#wait").css("display", "block");
  });
  $(document).ajaxComplete(function(){
    $("#wait").css("display", "none");
  });
  
  var urlBase = "http://0.0.0.0:30001";
  // var urlBase = "http://10.0.2.2:30001";

  $("#uploooooad").click(function(){
    console.log("10");
    var uploadFile = urlBase+"/uploadFile";
   var filea = document.getElementById("filepath").files[0]
    console.log(filea)
  
  $.ajax({
    method: 'POST',
    url: uploadFile,
    data: { key: filea }, // sends fields with filename mimetype etc
    // data: aFiles[0], // optional just sends the binary
    processData: false, // don't let jquery process the data
    contentType: false, // let xhr set the content type
    
   // crossDomain:true,
    //data: { key: filea },
    success: function(data, status, jqXHR){
  
       console.log("sucsess");
  
    },
    error: function(xhr, textStatus, error){
        console.log(xhr.statusText);
        console.log(textStatus);
        console.log(error);
        alert("Error ipfs:"+ xhr.responseText);
    }
  });
  
  
  })
  $("#add-client").on('shown.bs.modal', function () {
    $("#clientPhone").keypress(function (e) {
      console.log("pressed")
      //if the letter is not digit then display error and don't type anything
    if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
       //display error message
       $("#clientPhoneError").html("Digits Only").show().fadeOut("slow");
          return false;
     }
    });

    $("#addClient").click(function(){
      console.log("3")
      console.log($('#clientEmail').val());
      console.log("in modal")
      var addNewUserUrl = urlBase+"/addClient";
      var formData = {
        firstName: $('#clientFirstName').val(),
        lastName: $('#clientLastName').val(),
        phoneNumber:$('#clientPhone').val(),
        email: $('#clientEmail').val(),
      }
      console.log(formData);
      console.log(addNewUserUrl);
      $.ajax({
        type: 'POST',
        url: addNewUserUrl,
        data: formData,
        success: function(data, status, jqXHR){
          if(status==='success'){
           console.log("successfully add user  in blockchain");
            alert("successfully add user  in blockchain");
         }
        },
        error: function(xhr, textStatus, error){
         console.log("error add project  in blockchain");
    
            console.log(xhr.statusText);
            console.log(textStatus);
            console.log(error);
            alert("Error while adding user: "+ xhr.responseText);
        }
      });
    })
});






$("#add-employee").on('shown.bs.modal', function () {
  $("#employeePhone").keypress(function (e) {
    console.log("pressed")
    //if the letter is not digit then display error and don't type anything
  if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
     //display error message
     $("#employeePhoneError").html("Digits Only").show().fadeOut("slow");
        return false;
   }
  });

  $("#addEmployee").click(function(){
    var addNewUserUrl = urlBase+"/addEmployee";
    var formData = {
      firstName: $('#employeeFirstName').val(),
      lastName: $('#employeeLastName').val(),
      phoneNumber:$('#employeePhone').val(),
      email: $('#employeeEmail').val(),
    }
    $.ajax({
      type: 'POST',
      url: addNewUserUrl,
      data: formData,
      success: function(data, status, jqXHR){
        if(status==='success'){
         console.log("successfully add user  in blockchain");
          alert("successfully add user  in blockchain");
       }
      },
      error: function(xhr, textStatus, error){
       console.log("error add project  in blockchain");
 
          console.log(xhr.statusText);
          console.log(textStatus);
          console.log(error);
          alert("Error while adding user: "+ xhr.responseText);
      }
    });
  })

});












});

// const ipfsClient = require('ipfs-http-client')
// const ipfs = ipfsClient.create({host:'172.29.3.95' , port:'5001', protocol:'http'});



// const uploadFile =async (file)=>{
//   console.log("submit file to ipfs")
//   const  fileadded=await ipfs.add(file)
//   //const fileadded = await client.add(file)
//   if(fileadded.path!='')
//   return fileadded.path
//   else
//   console.log("could not upload the file to ipfs")

// }