<div class="delivery-class" data-delivery-class="{$deliveryPrefix}[ulozenka]">
   {if $object.cart_description}
      {$object.cart_description nofilter}
   {else}
      <p>Zásilky běžně doručujeme do&nbsp;druhého dne.</p>
   {/if}
   {$info = $object->getInfo($object.ulozenka_id)}
   <div class="pos-rel">
      <input type="hidden" name="{$deliveryPrefix}[ulozenka_id]" value="{$info.id}"">
      <input name="{$deliveryPrefix}[delivery_ulozenka_zip]" value="{$info.address}" class="form-control" placeholder="PSČ, město"
             autocomplete="off">
      <button type="button" class="delete-zip" title="{t}Smazat hledané PSČ, město{/t}"><span class="fc lightbox_close"></span>
      </button>
   </div>
   <div data-reload="ulozenka_{$object.id}" class="delivery-class-info">
      {if $info.zip}
         {if $info.id}
            <input type="hidden" name="delivery_ulozenka_valid" value="1">
            <p><strong>Zboží doručíme na pobočku:</strong></p>
            <p><strong>{$info.name}</strong><br>
               {$info.street}<br>{$info.zip} {$info.city}<br>
            </p>
         {else}
            <p><strong>Pobočka nenalezena</strong></p>
            <p>Na hledaném místě neexistuje žádná pobočka Uloženky.</p>
         {/if}
      {/if}
   </div>
</div>

<script id="{$deliveryPrefix}[ulozenkaSearch]" type="text/x-dot-template">
   {literal}
      <div class="ac-psc">
         <ul>
            {{? it.items.length > 0}}
            {{~it.items :item :index}}
            <li data-autocomplete-item="product" data-autocomplete-value="{{=item.value}}"
                data-autocomplete-name="{{=item.zip}} {{=item.name}}">
               <span>{{=item.zip}} {{=item.name}}</span>
            </li>
            {{~}}
            {{??}}
            <li>Pobočka Uloženky nenalezena</li>
            {{?}}
         </ul>
      </div>
   {/literal}
</script>

<script>
   wpj.onReady.push(function() {
      var errNotFound = "{t}Pobočka Uloženky nebyla nalezena. Vyberte prosím jinou pobočku, nebo změňte způsob dopravy.{/t}";

      var $deliveryWrapper = $('[data-delivery-class="{$deliveryPrefix}[ulozenka]"]');
      var $input = $deliveryWrapper.find('input[name="{$deliveryPrefix}[delivery_ulozenka_zip]"]');
      var $inputId = $deliveryWrapper.find(`input[name="{{$deliveryPrefix}}[ulozenka_id]"]`);

      {literal}

      $input.deliveryAutocomplete({
         source: '{/literal}{path('kupshop_catalog_autocomplete_autocomplete', ['type' => 'delivery_ulozenka'])}{literal}&term={{=it.value}}',
         template: '#{/literal}{$deliveryPrefix|replace:'[':'\\\['|replace:']':'\\\]'}{literal}\\[ulozenkaSearch\\]',
         inputId: $inputId
      });

      $input.closest('form').on('success.form.bv', function(w, hasError) {
         if ($input.is(':visible') && $input.parents(':hidden').length === 0) {
            if (!$('[name="delivery_ulozenka_valid"]').val()) {
               window.alert(errNotFound);
               if (typeof hasError !== "undefined") {
                  hasError.error = true;
               }
               setTimeout(function() {
                  $input.get(0).focus();
               }, 100);
               return false;
            }
         }
      });

      {/literal}
   });
</script>
