/**
 * Created by zjwen on 2018/11/2.
 */
(function(){
    var isEnd = 0;// 是否有下一页
    var wp = '';// 分页数据
    var type = 1;
    var top = $('.tab').offset().top;
    var close = false;

    function AppRoLink(link,type){
        if(isStart == 0 && !mobileUtil.isWeixin && !type) {
            $('.btn2-ok').data('href',link);
            $('.tips2').show();
        } else {
            // if(mobileUtil.isApp){
            //     var data= {
            //         type: 'taobao' ,
            //         static: {
            //             url: link,
            //         }
            //     };
            //     shbridge.sqb(data);
            // }else
            if(mobileUtil.isWeixin){
                alert('请右上角选择浏览器打开');
            }else{
                window.location.href = link;
            }
            $('.tips2').hide();
        }
    }
    // 商品加载
    handleDate(wp,type,function(list){
        list.forEach(function(val){
            $('.items').append(template(val));
        });
        $('.item').click(function(){
            AppRoLink($(this).data('href'));
        });
    });

    $('.btn2-cancel').click(function(){
        $('.tips2').hide();
    });

    $('.btn2-ok').click(function(){
        AppRoLink($(this).data('href'),1);
    });

    function template(data){
        var text = type == 1 ? '抢免单': '抢秒杀';
        var bgm = type == 1 ? 'bgm1' :  'bgm2';
        var textbgm = type == 1 ? 'bgm1' :  'bgm2';

        var gotobgm = isStart == 1 ? bgm : 'bgm3';
        var gototext = isStart == 1 ? text : '11日开抢';
        return '<div class="item" data-href="'+data.link+'">'+
            '<div class="img">'+
            '<img src="'+data.img+'" alt="">'+
            '</div>'+
            '<div class="title">'+data.title+'</div>'+
            '<div class="info">'+
            '<div class="price">'+
            '<div class="icon"></div>'+
            '<div class="num">'+data.price+'</div>'+
            '</div>'+
            '<div class="goto '+gotobgm+'">'+gototext+'</div>'+
            '</div>'+
            '<div class="text '+textbgm+'">'+data.remark+'</div>'+
            '</div>';
    }

    function handleDate($wp,$type,callBack){
        var params = {
            wp: $wp,
            pid: pid,
            type: $type,
        };
        if(close) return ;
        close = true;
        $.ajax({
            url: '/h5/activity/double11Items',
            dataType: 'json',
            data: params,
            timeout: 60000,
            success: function(json){
                if(json.status.code == 1001){
                    close = false;
                    isEnd = json.result.isEnd;
                    wp = json.result.wp;
                    callBack(json.result.list);
                } else {
                    console.error(json.status.msg)
                }
            },
            error: function(xhr, type){
                console.error('Ajax error!');
            }
        });
    }

    $(window).scroll(function(){
        var scrollTop = $(window).scrollTop();
        var scrollHeight = $(document).height();
        var windowHeight = $(window).height();
        var offset = 1;
        if(scrollTop + windowHeight >= (scrollHeight - offset)){
            if(isEnd) return false;
            handleDate(wp,type,function(list){
                list.forEach(function(val){
                    $('.items').append(template(val));
                });
                $('.item').click(function(){
                    AppRoLink($(this).data('href'));
                })
            });
        };
        if($(window).scrollTop() > 350 ){
            var fixed = {
                'position':'fixed',
                'top': 0,
                'left': 0,
                'background': 'rgba(0,0,0,0.8)',
            }
            $('.tab').css(fixed)
        }else{
            var fixed = {
                'position':'static',
                'background': 'none',
            }
            $('.tab').css(fixed)
        }
    });

    $('.tab-1').on('click',function(){
        $('.tab-1').removeClass('action');
        $(this).addClass('action');
        type = $(this).data('type');
        if(type == 1){
            $('.tips').html(
                '<img src="http://qnoss3.lanlanlife.com/9f0bf676e8732cd2ff2661f076000cc3_136x728.png" alt="">'
            );
        }else{
            $('.tips').html(
            '<img src="http://qnoss.lanlanlife.com/5a04ff1fe5c8fcb6b5b1f67b31ba9433_136x728.png" alt="">'
            );
        }
        handleDate('',type,function(list){
            var el = '';
            list.forEach(function(val){
                el = el + template(val).toString();
            });
            $('.items').html(el);
            $('.item').click(function(){
                AppRoLink($(this).data('href'));
            })
        });
    })
})();

(function(){
    $('#share').on('click',function(){
        if(mobileUtil.isApp){
            var data= {
                type: 'share' ,
                static: {
                    src: shareImg
                }
            };
            shbridge.sqb(data);
        }else{
            $('.modal').show();
        }
    });

    $('.close').on('click',function(){
        $('.modal').hide();
    });
})();

(function(){
    $('.tips-go').click(function(){
        $('.tips1').show();
    });
    $('.btn1-ok').click(function(){
        $('.tips1').hide();
    });
})()