{extends "ordering.tpl"}

{block "order-content"}
   <h1>{$view->getTitle()}</h1>
   <div class="deliveries" data-cart="delivery">
      {$availability = $body->getAvailability()}
      {$deliveries = $body->getDeliveries()}
      {$payments = $body->getPayments()}

      {include "ordering/ordering.delivery.deliveries.tpl"}
      {include "ordering/ordering.delivery.payments.tpl"}
   </div>
{/block}

{block "js-dynamic-load" append}
    {$unique_deliveries = $deliveries|array_unique}
    {get_smarty assign='smarty_object'}

    {foreach $unique_deliveries as $id => $delivery}
        {$delivery->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"},
         custom_delivery_error_callback: true,
         deliveryerror: function() {
            var $placeholder = $('[data-delivery-error]');
            $('[name="OrderNext"]').prop('disabled', true).text("{t}Zvolte dopravu{/t}");

            if (!wpj.domUtils.isInView($placeholder)) {
               wpj.domUtils.scrollTo($placeholder);
            }
         },
         paymenterror: function() {
            var $placeholder = $('[data-payment-error]');
            $('[name="OrderNext"]').prop('disabled', true).text("{t}Zvolte platbu{/t}");

            if (!wpj.domUtils.isInView($placeholder)) {
               wpj.domUtils.scrollTo($placeholder);
            }
         }
         {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();
         }
      }).on('click', '.payment-item:not(.disabled) ', function() {
         if (!$(this).closest('label').length) {
            $(this).find('[name=\'payment_id\']:not(disabled)').prop('checked', true).trigger('change');
         }
      }).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}
