{extends "ordering.tpl"}

{function delivery_row}
    {block "delivery-row-function"}
       <div
          class="delivery-item display-row {if $delivery.class}delivery-item-{$delivery.class|lower}{/if} {if $delivery.exception}disabled{/if}"
          data-cart="item"
       data-tracking-click-delivery-type='{get_gtm_data encode=true type='ecommerce' method='AddDeliveryInfo' data=['delivery' => $delivery]}' >
          <div class="display-cell">
             <label class="custom-radio custom-control c-input c-radio" for="del{$id}">
                <input type="radio" name="delivery_id" value="{$id}" id="del{$id}" class="custom-control-input"
                       {if !$delivery.exception and ($body.input.balik_na_postu_zip.value or $body.delivery_id == $id)}checked{/if}
                        {if $delivery.exception}disabled{/if}>
                <span class="custom-control-indicator c-indicator"></span>
                 {photo photo=$delivery.photo}
                <p>{$delivery.name}
                    {block "delivery-time"}
                       <small>
                           {if $delivery.exception}
                               {$delivery.exception->getShortMessage()}
                           {elseif $availability == 1}
                               {$date = $delivery->getDeliveryDate()}
                               {if $date}
                                   {if $delivery->isInPerson()}
                                       {t dne=$date|format_date_locale_pretty:'d. L.' escape=false}K vyzvednutí {dne}{/t}
                                   {else}
                                       {t dne=$date|format_date_locale_pretty:'d. L.' escape=false}Doručíme {dne}{/t}
                                   {/if}
                               {/if}
                           {/if}
                       </small>
                    {/block}
                </p>

                 {block "delivery-description"}
                     {$templateDescription = $delivery->getCartDescription()}
                     {if $templateDescription or $delivery->cart_description}
                        <div class="delivery_description" id="delivery_description_{$delivery.id}" style="display:none">
                            {if $templateDescription}
                                {$templateDescription nofilter}
                            {elseif $delivery->cart_description}
                                {$delivery->cart_description nofilter}
                            {/if}
                        </div>
                     {/if}
                 {/block}
             </label>
          </div>

           {block "cart-delivery-price"}
              <div class="display-cell delivery-price">
                  {$deliveryPrice = $view->getDeliveryPrice($id)}
                  {if $delivery.exception}
                      {* nic *}
                  {elseif $deliveryPrice and $deliveryPrice.value_with_vat->isPositive()}
                      {$deliveryPrice.value_with_vat|format_price}
                  {else}
                      {t}zdarma{/t}
                  {/if}
              </div>
           {/block}
       </div>
    {/block}
{/function}

{block "order-content"}
   <div class="row deliveries" data-cart="delivery">
       {block "cart-deliveries"}
          <div class="{block "delivery-col"}col-md-6 col-xs-12{/block}">
              {block "cart-deliveries-header"}
                 <h1 class="h2">{t}Doprava{/t}</h1>
              {/block}
             <div class="cart-box display-table post">
                 {$availability = $body->getAvailability()}

                 {$deliveries = $body->getDeliveries()}
                 {foreach $deliveries as $id => $delivery}
                     {if $delivery->isInPerson()}
                         {$inpersonExists = true}
                     {else}
                         {delivery_row delivery=$delivery id=$id}
                     {/if}
                 {/foreach}
             </div>
              {block 'cart-inperson'}
                  {if $inpersonExists == true}
                     <h2>{t}Osobní odběr{/t}</h2>
                     <div class="cart-box cart-box-inperson display-table">
                         {foreach $deliveries as $id => $delivery}
                             {if $delivery->isInPerson()}
                                 {delivery_row delivery=$delivery id=$id inperson=true}
                             {/if}
                         {/foreach}
                     </div>
                  {/if}
              {/block}
          </div>
       {/block}

       {block "cart-payments"}
          <div class="{block "payment-col"}col-md-6 col-xs-12{/block}">
             <h2>{t}Platba{/t}</h2>

             <div class="cart-box display-table" data-reload="payment">

                 {$payments = $body->getPayments()}

                 {foreach $payments as $id => $payment}
                     {if !$payment.class or !$payment.class->hasCartTemplate()}
                        <div
                           class="payment-item display-row {if $payment.disabled}disabled{/if} {if $payment.exception}disabled exception{/if}"
                           data-cart="item"
                        data-tracking-click-delivery-type='{get_gtm_data encode=true type='ecommerce' method='AddPaymentInfo' data=['payment' => $payment]}' >
                           <div class="display-cell">
                              <label class="custom-radio custom-control c-input c-radio" for="pay{$id}">
                                 <input type="radio" name="payment_id" value="{$id}"
                                        class="custom-control-input" {if !$payment.exception}{$body.payment_id|checked:$id nofilter}{/if}
                                        id="pay{$id}" {if $payment.disabled}disabled{/if}  {if $payment.exception}disabled data-exception="1"{/if}>
                                 <span class="custom-control-indicator c-indicator"></span>
                                  {photo photo=$payment.photo}
                                 <div>
                                    <span class="nowrap">{$payment.name}{if $payment.exception}
                                          <small>{$payment.exception->getShortMessage()}</small>{/if}</span><br>
                                     {if $payment.cart_description}
                                        <div class="payment_description" id="payment_description_{$payment.id}"{if $body.payment_id != $id} style="display: none"{/if}>
                                            {$payment.cart_description nofilter}
                                        </div>
                                     {/if}
                                 </div>
                              </label>
                           </div>
                            {block "cart-payment-price"}
                               <div class="display-cell delivery-price text-right">
                                   {$paymentPrice = $view->getPaymentPrice($id)}
                                   {if $paymentPrice != null}
                                       {if $paymentPrice.value_with_vat -> isPositive()}
                                           {$paymentPrice.value_with_vat|format_price}
                                       {else}
                                           {t}zdarma{/t}
                                       {/if}
                                   {/if}
                               </div>
                            {/block}
                        </div>
                     {/if}
                 {/foreach}
             </div>

              {block "payment-class-wrapper"}
                  {* ThePay zobrazujeme stranou od ostatnich plateb *}
                  {foreach $payments as $id => $payment}
                      {if $payment.class and $payment.class->hasCartTemplate()}
                         <h2>{photo photo=$payment.photo} {$payment.name}</h2>
                         <div class="cart-box display-table thepay-method" data-reload="payment-thepay">
                             {* pta se jestli to ma navazanou sablonu pro kosik, napr. payment.ThePay.cart.tpl *}
                             {* pokud ano tak ji vypise *}
                             {if $payment.class}
                                 {get_smarty assign='smarty_object'}
                                 {$payment.class -> getCartTitle($smarty_object) nofilter}
                             {/if}
                         </div>
                      {/if}
                  {/foreach}
              {/block}
          </div>
       {/block}
   </div>
    {block "pricebox-row"}
       <div class="row">
          <div class="col-md-6 col-xs-12 pull-right">
              {showPriceBox transport=1}
          </div>
       </div>
    {/block}
    {block 'cart-buttons'}
        {cartBtns submitName='OrderNext' valueNext='user' msg="{t}V dalším kroku vyplňte, kam chcete zboží doručit{/t}"}
    {/block}
{/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}
{/block}
