{extends "index.tpl"}

{block "css-entry" append}
   {encore_entry_link_tags entry='preorders'}
{/block}

{block "body-container"}{/block}

{block "main"}
   {$dynamic_filter = $body.dynamic_filter.filter}
   <main class="main preorders-page" data-filter-element data-filter="wrapper">
      <div class="container-fluid">
         {include "components/breadcrumbs.tpl"}
      </div>

      {block "content"}
         {get_contexts currency=1 assign='contexts'}
         {if $body.date}
            {if $body.date.base_price_total && $body.date.price_total}
               {$totalPrice = $body.date.price_total->getPriceWithoutVat()}
               {foreach $body.date.discount_levels as $level}
                  {if $level.price_from < $body.date.base_price_total->getPriceWithoutVat() and (!$level.price_to || $body.date.base_price_total->getPriceWithoutVat() < $level.price_to)}
                     {$activeLevel = $level}
                     {$activeLevelIndex = $level@index}
                  {/if}
               {/foreach}
               {$savedMoney = $body.date.base_price_total->getPriceWithoutVat()->sub($body.date.price_total->getPriceWithoutVat())}
               {if is_null($activeLevelIndex)}
                  {$priceFrom = toDecimal($body.date.discount_levels[0].price_from)}
               {else}
                  {$priceFrom = toDecimal($body.date.discount_levels[$activeLevelIndex + 1].price_from)}
               {/if}
               {$toNextLevel = $priceFrom->sub($body.date.base_price_total->getPriceWithoutVat())}
            {else}
               {$toNextLevel = toDecimal($body.date.discount_levels[0].price_from)}
            {/if}

            <div class="container">
               {block "title"}
                  <h1>{t}Předobjednávky{/t}</h1>
               {/block}

               <div class="preorders-top">
                  <div class="preorders-dates-wrapper row">
                     <div class="col-lg-8 col-xxs-12">
                         {block "top-dates"}
                           <div class="preorders-dates">
                               {if $body.isDate}
                                  <p class="title-default">
                                      {block "top-dates-title-term"}
                                          {t}Vyberte termín{/t}
                                      {/block}
                                  </p>
                                   {*todo separate šablony*}
                                  <div class="dates">
                                      {foreach $body.date.dates as $date}
                                         <div class="date-item date-item-{$date.id}">
                                            <a href="{path('kupshop_preorders_show_active', ['id_date' => $date.id])}"
                                               class="date-item-inner{if $body.date.id == $date.id} active{/if}"
                                               title="{$date.name}">
                                               <div class="date-item-title">
                                                  <p class="title-default">{t}Ukončení{/t}: {$date.date_end|format_date:'d.m.Y'}</p>
                                                  <span>{$date.name}</span>
                                               </div>
                                            </a>
                                         </div>
                                      {/foreach}
                                  </div>
                               {else}
                                  <p class="title-default">
                                      {block "top-dates-title-preorder"}
                                          {t}Vyberte předobjednávku{/t}
                                      {/block}
                                  </p>
                                  <div class="dates">
                                      {foreach $body.date.preorders as $preorder}
                                         <div class="date-item date-item-{$preorder.id}">
                                            <a href="{path('kupshop_preorders_show_active', ['id_date' => $preorder.id])}"
                                               class="date-item-inner{if $body.date.id_preorder == $preorder.id} active{/if}"
                                               title="{$preorder.name}">
                                               <div class="date-item-title">
                                                  <p class="title-default">{$preorder.name}</p>
                                               </div>
                                            </a>
                                         </div>
                                      {/foreach}
                                  </div>
                               {/if}
                           </div>
                         {/block}
                     </div>

                     <div class="col-lg-4 col-xxs-12">
                        {block "top-search"}
                           <div class="preorders-search">
                              <p class="title-default">{t}Vyhledat produkt{/t}</p>
                              <input type="text" name="filter[search]" class="form-control fulltext-search"
                                     placeholder="{t}Napište název, kód{/t}"
                                     data-filter-input="hledani" id="filter_search">
                           </div>
                        {/block}
                     </div>
                  </div>

                  <div class="preorders-filter-wrapper row">
                     <div class="col-lg-8 col-xxs-12">
                         {block "preorders-filters"}
                            <form class="filter-form" data-filter=form>
                                {$dynamic_filter = $body.dynamic_filter.filter}
                                {$body.dynamic_filter.producers = true}
                                {include "filter-preorders/block.filter-preorders.producers.tpl" filter_openers=true}
                                {include "filter-preorders/block.filter-preorders.sections.tpl" filter_openers=true}
                                {include "filter-preorders/block.filter-preorders.price.tpl" filter_openers=true}
                                {include "filter-preorders/block.filter-preorders.script.tpl"}
                            </form>
                         {/block}
                     </div>

                     <div class="col-lg-4 col-xxs-12">
                        <div class="list-projection">
                           <label class="custom-checkbox custom-control c-input c-checkbox" for="sh1">
                              <span class="title">{t}Rozbalit varianty{/t}</span>
                              <input type="hidden" name="show" value="2" id="sh2" data-filter-input="{t}zobrazeni{/t}">
                              <input class="custom-control-input" type="checkbox" name="show" value="1" id="sh1"
                                     {if $body.listShow == 1}checked{/if} data-filter-input="{t}zobrazeni{/t}">
                              <span class="custom-control-indicator c-indicator"></span>

                           </label>
                        </div>
                     </div>
                  </div>

                   {include "filter-preorders/filter-preorders.reset.tpl"}
               </div>

               <div data-filter="products">
                  <div class="preorders-products">
                     {include "preOrders.products.list.tpl"}
                  </div>
                  <div data-reload="products-pager">
                     {if $body.pager.count > 1}
                        {$loader_text = "{t}Zobrazit další produkty{/t}"}
                        {include "components/pager.tpl" pager=$body.pager show_arrows=true}
                        {*todo pager (asi nepojede reload ale mělo by fungovat stránkování jako takové)*}
                     {/if}
                  </div>
               </div>
            </div>

            <div class="preorders-panel" id="submit-block">
               <div class="container">
                  {block "submit-top"}
                     <div class="panel-top">
                        <p class="consent">{t url={url s=page label='shopping-rules'} escape=false}Uložením předobjednávky souhlasíte s
                              <a href="{url}" target="_blank">obchodními podmínkami</a>
                              .{/t}</p>
                         {if $body.date.discount_levels|count}
                            <p class="price-level-progress">{t}Pro lepší ceny Vám zbývá nakoupit ještě za{/t} <strong
                                  data-next-price>{if $toNextLevel->isPositive()}{$toNextLevel|format_price}{else}{DecimalConstants::zero()|format_price}{/if}</strong>.
                                {include "block.prices-tooltip.tpl" css_class="left"}
                            </p>
                         {/if}
                     </div>
                  {/block}

                  {block "submit-main"}
                     <div class="panel-submit-block">
                        <div class="xls-link-wrapper">
                            {if $body.date.export_available}
                               <a class="xls-link" href="{path('kupshop_preorders_export_user', ['id_date' => $body.date.id])}" download><span
                                     class="fc icons_file"></span>{t}Stáhnout XLS{/t}</a>
                            {/if}
                        </div>
                        <div class="price-block">
                           <p class="moc">{t}Základní velkoobchodní cena bez DPH{/t} <span
                                 data-preorders-total-price-before-discount>{$body.date.base_price_total|format_price:"withVat=false"}</span></p>
                            {if $body.date.discount_levels}
                               <p class="save">{t}Ušetříte celkem{/t} <span data-preorders-total-discount>{$savedMoney|format_price}</span></p>
                            {/if}
                           <p class="voc">{t}Vaše cena celkem bez DPH{/t} <span
                                 data-preorders-total-price-after-discount>{$totalPrice|format_price}</span></p>
                            {if $body.date.min_price}
                               <p class="total-price-warning"{if $body.date.min_price < $totalPrice} style="display: none;"{/if}>{t price=$body.date.min_price|format_price}Minimální hodnota předobjednávky je {price} bez DPH{/t}</p>
                            {/if}
                        </div>
                        <button class="btn btn-primary" type="submit" id="submit-btn" form="preOrders">{t}Uložit{/t}</button>
                     </div>
                  {/block}

                  {block "submit-bottom"}{/block}
               </div>
            </div>
         {else}
            <div class="container">
               {block "content-empty"}
                  <h1>{$view->getTitle()}</h1>
                  <p>{t}Momentálně nemáte k dispozici žádné termíny předobjednávek{/t}</p>
               {/block}
            </div>
         {/if}
      {/block}

      <script>{* @formatter:off *}
         wpj.preorders = {
            {if $body.date.base_price_total && $body.date.price_total}
            price: '{$body.date.price_total->getPriceWithoutVat()}',
            priceBase: '{$body.date.base_price_total->getPriceWithVat()}',
            {/if}
            priceSaved: '{$savedMoney}',
            priceMin: '{$body.date.min_price}',
            priceToNextLevel: '{$toNextLevel}',
            currency: '{$contexts.currency->getActive()->getSymbol()}',
            currency_decimals: '{$contexts.currency->getActive()->getPricePrecision()}',
            activeLevelIndex: {$activeLevelIndex|default:0},
            levels: {ldelim}{rdelim},
            priceOffsets: {
                totalPrice: {$body.date.price_total|format_price:'format=float,withVat=false'} - {if $body.date.partial_price_total}{$body.date.partial_price_total|format_price:'format=float,withVat=false'}{else}0{/if},
                basePrice: {$body.date.base_price_total|format_price:'format=float,withVat=false'} - {if $body.date.partial_price_base}{$body.date.partial_price_base|format_price:'format=float,withVat=false'}{else}0{/if},
            },
            };{* @formatter:on *}
      </script>
      <script>{* @formatter:off *}
         wpj.preorders.levels[0] = {
            priceFrom: 0,
            priceTo: '{$body.date.discount_levels[0].price_from}',
            index: 0,
            isActive: {if 0 == $activeLevelIndex}true{else}false{/if},
            idPriceLevel: '',
            idPriceList: '',
         };{* @formatter:on *}
      </script>
      {foreach $body.date.discount_levels as $level}
         <script>{* @formatter:off *}
            wpj.preorders.levels[{$level@iteration}] = {
               priceFrom: '{$level.price_from}',
               priceTo: '{$level.price_to}',
               index: {$level@iteration},
               isActive: {if $level@iteration == $activeLevelIndex}true{else}false{/if},
               idPriceLevel: '{$level.id_price_level}',
               idPriceList: '{$level.id_pricelist}',
               };{* @formatter:on *}
         </script>
      {/foreach}
   </main>
   <div class="preorder-sticky-helper"></div>
{/block}

{block "js-entry" append}
   {encore_entry_script_tags entry='preorders'}
{/block}
