window.addEventListener('load', function () {
  $('.btn-remove').on('click', function (e) {
    e.preventDefault();

    const itemId = $(this).data('id');
    const cityName = $(this).data('city-name');

    $modal = $('#remove-geofence-area-city-modal');
    $modal.find('h4.modal-title').text(`Removing city: ${cityName}`);
    $modal.find('#removeBtn').data('id', itemId);
  });

  $('#removeBtn').on('click', function (e) {
    e.preventDefault();
    const itemId = $(this).data('id');
    window.__api__.remove(`/geofence_area_city/${itemId}/delete`, itemId);
  })
});

function correctName(el_update,id, latitude, longitude) {
  $.ajax({
    type: 'GET',
    url: "/ajax/updatecity/" + id+'/'+latitude+'/'+longitude,
    success: function (data) {
      var data = JSON.parse(data);
      if (data.error == 0) {
        if(el_update!=""){
          $("#"+el_update).html(data.city);
        }
      }
      alert(data.msg);
    },
    error: function (data) {
      console.log(data);
    }
  });
  return ;
}

function cleanUp() {

  if(confirm("Clean up cities?")){
    $.ajax({
      type: 'GET',
      url: "/ajax/cleanupCities/",
      success: function (data) {
        var data = JSON.parse(data);
        alert(data.msg);
      },
      error: function (data) {
        console.log(data);
      }
    });
      
  }
  return ;
}