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

var queryClientUrl = urlBase+"/queryAllClient";
var queryEmployeeUrl = urlBase+"/queryAllEmployee";



$.ajax({
  type: 'GET',
  url: queryClientUrl,

  success: function(data, status, jqXHR){

    $.getScript( urlDrop, function() {


      let r = $.map(data, function (obj) {
        obj.id = obj.Record.clientId; 
        obj.text = obj.Record.firstName+ " "+obj.Record.lastName; 
        return obj;
      });

      $("#clientName").select2({
        placeholder: 'Select a Client...',
        theme: "dark",
        data: r,
        
      })


       });


  },
  error: function(xhr, textStatus, error){
      console.log(xhr.statusText);
      console.log(textStatus);
      console.log(error);
      alert("Error: "+ xhr.responseText);
  }
});



$.ajax({
  type: 'GET',
  url: queryEmployeeUrl,

  success: function(data, status, jqXHR){

    $.getScript( urlDrop, function() {


      let r = $.map(data, function (obj) {
        obj.id = obj.Record.employeeId; 
        obj.text = obj.Record.firstName+ " "+obj.Record.lastName; 
        return obj;
      });

      $("#projectLeader").select2({
        placeholder: 'Select a Employee...',
        theme: "dark",
        data: r,
        
      })
       });


  },
  error: function(xhr, textStatus, error){
      console.log(xhr.statusText);
      console.log(textStatus);
      console.log(error);
      alert("Error: "+ xhr.responseText);
  }
});







 $("#addNewProject").click(function(){
  event.preventDefault();
  var addNewProjectUrl = urlBase+"/addNewProject";

  var formData = {
   projectName: $('#projectName').val(),
   descreption: $('#descreption').val(),
   projectStatus:$('#projectStatus').val(),
   clientID: $('#clientName').val(),
   projectLeaderID: $('#projectLeader').val(),
   estimatedBudget: $('#estimatedBudget').val(),
   estimatedDuration: $('#estimatedDuration').val(),
   deadline: $('#deadline').val()
  
  }

  console.log($("#projectLeader").val())

  $.ajax({
    type: 'POST',
    url: addNewProjectUrl,
    data: formData,
    success: function(data, status, jqXHR){
      if(status==='success'){
       console.log("successfully add project  in blockchain");
        alert("successfully add project  in blockchain");
        window.location.reload();
      }
      showTab("query");
    },
    error: function(xhr, textStatus, error){
     console.log("error add project  in blockchain");

        console.log(xhr.statusText);
        console.log(textStatus);
        console.log(error);
        alert("Error: "+ xhr.responseText);
    }
  });
});














 $("#estimatedBudget").keypress(function (e) {
  //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
   $("#estimatedBudgetError").html("Digits Only").show().fadeOut("slow");
      return false;
 }
});
  
$("#estimatedDuration").keypress(function (e) {
  //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
   $("#estimatedDurationError").html("Digits Only").show().fadeOut("slow");
      return false;
 }
});
 





});
