{extends "orderView.tpl"}

{block "wtf-products"}
   <div class="messages" data-reload="messages">
      {if $body.failedDelivery}<div class="alert alert-danger" role="alert">{$body.failedDelivery nofilter}</div>{/if}
      {if $body.failedProducts}
         {$error = translate('error', 'order')}
         {$error = $error.not_stocked_products}
         {foreach $body.failedProducts as $product}
            {$productName = "{$product['title']} {$product['variation_title']}"|trim}
            {$message = str_replace('{PRODUCT}', $productName, $error)}
            <div class="alert alert-danger outOfStock" role="alert">{$message nofilter}</div>
         {/foreach}
      {/if}
   </div>
   <h3>{t}Objednané zboží{/t}</h3>
   <div class="table-cart" data-reload="products">
      {foreach $body.order.items as $product}
         <div class="product" data-cart="row">
            <div class="product-wrapper">
               <div class="col-xs-1 product-image">
                  {if $product.id_product}
                     <a title="{t}Zobrazit zboží{/t}" href="{url s=product IDproduct=$product.id_product TITLE=$product.descr}">
                        {if $product.photo}
                           <img src="{get_photo id=$product.photo image=2}" alt="{$product.descr}" class="img-responsive">
                        {/if}
                     </a>
                  {/if}
               </div>
               <div class="col-xs- product-title">
                  {if $product.id_product}
                     <a title="{t}Zobrazit zboží{/t}" href="{url s=product IDproduct=$product.id_product TITLE=$product.descr}">{$product.descr}</a>
                  {else}
                     {$product.descr}
                  {/if}
               </div>
               <div class="col-xs-2 product-pieces">
                  {if $product.id_product and $body.editable}
                     {if $product.pieces >= 0}
                        {$product.product->fetchParameters([23])|silent}
                        {$step = 1}
                        {if $product.product.param[23] and $product.product.param[23].value > 1}{$step = $product.product.param[23].value}{/if}
                        <input type="number" min="{$step}" step="{$step}" name="pieces[{$product.id}]" value="{$product.pieces}" class="form-control" data-cart="pieces">
                        <div class="buy_count">
                              <button type="button" class="fc fc-angle-up plus_unit" title="{t}Přidat 1 ks{/t}"></button>
                              <button type="button" class="fc fc-angle-down minus_unit" title="{t}Odebrat 1 ks{/t}"></button>
                        </div>
                     {else}
                        <input type="hidden" name="pieces[{$product.id}]" value="{$product.pieces}">
                        {$product.pieces} {t}Ks{/t}
                     {/if}
                  {else}
                     {$product.pieces} {t}Ks{/t}
                  {/if}
               </div>
               <div class="col-xs-2 product-price-total">
                  {$product.total_price.value_with_vat|format_price}
               </div>
               <div class="col-xs-1 product-delete">
                  {if $product.id_product and $body.editable and $product.pieces > 0}
                     <a href="{url s=orderView acn=deleteItem IDo=$body.order.id IDi=$product.id edit=true}" title="{t}Odstranit{/t}" data-cart="delete" class="delete-product">
                        <i class="fc fc-remove"></i>
                     </a>
                  {/if}
               </div>
            </div>
         </div>
      {/foreach}
   </div>
   {$coupons = $body.order->getPurchaseState()->getActiveCoupons()}
   {if $coupons}
      <p>{t}Slevový kód{/t}: <strong>{implode(', ', $coupons)}</strong> - {t}Sleva bude přičtena po potvrzení změn objednávky.{/t}</p>
   {/if}
{/block}

{block "content"}
   <form method="post" id="cart">
      {$smarty.block.parent}
   </form>
{/block}

{block "wtf" prepend}
<div data-reload="price">
   {/block}

   {block "price" append}
</div>

{********* DÁREK, SLEVY **********}
   <div data-reload="discount">
      {foreach $body.order->getPurchaseState()->getDiscountHandlers() as $handler}
         {$handler->render() nofilter}
      {/foreach}
   </div>

   <div class="row">
      {insert_cart_info return='cartInfo'}
      <div class="col-xl-4 p-b-1">
         {if $cartInfo.cartInfo.totalProducts > 0}
            <button class="btn btn-block btn-blue" type="submit" name="acn" value="addCart">{t}Přidat obsah z košíku{/t} <strong>({$cartInfo.cartInfo.totalProducts} {t}produktů{/t})</strong>
            </button>
         {/if}
      </div>
      <div class="col-xl-2 p-b-1">
         <button class="btn  btn-block btn-red" type="submit" name="acn" value="storno" title="{t}stornovat objednávku{/t}">{t}Stornovat{/t}</button>
      </div>
      <div class="col-xl-2 p-b-1">
         <button class="btn btn-block btn-gray" type="submit" name="acn" value="cancel">{t}Zrušit editaci{/t}</button>
      </div>
      <div class="col-xl-4 pull-right">
         <button class="btn btn-nextstep btn-block" type="submit" name="acn" value="send">{t}Potvrdit změny{/t} <i class="fc fc-caret-right"></i></button>
      </div>

      <div class="col-xl-2 p-b-1">
         <button class="btn btn-block btn-gray" style="display: none;" type="submit" name="acn" value="setPieces">{t}Přepočítat{/t}</button>
      </div>
   </div>
{/block}

{block "history"}
{/block}

{block "js-dynamic-load" append}
   <script src="/common/static/wpj/wpj.cart.js"></script>
{/block}

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

   $cart.cart();

   $cart.on('click', 'button[value=storno]', function() {
      return confirm('{t}Opravdu chcete stornovat celou objednávku?{/t}');
   });

   $cart.on('click', '.buy_count button', function() {
      var $button = $(this);
      var $input = $button.closest('.product-pieces').find('input[type=number]');
      var oldValue = $input.val();
      var step = parseFloat($input.attr('step'));
      var newVal;

      if ($button.hasClass('plus_unit')) {
         newVal = parseFloat(oldValue) + step;
      } else {
         oldValue > step ? newVal = parseFloat(oldValue) - step : newVal = step;
      }

      $input.val(newVal).change();
   });

   $cart.on('change', '[data-cart="pieces"]', function() {
      var $input = $(this);
      var step = parseFloat($input.attr('step'));
      var value = $input.val();

      if (value < 0)
         $input.val(step);

      if (value % step > 0) {
         var newVal = Math.round(value / step) * step;
         if (newVal <= 0)
            newVal = step;

         $input.val(newVal);
      }
   });

   $cart.on('keydown', '.product-pieces input.form-control', function(e) {
      if (e.keyCode === 13) {
         e.preventDefault();
         $(this).blur();
      }
   });
   {/block}
</script>
