<div id="basket-container">
  <div id="content">
    <?= $this -> basket_details;?>
  </div>
  <? if ( is_array( $this -> basket ) and count( $this -> basket ) ):?>
    <div class="basket-options">
      <div class="content">
        <div class="left">
          <div class="box-title"><?= ucfirst( \S::lang( 'dostawa' ) );?>:</div>
          <div id="transport-methods">
            <?= $this -> transport_methods;?>
          </div>
          <div class="inpost-map-container">
            <a href="#" onclick="inpost_check(); return false;" class="inpost-hide"><?= \S::lang( 'zamknij' );?></a>
            <div id="inpost-map"></div>
          </div>
        </div>
        <div class="right">
          <div class="box-title"><?= ucfirst( \S::lang( 'platnosc' ) );?>:</div>
          <div id="payment-methods"></div>
        </div>
      </div>
    </div>
    <div class="row">
      <div class="col-12">
        <a href="/koszyk-podsumowanie" id="btn-basket-summary" class="btn btn-success disabled float-right"><span class="text"><?= ucfirst( \S::lang( 'przejdz-do-podsumowania' ) );?></span></a>
      </div>
    </div>
  <? endif;?>
</div>
<link class="footer" rel="stylesheet" type="text/css" href="/libraries/grid/plugins/icheck/skins/square/_all.css">
<link class="footer" rel="stylesheet" type="text/css" href="https://geowidget.easypack24.net/css/easypack.css">
<script class="footer" type="text/javascript" src="/libraries/grid/plugins/icheck/icheck.min.js"></script>
<script class="footer" type="text/javascript" src="https://geowidget.easypack24.net/js/sdk-for-javascript.js"></script>
<script class="footer" type="text/javascript">
  var inpost_transport_method_id = '';

  function inpost_check() {

    $.ajax({
      type: 'POST',
      cache: false,
      url: '/shopBasket/inpost_check',
      beforeSend: function() {

      },
      success: function( response ) {
        data = jQuery.parseJSON( response );
        if ( data.result == 'bad' ) {
          $( '.inpost-info-' + inpost_transport_method_id ).html( '<a href="#" onclick="inpost_map( ' + inpost_transport_method_id + ' ); return false;">wybierz</a>' );
        }

        $( '.inpost-map-container' ).hide();
      }
    });
    return false;
  }

  function payment_checked( payment_method_id )
  {
    $.ajax(
    {
      type: 'POST',
      cache: false,
      url: '/shopBasket/basket_payment_method_set',
      data:
      {
        payment_method_id: payment_method_id
      },
      beforeSend: function()
      {
        $( '#basket-container *' ).addClass( 'disabled' );
        $( '#payment-methods .icheck, #transport-methods .icheck' ).iCheck( 'disable' );
      },
      success: function( response )
      {
        response = jQuery.parseJSON( response );
        $( '#basket-container *' ).removeClass( 'disabled' );
        $( '#payment-methods .icheck, #transport-methods .icheck' ).iCheck( 'enable' );
        $( '#btn-basket-summary' ).removeClass( 'disabled' );
      }
    });
    return false;
  }

  function inpost_map( transport_method_id )
  {
    inpost_transport_method_id = transport_method_id;

    window.easyPackAsyncInit = function () {
      easyPack.init({
        mapType: 'osm',
        searchType: 'osm',
      });

      var map = easyPack.mapWidget( 'inpost-map', function(point)
      {
        $.ajax(
        {
          type: 'POST',
          cache: false,
          url: '/shopBasket/inpost_save',
          data: {
            paczkomat: point.name + ' | ' + point.address.line1 + ', ' + point.address.line2
          },
          beforeSend: function() {},
          success: function( response ) {
            $( '.inpost-info-' + transport_method_id ).html( point.name + ' | ' + point.address.line1 + ', ' + point.address.line2 + '<a href="#" onclick="inpost_map( ' + transport_method_id + ' ); return false;">zmień</a>' );
            $( '.inpost-map-container' ).hide();
          }
        });
      });
    };
    $( '.inpost-map-container' ).show();
  }

  function transport_checked( transport_method_id, payment_method_id, event_click )
  {
    $.ajax({
      type: 'POST',
      cache: false,
      url: '/shopBasket/basket_payments_methods',
      data:
      {
        payment_method_id: payment_method_id,
        transport_method_id: transport_method_id
      },
      beforeSend: function()
      {
        $( '#basket-container *' ).addClass( 'disabled' );
        $( '#transport-methods .icheck, #transport-methods .icheck' ).iCheck( 'disable' );
      },
      success: function( response )
      {
        response = jQuery.parseJSON( response );
        $( '#basket-container *' ).removeClass( 'disabled' );
        $( '#transport-methods .icheck, #transport-methods .icheck' ).iCheck( 'enable' );
        $( '#payment-methods' ).html( response.payment_methods );
        $( '#payment-methods .icheck' ).iCheck({
          checkboxClass: 'icheckbox_square-blue',
          radioClass: 'iradio_square-blue'
        });

        if ( payment_method_id )
          $( '#btn-basket-summary' ).removeClass( 'disabled' );

        $( '#payment-methods .icheck' ).on( 'ifChecked', function( event )
        {
          var payment_method_id = $( this ).val();
          payment_checked( payment_method_id );
        });

        if ( event_click && ( transport_method_id == '1' || transport_method_id == '2' ) )
          inpost_map( transport_method_id )
      }
    });
    return false;
  }

  $( function()
  {
    $( 'body' ).on( 'change', '#basket .basket-product-message textarea', function()
    {
      var _this = $( this);

      $.ajax({
        type: 'POST',
        cache: false,
        url: '/shopBasket/product_message_change',
        data: {
          product_message: _this.val(),
          position_code: _this.attr( 'position' )
        },
        beforeSend: function() {},
        success: function() {}
      });
    });

    $( 'body' ).on( click_event, '#btn-basket-summary', function(e) {

      e.preventDefault();
      $.ajax({
        type: 'POST',
        cache: false,
        url: '/shopBasket/transport_method_inpost_check',
        data: {},
        beforeSend: function() {
          $( '#basket-container *' ).addClass( 'disabled' );
        },
        success: function( response ) {

          data = jQuery.parseJSON( response );
          $( '#basket-container *' ).removeClass( 'disabled' );
          if ( data.result === 'bad' ) {

            $.alert({

              title: '<?= ucfirst( \S::lang( 'uwaga' ) );?>',
              content: 'Proszę wybrać paczkomat.',
              type: 'blue',
              closeIcon: true,
              closeIconClass: 'fas fa-times',
              typeAnimated: true,
              animation: 'opacity',
              useBootstrap: false,
              theme: 'modern',
              autoClose: 'confirm|5000',
              icon: 'fas fa-exclamation',
              buttons: {
                confirm: {
                  text: '<?= \S::lang( 'zamknij' );?>',
                  btnClass: 'btn-blue',
                  keys: ['enter'],
                  action: function () {}
                }
              }
            });
          } else {
            document.location.href = '/koszyk-podsumowanie';
          }
        }
      });
    });

    $( 'body' ).on( 'change', '#basket-message', function()
    {
      var _this = $( this);
      $.ajax({
        type: 'POST',
        cache: false,
        url: '/shopBasket/basket_message_save',
        data: {
          basket_message: _this.val()
        },
        beforeSend: function() {
          $( '#basket-container *' ).addClass( 'disabled' );
        },
        success: function( response ) {
          data = jQuery.parseJSON( response );

          $( '#basket-container *' ).removeClass( 'disabled' );
        }
      });
    });

    $( 'body' ).on( 'change', '.basket-product .int-format', function()
    {
      var product_hash = $( this ).attr( 'product-hash' );
      var quantity = $( this ).val();

      $.ajax(
      {
        type: 'POST',
        cache: false,
        url: '/shopBasket/basket_change_quantity_product',
        data:
        {
          product_hash: product_hash,
          quantity: quantity
        },
        beforeSend: function()
        {
          $( '#basket-container *' ).addClass( 'disabled' );
          $( '#payment-methods .icheck, #transport-methods .icheck' ).iCheck( 'disable' );
        },
        success: function( response )
        {
          data = jQuery.parseJSON( response );

          if ( data.products_count === 0 )
          {
            document.location.href = '/koszyk';
            $( '.mini-box' ).hide();
            return false;
          }

          $( '#basket-container *' ).removeClass( 'disabled' );
          $( '#payment-methods .icheck, #transport-methods .icheck' ).iCheck( 'enable' );
          $( '#basket-container #content' ).html( data.basket );

          $( '#basket-mini #products-count' ).html( data.basket_mini_count );
          $( '#basket-mini #basket-value').html( data.basket_mini_value );

          $( '#basket-container #transport-methods' ).html( data.transport_methods );
        }
      });
      return false;
    });

    $( 'body' ).on( click_event, '.basket-product .btn-minus', function() {
      var product_hash = $( this ).attr( 'product-hash' );

      $.ajax({
        type: 'POST',
        cache: false,
        url: '/shopBasket/basket_decrease_quantity_product',
        data: {
          product_hash: product_hash
        },
        beforeSend: function() {
          $( '#basket-container *' ).addClass( 'disabled' );
          $( '#payment-methods .icheck, #transport-methods .icheck' ).iCheck( 'disable' );
        },
        success: function( response ) {

          data = jQuery.parseJSON( response );

          if ( data.products_count === 0 ) {
            document.location.href = '/koszyk';
            $( '.mini-box' ).hide();
            return false;
          }

          $( '#basket-container *' ).removeClass( 'disabled' );
          $( '#payment-methods .icheck, #transport-methods .icheck' ).iCheck( 'enable' );
          $( '#basket-container #content' ).html( data.basket );

          $( '#basket-mini #products-count' ).html( data.basket_mini_count );
          $( '#basket-mini #basket-value').html( data.basket_mini_value );

          $( '#basket-container #transport-methods' ).html( data.transport_methods );
        }
      });
      return false;
    });

    $( 'body' ).on( click_event, '.basket-product .btn-plus', function()
    {
      var product_hash = $( this ).attr( 'product-hash' );

      $.ajax(
      {
        type: 'POST',
        cache: false,
        url: '/shopBasket/basket_increase_quantity_product',
        data: {
          product_hash: product_hash
        },
        beforeSend: function() {
          $( '#basket-container *' ).addClass( 'disabled' );
          $( '#payment-methods .icheck, #transport-methods .icheck' ).iCheck( 'disable' );
        },
        success: function( response ) {
          data = jQuery.parseJSON( response );

          if ( data.products_count === 0 )
          {
            document.location.href = '/koszyk';
            $( '.mini-box' ).hide();
            return false;
          }

          $( '#basket-container *' ).removeClass( 'disabled' );
          $( '#payment-methods .icheck, #transport-methods .icheck' ).iCheck( 'enable' );
          $( '#basket-container #content' ).html( data.basket );

          $( '#basket-mini #products-count' ).html( data.basket_mini_count );
          $( '#basket-mini #basket-value').html( data.basket_mini_value );

          $( '#basket-container #transport-methods' ).html( data.transport_methods );
        }
      });
      return false;
    });

    $( 'body' ).on( click_event, '.basket-product .btn-delete', function()
    {
      var product_hash = $( this ).attr( 'product-hash' );

      $.alert({
        title: '<?= ucfirst( \S::lang( 'potwierdz' ) );?>',
        content: '<?= \S::lang( 'potwierdz-usuniecie-produktu-z-koszyka' );?>',
        closeIcon: true,
        closeIconClass: 'fas fa-close',
        typeAnimated: true,
        animation: 'opacity',
        useBootstrap: false,
        theme: 'modern',
        autoClose: 'cancel|10000',
        buttons: {
          confirm: {
            text: '<?= \S::lang( 'usun' );?>',
            btnClass: 'btn-orange',
            keys: ['enter'],
            action: function() {
              $.ajax({
                type: 'POST',
                cache: false,
                url: '/shopBasket/basket_remove_product',
                data: {
                  product_hash: product_hash
                },
                beforeSend: function() {
                  $( '#basket-container *' ).addClass( 'disabled' );
                  $( '#payment-methods .icheck, #transport-methods .icheck' ).iCheck( 'disable' );
                },
                success: function( response )
                {
                  data = jQuery.parseJSON( response );

                  if ( data.products_count === 0 )
                  {
                    document.location.href = '/koszyk';
                    $( '.mini-box' ).hide();
                    return false;
                  }

                  $( '#basket-container *' ).removeClass( 'disabled' );
                  $( '#payment-methods .icheck, #transport-methods .icheck' ).iCheck( 'enable' );
                  $( '#basket-container #content' ).html( data.basket );

                  $( '#basket-mini #products-count' ).html( data.basket_mini_count );
                  $( '#basket-mini #basket-value').html( data.basket_mini_value );

                  $( '#basket-container #transport-methods' ).html( data.transport_methods );
                }
              });
            }
          },
          cancel: {
            text: '<?= \S::lang( 'zamknij' );?>',
            btnClass: 'btn-blue',
            action: function() {}
          }
        }
      });
      return false;
    });
  });
</script>