$(document).ready(function() {
  var token = $('body').data('token');
  function capture() {
    var amt = $('#paypal_capture_amount').val();

    if (amt == '' || amt == 0) {
      alert(window.errorCaptureAmt);
      return false;
    } else {
      var captureComplete = $('#paypal_capture_complete').prop('checked') == true ? 1 : 0;
      voidTransaction = false;

      $.ajax({
        type: 'POST',
        dataType: 'json',
        data: { amount: amt, order_id: window.oderId, complete: captureComplete },
        url: 'index.php?route=payment/pp_express/capture&token=' + token,
        beforeSend: function() {
          $('#btn_capture').hide();
          $('#img_loading_capture').show();
        },
        success: function(data) {
          if (data.error == false) {
            html = '';
            html += '<div class="row no-space-xs middle-xs nowrap-xs padding-large bottom-border-gray">';
            html += '<div class="col-xs">';
            html += '<div class="content-box">';
            html += '<div class="row middle-xs">';
            html += '<div class="col-xs-12 col-md-width-auto">';
            html += '<div class="content-box">';
            html += '<span class="text-small text-dark text-bold visible-block rel" data-tooltip="ID DA TRANSAÇÃO">';
            html += '#' + data.data.transaction_id;
            html += '</span>';
            html += '<span class="text-small text-gray rel" data-tooltip="CRIADO">' + data.data.created + '</span>';
            html += '</div>';
            html += '</div>';
            html += '<div class="col-xs-6 col-md">';
            html += '<div class="content-box">';
            html += '<span class="text-small text-dark visible-block">R$ ' + data.data.amount + '</span>';
            html += '</div>';
            html += '</div>';
            html += '<div class="col-xs-6 col-md">';
            html += '<div class="content-box">';
            html +=
              '<span class="text-small text-dark rel" data-tooltip="STATUS">' + data.data.payment_status + '</span>';
            html += '</div>';
            html += '</div>';
            html += '<div class="col-xs-6 col-md">';
            html += '<div class="content-box">';
            html += '<span class="text-small text-dark visible-block">' + data.data.payment_type + '</span>';
            html += '<span class="text-small text-gray">Tipo de pagamento</span>';
            html += '</div>';
            html += '</div>';
            html += '<div class="col-xs-6 col-md">';
            html += '<div class="content-box">';
            html += '<span class="text-small text-dark visible-block">' + data.data.pending_reason + '</span>';
            html += '<span class="text-small text-gray">Razão pendente</span>';
            html += '</div>';
            html += '</div>';
            html += '</div>';
            html += '</div>';
            html += '</div>';
            html += '<div class="col-xs-width-auto">';
            html += '<div class="content-box">';
            html += '<div class="new-dropdown visible-inline-block js-new-dropdown">';
            html += '<div class="new-dropdown-control">';
            html += '<span class="new-dropdown-control-dot"></span>';
            html += '<span class="new-dropdown-control-dot"></span>';
            html += '<span class="new-dropdown-control-dot"></span>';
            html += '</div>';
            html += '<div class="new-dropdown-content background-white border-gray shadow-medium right js-new-dropdown-content">';
            html +=
              '<a href="' +
              window.viewLink +
              '&transaction_id=' +
              data.data.transaction_id +
              '" class="new-dropdown-content-item text-small text-dark text-decoration-none sincronizar">' +
              window.textView +
              '</a>';
            html +=
              '<a href="' +
              window.refundLink +
              '&transaction_id=' +
              data.data.transaction_id +
              '" class="new-dropdown-content-item text-small text-dark text-decoration-none sincronizar">' +
              window.textRefund +
              '</a>';
            html += '</div>';
            html += '</div>';
            html += '</div>';
            html += '</div>';
            html += '</div>';

            $('#paypal_captured').text(data.data.captured);
            $('#paypal_capture_amount').val(data.data.remaining);
            $('#paypal_transactions').append(html);

            if (data.data.void != '') {
              html += '<div class="row no-space-xs middle-xs nowrap-xs padding-large bottom-border-gray">';
              html += '<div class="col-xs">';
              html += '<div class="content-box">';
              html += '<div class="row middle-xs">';
              html += '<div class="col-xs-12 col-md-width-auto">';
              html += '<div class="content-box">';
              html += '<span class="text-small text-dark text-bold visible-block rel" data-tooltip="ID DA TRANSAÇÃO">';
              html += '#' + data.data.void.transaction_id;
              html += '</span>';
              html +=
                '<span class="text-small text-gray rel" data-tooltip="CRIADO">' + data.data.void.created + '</span>';
              html += '</div>';
              html += '</div>';
              html += '<div class="col-xs-6 col-md">';
              html += '<div class="content-box">';
              html += '<span class="text-small text-dark visible-block">R$ ' + data.data.void.amount + '</span>';
              html += '</div>';
              html += '</div>';
              html += '<div class="col-xs-6 col-md">';
              html += '<div class="content-box">';
              html +=
                '<span class="text-small text-dark rel" data-tooltip="STATUS">' +
                data.data.void.payment_status +
                '</span>';
              html += '</div>';
              html += '</div>';
              html += '<div class="col-xs-6 col-md">';
              html += '<div class="content-box">';
              html += '<span class="text-small text-dark visible-block">' + data.data.void.payment_type + '</span>';
              html += '<span class="text-small text-gray">Tipo de pagamento</span>';
              html += '</div>';
              html += '</div>';
              html += '<div class="col-xs-6 col-md">';
              html += '<div class="content-box">';
              html += '<span class="text-small text-dark visible-block">' + data.data.void.pending_reason + '</span>';
              html += '<span class="text-small text-gray">Razão pendente</span>';
              html += '</div>';
              html += '</div>';
              html += '</div>';
              html += '</div>';
              html += '</div>';
              html += '</div>';
              $('#paypal_transactions').append(html);
            }

            if (data.data.status == 1) {
              $('#capture_status').text(window.textComplete);
              $('.paypal_capture').hide();
            }
          }

          if (data.error == true) {
            alert(data.msg);
            if (data.failed_transaction) {
              html = '';
              html += '<div class="row no-space-xs middle-xs nowrap-xs padding-large bottom-border-gray">';
              html += '<div class="col-xs">';
              html += '<div class="content-box">';
              html += '<div class="row middle-xs">';
              html += '<div class="col-xs-12 col-md-width-auto">';
              html += '<div class="content-box">';
              html +=
                '<span class="text-small text-gray rel" data-tooltip="CRIADO">' +
                data.failed_transaction.created +
                '</span>';
              html += '</div>';
              html += '</div>';
              html += '<div class="col-xs-6 col-md">';
              html += '<div class="content-box">';
              html +=
                '<span class="text-small text-dark visible-block">R$ ' + data.failed_transaction.amount + '</span>';
              html += '</div>';
              html += '</div>';
              html += '<div class="col-xs-6 col-md">';
              html += '<div class="content-box">';
              html += '<span class="text-small text-dark visible-block">' + data.data.void.pending_reason + '</span>';
              html += '<span class="text-small text-gray">Razão pendente</span>';
              html += '</div>';
              html += '</div>';
              html += '<div class="col-xs-12 col-md-width-auto">';
              html += '<div class="content-box">';
              html +=
                '<a onclick="resendTransaction(this); return false;" href="' +
                window.resendLink +
                '&paypal_order_transaction_id=' +
                data.failed_transaction.paypal_order_transaction_id +
                '">' +
                window.textResend +
                '</a>';
              html += '</div>';
              html += '</div>';
              html += '</div>';
              html += '</div>';
              html += '</div>';
              html += '</div>';
              $('#paypal_transactions').append(html);
            }
          }

          $('#btn_capture').show();
          $('#img_loading_capture').hide();
        },
      });
    }
  }

  function doVoid() {
    if (confirm(window.textConfirmVoid)) {
      $.ajax({
        type: 'POST',
        dataType: 'json',
        data: { order_id: window.oderId },
        url: 'index.php?route=payment/pp_express/void&token=' + token,
        beforeSend: function() {
          $('#btn_void').hide();
          $('#img_loading_void').show();
        },
        success: function(data) {
          if (data.error == false) {
            html = '';
            html += '<div class="row no-space-xs middle-xs nowrap-xs padding-large bottom-border-gray">';
            html += '<div class="col-xs">';
            html += '<div class="content-box">';
            html += '<div class="row middle-xs">';
            html += '<div class="col-xs-12 col-md-width-auto">';
            html += '<div class="content-box">';
            html += '<span class="text-small text-gray rel" data-tooltip="CRIADO">' + data.data.created + '</span>';
            html += '</div>';
            html += '</div>';
            html += '<div class="col-xs-6 col-md"><div class="content-box"></div></div>';
            html += '<div class="col-xs-6 col-md">';
            html += '<div class="content-box">';
            html +=
              '<span class="text-small text-dark rel" data-tooltip="STATUS">' + data.data.payment_status + '</span>';
            html += '</div>';
            html += '</div>';
            html += '<div class="col-xs-6 col-md"><div class="content-box"></div></div>';
            html += '<div class="col-xs-6 col-md"><div class="content-box"></div></div>';
            html += '</div>';
            html += '</div>';
            html += '</div>';
            html += '</div>';

            $('#paypal_transactions').append(html);
            $('#capture_status').text(window.textComplete);
            $('.paypal_capture_live').hide();
          }
          if (data.error == true) {
            alert(data.msg);
          }
          $('#btn_void').show();
          $('#img_loading_void').hide();
        },
      });
    }
  }

  function resendTransaction(element) {
    $.ajax({
      type: 'GET',
      dataType: 'json',
      url: $(element).attr('href'),
      beforeSend: function() {
        $(element).hide();
        $(element).after('<img src="' + window.configAdmSsl + 'view/image/loading.gif" class="loading" />');
      },
      success: function(data) {
        $(element).show();
        $('.loading').remove();

        if (data.error) {
          alert(data.error);
        }

        if (data.success) {
          location.reload();
        }
      },
    });
  }
});
