{extends "ordering.tpl"}

{block "content"}
    {if $tpl_vars.delivery_country_picker}
        {block 'delivery-country-picker'}
            {include 'ordering/ordering.delivery.country.tpl'}
        {/block}
    {/if}
   {$deliveries = $deliveries|default:$body->getDeliveries()}
   {$payments = $payments|default:$body->getPayments()}
   <div class="deliveries {if $tpl_vars.delivery_collapsible}deliveries-collapsible{/if}
      {if $body.delivery_id and $deliveries[$body.delivery_id]}delivery-selected{/if} {if $body.payment_id and $payments[$body.payment_id]}payment-selected{/if}" data-cart="delivery">
      <div data-box="deliveries" class="deliveries-box">
          {include "ordering/ordering.delivery.deliveries.tpl"}
      </div>
      <div data-box="payments" class="payments-box">
          {include "ordering/ordering.delivery.payments.tpl"}
      </div>
   </div>
{/block}

{block 'cart-footer'}
   <div class="cart-footer">
       {include "ordering/ordering.pricebox.tpl" transport=1}
   </div>
{/block}

{block 'cart-btns'}
    {if !$tpl_vars.btns_in_sidebar}
        {include "ordering/ordering.btns.tpl" submitName='OrderNext' valueNext='user'}
    {/if}
{/block}

{block 'main' append}
   <span data-alert-choose-delivery="{t}Vyberte prosím způsob dopravy{/t}"></span>
   <span data-alert-choose-payment="{t}Vyberte prosím typ platby{/t}"></span>
{/block}

{block "js-dynamic-load" append}
   {$deliveries = $body->getDeliveries()}
   {$unique_deliveries = $deliveries|array_unique}
   {get_smarty assign='smarty_object'}

   {foreach $unique_deliveries as $id => $delivery}
      {$delivery->getInitTemplate($smarty_object)}
   {/foreach}

   {$payments = $body->getPayments()}
   {$unique_payments = []}
   {foreach $payments as $payment}
         {$unique_payments[] = $payment.class}
   {/foreach}

   {$unique_payments = $unique_payments|array_unique}
   {foreach $unique_payments as $id => $payment}
      {$payment->getInitTemplate($smarty_object)}
   {/foreach}

   <script>
      var $form = $('#cart');

      $form.cart({
         delivery_types: {
             {foreach $body.delivery_types as $delivery}
            "{$delivery.id}": [{$delivery.id_delivery}, {$delivery.id_payment}]{if !$delivery@last},{/if}
             {/foreach}
         },
         select_first: {$tpl_vars.preselect_delivery|default:"true"},
          {block "cart-script-append"}{/block}
      }).on('click', '.delivery-item', function () {
         if (!$(this).closest('label').length) {
            var $selectedInput = $(this).find('[name=\'delivery_id\']:not(disabled):not(:checked)');
            $selectedInput.click();
         }
          {if $tpl_vars.delivery_collapsible}
         if (!wpj.domUtils.isInView($(this)) && !$(this).hasClass('delivery-item-odbernaprodejne')){
            wpj.domUtils.scrollTo($(this));
         }
          {/if}

      }).on('click', '.payment-item:not(.disabled) ', function () {
         if (!$(this).closest('label').length) {
            $(this).find('[name=\'payment_id\']:not(disabled)').prop('checked', true).trigger('change');
         }
         {if $tpl_vars.delivery_collapsible}
         if (!wpj.domUtils.isInView($(this))){
            wpj.domUtils.scrollTo($(this));
         }
         {/if}
      }).on('submit', function () {
         var $checked_delivery_wrapper = $(this).find('[name="delivery_id"]:checked').closest('[data-cart="item"]');

          {* data-delivery-validity_check rika, ze se jedna o typ dopravy, kde je potreba kontrolovat input.
             data-delivery-invalid rika, ze neni vybrana spravna pobocka. Obsah data atributu je chybova hlaska co se vypise v alertu.
             Nasazene napr. na delivery.zasilkovna.widget.tpl v X. *}

         if ($checked_delivery_wrapper.find('[data-delivery-validity_check]').length) {
            var $invalid = $checked_delivery_wrapper.find('[data-delivery-invalid]');

            if ($invalid.length) {
               window.alert($invalid.data('delivery-invalid'));
               return false;
            }
         }

         var hasError = {ldelim}{rdelim};
          {* zpetna kompatibilita, v cart.js je na success.form.bv navazany check doprav. *}
         $(this).trigger('success.form.bv', [hasError]);
         return !hasError.error;
      });

      wpj.domUtils.initOpeners();
   </script>
{/block}
