<?php
/**
 * Copyright © 2017 Codazon, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
?>
<?php
$helper = $this->helper('Codazon\ThemeLayoutPro\Helper\Data');
$displayTimer = $helper->getConfig('pages/product_view/display_sale_timer');
?>
<?php if ($displayTimer) : ?>
<?php
$_product = $block->getProduct();
/* $inventory = $_product->getData('quantity_and_stock_status');
$isInStock = $inventory['is_in_stock'];
if ($_product->getTypeId() === 'configurable') {
    $_children = $_product->getTypeInstance()->getUsedProducts($_product);
    $qty = 0;
    foreach ($_children as $_child){
        $_child->load('quantity_and_stock_status');
        $_childInventory = $_child->getData('quantity_and_stock_status');
        $qty += $_childInventory['qty'];
    }
    
} else {
    $qty = $inventory['qty'];
} */

$isSale = false;
//if ($_product->getTypeId() != 'configurable') {
    $isSale = $_product->getPrice() > $_product->getFinalPrice();
    $_saleItem = $_product;
    if ((!$isSale) && ($_product->getTypeId() == 'configurable')) {
        $_children = $_product->getTypeInstance()->getUsedProducts($_product);
        foreach ($_children as $_child) {
            $isSale = $_child->getPrice() > $_child->getFinalPrice();
            if ($isSale) {
                $_saleItem = $_child;
                break;
            }
        }
    }
    if ($isSale) {
        $specialFromDate = $_saleItem->getData('special_from_date');
        $specialToDate = str_replace(' 00:00:00', ' 23:59:59', (string)$_saleItem->getData('special_to_date'));
        if (!$specialToDate) {
            $isSale = false;
        }
        $widget = ['themewidgets' => [
            'codazon.minicountdown' => [
                'startDate'     => $specialFromDate,
                'stopDate'      => $specialToDate,
                'dayLabel'      => __('Day(s)'),
                'hourLabel'     => __('Hour(s)'),
                'minLabel'      => __('Minute(s)'),
                'secLabel'      => __('Second(s)'),
            ]
        ]];
    }
//}
?>

<?php if ($_product->isSaleable() && $isSale) : ?>
<div class="product-inventory-deal">
    <div class="product-deal">
        <div class="deal-wrap" data-mage-init='<?php echo json_encode($widget); ?>'></div>
    </div>
</div>
<?php endif ?>
<?php endif ?>