{block name='page-free-gift'}
    {opcMountPoint id='opc_before_free_gift' inContainer=true}
    {et_isShopVersionAtLeast value="5.4.0-rc.1" assign='shopIsAtLeast54'}
    {et_isShopVersionAtLeast value="5.7.0-rc.3" assign='shopIsAtLeast57'}
    <div class="page-freegift">
        <p>{lang key='freeGiftFromOrderValue'}</p>
        {if $shopIsAtLeast54 && !$shopIsAtLeast57}
            {* Fixes a JTL bug where unavailable or the wrong free gifts are not shown here - we force reload the free gifts. *}
            {* Collect all originally shown products, these are available to put in the cart. *}
            {$freeGiftsAvailableIds = []}
            {foreach $freeGifts as $freeGiftProduct}
                {$freeGiftsAvailableIds[] = $freeGiftProduct->getProduct()->kArtikel}
            {/foreach}
            {if $Einstellungen.sonstiges.sonstiges_gratisgeschenk_noch_nicht_verfuegbar_anzeigen !== 'Y'}
                {* Override the list with ALL free gifts or else this page would not contain all free gifts *}
                {$freeGifts = JTL\Shop::Container()->getFreeGiftService()->getFreeGifts()->setStillMissingAmounts()}
            {/if}
        {elseif $shopIsAtLeast57}
            {* 5.7 fixes the above bug, but only assigns $freeGiftsAll if there are free gifts, else the value is unassigned. *}
            {if !empty($freeGiftsAll)}
                {$freeGifts = $freeGiftsAll}
            {else}
                {$freeGifts = []}
            {/if}
        {/if}
        {if !$shopIsAtLeast54 && !empty($oArtikelGeschenk_arr) || $shopIsAtLeast54 && !empty($freeGifts)}
            {opcMountPoint id='opc_before_free_gift_list'}
            <div class="row" id="freegift">
                {block name='page-freegift-freegifts'}
                    {if !$shopIsAtLeast54}
                        {$freeGifts = $oArtikelGeschenk_arr}
                    {/if}
                    {foreach $freeGifts as $freeGiftProduct}
                        {if $shopIsAtLeast54}
                            {$basketValue = $freeGiftProduct->availableFrom - $freeGiftProduct->getStillMissingAmount()}
                            {$isFreeGiftAvailableNow = $basketValue >= $freeGiftProduct->availableFrom}
                        {else}
                            {$oArtikelGeschenk = $freeGiftProduct}
                        {/if}
                        <div class="col-12 col-sm-6 col-md-4 page-freegift-item freegift" id="gift{if $shopIsAtLeast54}{$freeGiftProduct->getProduct()->kArtikel}{else}{$freeGiftProduct->kArtikel}{/if}">
                            {block name='page-freegift-freegift-image'}
                                {if $shopIsAtLeast54}
                                    <a href="{$freeGiftProduct->getProduct()->cURLFull}{if !empty($freeGiftsAvailableIds) && in_array($freeGiftProduct->getProduct()->kArtikel, $freeGiftsAvailableIds)}?isfreegift=1{/if}">
                                        {include file='snippets/image.tpl'
                                        item=$freeGiftProduct->getProduct()
                                        square=false
                                        srcSize='sm'
                                        sizes='200px'}
                                    </a>
                                {else}
                                    <a href="{$oArtikelGeschenk->cURLFull}">
                                        {include file='snippets/image.tpl'
                                        item=$oArtikelGeschenk
                                        square=false
                                        srcSize='sm'
                                        sizes='200px'}
                                    </a>
                                {/if}
                            {/block}
                            {block name='page-freegift-freegift-info'}
                                {if $shopIsAtLeast54}
                                    <p class="small text-muted-util">{lang key='freeGiftFrom1'} {$freeGiftProduct->getProduct()->cBestellwert} {lang key='freeGiftFrom2'}</p>
                                {else}
                                    <p class="small text-muted-util">{lang key='freeGiftFrom1'} {$oArtikelGeschenk->cBestellwert} {lang key='freeGiftFrom2'}</p>
                                {/if}
                            {/block}
                            {block name='page-freegift-freegift-link'}
                                {if $shopIsAtLeast54}
                                    <p><a href="{$freeGiftProduct->getProduct()->cURLFull}{if !empty($freeGiftsAvailableIds) && in_array($freeGiftProduct->getProduct()->kArtikel, $freeGiftsAvailableIds)}?isfreegift=1{/if}">{$freeGiftProduct->getProduct()->cName}</a></p>
                                {else}
                                    <p><a href="{$oArtikelGeschenk->cURLFull}">{$oArtikelGeschenk->cName}</a></p>
                                {/if}
                            {/block}
                        </div>
                    {/foreach}
                {/block}
            </div>
        {/if}
    </div>
{/block}
