var clickId;
document.title = "Lihat Token";
console.log(baseURL);
loadWebsiteData();
console.log($('#id_user').val());
function loadWebsiteData(){
  console.log("masuk gan");
  
    $id = $('#id_user').val();


    $.ajax({
      type: 'POST',
      dataType: 'json',
      url: baseURL + '/api/websitelist',
      data: {
          id_user         : $id
      },
      success: function(result){
        console.log(result.data);
        let data = result.data; 
        let counter = 1;
  
        for(webdata in data){
          var cols = "";
          cols += '<tr>'
          //cols += '<td>'+counter+'</td>'
          cols += '<td>'+data[webdata].website_name+'</td>';
          cols += '<td>'+data[webdata].website_domain+'</td>';
          cols += '<td id="token'+counter+'">'+data[webdata].token+'</td>';
          cols += '<td align="center"><button onclick="copyToClipboard('+counter+')" class="btn btn-success btn-sm" id="btntoken'+counter+'" align="center">copy</button><hr><button onclick="clickId = '+data[webdata].id_web+'" 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": false,
          "bLengthChange": false,
          "bAutoWidth": false,
          scrollY:        '350px',
          scrollX:        false,
          fixedHeader: true,
          scrollCollapse: true,
          autoWidth: false,
          paging:         true,
          columnDefs: [
            {width: 50, targets: 0},
            {width: 100, targets: 1},
            { width: 200, targets: 2 },
            { width: 30, targets: 3 },  
          ],
          //fixedColumns: true
      }); //success
  
      },
      error: function(xhr){
          alert(xhr.status+'-'+xhr.statusText);
      }
    });
}

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

  deletewebsite(clickId);
})


function deletewebsite(param){
  $.ajax({
    type: 'POST',
    dataType: 'json',
    async: false,
    url: baseURL + '/api/deletewebsite',
    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 copyToClipboard($id){
  console.log("copy function"+$id);
  
  var content = $('#token'+$id).html();
  console.log(content);

  if (document.selection) {
    var range = document.body.createTextRange();
    range.moveToElementText(document.getElementById("token"+$id));
    range.select().createTextRange();
    document.execCommand("copy");
    window.location.href = "menu"
  } else if (window.getSelection) {
    var range = document.createRange();
    range.selectNode(document.getElementById("token"+$id));
    window.getSelection().addRange(range);
    document.execCommand("copy");
    window.location.href = "menu";
  }
}


