<?php
if (!defined('B_PROLOG_INCLUDED') || B_PROLOG_INCLUDED !== true) die();

use Bitrix\Main\Localization\Loc;
use Bitrix\Catalog\ProductTable;

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

$this->setFrameMode(true);

use Bitrix\Catalog\PriceTable;

$priceRow = PriceTable::getList([
    'filter' => [
        'PRODUCT_ID' => $arResult['ID'],
        'CATALOG_GROUP_ID' => 1
    ],
    'select' => ['PRICE', 'CURRENCY']
])->fetch();

$oldPrice = $arResult['PROPERTIES']['ATT_OLD_PRICE']['VALUE'];

$price = $arResult['PROPERTIES']['FORMA']['VALUE'];
$camerasNum = $arResult['PROPERTIES']['CAMERAS_NUM']['VALUE'];
$chel = $arResult['PROPERTIES']['CHEL']['VALUE'];
$sbros_num = $arResult['PROPERTIES']['SBROS_NUM']['VALUE'];
$recycleVol = $arResult['PROPERTIES']['RECYCLE_VOL']['VALUE'];
$energyUse = $arResult['PROPERTIES']['ENERGY_USE']['VALUE'];

$passports = $arResult['PROPERTIES']['PASSPORT']['VALUE'];
$certs = $arResult['PROPERTIES']['CERTS']['VALUE'];
$instructions = $arResult['PROPERTIES']['MAN']['VALUE'];
$docs = $arResult['PROPERTIES']['DOC']['VALUE'];

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

$rating = getAverageRating($reviews);

