<?php

$_product_label_config =    $this->helper('Smartwave\Porto\Helper\Data')->getConfig('porto_settings/product_label');;
$_product =                 $block->getProduct();
$today =                    time();
$grouped =                  $_product->getTypeId() == Magento\GroupedProduct\Model\Product\Type\Grouped::TYPE_CODE;

$orgprice = $specialprice = $specialfromdate = $specialtodate = NULL;

if(!$grouped)
{
    $orgprice =         $_product->getPrice();
    $specialprice =     $_product->getSpecialPrice();
    $specialfromdate =  $_product->getSpecialFromDate();
    $specialtodate =    $_product->getSpecialToDate();
}
else
{
    $save_percent_max = 0;
    $orgprice = $specialprice = $specialfromdate = $specialtodate = '';

    $_children = $_product->getTypeInstance()->getAssociatedProducts($_product);
    foreach ($_children as $child) {
        if( $child->getSpecialPrice()
            &&  $child->getSpecialPrice() < $child->getPrice()
            &&  (  (   is_null($child->getSpecialFromDate())
                && is_null($child->getSpecialToDate()))
                || ($today >= strtotime($child->getSpecialFromDate())
                    &&  is_null($child->getSpecialToDate()))
                || ($today-86400 <= strtotime($child->getSpecialToDate())
                    && is_null($child->getSpecialFromDate()))
                || ($today >= strtotime($child->getSpecialFromDate())
                    && $today-86400 <= strtotime($child->getSpecialToDate()))))
        {
            $save_percent = 100-ceil(($child->getSpecialPrice()/$child->getPrice())*100);

            if($save_percent > $save_percent_max)
            {
                $orgprice =         $child->getPrice();
                $specialprice =     $child->getSpecialPrice();
                $specialfromdate =  $child->getSpecialFromDate();
                $specialtodate =    $child->getSpecialToDate();

                $save_percent_max = $save_percent;
            }
        }
    }
}

if(!$specialprice)
{
    $specialprice = $orgprice;
}
if($specialprice < $orgprice)
{
    if((    is_null($specialfromdate)
        && is_null($specialtodate))
        || ($today >= strtotime($specialfromdate)
            && is_null($specialtodate))
        || ($today-86400  <= strtotime($specialtodate)
            && is_null($specialfromdate))
        || ($today >= strtotime($specialfromdate)
            && $today-86400  <= strtotime($specialtodate)))
    {
        $save_percent = 100-ceil(($specialprice/$orgprice)*100);

        if($_product_label_config['sale_label_percent']) : ?>

            <div class="product-label sale-label">
            	<span class="text"><?= __($grouped ? 'bis zu' : 'jetzt') ?></span>
            	<span class="value"><?= $save_percent ?>%</span>
            	<span class="save"><?= __('sparen') ?></span>
            </div>

<?php   else : ?>
             <div class="product-label sale-label"><?= $_product_label_config['sale_label_text'] ?></div>
<?php   endif;
    }
}
?>
