<?php
/** @var $this \yii\web\View */
/** @var $productProvider \yii\data\ActiveDataProvider */
/** @var $brandProvider \yii\data\ActiveDataProvider */


use yii\helpers\Url;
use common\modules\product\helpers\ProductHelper;

$this->title =  $category->categoryName->value;
foreach($category->getParents()->all() as $parent) {
    $this->params['breadcrumbs'][] = ['label' => $parent->categoryName->value, 'url' => ['catalog/category', 'category' => $parent]];
}
$this->params['breadcrumbs'][] = $category->categoryName->value;

$this->params['seo']['seo_text'] = 'TEST SEO TEXT';
$this->params['seo']['h1'] = 'TEST H1';
$this->params['seo']['description'] = 'TEST DESCRIPTION';
$this->params['seo']['fields']['name'] = 'TEST NAME FROM FIELD';
$this->params['seo']['key']= 'product_list';
?>
<script type="text/javascript">
<?php if ($priceLimits['min'] < $priceLimits['max']) :?>
    $(document).ready(function() {
        // price rangeslider (filter price slider)
        $("#price_interval").ionRangeSlider({
            type: "double",
            min: <?= $priceLimits['min']?>,
            max: <?= $priceLimits['max']?>,
            from: <?= empty($filter['prices']['min']) ? $priceLimits['min'] : $filter['prices']['min']?>,
            to: <?= empty($filter['prices']['max']) ? $priceLimits['max'] : $filter['prices']['max']?>,
            grid: false,
            onFinish: function(e) {
<?php
$filterWhitoutPrice = $filter;
$filterWhitoutPrice['prices'] = [
    'min' => '{from}',
    'max' => '{to}',
];
?>
                var url = "<?= Url::to(['catalog/category', 'category' => $category, 'filter' => $filterWhitoutPrice])?>";
                var from = e.from;
                var to = e.to;
                document.location = url.replace('{from}', from).replace('{to}', to);
            }
        });
    });
