<script type="text/javascript">
(function() {
  var orderId = <?= (int)($this->order_id ?? 0);?>;
  var currentTrustmateState = <?= ((int)($this->trustmate_send ?? 0) === 1) ? 'true' : 'false';?>;

  $(function() {
    var btn = $('#integrationsDropdownBtn');
    var menu = $('#integrationsDropdownMenu');

    btn.wrap('<div class="dropdown d-inline-block pull-right"></div>');
    menu.appendTo(btn.parent());

    btn.on('click', function(e) {
      e.preventDefault();
      e.stopPropagation();
      menu.toggleClass('show');
    });

    $(document).on('click', function(e) {
      if (!btn.is(e.target) && !menu.is(e.target) && menu.has(e.target).length === 0) {
        menu.removeClass('show');
      }
    });
  });

  $(function() {
    var timer = '';

    $('#notes').keyup(function() {
      var textarea = $(this);
      clearTimeout(timer);
      timer = setTimeout(function() {
        $.ajax({
          type: 'POST',
          cache: false,
          url: '/admin/shop_order/notes_save/',
          data: {
            order_id: orderId,
            notes: textarea.val()
          }
        });
      }, 500);
    });

    $('body').on('click', '.btn-send-order-to-apilo', function(e) {
      e.preventDefault();
      var href = $(this).attr('href');
      $.alert({
        title: 'Potwierdź',
        content: 'Czy na pewno chcesz wysłać zamówienie do apilo.com?',
        type: 'orange',
        closeIcon: true,
        closeIconClass: 'fa fa-times',
        typeAnimated: true,
        animation: 'opacity',
        columnClass: 'col-12 col-lg-10',
        theme: 'modern',
        icon: 'fa fa-question',
        buttons: {
          confirm: {
            text: 'Tak',
            btnClass: 'btn-success',
            keys: ['enter'],
            action: function() {
              document.location.href = href;
            }
          },
          cancel: {
            text: 'Nie',
            btnClass: 'btn-dark',
            action: function() {}
          }
        }
      });
    });
  });

  $('body').on('click', '.set_order_as_unpaid', function() {
    var href = $(this).attr('href');
    $.alert({
      title: 'Pytanie',
      content: 'Zmienić zamówienie na nieopłacone?',
      type: 'orange',
      closeIcon: true,
      closeIconClass: 'fa fa-times',
      typeAnimated: true,
      animation: 'opacity',
      columnClass: 'col-12 col-lg-10',
      theme: 'modern',
      icon: 'fa fa-question',
      buttons: {
        confirm: {
          text: 'Tak',
          btnClass: 'btn-success',
          keys: ['enter'],
          action: function() {
            document.location.href = href;
          }
        },
        cancel: {
          text: 'Nie',
          btnClass: 'btn-dark',
          action: function() {}
        }
      }
    });
    return false;
  });

  $('body').on('click', '.set_order_as_paid', function() {
    var href = $(this).attr('href');
    $.alert({
      title: 'Pytanie',
      content: 'Zmienić zamówienie na opłacone?',
      type: 'orange',
      closeIcon: true,
      closeIconClass: 'fa fa-times',
      typeAnimated: true,
      animation: 'opacity',
      columnClass: 'col-12 col-lg-10',
      theme: 'modern',
      icon: 'fa fa-question',
      buttons: {
        confirm: {
          text: 'Tak',
          btnClass: 'btn-success',
          keys: ['enter'],
          action: function() {
            document.location.href = href;
          }
        },
        confirm2: {
          text: 'Tak (wyślij mail)',
          btnClass: 'btn-primary',
          keys: ['enter'],
          action: function() {
            document.location.href = href + '&send_mail=1';
          }
        },
        cancel: {
          text: 'Nie',
          btnClass: 'btn-dark',
          action: function() {}
        }
      }
    });
    return false;
  });

  $('body').on('click', '.order_status_change_email', function() {
    $.alert({
      title: 'Pytanie',
      content: 'Na pewno chcesz wysłać mail o zmianie statusu?',
      type: 'orange',
      closeIcon: true,
      closeIconClass: 'fa fa-times',
      typeAnimated: true,
      animation: 'opacity',
      columnClass: 'col-12 col-lg-10',
      theme: 'modern',
      icon: 'fa fa-question',
      buttons: {
        confirm: {
          text: 'Tak',
          btnClass: 'btn-success',
          keys: ['enter'],
          action: function() {
            order_status_change(orderId, $('#order-status').val(), true);
          }
        },
        cancel: {
          text: 'Nie',
          btnClass: 'btn-dark',
          action: function() {}
        }
      }
    });

    return false;
  });

  $('body').on('click', '.resend_order_confirmation_email button', function() {
    $.alert({
      title: 'Pytanie',
      content: 'Na pewno chcesz wysłać mail o złożonym zamówieniu?',
      type: 'orange',
      closeIcon: true,
      closeIconClass: 'fa fa-times',
      typeAnimated: true,
      animation: 'opacity',
      columnClass: 'col-12 col-lg-10',
      theme: 'modern',
      icon: 'fa fa-question',
      buttons: {
        confirm: {
          text: 'Tak',
          btnClass: 'btn-success',
          keys: ['enter'],
          action: function() {
            $.ajax({
              type: 'POST',
              cache: false,
              url: '/admin/shop_order/order_resend_confirmation_email/',
              data: {
                order_id: orderId
              },
              beforeSend: function() {
                $('#overlay').show();
              },
              success: function(response) {
                $('#overlay').hide();
                var data = jQuery.parseJSON(response);

                if (data.result === true) {
                  return $.alert({
                    title: 'Informacja',
                    content: 'Wiadomość została wysłana',
                    type: 'orange',
                    closeIcon: true,
                    closeIconClass: 'fa fa-close',
                    typeAnimated: true,
                    animation: 'opacity',
                    autoClose: 'confirm|10000',
                    columnClass: 'col-12 col-lg-10',
                    theme: 'modern',
                    icon: 'fa fa-info',
                    buttons: {
                      confirm: {
                        text: 'Zamknij',
                        btnClass: 'btn-success',
                        keys: ['enter'],
                        action: function() {
                          location.reload();
                        }
                      }
                    }
                  });
                }

                if (data.result === false) {
                  return $.alert({
                    title: 'Błąd',
                    content: 'Podczas wysyłania wiadomości wystąpił błąd',
                    type: 'orange',
                    closeIcon: true,
                    closeIconClass: 'fa fa-close',
                    typeAnimated: true,
                    animation: 'opacity',
                    columnClass: 'col-12 col-lg-10',
                    theme: 'modern',
                    icon: 'fa fa-exclamation',
                    buttons: {
                      confirm: {
                        text: 'Zamknij',
                        btnClass: 'btn-danger',
                        keys: ['enter'],
                        action: function() {
                          location.reload();
                        }
                      }
                    }
                  });
                }
              }
            });
          }
        },
        cancel: {
          text: 'Nie',
          btnClass: 'btn-dark',
          action: function() {}
        }
      }
    });

    return false;
  });

  $('body').on('click', '.order_status_change', function() {
    order_status_change(orderId, $('#order-status').val(), false);
    return false;
  });

  function order_status_change(order_id, status, email) {
    $.ajax({
      type: 'POST',
      cache: false,
      url: '/admin/shop_order/order_status_change/',
      data: {
        order_id: order_id,
        status: status,
        email: email
      },
      beforeSend: function() {
        $('#overlay').show();
      },
      success: function(response) {
        $('#overlay').hide();
        var data = jQuery.parseJSON(response);

        if (data.email === true) {
          return $.alert({
            title: 'Informacja',
            content: 'Wiadomość o zmiane statusu została wysłana',
            type: 'orange',
            closeIcon: true,
            closeIconClass: 'fa fa-close',
            typeAnimated: true,
            animation: 'opacity',
            autoClose: 'confirm|10000',
            columnClass: 'col-12 col-lg-10',
            theme: 'modern',
            icon: 'fa fa-info',
            buttons: {
              confirm: {
                text: 'Zamknij',
                btnClass: 'btn-success',
                keys: ['enter'],
                action: function() {
                  location.reload();
                }
              }
            }
          });
        }

        if (data.email === false) {
          return $.alert({
            title: 'Błąd',
            content: 'Podczas wysyłania wiadomości wystąpił błąd',
            type: 'orange',
            closeIcon: true,
            closeIconClass: 'fa fa-close',
            typeAnimated: true,
            animation: 'opacity',
            columnClass: 'col-12 col-lg-10',
            theme: 'modern',
            icon: 'fa fa-exclamation',
            buttons: {
              confirm: {
                text: 'Zamknij',
                btnClass: 'btn-danger',
                keys: ['enter'],
                action: function() {
                  location.reload();
                }
              }
            }
          });
        }

        if (data.result == true) {
          return $.alert({
            title: 'Informacja',
            content: 'Status zamówienia został zmieniony',
            type: 'orange',
            closeIcon: true,
            closeIconClass: 'fa fa-close',
            typeAnimated: true,
            animation: 'opacity',
            autoClose: 'confirm|10000',
            columnClass: 'col-12 col-lg-10',
            theme: 'modern',
            icon: 'fa fa-info',
            buttons: {
              confirm: {
                text: 'Zamknij',
                btnClass: 'btn-success',
                keys: ['enter'],
                action: function() {
                  location.reload();
                }
              }
            }
          });
        }
      }
    });
  }

  $('body').on('click', '.btn-toggle-trustmate', function(e) {
    e.preventDefault();

    $.alert({
      title: 'Potwierdź',
      content: currentTrustmateState
        ? 'Czy na pewno chcesz odznaczyć zamówienie jako wysłane do trustmate.io?'
        : 'Czy na pewno chcesz zaznaczyć zamówienie jako wysłane do trustmate.io?',
      type: 'orange',
      closeIcon: true,
      closeIconClass: 'fa fa-times',
      typeAnimated: true,
      animation: 'opacity',
      columnClass: 'col-12 col-lg-10',
      theme: 'modern',
      icon: 'fa fa-question',
      buttons: {
        confirm: {
          text: 'Tak',
          btnClass: 'btn-success',
          keys: ['enter'],
          action: function() {
            $.ajax({
              type: 'POST',
              cache: false,
              url: '/admin/shop_order/toggle_trustmate_send/',
              data: {
                order_id: orderId
              },
              beforeSend: function() {
                $('#overlay').show();
              },
              success: function(response) {
                $('#overlay').hide();
                var data = jQuery.parseJSON(response);
                if (data.result === true) {
                  location.reload();
                }
              }
            });
          }
        },
        cancel: {
          text: 'Nie',
          btnClass: 'btn-dark',
          action: function() {}
        }
      }
    });
  });
})();
</script>
