
<script>

    <?php
        $productsIdsInBasket = $this->context->getUser()->getProductsIdsInBasket();
        $groupProductsIdsInBasket = array();
        foreach ($productsIdsInBasket as $id => $count)
        {
            $groupProductsIdsInBasket[] = array(
                'id' => $id,
                'count' => $count
            );
        }
    ?>


</script>

<style>
    .list_page__cont__inner{
        /*position: fixed;*/
        display: block;
        position: relative;
        background-color: #fff;
        padding: 0 20px 0 20px;
    }
    .list__inner_fixed{
        position: fixed;
        z-index: 10;
    }
    .er_footer{
        visibility: hidden;
    }
    .list__search{
        display: block;
    }
</style>

<div class="er_content__main__search list__search" style="padding:20px;">
    <form action="<?php echo url_for('@catalog_good_list'); ?>" method="post">
        <table cellpadding="0" cellspacing="0" width="100%">
            <tbody><tr>
                <td class="pr_20" style="clear:both;">
                    <input id="title-search-input" type="text" name="query" value="<?php echo $query ?>" autocomplete="off" placeholder="Поиск по каталогу" style="z-index:102;position:relative;width:100%;border-radius:2px 0 0 2px;">
                </td>
                <td width="1">
                    <button id="search-submit-button" type="submit" class="green_button er_content__main__search__btn">Найти</button>
                </td>
            </tr>
            </tbody></table>
    </form>
    <div class="folder_cat">Каталог</div>
    <div class="folder_cat_drop">
        <div class="folder_cat_drop__close" title="Закрыть категории" onclick="$('.folder_cat').find('span')[0].click();"></div>
        <div class="folder_cat_drop_overflow">
            <div class="folder_cat_drop__inner"></div>
        </div>
    </div>
</div>

<div class="er_rows">
    <b class="fs_24">Каталог списком</b>
</div>

<div class="er_rows" style="padding: 0;">
    <div class="list_page__cont" onclick="event.stopPropagation();">
        <div class="list_page__cont__inner"></div>
    </div>
</div>
<script type="text/javascript">
    js_cat.preloader($('body'), 'Подождите, пожалуйста...');
    var curDomain = '<?php echo url_for('@homepage');?>';
</script>
<script type="text/javascript" src="/js/goods_list.js"></script>

<?php
$getPart = '';
if ($query)
{
    $getPart = '?q='.$query;
}
?>


<script type="text/javascript" src="/db2.js.php<?php echo $getPart ?>"></script>

