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

use Bitrix\Main;
use Bitrix\Main\Localization\Loc;

/**
 * @var array $arParams
 * @var array $arResult
 * @var string $templateFolder
 * @var string $templateName
 * @var CMain $APPLICATION
 * @var CBitrixBasketComponent $component
 * @var CBitrixComponentTemplate $this
 * @var array $giftParameters
 */

$documentRoot = Main\Application::getDocumentRoot();

\CJSCore::Init(array('fx', 'popup', 'ajax'));
Main\UI\Extension::load(['ui.mustache']);

$this->addExternalJs($templateFolder.'/js/action-pool.js');
$this->addExternalJs($templateFolder.'/js/filter.js');
$this->addExternalJs($templateFolder.'/js/component.js');

$jsTemplates = new Main\IO\Directory($documentRoot.$templateFolder.'/js-templates');
/** @var Main\IO\File $jsTemplate */
foreach ($jsTemplates->getChildren() as $jsTemplate)
{
    include($jsTemplate->getPath());
}

?>
<?$APPLICATION->IncludeComponent("bitrix:breadcrumb","breadcrumbs",Array(
        "START_FROM" => "0",
        "PATH" => "",
        "SITE_ID" => "s1"
    )
);?>
<?php if (empty($arResult['ERROR_MESSAGE'])): ?>
<div class="cart-popup__wrapper cart-popup__wrapper--hidden">
    <div class="cart-popup">
        <button class="button cart-popup__close js-cart__popup-close">
            <img src="<?= DEFAULT_TEMPLATE_PATH ?>/img/svg/cancel.svg" width="20" height="20" alt="close-icon">
        </button>
        <h2>
            Оформление заказа
        </h2>
        <form id="cart-form" class="cart-popup__form">
            <label>
                <input type="text" name="name" placeholder="" required>
                <span class="cart-popup__input-label">Имя</span>
            </label>
            <label>
                <input type="tel" name="phone" placeholder="" required>
                <span class="cart-popup__input-label"> Номер телефона*</span>
            </label>
            <label>
                <input type="email" name="email" placeholder="">
                <span class="cart-popup__input-label">Почта</span>
            </label>
            <label class="cart-popup__select">
                <span class="cart-popup__select-label">Способ получение заказа</span>
                <select class="js-cart__order-select" name="delivery_method">
                    <option value="self-delivery">
                        Самовывоз
                    </option>
                    <option value="service-delivery">
                        Службой доставки
                    </option>
                </select>
            </label>
            <label class="cart-popup__select cart-popup__self-delivery js-cart__self-delivery">
                <span class="cart-popup__select-label">Адрес самовывоза</span>
                <select name="pickup_address" disabled>
                    <option>
                        Московская область, г. Наро-Фоминск, ул. Московская улица, 3А
                    </option>
                </select>
            </label>
            <label class="cart-popup__label-delivery cart-popup__label-delivery--hidden js-cart__service-delivery">
                <input type="text" name="address" placeholder="">
                <span class="cart-popup__input-label">Адрес доставки</span>
            </label>
            <label class="cart-popup__label-delivery cart-popup__label-delivery--hidden js-cart__service-delivery">
                <input type="text" name="comment" placeholder="">
                <span class="cart-popup__input-label">Комментарий</span>
            </label>
            <label class="cart-popup__select">
                <span class="cart-popup__select-label">Способ оплаты</span>
                <select name="payment_method">
                    <option value="cash">
                        Наличными при получении
                    </option>
                    <option value="card">
                        Картой при получении
                    </option>
                </select>
            </label>
            <button type="submit" class="cart-popup__form-submit js-cart__popup-submit">
                Оформить заказ
            </button>
            <p class="order-call__personal">
                Нажимая кнопку «Заказать звонок», вы подтверждаете свое согласие на
                обработку <a href="#">персональных данных
                </a></p>
        </form>
    </div>
</div>

<section id="cart-page" class="cart-page section container">
    <h1 id="basket-items-list-wrapper" class="section-title">
        Корзина
        <span class="cart-page__quantity basket-items-list-header-filter-item active" data-entity="basket-items-count" data-filter="all" style="display: none;"></span>
    </h1>
    <div class="cart-page__content-wrapper js-cart__filled">
        <div id="cart-page__content" class="cart-page__content"></div>
        <aside class="cart-page__aside">
            <div id="cart-page__aside-content" class="cart-page__aside-content" data-entity="basket-total-block">

            </div>
            <div class="cart-page__aside-descr">
                <div class="cart-page__aside-descr">
                    <img src="<?= DEFAULT_TEMPLATE_PATH ?>/img/svg/vectortruck.svg" width="28" height="28" alt="delivery-icon">
                    <?$APPLICATION->IncludeComponent(
                        "bitrix:main.include",
                        "",
                        Array(
                            "AREA_FILE_SHOW" => "file",
                            "AREA_FILE_SUFFIX" => "",
                            "EDIT_TEMPLATE" => "",
                            "PATH" => "/include/cart-delivery.php"
                        )
                    );?>
                </div>
            </div>
        </aside>
    </div>
    <div class="cart-empty section container" style="display: none">
        <img src="<?= DEFAULT_TEMPLATE_PATH ?>/img/svg/cart-empty.svg" width="180" height="180" alt="cart-empty">
        <h2>
            <?=Loc::getMessage("SBB_EMPTY_BASKET_TITLE")?>
        </h2>
    </div>
</section>

<?php

if (!empty($arResult['CURRENCIES']) && Main\Loader::includeModule('currency'))
{
    CJSCore::Init('currency');

    ?>
    <script>
        BX.Currency.setCurrencies(<?=CUtil::PhpToJSObject($arResult['CURRENCIES'], false, true, true)?>);
    </script>
    <?
}

$signer = new \Bitrix\Main\Security\Sign\Signer;
$signedTemplate = $signer->sign($templateName, 'sale.basket.basket');
$signedParams = $signer->sign(base64_encode(serialize($arParams)), 'sale.basket.basket');
$messages = Loc::loadLanguageFile(__FILE__);

?>
<script>
    BX.message(<?=CUtil::PhpToJSObject($messages)?>);
    BX.Sale.BasketComponent.init({
        result: <?=CUtil::PhpToJSObject($arResult, false, false, true)?>,
        params: <?=CUtil::PhpToJSObject($arParams)?>,
        template: '<?=CUtil::JSEscape($signedTemplate)?>',
        signedParamsString: '<?=CUtil::JSEscape($signedParams)?>',
        siteId: '<?=CUtil::JSEscape($component->getSiteId())?>',
        siteTemplateId: '<?=CUtil::JSEscape($component->getSiteTemplateId())?>',
        templateFolder: '<?=CUtil::JSEscape($templateFolder)?>'
    });
</script>

<?php elseif ($arResult['EMPTY_BASKET']): ?>
    <?php include(Main\Application::getDocumentRoot().$templateFolder.'/empty.php'); ?>
<?php else: ?>
    <?php ShowError($arResult['ERROR_MESSAGE']); ?>
<?php endif; ?>