var homeData = new Vue({
    el: '#container',
    data: {
        userId:0,
        images: [],
        recommends: [],
        recommendPage: 1,
        pageFloors: [],
        advertising: [],
        asideNav: [],
        productIdArr:[],
    },
    methods: {
        getBanners: function(){
            var _self = this;
            $.getJSON("/home/banner").done(function (r) {
                if (r.code === 0 && r.data) {
                    _self.images = r.data;
                    setTimeout(function(){
                        if (isPC) {
                            $('#swiper-container').slide({
                                mainCell:".swiper-wrapper"
                                ,titCell:".swiper-pagination span"
                                ,effect: "leftLoop"
                                ,prevCell:".swiper-button-prev"
		                        ,nextCell:".swiper-button-next"
                                ,interTime: 2000
                                ,autoPlay: true
                                ,autoPage: false
                                ,trigger: "mouseover"
                            });
                        } else {
                            var swiper = new Swiper('#swiper-container', {
                                loop : true,
                                autoplay: {
                                    delay: 2000,
                                    disableOnInteraction: false
                                },
                                navigation: {
                                    nextEl: '.swiper-button-next',
                                    prevEl: '.swiper-button-prev'
                                },
                                pagination: {
                                    el: '.swiper-pagination',
                                    clickable :true
                                }
                            });
                        }
                    },500);
                }
            });
        },
        getRecommends: function(){
            var _self = this;
            $.getJSON("/home/recommend").done(function (r) {
                if (r.code === 0 && r.data) {
                    _self.recommends = r.data;
                    _self.recommendPage = Math.ceil(r.data.length / 4);
                    setTimeout(function(){
                        if (isPC) {
                            $('#recommendBox').slide({
                                mainCell:".swiper-wrapper"
                                ,titCell:".swiper-pagination span"
                                ,effect: "leftLoop"
                                ,interTime: 3000
                                ,autoPlay: true
                                ,scroll:4
                                ,vis:4
                                ,trigger: "mouseover"
                            });
                        } else {
                            var swiper = new Swiper('#recommendBox', {
                                slidesPerView: 2,
                                slidesPerColumn: 2,
                                spaceBetween: 0,
                                slidesPerGroup: 2,
                                autoplay: {
                                    delay: 3000,
                                    disableOnInteraction: false
                                },
                                pagination: {
                                    el: '.swiper-pagination'
                                }
                            });
                        }
                    },500);
                    // 获取价格
                    // if(_self.userId && _self.userId>0){
                        var productIdArr = [];
                        _self.recommends.map(function (item) {
                            // 0公开价格 1不公开价格 2仅对会员机构公开
                            if (item.priceFlag !== 1) {
                                productIdArr.push(item.id)
                            }
                        });
                        setProductPrice(_self.recommends, productIdArr.join(","), _self.userId, function () {});
                        console.log(_self.recommends)
                    // }
                }
            });
        },

        getFloorData: function(){
            var _self = this;
            $.getJSON("/home/floor").done(function (r) {
                if (r.code === 0 && r.data) {
                     _self.pageFloors = r.data;
                     setTimeout(function(){
                        // 设置侧边导航数据
                        _self.setAsideNav();
                        // 商品专题
                        if (isPC) {
                            $('#goodsTopic').slide({
                                mainCell:".swiper-wrapper"
                                ,titCell:".swiper-pagination span"
                                ,effect: "leftLoop"
                                ,prevCell:".swiper-button-prev"
		                        ,nextCell:".swiper-button-next"
                                ,interTime: 2000
                                ,autoPlay: true
                                ,autoPage: false
                                ,trigger: "mouseover"
                            });
                        } else {
                            var swiper = new Swiper('#goodsTopic', {
                                loop : true,
                                autoplay: {
                                    delay: 2000,
                                    disableOnInteraction: false
                                },
                                navigation: {
                                    nextEl: '.swiper-button-next',
                                    prevEl: '.swiper-button-prev'
                                },
                                pagination: {
                                    el: '.swiper-pagination',
                                    clickable :true
                                }
                            });
                        }
                    },500)
                }
            });
        },
        getAdvertising: function(){
            var _self = this;
            $.getJSON("/home/advertising").done(function (r) {
                if (r.code === 0 && r.data) {
                    _self.advertising = r.data;
                }
            });
        },
        setAsideNav: function(){
            var _self = this;
            var titArr = $('.floorTit').find('h2,h3');
            for (var i=0; i<titArr.length; i++){
                _self.asideNav.push({
                    id: $(titArr[i]).attr("data-id"),
                    value: $(titArr[i]).text()
                })
            }
        },
        showSubNav: function(event){
            var el = event.currentTarget;
            var parent = $(el).parent();
            parent.siblings().removeAttr("style").removeClass("on").find(".navItem").hide();
            if (parent.hasClass("on")) {
                parent.removeAttr("style").removeClass("on").find(".navItem").slideUp(200);
            } else {
                var height = parent.height();
                parent.addClass("on").find(".navItem").slideDown(200);
                if (parent.find(".navItem").length>0) {
                    setTimeout(function(){
                        height += parent.find(".navItem").height();
                        parent.css("height",height);
                    },200);
                }
            }
        }

    },
    created: function() {
        var userInfo = localStorage.getItem('userInfo');
        if(userInfo){
            this.userId = JSON.parse(userInfo).userId;
        }
        this.getBanners();
        this.getRecommends();
        this.getFloorData();
    },
    mounted: function() {
        this.getAdvertising();
        // 侧边栏滚动判断
        var distance = 0;
        $(window).scroll(function () {
            if($('body').attr("style") && $('body').attr("style").indexOf('fixed')>0){
                return false;
            }
            var scoll = Math.max($('html').scrollTop(), $('body').scrollTop());
            if(scoll >200){
                $('#sideNav').show();
                if(isPC){
                    $('#advertising').show();
                    // 鼠标滚动楼层
                    setTimeout(function () {
                        distance = scoll;
                    },0);
                    $('.floorTit').each(function (i,e) {
                        //每个元素距离顶部的距离
                        var top = $(e).offset().top;
                        if(scoll > distance){
                            if(scoll >= (top-300)){
                                $('#sideNav').find('a').eq(i).addClass('on').siblings().removeClass("on");
                            }
                        }else {
                            if(scoll >= (top-600)){
                                $('#sideNav').find('a').eq(i).addClass('on').siblings().removeClass("on");
                            }
                        }
                    });
                }
            }else {
                $('#sideNav').hide();
                $('#advertising').hide();
            }
        });
        // 置顶
        $('#sideNav').on("click", '.toTop',function () {
            $("html,body").animate({scrollTop:0},500);
        });
        // 侧边导航
        $('#sideNav').on("click", 'a[data-id]',function () {
            var id = $(this).attr('data-id');
            var floorScroll = $('.floorTit [data-id='+id+']').offset().top;
            $('html,body').animate({scrollTop: floorScroll},600);
        });
        //关闭广告图
        $('#advertising').on('click','.close',function () {
            $(this).parents('.item').hide();
        })
    }
});

