const code = 20;
var clickId; 

document.title = "Manage User";
console.log(baseURL);
loadUserData();
console.log($('#id_user').val());

function loadUserData(){
  console.log("masuk gan");

  //$id = $('#id_user').val();
  $.ajax({
    type: 'POST',
    dataType: 'json',
    url: baseURL + '/api/loadusername',
    data: {
        id_user         : null
    },
    success: function(result){
      // console.log(result.data);
      let data = result.data; 
      let counter = 1;

      for(userdata in data){
        var cols = "";
        cols += '<tr>'
        cols += '<td>'+counter+'</td>'
        cols += '<td>'+data[userdata].username+'</td>';
        cols += '<td>'+data[userdata].name+'</td>';
        cols += '<td id="token'+counter+'">'+data[userdata].email+'</td>';
        cols += '<td id="token'+counter+'">'+data[userdata].status+'</td>';
        cols += `<td align="center">`
        if(data[userdata].status == 10){
          cols += `<button onclick="clickId =`+data[userdata].iduser+`" data-toggle="modal" data-target="#deactivateModal" class="btn btn-secondary btn-sm" id="activate`+data[userdata].iduser+`" align="center">deactivate</button> `;
        } else {
          cols += `<button onclick="clickId =`+data[userdata].iduser+`" data-toggle="modal" data-target="#activateModal" class="btn btn-success btn-sm" id="activate`+data[userdata].iduser+`" align="center">activate</button> `;
        }
        cols += `<button onclick="clickId =`+data[userdata].iduser+`" data-toggle="modal" data-target="#resetModal" class="btn btn-primary btn-sm" id="btntoken`+counter+`" align="center">reset</button> `;
        cols += `<button onclick="clickId =`+data[userdata].iduser+`" data-toggle="modal" data-target="#deleteModal" class="btn btn-danger btn-sm" id="delete`+counter+`">delete</button></td>`;
        cols += '</tr>'
        $("#tabeltoken").append(cols);
        counter++;
        
      }

      //console.log(data);
      $('#dataTable1').DataTable({
        "searching": true,
        "bLengthChange": false,
        "bAutoWidth": false,
        scrollY:        '350px',
        scrollX:        true,
        fixedHeader: true,
        scrollCollapse: true,
        autoWidth: false,
        paging:         true,
        columnDefs: [
          {width: 30, targets: 0},
          {width: 150, targets: 1},
          { width: 150, targets: 2 },
          { width: 150, targets: 3 }, 
          { width: 50, targets: 4 }, 
        ],
        //fixedColumns: true
    }); //success

    },
    error: function(xhr){
        alert(xhr.status+'-'+xhr.statusText);
    }
  });
}

function getId(param){
  return param;
}

function disabledButton(){

}

$("#deleteModal").on("click", function(e){
  e.preventDefault();

  deleteuser(clickId);
})

$("#btnReset").on("click", function(e){
  e.preventDefault();

  resetPassword(clickId);
})

$("#btnActivate").on("click", function(e){
  e.preventDefault();

  activateUser(clickId);
})

$("#btnDeactivate").on("click", function(e){
  e.preventDefault();

  deactivateUser(clickId);
})

function deleteuser(param){
  $.ajax({
    type: 'POST',
    dataType: 'json',
    async: false,
    url: baseURL + '/api/deleteuser',
    data: {
        id   : param,
    },
    success: function(result){
        
        if(result.code == 0){
            
            alert('sukses delete');
            //loadWebsiteData();
            location.reload();

        }else{
            alert('gagal');
        }
    },
    error: function(xhr) {
        alert(xhr.status+'-'+xhr.statusText);            
    }
  });
}

function resetPassword(param){
  $.ajax({
    type: 'POST',
    dataType: 'json',
    async: false,
    url: baseURL + '/api/updatePassword',
    data: {
        id    : param,
        code  : code,
    },
    success: function(result){
        
        if(result.code == 0){
            
            alert(result.info);
            //loadWebsiteData();
            location.reload();

        }else{
            alert('gagal');
        }
    },
    error: function(xhr) {
        alert(xhr.status+'-'+xhr.statusText);            
    }
  });
}

function activateUser(param){
  var code = "ACTIVATE";
  
  $.ajax({
    type: 'POST',
    dataType: 'json',
    async: false,
    url: baseURL + '/api/activateUser',
    data: {
        id    : param,
        code  : code,
    },
    success: function(result){
        
        if(result.code == 0){
            
            alert(result.info);
            //loadWebsiteData();
            location.reload();

        }else{
            alert('gagal');
        }
    },
    error: function(xhr) {
        alert(xhr.status+'-'+xhr.statusText);            
    }
  });
}

function deactivateUser(param){
  var code = "DEACTIVATE";
  
  $.ajax({
    type: 'POST',
    dataType: 'json',
    async: false,
    url: baseURL + '/api/activateUser',
    data: {
        id    : param,
        code  : code,
    },
    success: function(result){
        
        if(result.code == 0){
            
            alert(result.info);
            //loadWebsiteData();
            location.reload();

        }else{
            alert('gagal');
        }
    },
    error: function(xhr) {
        alert(xhr.status+'-'+xhr.statusText);            
    }
  });
}