window.addEventListener('load', function() {
  $('#searchForm').on('submit', function(e) {
    const searchKeyword = $("#searchText").val();

    if (!searchKeyword) {
      $('#searchText').attr('disabled', 'disabled');
    }

    return true;
  });

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

    const itemId = $(this).data('id');
    const transport_provider = $(this).data('android-automation-job-detail');

    $modal = $('#remove-android-automation-job-detail-warning-modal');
    $modal.find('h4.modal-title').text(`Removing name: ${transport_provider}`);
    $modal.find('#removeBtn').data('id', itemId);
  });

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

  $("#quote_date").datepicker({
    format: "yyyy-mm-dd",
    autoclose: true
  });

  $(document).on('change', '#filter_transport_provider', function(e) {
    get_android_automation_jobs_by_transport_provider(e.target.value);
  });

  function get_android_automation_jobs_by_transport_provider(transport_provider_id) {
    
    $('#btnSearch').button("loading");
    $.ajax({
      url: '/automation_jobs/getAndroidAutomationJobsByTransportProvider',
      type: 'get',
      dataType: 'json',
      data : {
        'transport_provider_id' : transport_provider_id
      },
      success: function(data) {
        var options = $('#filter_android_automation_job');
        options.empty();
        options.append($("<option />").val('').text('All'));
        $.each(data, function () {
          $.each(this, function(k, v) {
            options.append($("<option />").val(v.id).text(v.id));
          });
        });
      },
      complete: function() {
        $('#btnSearch').button("reset");
      }
    });

  }
});