$(document).ready(function(){
  $(document).ajaxStart(function(){
    $("#wait").css("display", "block");
  });
  $(document).ajaxComplete(function(){
    $("#wait").css("display", "none");
  });
  
  urlDrop="/dist/js/select2.min.js";
  var urltable ="/js/dataTable.js"
  var urlrender="/js/renderTable.js"

var url_string = window.location.href;
var url = new URL(url_string);
var searchKey = url.searchParams.get("id");

//console.log("1")
//console.log(searchKey);
var urlBase = "http://0.0.0.0:30001";

if(!searchKey)
{
// console.log("2")
// window.location.replace("href="/projectDetail?id='+data+'"");
 return;
}

  //console.log("3")
    var queryUrl = urlBase+"/queryTask";
 
  



    $.ajax({
      type: 'GET',
      url: queryUrl,
      data: { key: searchKey },
      crossDomain:true,
      
      success: function(data, status, jqXHR){
          let record = data.Record;
          //console.log(data);
        $("#projectID").text(data.projectID);
          $("#taskName").text(data.taskName);
          $("#taskdescreption").text(data.taskdescreption);
          $("#taskStatus").text(data.taskStatus);
          $("#taskId").val(data.taskId);

          
         // $("#projectLeaderID").text(data.projectLeaderID);
          //$("#clientID").text(data.clientID);
          
          $("#defecaltDegree").text(data.defecaltDegree);
          $("#estematedTaskDuration").text(data.estematedTaskDuration);
          $("#taskDeadline").text(data.taskDeadline);
          $("#createDateTime").text(data.createDateTime);
          $("#lastUpdated").text(data.lastUpdated);
          $("#taskWage").text(data.taskWage);
          $("#ipfsCid").text(data.ipfsCid);

         
      },
      error: function(xhr, textStatus, error){
          console.log(xhr.statusText);
          console.log(textStatus);
          console.log(error);
          alert("Error: llllllllllllllllllllll"+ xhr.responseText);
      }
    });
    
////////////////////////////////
var queryEmployeeTask=urlBase+"/queryEmployeeTask";

$.ajax({
    type: 'GET',
    url: queryEmployeeTask,
    //crossDomain:true,
    data: { key: searchKey },
    success: function(data, status, jqXHR){
        let record = data.Record;

      
        $.getScript( urlrender, function() {
          $.getScript( urltable, function() {
                 //     console.log("before  data table===== "+data); 
           $('#taskemployee').DataTable({
               data: data,
  
               columns: [                        
                   { 
                       data: 'Record.employeeId',
                       render: function (data, type, row, meta) {
                           return ('<tr>' +'<th scope="col">'+'</th>');    
                       },
                   },
       
                   { 
                       data: 'Record.firstName',
                       render: function (data, type, row, meta) {
                           return   ('  <td >' +data + ' </td>')
                       },
                   },
       
                   { 
                       data: 'Record.lastName' ,
                       render: function (data, type, row, meta) {
                           return  '<tr>'+'<th scope="col">'+ '</th>'+'  <td >'+data +' </td>'
                       },
                   },
                   { 
                    data: 'Record.phoneNumber' ,
                    render: function (data, type, row, meta) {
                        return  '<tr>'+'<th scope="col">'+ '</th>'+'  <td >'+data +' </td>'
                    },
                    },
                   { 
                       data: 'Record.email' ,
                       render: function (data, type, row, meta) {
                           return  (
                             '<tr>'+'<th scope="col">'+ '</th>'+'  <td >'+data +' </td>')
                       },
                   },









               ],
  
           });





           });
       });





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

var queryHistoryByKey=urlBase+"/queryHistoryByKey"

  $.ajax({
    type: 'GET',
    url: queryHistoryByKey,
    //crossDomain:true,
    data: { key: searchKey },
    success: function(data, status, jqXHR){
        let record = data.Record;
console.log(data);
      
        $.getScript( urlrender, function() {
          $.getScript( urltable, function() {
 
           $('#taskhistory').DataTable({
               data: data,
  
               columns: [                        
                   { 
                       data: 'taskId',
                       render: function (data, type, row, meta) {
                           return ('<tr>' +'<th scope="col">'+'</th>');    
                       },
                   },
       
                   { 
                       data: 'lastUpdated',
                       render: function (data, type, row, meta) {
                           return   ('  <td >' +data + ' </td>')
                       },
                   },
       
                   { 
                       data: 'taskStatus' ,
                       render: function (data, type, row, meta) {
                           return  '<tr>'+'<th scope="col">'+ '</th>'+'  <td >'+data +' </td>'
                       },
                   },
                   { 
                    data: 'commitName' ,
                    render: function (data, type, row, meta) {
                        return  '<tr>'+'<th scope="col">'+ '</th>'+'  <td >'+data +' </td>'
                    },
                    },
                   { 
                       data: 'cid' ,
                       render: function (data, type, row, meta) {
                        if(data!=""&&data)
                           {return  (
                          // ' <a href="'+data+'">'+data.commitName+'</a>'+
                             '<tr>'+'<th scope="col">'+ '</th>'+'  <td >'+' <a href="'+data+'">'+'CID'+'</a>' +' </td>')}
                             else
                             {return('<tr>'+'<th scope="col">'+ '</th>'+'  <td >'+'No file Uploaded yet' +' </td>')}
                       },
                   },









               ],
  
           });





           });
       });





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

    
  });




$("#showIpfsCid").click(function(){
cid=$("#ipfsCid").val()
//cid='http://172.29.3.95:8080/ipfs/QmTpk4R498PuUrmNfwFxSTyfotUDy3T5h19qMJTe2hmbZT'
if(cid=="")
alert("No files have been uploaded yet")

else{

  window.location.replace(cid);

}

})




$("#changeStatus").click(function(){
  event.preventDefault();

st=$("#taskStatus").text()
s=$("#changeTaskStatus").val()
console.log(st)
console.log(s)
if(st==s)
{
  alert("alrady"+st)
  return
}


let changeStatusTask=urlBase+"/changeStatusTask"
var formData = {
  taskStatus: s,
  taskId:searchKey,
 }

$.ajax({
  type: 'POST',
  url: changeStatusTask,
  data: formData,
  crossDomain:true,
  
  success: function(data, status, jqXHR){
      let record = data.Record;
      //console.log(data);
      window.location.reload();
alert("done!")     
  },
  error: function(xhr, textStatus, error){
      console.log(xhr.statusText);
      console.log(textStatus);
      console.log(error);
      alert("Error: llllllllllllllllllllll"+ xhr.responseText);
  }
});


  
  })


  $("#uploadF").click(function(){
    event.preventDefault();




    let uploadFile=urlBase+"/uploadFile"
    var formData = {
        taskId:$('#taskId').val(),
        fileName:$('#fileName').val(),
        file:$('#file').val(),
        
     }


    $.ajax({
      type: 'POST',
      url: uploadFile,
      data: formData,
      crossDomain:true,
      
      success: function(data, status, jqXHR){
          let record = data.Record;
          //console.log(data);
    alert("Complete!")     
      },
      error: function(xhr, textStatus, error){
          console.log(xhr.statusText);
          console.log(textStatus);
          console.log(error);
          alert("Error: llllllllllllllllllllll"+ xhr.responseText);
      }
    });
    
    
      
      })







});
