<? if ( $this -> shoppro_enabled ):?>
<script type="text/javascript">
  $(function() {
    var $header = $( '.panel-heading .col-sm-8' );
    if ( $header.length ) {
      $header.append( ' <a href="#" class="btn btn-danger btn-sm btn-shoppro-product-import"><i class="fa fa-download mr5"></i>Pobierz produkt z shopPRO</a>' );
    }
  });
</script>
<? endif;?>

<style type="text/css">
  .product-image {
    display: inline-block;
    vertical-align: top;
    width: 50px;
    margin-right: 8px;
  }
  .product-image img {
    max-width: 50px;
    max-height: 50px;
  }
  .product-name {
    display: inline-block;
    vertical-align: top;
  }
  .product-categories {
    display: block;
  }
</style>

<script type="text/javascript">
$(function() {

  // --- Inline price save ---
  $( 'body' ).on( 'change', '.product-price', function() {
    var $el = $( this );
    var price = $el.val().replace( ' ', '' );
    price = parseFloat( price.replace( ',', '.' ) * 1 );
    price = number_format( price, 2, '.', '' );
    $el.val( price );

    $.ajax({
      type: 'POST',
      cache: false,
      url: '/admin/shop_product/product_change_price_brutto/',
      data: { product_id: $el.attr( 'product-id' ), price: price },
      beforeSend: function() { $( '#overlay' ).show(); },
      success: function( data ) {
        $( '#overlay' ).hide();
        var response = jQuery.parseJSON( data );
        if ( response.status !== 'ok' ) create_error( response.msg );
      }
    });
  });

  $( 'body' ).on( 'change', '.product-price-promo', function() {
    var $el = $( this );
    var price = $el.val().replace( ' ', '' );
    price = parseFloat( price.replace( ',', '.' ) * 1 );
    price = number_format( price, 2, '.', '' );
    $el.val( price );

    $.ajax({
      type: 'POST',
      cache: false,
      url: '/admin/shop_product/product_change_price_brutto_promo/',
      data: { product_id: $el.attr( 'product-id' ), price: price },
      beforeSend: function() { $( '#overlay' ).show(); },
      success: function( data ) {
        $( '#overlay' ).hide();
        var response = jQuery.parseJSON( data );
        if ( response.status !== 'ok' ) create_error( response.msg );
      }
    });
  });

  // --- Duplicate product ---
  $( 'body' ).on( 'click', '.duplicate-product', function(e) {
    e.preventDefault();
    var product_id = $( this ).attr( 'product-id' );

    $.alert({
      title: 'Pytanie',
      content: 'Na pewno chcesz wykonać duplikat produktu?',
      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 (produkt bez kombinacji)',
          btnClass: 'btn-success',
          keys: ['enter'],
          action: function() {
            document.location.href = '/admin/shop_product/duplicate_product/product-id=' + product_id;
          }
        },
        confirm2: {
          text: 'Tak (produkt z KOMBINACJAMI)',
          btnClass: 'btn-primary',
          action: function() {
            document.location.href = '/admin/shop_product/duplicate_product/product-id=' + product_id + '&combination=1';
          }
        },
        cancel: {
          text: 'Nie',
          btnClass: 'btn-dark',
          action: function() {}
        }
      }
    });
  });

  // --- shopPRO import ---
  $( 'body' ).on( 'click', '.btn-shoppro-product-import', function(e) {
    e.preventDefault();

    $.alert({
      title: 'Import produktu',
      content: 'Wprowadź ID produktu z shopPRO',
      type: 'orange',
      closeIcon: true,
      closeIconClass: 'fa fa-times',
      typeAnimated: true,
      animation: 'opacity',
      columnClass: 'col-12 col-lg-8',
      theme: 'material',
      icon: 'fa fa-exclamation-triangle',
      buttons: {
        confirm: {
          text: 'Importuj',
          btnClass: 'btn-success',
          keys: ['enter'],
          action: function() {
            var product_id = $( '#shoppro-product-id' ).val();
            if ( product_id ) {
              document.location.href = '/admin/integrations/shoppro_product_import/product_id=' + product_id;
            }
          }
        },
        cancel: {
          text: 'Anuluj',
          btnClass: 'btn-danger',
          action: function() {}
        }
      },
      onOpenBefore: function() {
        this.setContent( '<input type="text" class="form-control" id="shoppro-product-id" placeholder="ID produktu z shopPRO">' );
      }
    });
  });

  // --- Apilo ---
  $( 'body' ).on( 'click', '.apilo-product-search', function() {
    var product_id = $( this ).attr( 'product-id' );

    $.ajax({
      type: 'POST',
      cache: false,
      url: '/admin/integrations/apilo_product_search/',
      data: { product_id: product_id },
      beforeSend: function() { $( '#overlay' ).show(); },
      success: function( response ) {
        $( '#overlay' ).hide();
        var data = jQuery.parseJSON( response );

        if ( data.status == 'SUCCESS' ) {
          if ( data.products.length == 0 ) {
            var html = '<div class="apilo-found-products">';
            html += '<p>Nie znaleziono produktów</p>';
            html += '<a href="/admin/integrations/apilo_create_product/product-id=' + product_id + '" class="btn btn-success btn_apilo_create_product" product_id="' + product_id + '">Utwórz produkt</a> ';
            html += '<button class="btn btn-default apilo-cancel">Anuluj</button>';
            html += '</div>';
            $( 'span.apilo-product-search[product-id="' + product_id + '"]' ).closest( 'td' ).append( html );
          } else {
            var html = '<div class="apilo-found-products">';
            html += '<p>Znaleziono ' + data.products.length + ' produktów</p>';
            html += '<select class="form-control apilo-product-select" product-id="' + product_id + '">';
            $.each( data.products, function( index, value ) {
              html += '<option value="' + value.id + '">' + value.name + ' SKU: ' + value.sku + '</option>';
            });
            html += '</select>';
            html += '<button class="btn btn-success apilo-product-select-save" product-id="' + product_id + '">Zapisz</button> ';
            html += '<button class="btn btn-default apilo-cancel">Anuluj</button>';
            html += '</div>';
            $( 'span.apilo-product-search[product-id="' + product_id + '"]' ).closest( 'td' ).append( html );
          }
        } else if ( data.status == 'error' ) {
          $.alert({
            title: 'Błąd',
            content: data.msg,
            type: 'red',
            closeIcon: true,
            closeIconClass: 'fa fa-times',
            typeAnimated: true,
            animation: 'opacity',
            columnClass: 'col-12 col-lg-10',
            theme: 'modern',
            icon: 'fa fa-exclamation-triangle',
            buttons: {
              confirm: {
                text: 'OK',
                btnClass: 'btn-danger',
                keys: ['enter'],
                action: function() {}
              }
            }
          });
        }
      }
    });
  });

  $( 'body' ).on( 'click', '.apilo-cancel', function() {
    $( this ).closest( '.apilo-found-products' ).remove();
  });

  $( 'body' ).on( 'click', '.apilo-delete-linking', function() {
    var product_id = $( this ).attr( 'product-id' );

    $.ajax({
      type: 'POST',
      cache: false,
      url: '/admin/integrations/apilo_product_select_delete/',
      data: { product_id: product_id },
      beforeSend: function() { $( '#overlay' ).show(); },
      success: function( response ) {
        $( '#overlay' ).hide();
        var data = jQuery.parseJSON( response );
        if ( data.status == 'ok' ) {
          $( 'span.apilo-delete-linking[product-id="' + product_id + '"]' ).closest( 'td' ).html( '<span class="text-danger apilo-product-search" product-id="' + product_id + '">nie przypisano <i class="fa fa-search"></i></span>' );
        }
      }
    });
  });

  $( 'body' ).on( 'click', '.btn_apilo_create_product', function(e) {
    e.preventDefault();
    var product_id = $( this ).attr( 'product_id' );

    $.alert({
      title: 'Pytanie',
      content: 'Na pewno chcesz utworzyć produkt w bazie Apilo?',
      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 = '/admin/integrations/apilo_create_product/product_id=' + product_id;
          }
        },
        cancel: {
          text: 'Nie',
          btnClass: 'btn-danger',
          action: function() {}
        }
      }
    });
  });

  $( 'body' ).on( 'click', '.apilo-product-select-save', function() {
    var product_id = $( this ).attr( 'product-id' );
    var apilo_product_id = $( '.apilo-product-select[product-id="' + product_id + '"]' ).val();
    var apilo_product_name = $( '.apilo-product-select[product-id="' + product_id + '"] option:selected' ).text();

    $.ajax({
      type: 'POST',
      cache: false,
      url: '/admin/integrations/apilo_product_select_save/',
      data: {
        product_id: product_id,
        apilo_product_id: apilo_product_id,
        apilo_product_name: apilo_product_name
      },
      beforeSend: function() { $( '#overlay' ).show(); },
      success: function( response ) {
        $( '#overlay' ).hide();
        var data = jQuery.parseJSON( response );
        if ( data.status == 'ok' ) {
          $( '.apilo-product-select[product-id="' + product_id + '"]' ).closest( '.apilo-found-products' ).remove();
          $( 'span.apilo-product-search[product-id="' + product_id + '"]' ).html( '<span title="' + apilo_product_name + '">' + apilo_product_name.substr( 0, 25 ) + '...</span>' ).removeClass( 'apilo-product-search' ).removeClass( 'text-danger' );
        }
      }
    });
  });

});
</script>