?>
<?$APPLICATION->IncludeComponent("bitrix:breadcrumb","breadcrumbs",Array(
        "START_FROM" => "0",
        "PATH" => "",
        "SITE_ID" => "s1"
    )
);?>
<section class="product-page container section">
    <div class="product-page__slider">
        <div class="product-page__slider-top swiper">
            <div class="swiper-wrapper">
                <?php if (!empty($arResult['DETAIL_PICTURE']['SRC'])): ?>
                    <div class="swiper-slide">
                        <img src="<?= $arResult['DETAIL_PICTURE']['SRC'] ?>" alt="<?= $arResult['DETAIL_PICTURE']['ALT'] ?>" width="60" height="60">
                    </div>
                <?php endif; ?>
                <?php if (!empty($arResult['PROPERTIES']['ALL_PHOTOS']['VALUE'])): ?>
                    <?php foreach ($arResult['PROPERTIES']['ALL_PHOTOS']['VALUE'] as $imageId): ?>
                        <?php
                        $imagePath = CFile::GetPath($imageId);
                        $alt = $arResult['NAME'] ?? 'Фото';
                        ?>
                        <div class="swiper-slide">
                            <img src="<?= htmlspecialchars($imagePath) ?>" alt="<?= htmlspecialchars($alt) ?>" width="60" height="60">
                        </div>
                    <?php endforeach; ?>
                <?php endif; ?>
            </div>
        </div>
        <div class="product-page__slider-bottom swiper">
            <div class="swiper-wrapper">
                <?php if (!empty($arResult['DETAIL_PICTURE']['SRC'])): ?>
                    <div class="swiper-slide">
                        <img src="<?= $arResult['DETAIL_PICTURE']['SRC'] ?>" alt="<?= $arResult['DETAIL_PICTURE']['ALT'] ?>" width="60" height="60">
                    </div>
                <?php endif; ?>
                <?php if (!empty($arResult['PROPERTIES']['ALL_PHOTOS']['VALUE'])): ?>
                    <?php foreach ($arResult['PROPERTIES']['ALL_PHOTOS']['VALUE'] as $imageId): ?>
                        <?php
                        $imagePath = CFile::GetPath($imageId);
                        $alt = $arResult['NAME'] ?? 'Фото';
                        ?>
                        <div class="swiper-slide">
                            <img src="<?= htmlspecialchars($imagePath) ?>" alt="<?= htmlspecialchars($alt) ?>" width="60" height="60">
                        </div>
                    <?php endforeach; ?>
                <?php endif; ?>
            </div>
        </div>
    </div>
    <div class="product-page__info">
        <h1 class="product-page__title">
            <?= $arResult['NAME'] ?>
        </h1>
        <div class="product-page__about">
            <div class="product-page__rating rating rating--<?= $rating ?>">
                <?= $rating ?>/5
            </div>
            <div class="product-page__rating-count">
                 <?= is_array($reviews) ? count($reviews) : 0 ?>
                 <?= getReviewWordForm($reviews) ?>
            </div>
        </div>
        <?php if ($arResult['DETAIL_TEXT']): ?>
            <div class="product-page__descr">
                <?= $arResult['~DETAIL_TEXT'] ?>
            </div>
        <?php endif; ?>
        <div class="product-page__list">
            <?php if (!empty($price)): ?>
                <dl class="product-page__line">
                    <dt>Форма корпуса</dt>
                    <dd><?= $price ?></dd>
                </dl>
            <?php endif; ?>
            <?php if (!empty($camerasNum)): ?>
                <dl class="product-page__line">
                    <dt>Количество камер</dt>
                    <dd><?= $camerasNum ?></dd>
                </dl>
            <?php endif; ?>
            <?php if (!empty($chel)): ?>
                <dl class="product-page__line">
                    <dt>Кол-во человек</dt>
                    <dd><?= $chel ?></dd>
                </dl>
            <?php endif; ?>
            <?php if (!empty($sbros_num)): ?>
                <dl class="product-page__line">
                    <dt>Залповый сброс, л</dt>
                    <dd><?= $sbros_num ?></dd>
                </dl>
            <?php endif; ?>
            <?php if (!empty($recycleVol)): ?>
                <dl class="product-page__line">
                    <dt>Объем переработки, л/сутки</dt>
                    <dd><?= $recycleVol ?></dd>
                </dl>
            <?php endif; ?>
            <?php if (!empty($energyUse)): ?>
                <dl class="product-page__line">
                    <dt>Потреб. энергия, вт/час</dt>
                    <dd><?= $energyUse ?></dd>
                </dl>
            <?php endif; ?>
            <a class="product-page__link" href="#product-tabs">Смотреть все характеристики</a>
        </div>
    </div>
    <div class="product-page__data">
        <div class="product-page__price">
            <div class="product-page__price-line">
                <span class="product-page__new-price">
                    <?= number_format($priceRow['PRICE'], 0, '', ' ') ?> ₽
                </span>
                <?php if ($oldPrice): ?>
                    <span class="product-page__old-price">
                        <?= number_format($oldPrice, 0, '', ' ') ?> ₽
                    </span>
                <?php endif; ?>
            </div>
            <form class="product-page__prices 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['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['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 class="product-page__delivery">
            <div class="product-page__delivery-data">
                <div class="product-page__delivery-line">
                    <span>Самовывоз из магазина</span> - <span>сегодня</span>
                </div>
                <div class="product-page__delivery-line">
                    <span>Доставка курьером</span> - <span>завтра</span>
                </div>
                <div class="product-page__delivery-line">
                    <span>Доставка почтой</span> - <span>1-2 дня</span>
                </div>
            </div>
        </div>
    </div>
</section>

