{extends "index.tpl"}

{block "meta-robots"}
   <meta name="robots" content="noindex, nofollow">
{/block}

{block "css-entry" append}
   {encore_entry_link_tags entry='cart'}
{/block}

{function cartBtns}
   {block 'cart-btns'}
      {if $dbcfg.gdpr.enable == 'Y' and $body.stepName == 'summary'}
         {include "block.ordering.gdpr.tpl"}
      {/if}

      {include "ordering/ordering.btns.tpl"}
   {/block}
{/function}

{block "body-container"}
   {block "tpl-vars"}
      {$tpl_vars.gdpr_checkbox = ($dbcfg.consent.enable == 'Y')}
   {/block}

   {function showPriceBox}
      {include "ordering/ordering.pricebox.tpl"}
   {/function}

   {block 'cart-vars'}
      {$cartVars=['butttonPrev'=>'btn-prevstep m-b-1','butttonNext'=>'btn-nextstep m-b-1','text'=>"{t}Pokračovat na další krok{/t}"]}
   {/block}

   <div class="container order-container">
      {block "order-process"}
         {include "ordering/ordering.process.tpl"}
      {/block}

      <div class="row">
         <div class="col-xs-12">

            <form name="login" method="post" action="" role="form" id="cart" class="cart-form cart-{$body.stepName}">
               {include "ordering/ordering.messages.tpl"}

               {block "order-content"}
               {/block}
            </form>
         </div>
      </div>
   </div>
{/block}

{block "js-entry" append}
   {ifmodule WEBPACK}
   {encore_entry_script_tags entry='cart'}
   {/ifmodule}
{/block}

{block "js-dynamic-load" append}
   {asset_compile}
      <script src="/common/static/bootstrapvalidator/js/bootstrapValidator.js"></script>
      <script src="/common/static/bootstrapvalidator/js/language/{$ctrl.active_language_code}.js"></script>
   {/asset_compile}
{/block}

<script>
   {block onready append}
   var $form = $('#cart');

   $form.bootstrapValidator({
      feedbackIcons: {
         valid: 'fc fc-check',
         invalid: 'fc fc-remove',
         validating: 'fc fc-refresh'
      }
   }).on('change', '[data-bv-notempty]', function(event) {
      var $this = $(event.target),
              elementName = $this.attr('name');

      $(event.target.form)
              // Get the bootstrapValidator instance
              .data('bootstrapValidator')
              // Mark the field as not validated, so it'll be re-validated when the user change date
              .updateStatus(elementName, 'NOT_VALIDATED', null)
              // Validate the field
              .validateField(elementName);
   }).on('validationError.validator.bv', function(e, data) {
      // Disable multiple error messages for one field
      data.element.data('bv.messages')
              // Hide all the messages
              .find('.help-block[data-bv-for="' + data.field + '"]').hide()
              // Show only message associated with current validator
              .filter('[data-bv-validator="' + data.validator + '"]').show();
   });

   // Openers
   wpj.domUtils.initOpeners();

   // Cart
   var delivery_types = {
      {foreach $body.delivery_types as $id => $delivery}
      "{$delivery.id}": [{$delivery.id_delivery}, {$delivery.id_payment}]{if !$delivery@last},{/if}
      {/foreach}
   };

   var formValidation = $form.data('bootstrapValidator');

   {block "cart-init"}
   $('#cart').cart({
      delivery_types: delivery_types,
      deliverychange: function() {
         formValidation.disableSubmitButtons(false);
      },
      paymentchange: function() {
         formValidation.disableSubmitButtons(false);
      },
      select_first: {$tpl_vars.preselect_delivery|default:"true"}
   });
   {/block}

   // spravne selectovani a disablovani doprav a plateb
   var looping = false;
   $('[data-cart="delivery"]').on('click', '.delivery-item', function(e) {
      if (looping)
         return;

      var $selectedInput = $(this).find('[name=\'delivery_id\']:not(disabled):not(:checked)');
      looping = true;
      $selectedInput.click();
      looping = false;
   });

   $('[data-cart="delivery"]').on('click', '.payment-item:not(.disabled) ', function(e) {
      $(this).find('[name=\'payment_id\']:not(disabled)').prop('checked', true).trigger('change');
   });

   {if $body.error_id}
   var $alert = $('.alert').first();
   if ($alert.length && !wpj.domUtils.isInView($alert)) {
      wpj.domUtils.scrollTo($alert);
   }
   {/if}

   // todo do shared
   $form.on('submit', function() {
      var err = $('.has-error').first();

      if (err.length && !wpj.domUtils.isInView(err)) {
         wpj.domUtils.scrollTo(err);
      }

      if ($('.signinrow.invalid-email').length) {
         wpj.domUtils.scrollTo($('.signinrow.invalid-email'));
         return false;
      }

      return true;
   });

   {include "block.gtm.tpl" gtm_type="cartOnReady"}

   {/block}
</script>
