jQuery(document).ready(function($) {
  function updateStatusClass(element, newStatus) {
    jQuery(element).removeClass(function (i, className) {
        return (className.match(/\bmepr_status_\S+/g) || []).join(' ');
    }).addClass('mepr_status_' + newStatus);
  }

  //Click initial status field, show select
  jQuery('.status_initial').click(function() {
    var i = jQuery(this).attr('data-value');
    jQuery(this).hide();
    jQuery('.status_editable_'+i).show();
    return false;
  });

  //Click cancel
  jQuery('.cancel_change').click(function() {
    var i = jQuery(this).attr('data-value');
    jQuery('div.status_editable_' + i).hide();
    jQuery('div.status_initial_' + i).show();
  });

  //Saving status field
  jQuery('.status_save').click(function() {
    var i = jQuery(this).attr('data-value');
    var v = jQuery('.status_edit_'+i).val();
    var data = {
             action: 'mepr_edit_status',
             id: i,
             value: v,
             edit_txn_status_nonce: MeprTxn.edit_txn_status_nonce
    };
    jQuery('.status_editable_'+i).hide();
    jQuery('.status_saving_'+i).show();
    jQuery.post(ajaxurl, data, function(response) {
      var trimmed_data = response.replace(/^\s+|\s+$/g, ''); //Trim whitespace

      jQuery('.status_initial_'+i+' a').html(trimmed_data);
      jQuery('.status_saving_'+i).hide();
      jQuery('.status_initial_'+i).show();
      updateStatusClass(jQuery('.status_initial_'+i), v);
    });
    return false;
  });

  jQuery('a.remove-txn-row').click(function(e) {
    e.preventDefault();
    var transactionId = jQuery(this).attr('data-value');
      window.MeprModal({
        title: MeprTxn.del_txn_title,
        message: MeprTxn.del_txn,
        confirmText: MeprTxn.confirm_text,
        cancelText: MeprTxn.cancel_text,
        isDestructive: true,
        ajaxData: {
          action: 'mepr_delete_transaction',
          id: transactionId,
          mepr_transactions_nonce: MeprTxn.delete_transaction_nonce
        },
        onError: function() {
          alert(MeprTxn.del_txn_error);
        },
        onSuccess: function() {
          jQuery('tr#record_' + transactionId).fadeOut('slow');
        }
      });
    }
  );
  //Resend TXN Email JS
  jQuery('a.mepr_resend_txn_email').click(function() {
    var i = jQuery(this).attr('data-value');

    jQuery('tr#record_' + i + ' .mepr_loader').show();

    var data = {
      action: 'mepr_resend_txn_email',
      id: i
    };

    jQuery.post(ajaxurl, data, function(response) {
      var trimmed_data = response.replace(/^\s+|\s+$/g, ''); //Trim whitespace

      jQuery('tr#record_' + i + ' .mepr_loader').hide();

      alert(trimmed_data);
    });

    return false;
  });

  //Send Welcome Email
  jQuery('a.mepr_send_welcome_email').click(function() {
    var i = jQuery(this).attr('data-value');

    jQuery('tr#record_' + i + ' .mepr_loader').show();

    var data = {
      action: 'mepr_send_welcome_email',
      id: i
    };

    jQuery.post(ajaxurl, data, function(response) {
      var trimmed_data = response.replace(/^\s+|\s+$/g, ''); //Trim whitespace

      jQuery('tr#record_' + i + ' .mepr_loader').hide();

      alert(trimmed_data);
    });

    return false;
  });


  // Refund TXN JS
  jQuery('a.mepr-refund-txn').click(function(e) {
    e.preventDefault();
    var transactionId = jQuery(this).attr('data-value');
    window.MeprModal({
      title: MeprTxn.refund_txn_title,
      message: MeprTxn.refund_txn,
      confirmText: MeprTxn.confirm_text,
      cancelText: MeprTxn.cancel_text,
      ajaxData: {
        action: 'mepr_refund_transaction',
        id: transactionId,
        refund_txn_nonce: MeprTxn.refund_txn_nonce
      },
      onError: function(response) {
        alert(MeprTxn.refund_txn_error + ": " + response);
      },
      onSuccess: function() {
        jQuery('div.status_initial_' + transactionId + ' a').text(MeprTxn.refunded_text);
        jQuery('select.status_edit_' + transactionId).val('refunded');
        jQuery('tr#record_' + transactionId + ' .mepr-refund-txn-action').remove();
        jQuery('tr#record_' + transactionId + ' .mepr-refund-txn-and-cancel-sub-action').remove();
        alert(MeprTxn.refund_txn_success);
      }
    });
  });

  // Refund TXN & Cancel SUB JS
  jQuery('a.mepr-refund-txn-and-cancel-sub').click(function(e) {
    e.preventDefault();
    var transactionId = jQuery(this).attr('data-value');
    window.MeprModal({
      title: MeprTxn.refund_txn_and_cancel_sub_title,
      message: MeprTxn.refund_txn_and_cancel_sub,
      confirmText: MeprTxn.confirm_text,
      cancelText: MeprTxn.cancel_text,
      ajaxData: {
        action: 'mepr_refund_txn_and_cancel_sub',
        id: transactionId,
        refund_txn_cancel_sub_nonce: MeprTxn.refund_txn_cancel_sub_nonce
      },
      onError: function(response) {
        alert(MeprTxn.refund_txn_error + ": " + response);
      },
      onSuccess: function() {
        jQuery('div.status_initial_' + transactionId + ' a').text(MeprTxn.refunded_text);
        jQuery('select.status_edit_' + transactionId).val('refunded');
        jQuery('tr#record_' + transactionId + ' .mepr-refund-txn-action').remove();
        jQuery('tr#record_' + transactionId + ' .mepr-refund-txn-and-cancel-sub-action').remove();
        alert(MeprTxn.refund_txn_and_cancel_sub_success);
      }
    });
    return false;
  });

  if($.fn.tooltipster) {
    $('.wp_list_mepr_transactions .column-col_trans_num > a').each(function () {
      var $link = $(this),
          tooltip_text = $link.data('tooltip-text'),
          order_trans_num = $link.data('order-trans-num');

      if(tooltip_text && order_trans_num) {
        var $content = $('<span>').text(tooltip_text);

        if(window.ClipboardJS) {
          var $icon = $('<i class="mp-clipboardjs mp-icon mp-icon-clipboard mp-16"></i>').attr('data-clipboard-text', order_trans_num).attr('title', MeprTxn.click_to_copy),
              clipboard = new ClipboardJS($icon.get(0)),
              timeout;

          clipboard.on('success', function () {
            clearTimeout(timeout);

            $icon.removeClass('mp-icon-clipboard').addClass('mp-icon-ok');

            timeout = setTimeout(function () {
              $icon.removeClass('mp-icon-ok').addClass('mp-icon-clipboard');
            }, 1000);
          });

          $content = $content.add($icon);
        }

        $link.tooltipster({
          theme: ['tooltipster-borderless', 'mepr-order-trans-num-tooltip'],
          content: $content,
          interactive: true
        });
      }
    });
  }
});