<section class="product-tabs section container" id="product-tabs">
    <input class="product-tabs__radio visually-hidden" id="tabs_characteristic" type="radio" name="tabs" value="characteristic" checked="">
    <?php if (!empty($certs)): ?>
        <input class="product-tabs__radio visually-hidden" id="tabs_sertificates" type="radio" name="tabs" value="sertificates">
    <?php endif; ?>
    <?php if (!empty($passports)): ?>
        <input class="product-tabs__radio visually-hidden" id="tabs_passport" type="radio" name="tabs" value="passport">
    <?php endif; ?>
    <?php if (!empty($instructions)): ?>
        <input class="product-tabs__radio visually-hidden" id="tabs_instructions" type="radio" name="tabs" value="instructions">
    <?php endif; ?>
    <?php if (!empty($docs)): ?>
        <input class="product-tabs__radio visually-hidden" id="tabs_documentation" type="radio" name="tabs" value="documentation">
    <?php endif; ?>
    <div class="product-tabs__list">
        <label class="product-tabs__item" for="tabs_characteristic">Характеристика</label>
        <?php if (!empty($certs)): ?>
            <label class="product-tabs__item" for="tabs_sertificates">Сертификаты</label>
        <?php endif; ?>
        <?php if (!empty($passports)): ?>
            <label class="product-tabs__item" for="tabs_passport">Паспорт</label>
        <?php endif; ?>
        <?php if (!empty($instructions)): ?>
            <label class="product-tabs__item" for="tabs_instructions">Инструкция</label>
        <?php endif; ?>
        <?php if (!empty($docs)): ?>
            <label class="product-tabs__item" for="tabs_documentation">Документация</label>
        <?php endif; ?>
    </div>
    <div class="product-tabs__data">
        <h2 class="visually-hidden">Характеристика</h2>
        <div class="product-tabs__data-item">
            <div class="product-page__list">
                <dl class="product-page__line">
                    <dt>Форма корпуса</dt>
                    <dd>цилиндрический</dd>
                </dl>
                <dl class="product-page__line">
                    <dt>Количество камер</dt>
                    <dd>5 циклов очистки</dd>
                </dl>
                <dl class="product-page__line">
                    <dt>Кол-во человек</dt>
                    <dd>60</dd>
                </dl>
                <dl class="product-page__line">
                    <dt>Залповый сброс, л</dt>
                    <dd>Россия</dd>
                </dl>
                <dl class="product-page__line">
                    <dt>Объем переработки, л/сутки</dt>
                    <dd>180</dd>
                </dl>
                <dl class="product-page__line">
                    <dt>Потреб. энергия, вт/час</dt>
                    <dd>40</dd>
                </dl>
            </div>
        </div>
        <?php if (!empty($certs)): ?>
            <div class="product-tabs__data-item">
            <h2 class="visually-hidden">Сертификаты</h2>
            <div class="product-page__images documents">
                <?php foreach ($certs as $cert): ?>
                    <?php
                        $imagePath = CFile::GetPath($cert['SUB_VALUES']['CERT_JPG']['VALUE']);
                        $alt = $cert['SUB_VALUES']['CERT_NAME']['VALUE'];
                    ?>
                    <div class="documents__item">
                        <div class="documents__wrapper">
                            <img class="documents__image" src="<?= htmlspecialchars($imagePath) ?>" data-fancybox="certificate" width="392" height="520" alt=""<?= htmlspecialchars($alt) ?>">
                            <img class="documents__zoom" src="<?= DEFAULT_TEMPLATE_PATH ?>/img/svg/zoom.svg" alt="">
                        </div>
                        <p class="product-page__docname">
                            <?= $cert['SUB_VALUES']['CERT_NAME']['VALUE'] ?>
                            <?php if (!empty($cert['SUB_VALUES']['CERT_PDF']['VALUE'])): ?>
                                <?php
                                    $certFile = CFile::GetFileArray($cert['SUB_VALUES']['CERT_PDF']['VALUE']);
                                ?>
                                <a class="download-link" download="<?= $certFile['ORIGINAL_NAME'] ?>" href="<?= $certFile['SRC'] ?>">
                                    <span>Скачать PDF</span>
                                </a>
                            <?php endif; ?>
                        </p>
                    </div>
                <?php endforeach; ?>
            </div>
        </div>
        <?php endif; ?>

        <?php if (!empty($passports)): ?>
            <div class="product-tabs__data-item product-tabs__data-item--docs">
                <div class="product-tabs__data-items">
                    <?php foreach ($passports as $passport): ?>
                        <div class="product-page__text documents">
                            <?php
                            $imagePath = CFile::GetPath($passport['SUB_VALUES']['PASSPORT_JPG']['VALUE']);
                            $alt = $passport['SUB_VALUES']['PASSPORT_NAME']['VALUE'];
                            ?>
                            <div class="documents__item">
                                <div class="documents__wrapper">
                                    <img class="documents__image" src="<?= htmlspecialchars($imagePath) ?>" data-fancybox="passport" width="392" height="520" alt=""<?= htmlspecialchars($alt) ?>">
                                    <img class="documents__zoom" src="<?= DEFAULT_TEMPLATE_PATH ?>/img/svg/zoom.svg" alt="">
                                </div>
                                <p class="product-page__docname">
                                    <?= $passport['SUB_VALUES']['PASSPORT_NAME']['VALUE'] ?>
                                    <?php if (!empty($passport['SUB_VALUES']['PASSPORT_PDF']['VALUE'])): ?>
                                        <?php
                                        $passportFile = CFile::GetFileArray($passport['SUB_VALUES']['PASSPORT_PDF']['VALUE']);
                                        ?>
                                        <a class="download-link" download="<?= $passportFile['ORIGINAL_NAME'] ?>" href="<?= $passportFile['SRC'] ?>">
                                            <span>Скачать PDF</span>
                                        </a>
                                    <?php endif; ?>
                                </p>
                            </div>
                            <div class="product-page__article">
                                <h2 class="product-page__article-name">
                                    <?= $passport['SUB_VALUES']['PASSPORT_NAME']['VALUE'] ?>
                                </h2>
                                <?php if ($passport['SUB_VALUES']['PASSPORT_TEXT']['VALUE']): ?>
                                    <div class="product-page__article-text">
                                        <?= $passport['SUB_VALUES']['PASSPORT_TEXT']['~VALUE']['TEXT'] ?>
                                    </div>
                                <?php endif; ?>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
            </div>
        <?php endif; ?>

        <?php if (!empty($instructions)): ?>
        <div class="product-tabs__data-item product-tabs__data-item--docs">
            <div class="product-tabs__data-items">
                <?php foreach ($instructions as $instruction): ?>
                    <?php
                    $imagePath = CFile::GetPath($instruction['SUB_VALUES']['MAN_JPG']['VALUE']);
                    $alt = $instruction['SUB_VALUES']['MAN_NAME']['VALUE'];
                    ?>
                    <div class="product-page__text documents">
                        <div class="documents__item">
                            <div class="documents__wrapper">
                                <img class="documents__image" src="<?= htmlspecialchars($imagePath) ?>" data-fancybox="instruction" width="392" height="520" alt=""<?= htmlspecialchars($alt) ?>">
                                <img class="documents__zoom" src="<?= DEFAULT_TEMPLATE_PATH ?>/img/svg/zoom.svg" alt="">
                            </div>
                            <p class="product-page__docname">
                                <?= $instruction['SUB_VALUES']['MAN_NAME']['VALUE'] ?>
                                <?php if (!empty($instruction['SUB_VALUES']['MAN_PDF']['VALUE'])): ?>
                                    <?php
                                    $instructionFile = CFile::GetFileArray($instruction['SUB_VALUES']['MAN_PDF']['VALUE']);
                                    ?>
                                    <a class="download-link" download="<?= $instructionFile['ORIGINAL_NAME'] ?>" href="<?= $instructionFile['SRC'] ?>">
                                        <span>Скачать PDF</span>
                                    </a>
                                <?php endif; ?>
                            </p>
                        </div>
                        <div class="product-page__article">
                            <h2 class="product-page__article-name">
                                <?= $instruction['SUB_VALUES']['MAN_NAME']['VALUE'] ?>
                            </h2>
                            <?php if ($instruction['SUB_VALUES']['MAN_TEXT']['VALUE']): ?>
                                <div class="product-page__article-text">
                                    <?= $instruction['SUB_VALUES']['MAN_TEXT']['~VALUE']['TEXT'] ?>
                                </div>
                            <?php endif; ?>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
        </div>
        <?php endif; ?>

        <?php if (!empty($docs)): ?>
            <div class="product-tabs__data-item">
                <h2 class="visually-hidden">Документация</h2>
                <div class="product-page__images documents">
                    <?php foreach ($docs as $doc): ?>
                        <?php
                            $imagePath = CFile::GetPath($doc['SUB_VALUES']['DOC_JPG']['VALUE']);
                            $alt = $doc['SUB_VALUES']['DOC_NAME']['VALUE'];
                        ?>
                        <div class="documents__item">
                            <div class="documents__wrapper">
                                <img class="documents__image" src="<?= htmlspecialchars($imagePath) ?>" data-fancybox="doc" width="392" height="520" alt=""<?= htmlspecialchars($alt) ?>">
                                <img class="documents__zoom" src="<?= DEFAULT_TEMPLATE_PATH ?>/img/svg/zoom.svg" alt="">
                            </div>
                            <p class="product-page__docname">
                                <?= $doc['SUB_VALUES']['DOC_NAME']['VALUE'] ?>
                                <?php if (!empty($doc['SUB_VALUES']['DOC_PDF']['VALUE'])): ?>
                                    <?php
                                        $docFile = CFile::GetFileArray($doc['SUB_VALUES']['DOC_PDF']['VALUE']);
                                    ?>
                                    <a class="download-link" download="<?= $docFile['ORIGINAL_NAME'] ?>" href="<?= $docFile['SRC'] ?>">
                                        <span>Скачать PDF</span>
                                    </a>
                                <?php endif; ?>
                            </p>
                        </div>
                    <?php endforeach; ?>
                </div>
            </div>
        <?php endif; ?>
    </div>