<?php endif?>
<?php /*
    $(document).ready(function() {
        $('form.filter-catalog-form').each(function() {
            var form = $(this);
            form.submit(function(e) {
                e.preventDefault();
                var brands = '';
                // Get brands values
                $('input[type=checkbox].brands-option:checked', form).each(function() {
                    var filter_value = $(this).val().replace(',', '~').replace('/', '&s;');
                    if (brands) {
                        brands += ','+ filter_value;
                    } else {
                        brands = filter_value;
                    }
                });
                // Formate query and redirect
                var filter = '';
                if (brands) {
                    filter += 'brands='+ brands;
                }
                filter += ';';

                if (filter) {
                    var request = '<?= Url::toRoute(['catalog/category/filter', 'alias' => $category->alias, 'filter' => '+ filter .'])?>';
                    window.location.href = request;
                }
            });
        });
    });
*/?>
</script>
<div class="w_960">
    <!-- side bar with all filters -->
    <div class="cat_p_filter_bar">
        <div class="title">ФИЛЬТРЫ</div>
        <div class="filter_list">
            <form action="#" name="filter_catalog_page_form" class="filter-catalog-form">
                <?php if (!empty($filter)) :?>
                <div class="filter_accept_bloc">
                    <!--                    <button type="submit" class="filter_accept_btn">применить</button>-->
                    <a href="<?= Url::to(['catalog/category', 'category' => $category])?>" class="_form_checkbox_reset">сбросить фильтры</a>
                </div>
                <?php endif?>
                <ul>
                    <?php if ($priceLimits['min'] < $priceLimits['max']) :?>
                    <li>Цена:
                        <div class="arrow"><img src="/images/head_up.png" alt=""></i></div>
                        <div class="price_filter first_price_li">
                            <div class="price_slider">
                                <input type="text" id="price_interval" name="price_interval" value="" />
                            </div>
                        </div>
                    </li>
                    <?php endif?>

                    <?php if ($brandProvider->totalCount > 0) :?>
                        <li>Бренд
                            <div class="arrow"><img src="/images/head_down.png" alt=""></i></div>
                            <div class="price_filter<?= !empty($filter['brands'])?' active-field':''?>">
                                <?php foreach($brandProvider->models as $brand) :
                                    $checked = !empty($filter['brands']) && in_array($brand->alias, $filter['brands']);
                                    $option_url = Url::to(['catalog/category', 'category' => $category, 'filter' => ProductHelper::getFilterForOption($filter, 'brands', $brand->alias, $checked)]);
                                    ?>
                                    <div class="checkbox">
                                        <label><input type="checkbox" class="brands-option" <?php /*name="brands[]" value="<?= $brand->alias?>"*/?><?= $checked ? ' checked' : ''?> onchange="document.location='<?= $option_url?>'" /></label>
                                        <a href="<?= $option_url?>"><?= $brand->name?></a>
                                    </div>
                                <?php endforeach?>
                            </div>
                        </li>
                    <?php endif?>

                    <?php if (!empty($groups)) :?>
                        <div class="title_2">ПОДБОР ПО ПАРАМЕТРАМ</div>

                        <?php foreach($groups as $group) :?>
                            <li><?= $group->name?>
                                <div class="arrow"><img src="/images/head_down.png" alt=""></i></div>
                                <div class="price_filter<?= isset($filter['options'][$group->alias])?' active-field':''?>">
                                    <?php foreach($group->_options as $option) :
                                        $checked = (isset($filter['options'][$group->alias]) && in_array($option->alias, $filter['options'][$group->alias]));
                                        $option_url = Url::to(['catalog/category', 'category' => $category, 'filter' => ProductHelper::getFilterForOption($filter, 'options', [$option->group->alias => [$option->alias]], $checked)]);
                                        ?>
                                        <div class="checkbox">
                                            <label><input type="checkbox" class="features-option" onchange="document.location='<?= $option_url?>'" <?php /* name="option[<?= $group->alias?>][]"value="<?= $option->alias?>"*/?><?= $checked ? ' checked' : ''?> /></label>
                                            <a href="<?= $option_url?>"><?= $option->ValueRenderHTML?></a>
                                        </div>
                                    <?php endforeach?>
                                </div>
                            </li>
                        <?php endforeach?>
                    <?php endif?>
                </ul>
            </form>
        </div>
    </div>

    <!-- catalog list with all item cards -->
    <div class="cat_p_catalog_list">
        <div class="title"><?= $category->categoryName->value?> <span>(<?= $productProvider->totalCount?>)</span></div>
        <?php if (!$productProvider->count) :?>
            <h2>По данному запросу товары не найдены.</h2><br>
            <?php if (!empty($category)) :?>
            <p>Показать <a href="<?= Url::to(['catalog/category', 'category' => $category])?>">все товары из категории "<?= $category->categoryName->value?>"</a></p>
            <?php endif?>
        <?php else :?>
            <!-- sort menu -->
            <div class="sort_menu">

                <div class="sort_price">
                    <span>Сортировка:</span>
                    <?= \yii\widgets\LinkSorter::widget([
                        'sort' => $productProvider->sort,
                        'attributes' => [
                            'name',
                            'price',
                        ]
                    ]);
                    ?>
                    <!--
                    <select name="sort_price" id="" class="sort_price_select">
                        <option value="price">по цене</option>
                        <option value="popular">новые</option>
                        <option value="sale">по акции</option>
                    </select>
                    <i class="fa fa-angle-down"></i>-->
                </div>

                <div class="show">
                    <!--<span>Показывать по:</span>
                    <ul>
                        <li><a class="active" href="#">24</a></li>
                        <li><a href="#">48</a></li>
                        <li><a href="#">96</a></li>
                    </ul>-->
                </div>

                <?php if ($productProvider->totalCount > $productProvider->pagination->pageSize) :?>
                <div class="show_pages">
                    Страница:
                    <?= \yii\widgets\LinkPager::widget([
                        'pagination' => $productProvider->pagination,
                        'options' => ['class' => 'pagination pull-right'],
                    ]);
                    ?>
                    <!--<i class="fa fa-caret-right"></i>-->
                </div>
                <?php endif?>

            </div>

            <div class="cat_p_item_card_list">
                <div class="novelty">
                    <div class="content">
                        <div class="novelty_cont">
                            <?php foreach($productProvider->models as $product) :?>
                                <?php require(__DIR__ .'/product_item.php')?>
                            <?php endforeach?>
                        </div>

                        <?php if ($productProvider->totalCount > $productProvider->pagination->pageSize) :?>
                            <!-- LOAD MORE BUTTON -->
                            <!--button class="load_more_btn">Загрузить еще <?= $productProvider->pagination->pageSize?> товара</button-->

                            <div class="show_pages">
                                Страница:
                                <?= \yii\widgets\LinkPager::widget([
                                    'pagination' => $productProvider->pagination,
                                    'options' => ['class' => 'pagination pull-right'],
                                ]);
                                ?>
                                <!--<i class="fa fa-caret-right"></i>-->
                            </div>
                        <?php endif ?>
                        <hr>

                        <?php if(!empty($category->description)) :?>
                            <div class="description">
                                <?= $category->description?>

                                <div class="empty_padding_400"></div>
                            </div>
                        <?php endif?>
                    </div>
                </div>
            </div>

        <?php endif?>
    </div>

    <?php if(!empty($last_products)) :?>
        <hr>
        <div class="watched_block">
            <h1>Вы недавно просматривали</h1>
            <div class="flex-container">
                <?php foreach($last_products as $product) :?>
                    <?php require(__DIR__ .'/product_smart.php')?>
                <?php endforeach?>
            </div>
        </div>
    <?php endif?>
</div>