<?php

if (!defined('B_PROLOG_INCLUDED') || B_PROLOG_INCLUDED !== true)
{
	die();
}

use Bitrix\Main;
use Bitrix\Catalog\PriceTable;

/**
 * @global CMain $APPLICATION
 * @var array $arParams
 * @var array $arResult
 * @var CatalogProductsViewedComponent $component
 * @var CBitrixComponentTemplate $this
 * @var string $templateName
 * @var string $componentPath
 * @var string $templateFolder
 */

$this->setFrameMode(true);

?>

<?php if (isset($arResult['ITEM'])): ?>
    <?php
        $productId = $arResult['ITEM']['ID'];
        $priceRow = PriceTable::getList([
            'filter' => [
                'PRODUCT_ID' => $productId,
                'CATALOG_GROUP_ID' => 1
            ],
            'select' => ['PRICE', 'CURRENCY']
        ])->fetch();

        $salePrice = $arResult['ITEM']['PROPERTIES']['ATT_OLD_PRICE']['VALUE'];
        $CHEL = $arResult['ITEM']['PROPERTIES']['CHEL']['VALUE'];
        $SBROS_NUM = $arResult['ITEM']['PROPERTIES']['SBROS_NUM']['VALUE'];
        $RECYCLE_VOL = $arResult['ITEM']['PROPERTIES']['RECYCLE_VOL']['VALUE'];

        $reviews = $arResult['ITEM']['PROPERTIES']['REV_COMPLITE']['VALUE'];

        $rating = getAverageRating($reviews);

    ?>
    <div class="product">
        <a class="product__image" href="<?= $arResult['ITEM']['DETAIL_PAGE_URL'] ?>">
            <img src="<?= $arResult['ITEM']['PREVIEW_PICTURE']['SRC'] ?>" width="336" height="340" alt="<?= $arResult['ITEM']['NAME'] ?>">
        </a>
        <div class="product__data-list">
            <?php if (!empty($arResult['ITEM']['PREVIEW_TEXT'])): ?>
                <p class="product__text">
                    <?= $arResult['ITEM']['PREVIEW_TEXT'] ?>
                </p>
            <?php endif; ?>
            <div class="product__info">
                <?php if (!empty($CHEL)): ?>
                    <p class="product__text">
                        Пользователей: <span><?= $CHEL ?></span>
                    </p>
                <?php endif; ?>
                <?php if (!empty($SBROS_NUM)): ?>
                    <p class="product__text">
                        Залповый сброс, л: <span><?= $SBROS_NUM ?></span>
                    </p>
                <?php endif; ?>
                <?php if (!empty($RECYCLE_VOL)): ?>
                    <p class="product__text">
                        Объем переработки, л/сутки: <span><?= $RECYCLE_VOL ?></span>
                    </p>
                <?php endif; ?>
            </div>
            <div class="product__rating rating rating--<?= $rating ?>"><?= $rating ?>/5</div>
        </div>
        <div class="product__data">
            <div class="product__rating rating rating--<?= $rating ?>"><?= $rating ?>/5</div>
            <div class="product__info">
                <a class="product__title" href="<?= $arResult['ITEM']['DETAIL_PAGE_URL'] ?>">
                    <?= $arResult['ITEM']['NAME'] ?>
                </a>
                <?php if (!empty($arResult['ITEM']['PREVIEW_TEXT'])): ?>
                    <p class="product__text">
                        <?= $arResult['ITEM']['PREVIEW_TEXT'] ?>
                    </p>
                <?php endif; ?>
            </div>
            <div class="product__info">
                <?php if (!empty($CHEL)): ?>
                    <p class="product__text">
                        Пользователей: <span><?= $CHEL ?></span>
                    </p>
                <?php endif; ?>
                <?php if (!empty($SBROS_NUM)): ?>
                    <p class="product__text">
                        Залповый сброс, л: <span><?= $SBROS_NUM ?></span>
                    </p>
                <?php endif; ?>
                <?php if (!empty($RECYCLE_VOL)): ?>
                    <p class="product__text">
                        Объем переработки, л/сутки: <span><?= $RECYCLE_VOL ?></span>
                    </p>
                <?php endif; ?>
            </div>
            <?php if (!empty($priceRow['PRICE'])): ?>
                <div class="product__price">
                    <?= number_format($priceRow['PRICE'], 0, '', ' ') ?> ₽
                </div>
            <?php endif; ?>
            <form class="product__btns add-to-cart-form" action="<?=POST_FORM_ACTION_URI?>" method="post" enctype="multipart/form-data">
                <input type="hidden" name="<?echo $arParams["PRODUCT_QUANTITY_VARIABLE"]?>" value="1" class="form-control" id="QUANTITY<?= $arResult['ITEM']['ID'] ?>">
                <input type="hidden" name="<?echo $arParams["ACTION_VARIABLE"]?>" value="ADD2BASKET">
                <input type="hidden" name="ajax_basket" value="Y">
                <input type="hidden" name="<?echo $arParams["PRODUCT_ID_VARIABLE"]?>" value="<?= $arResult['ITEM']['ID'] ?>" class="id-offer">
                <button type="submit" class="button button--orange add-to-cart-button" name="<?echo $arParams["ACTION_VARIABLE"]."ADD2BASKET"?>">
                    Купить
                </button>
                <button type="button" class="button button--outline">
                    Купить с установкой
                </button>
            </form>
        </div>
    </div>

<?php endif; ?>