// Shipments AJAX API

$(document).ready(function() {
  //handle shipping method edit click
  $('a.edit-method').click(toggleMethodEdit);
  $('a.cancel-method').click(toggleMethodEdit);

  handle_shipping_method_save = function(){
    var link = $(this);
    var shipment_number = link.data('shipment-number');
    var selected_shipping_rate_id = link.parents('tbody').find("select#selected_shipping_rate_id[data-shipment-number='" + shipment_number + "']").val();
    var url = Spree.url( Spree.routes.orders_api + "/" + order_number + "/shipments/" + shipment_number + ".json");

    $.ajax({
      type: "PUT",
      url: url,
      data: { shipment: { selected_shipping_rate_id: selected_shipping_rate_id  } }
    }).done(function( msg ) {
      window.location.reload();
    }).error(function( msg ) {
      console.log(msg);
    });
  }
  $('.admin-order-edit-form a.save-method').click(handle_shipping_method_save);

  //handle tracking info edit/delete

  // Show the input field to edit the tracking info
  // And hide the input field when cancel is clicked
  $('a.edit-tracking').click(toggleTrackingEdit);
  $('a.cancel-tracking').click(toggleTrackingEdit);

  saveTrackingInfo = function(){
    let shipmentNumber = $(this).data('shipment-number');
    let tracking = document.getElementById('tracking').value

    makeApiCall(trackingUrl(shipmentNumber), { shipment: { tracking: tracking } } )
  }

  deleteTrackingInfo = function(){
    let shipmentNumber = $(this).data('shipment-number');
    let tracking = ''

    confirmDelete(trackingUrl(shipmentNumber), { shipment: { tracking: tracking } })
  }

  trackingUrl = function(shipmentNumber){
    return Spree.url( Spree.routes.orders_api + "/" + order_number + "/shipments/" + shipmentNumber + ".json");
  }

  $('.admin-order-edit-form a.save-tracking').click(saveTrackingInfo);
  $('.admin-order-edit-form a.delete-tracking').click(deleteTrackingInfo);

  // handle note edit/delete

  // Show the input field to edit the note
  // And hide the input field when cancel is clicked
  $('a.edit-note.icon-edit').click(toggleNoteEdit);
  $('a.cancel-note').click(toggleNoteEdit);

  saveNote = function(){
    let note = document.getElementById('note').value
    makeApiCall(getNoteUrl(), { note: note })
  }

  deleteNote = function(){
    let note = ''
    confirmDelete(getNoteUrl(), { note: note })
  }

  getNoteUrl = function(){
    return Spree.url( Spree.routes.orders_api + "/" + order_number);
  }

  confirmDelete = function(url, params){
    displayDeleteAlert(function(confirmation) {
      if (confirmation) {
        makeApiCall(url, params)
      }
    }); 
  }

  $('.admin-order-edit-form a.save-note').click(saveNote);
  $('.admin-order-edit-form a.delete-note').click(deleteNote);

  // Makes API call for notes/tracking info
  makeApiCall = function(url, params) {
    $.ajax({
      type: "PUT",
      url: url,
      data: params
    }).done(function( msg ) {
      window.location.reload();
    }).error(function( msg ) {
      console.log(msg);
    });
  }

  displayDeleteAlert = function(callback) {
    i18nKey = "are_you_sure";
    $('#custom-confirm .message').html(
      ` ${t(i18nKey)}
              <div class="form">
              </div>`);
    $('#custom-confirm button.confirm').unbind( "click" ).click(() => {
      $('#custom-confirm').hide();
      callback(true);
    });
    $('#custom-confirm button.cancel').click(() => {
      $('#custom-confirm').hide();
      callback(false)
    });
    $('#custom-confirm').show();
  }

});