</section>

<?php if (!empty($reviews)): ?>
<section class="product-review reviews container section product__reviews-swiper">
    <h2 class="section-title">Отзывы о септике</h2>
    <div class="reviews__list swiper-wrapper">
        <?php foreach ($reviews as $review): ?>
            <div class="reviews__item swiper-slide">
                <div class="reviews__author reviews__author--with-icon">
                    <div class="reviews__author-icon">
                        <?= mb_substr($review['SUB_VALUES']['REV_NAME']['VALUE'], 0, 1) ?>
                    </div>
                    <div class="reviews__author-name">
                        <?= $review['SUB_VALUES']['REV_NAME']['VALUE'] ?>
                    </div>
                    <?php if (!empty($review['SUB_VALUES']['REV_CITY']['VALUE'])): ?>
                        <div class="reviews__author-city">
                            <?= $review['SUB_VALUES']['REV_CITY']['VALUE'] ?>
                        </div>
                    <?php endif; ?>
                </div>
                <?php if (!empty($review['SUB_VALUES']['REV_HEADER']['VALUE'])): ?>
                    <h3 class="reviews__name">
                        <?= $review['SUB_VALUES']['REV_HEADER']['VALUE'] ?>
                    </h3>
                <?php endif; ?>
                <?php if (!empty($review['SUB_VALUES']['REV_TEXT']['VALUE'])): ?>
                    <div class="reviews__message">
                        <?= $review['SUB_VALUES']['REV_TEXT']['~VALUE'] ?>
                    </div>
                <?php endif; ?>
                <div class="reviews__value">
                    <?php if (!empty($review['SUB_VALUES']['REV_DATE']['VALUE'])): ?>
                        <div class="reviews__date">
                            <?= $review['SUB_VALUES']['REV_DATE']['VALUE'] ?>
                        </div>
                    <?php endif; ?>
                    <?php if (!empty($review['SUB_VALUES']['REV_RATING']['VALUE'])): ?>
                        <div class="reviews__rating rating rating--<?= $review['SUB_VALUES']['REV_RATING']['VALUE'] ?>">
                            <?= $review['SUB_VALUES']['REV_RATING']['VALUE'] ?>/5
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        <?php endforeach; ?>
    </div>
    <div class="reviews__btns">
        <button class="reviews__btn reviews__btn--prev" type="button"></button>
        <button class="reviews__btn reviews__btn--next" type="button"></button>
    </div>