<script type="text/javascript">
    var basketMemory = {
        productsIdsInBasket: <?php echo json_encode($groupProductsIdsInBasket) ?>,

        getCount: function (id) {
            for(var i = 0;i < this.productsIdsInBasket.length;i++) {
                if (this.productsIdsInBasket[i].id == id) {
                    return this.productsIdsInBasket[i].count;
                }
            }
            return 0;
        },

        add: function (id, count) {
            var found = false;
            for(var i = 0;i < this.productsIdsInBasket.length;i++) {
                if (this.productsIdsInBasket[i].id == id) {

                    this.productsIdsInBasket[i].count = parseInt(this.productsIdsInBasket[i].count) + parseInt(count);

                    found = true;
                    break;
                }
            }
            if (!found) {
                this.productsIdsInBasket[this.productsIdsInBasket.length] = {"id":id,"count": parseInt(count)};
            }

            console.log(this.productsIdsInBasket);

        },

        remove: function (id, count) {

            for(var i = 0;i < this.productsIdsInBasket.length;i++) {
                if (this.productsIdsInBasket[i].id == id) {

                    if (this.productsIdsInBasket[i].count > 0)
                    {
                        this.productsIdsInBasket[i].count = parseInt(this.productsIdsInBasket[i].count) - parseInt(count);
                    }

                    break;
                }
            }

            console.log(this.productsIdsInBasket);

        }

    }
    var listCat = {
        catArr: [],
        countH: 63,
        productsIdsInBasket: <?php echo json_encode($groupProductsIdsInBasket) ?>,
        catActivity: function(scrollPos){
            var _this = this;
            var tempAll = '<table cellpadding="10" cellspacing="0" width="100%" class="std_table js_cat_view" style="margin-top: 20px;"><thead><tr>' +
                '<th data-field="name" width="100%" colspan="2" align="left"><span class="sortable__txt">Название</span></th>' +
                '<th style="width: 80px;">Код</th>' +
                '<th class="sortable" data-field="price" style="width: 90px;" align="right"><span class="sortable__txt">Цена</span></th>' +
                '<th style="width: 70px;">Мин.&nbsp;п.</th>' +
                '<th class="sortable" data-field="number_product" style="width: 100px;"><span class="sortable__txt">В&nbsp;наличии</span></th>' +
                '<th style="width: 135px;"></th>'+
                '</tr></thead><tbody class="js_cat_view__ct">';
            var countFolder = true;
            var htmlFolder = '';

            _this.listWrap.outerHeight(_this.countH * _this.catArr.length );
            _this.listInnerH = _this.listInner.outerHeight();
            _this.countView = parseInt(_this.listInnerH / _this.countH);

            var cl_was = false;
            $.each(_this.catArr.slice(parseInt(scrollPos / _this.countH) , parseInt(scrollPos / _this.countH) + _this.countView), function(i, obj){
                var elem;
                if(typeof obj.title == 'undefined'){
                    elem = db.cat[obj.id];

                    if(!cl_was){
                        cl(elem);
                        cl_was = true;
                    }

                    if(countFolder){
                        countFolder = false;
                        $.each(db.catFolder[obj.cat_id].parents, function(i, elFolder){
                            var _elFolder = db.catFolder[elFolder];
                            if(typeof _elFolder != 'undefined'){
                                htmlFolder += '<span data-fid="' + _elFolder.id + '">' +
                                    (_elFolder.title == 'Каталог' ? 'Все категории' : _elFolder.title) +
                                    '</span>' + (db.catFolder[obj.cat_id].parents.length - 1 == i ? '' : '&nbsp;/&nbsp;');
                            }
                        });
                        _this.folderCat.html(htmlFolder);
                    }

                    if(typeof user_profile != 'undefined'){
                        var price = elem.price;
                        var _price = 0;

                        _price = (elem.price - ((elem.price / 100) * user_profile.sum_discount)).toFixed(2);

                    }else{
                        _price = (parseFloat(elem.price)).toFixed(2);
                    }

                    var value = basketMemory.getCount(elem.id);




                    tempAll += list.product.add(elem.id, elem.code, elem.article, elem.name, _price, elem.min_purchase, elem.number_product, i, elem.img, 1, elem.number_product_in_way, value);
                }else{
                    tempAll += '<tr class="js_cat_view__ct__tr_folder"><td colspan="8" class="folder_title">' + obj.title + '</td></tr>';
                }
            });
            _this.listInner.html(tempAll);

            if(scrollLastFunc != null){
                scrollLastFunc();
                scrollLastFunc = null;
            }

        },


        init: function(){
            var _this = this;
            var _idFolder = 0;
            var iCounter = 0;
            var tempAll = '';
            _this.mainComtent = $('.er_content__main');
            _this.listInner = $('.list_page__cont__inner');
            _this.listWrap = $('.list_page__cont');
            _this.listInnerTop = null;
            _this.header = $('#head_wrapper');
            _this.searchBar = $('.list__search');
            _this.searchBarTop = _this.searchBar.offset().top;
            _this.folderCat = $('.folder_cat');
            _this.folderCatDrop = $('.folder_cat_drop');
            _this.folderCatPosArr = {};
            _this.events();

            _this.listInnerTop = _this.listInner.offset();

            $.each(db.cat_idx.title, function(idFolder, ids){

                idFolder = idFolder.replace('ob_', '');

                var titleFolder = '';
                var htmlFolder = '';
                var folder = {title:''};


                $.each(db.catFolder[idFolder].parents, function(i, elFolder){
                    var _elFolder = db.catFolder[elFolder];
                    if(typeof _elFolder != 'undefined' && i != 0){
                        titleFolder += _elFolder.title + (db.catFolder[idFolder].parents.length - 1 == i ? '' : '&nbsp;/&nbsp;');
                    }
                });
                folder.title = titleFolder;
                folder.id = idFolder;
                iCounter = _this.catArr.push(folder);
                _this.folderCatPosArr[idFolder] = iCounter - 1;


                $.each(ids, function(j, el){
                    var o = {
                        id: el,
                        cat_id: idFolder
                    };
                    _this.catArr.push(o);
                });
            });

            _this.catActivity(0);


            $.each(db.catFolder[0].children, function(i, elem){
                var _elem = db.catFolder[elem];
                if(typeof _elem != 'undefined'){
                    tempAll += '<div style="margin-bottom: 20px;color: #3C7AC0;"><div class="click_folder" data-fid="' + _elem.id + '"><span>' + _elem.title + '</span></div>';
                    if(_elem.children.length > 0){
                        tempAll += _this.folderCatBuild(_elem.children, 1);
                    }
                    tempAll += '</div>';
                }
            });

            _this.folderCatDrop.find('.folder_cat_drop__inner').html(tempAll);
            js_cat.preloader($('body'));
        },

        folderCatBuild: function(children, level){
            var tempAll = '';
            var _this = this;
            $.each(children, function(i, elem){
                var _elem = db.catFolder[elem];
                if(typeof _elem != 'undefined'){
                    tempAll += '<div class="click_folder" data-fid="' + _elem.id + '" style="padding-left:' + (level * 20) + 'px"><span>' + _elem.title + '</span></div>';

                    if(_elem.children.length > 0){
                        tempAll += _this.folderCatBuild(_elem.children, level + 1);
                    }

                }

            });
            return tempAll;
        },

        onRes: function(){
            var _this = this;
            var hMainContent = 0;
            _this.listInner.hide();
            _this.folderCatDrop.hide();
            hMainContent = _this.mainComtent.outerWidth();
            _this.searchBar.outerWidth(hMainContent);
            _this.listInner.outerWidth(hMainContent);
            _this.folderCatDrop.outerWidth(hMainContent);
            _this.listInner.show();
            _this.folderCatDrop.show();
            _this.listInner.outerHeight($(window).outerHeight() - _this.header.outerHeight() - _this.folderCat.outerHeight() - _this.searchBar.outerHeight());
            _this.listInner.parent().outerHeight(_this.listInner.outerWidth());
            _this.catActivity(0);
        },

        onScroll: function(scrollTop){
            var _this = this;
            if(scrollTop + _this.header.outerHeight() + _this.searchBar.outerHeight() >= _this.listInnerTop.top){
                _this.searchBar.addClass('er_content__main__search_fixed');
                _this.searchBar.css({
                    'width': _this.mainComtent.outerWidth(),
                    'top': _this.header.outerHeight()
                });
                _this.listInner.addClass('list__inner_fixed');
                _this.listInner.css('top', _this.header.outerHeight() + _this.searchBar.outerHeight() + _this.folderCat.outerHeight());

                _this.catActivity(scrollTop);
            }else{
                _this.listInner.removeClass('list__inner_fixed');
                _this.listInner.css('top', 'auto');

                _this.searchBar.css('top', '0');
                _this.searchBar.removeClass('er_content__main__search_fixed');

                _this.catActivity(0);
            }

        },

        events: function(){
            var _this = this;
            $(window).resize(function(){
                _this.onRes();
            }).resize();

            $(document).scroll(function(){
                _this.onScroll($(document).scrollTop());
            });

            _this.folderCat.on('click', 'span', function(){
                var id = $(this).data('fid');
                if(id == 0){
                    if(_this.folderCatDrop.hasClass('folder_cat_drop_active')){
                        _this.folderCatDrop.removeClass('folder_cat_drop_active');
                        _this.folderCatDrop.css('height', 0);
                    }else{
                        _this.folderCatDrop.addClass('folder_cat_drop_active');
                        _this.folderCatDrop.css('height', _this.listInner.outerHeight());
                    }
                    _this.overlayHideShow();
                }else{
                    $(document).scrollTop(_this.folderCatPosArr[id] * _this.countH);
                }

                console.log('ss');

            });

            _this.folderCatDrop.on('click', '.click_folder', function(){
                var id = $(this).data('fid');
                $(document).scrollTop(_this.folderCatPosArr[id] * _this.countH);
                _this.folderCatDrop.removeClass('folder_cat_drop_active');
                _this.folderCatDrop.css('height', 0);
                _this.overlayHideShow();
            });

            _this.folderCatDrop.on('scroll', function(){

            });

        },

        overlayHideShow: function(){

            var mw = window.innerWidth - document.body.clientWidth;
            var body = $('body');
            var html = $('html');

            mw = $('.max_width').eq(0).outerWidth() >= $(window).width() ? 0 : mw;

            console.log($('.max_width').eq(0).outerWidth(), $(window).width());

            //if(body.hasClass('scrollbarHidden')){
            //    body.css('width', 'auto');
            //    body.css('overflow-y', 'scroll');
            //    body.css('margin-left', '0');
            //    body.removeClass('scrollbarHidden');
            //}else{
            //    if(mw == 0){
            //        body.outerWidth(body.outerWidth());
            //    }
            //    body.css('overflow', 'hidden');
            //    body.css('margin-left', -mw);
            //    body.addClass('scrollbarHidden');
            //}
        }

    };


    $(window).load(function(){


        listCat.init();
    });
</script>