</section>
<?php endif; ?>

<section class="product-sale sale container section">
    <h2 class="section-title">Вас также могут заинтересовать</h2>
    <div class="sale__products">
        <div class="product">
            <a class="product__image" href="product.html">
                <picture><source srcset="png/product.webp" type="image/webp"><img src="png/product.png" width="336" height="340" alt="product"></picture>
            </a>
            <div class="product__data">
                <div class="product__rating rating rating--4">4/5</div>
                <div class="product__info">
                    <a class="product__title" href="product.html">Септик ЭРА 4 прин</a>
                    <p class="product__text">Решение для небольших дачных домов</p>
                </div>
                <div class="product__info">
                    <p class="product__text">Пользователей: <span>1-3 чел</span></p>
                    <p class="product__text">
                        Производительность: <span>0.5м2</span>
                    </p>
                    <p class="product__text">Сброс: <span>150 литров</span></p>
                    <p class="product__text">Питание: <span>не нужно</span></p>
                </div>
                <div class="product__price">77 000 ₽</div>
                <div class="product__btns">
                    <button class="button button--orange">Купить</button>
                    <button class="button button--outline">
                        Купить с установкой
                    </button>
                </div>
            </div>
        </div>

        <div class="product">
            <a class="product__image" href="product.html">
                <picture><source srcset="png/product.webp" type="image/webp"><img src="png/product.png" width="336" height="340" alt="product"></picture>
            </a>
            <div class="product__data">
                <div class="product__rating rating rating--4">4/5</div>
                <div class="product__info">
                    <a class="product__title" href="product.html">Септик ЭРА 4 прин</a>
                    <p class="product__text">Решение для небольших дачных домов</p>
                </div>
                <div class="product__info">
                    <p class="product__text">Пользователей: <span>1-3 чел</span></p>
                    <p class="product__text">
                        Производительность: <span>0.5м2</span>
                    </p>
                    <p class="product__text">Сброс: <span>150 литров</span></p>
                    <p class="product__text">Питание: <span>не нужно</span></p>
                </div>
                <div class="product__price">77 000 ₽</div>
                <div class="product__btns">
                    <button class="button button--orange">Купить</button>
                    <button class="button button--outline">
                        Купить с установкой
                    </button>
                </div>
            </div>
        </div>

        <div class="product">
            <a class="product__image" href="product.html">
                <picture><source srcset="png/product.webp" type="image/webp"><img src="png/product.png" width="336" height="340" alt="product"></picture>
            </a>
            <div class="product__data">
                <div class="product__rating rating rating--4">4/5</div>
                <div class="product__info">
                    <a class="product__title" href="product.html">Септик ЭРА 4 прин</a>
                    <p class="product__text">Решение для небольших дачных домов</p>
                </div>
                <div class="product__info">
                    <p class="product__text">Пользователей: <span>1-3 чел</span></p>
                    <p class="product__text">
                        Производительность: <span>0.5м2</span>
                    </p>
                    <p class="product__text">Сброс: <span>150 литров</span></p>
                    <p class="product__text">Питание: <span>не нужно</span></p>
                </div>
                <div class="product__price">77 000 ₽</div>
                <div class="product__btns">
                    <button class="button button--orange">Купить</button>
                    <button class="button button--outline">
                        Купить с установкой
                    </button>
                </div>
            </div>
        </div>
    </div>
</section>
