var supplierHome = new Vue({
    el: "#supplierHome",
    data: {
        requestFlag: true,
        userId: 0,
        supplierInfo:{},
        supplierBanner: [],
        mainProducts:[],
        params: {
            size: 12,
            num: 1,
            id: 0,
            keyword: ''
        },
        listRecord:0,
        productLists:[]
    },
    computed: {

    },
    methods: {
        getShopInfo: function(){
            var _self = this;
            $.getJSON(spiServer + "/supplier/home/detail",{supplierId: this.params.id}).done(function (r) {
                if (r.code === 0 && r.data) {
                    _self.supplierInfo = r.data;
                }
            });
        },
        getBanners: function(){
            var _self = this;
            $.getJSON(spiServer + "/supplier/home/images",{supplierId: this.params.id}).done(function (r) {
                if (r.code === 0 && r.data) {
                    _self.supplierBanner = r.data;
                    setTimeout(function(){
                        if (isPC) {
                            $('#supplierBanner').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('#supplierBanner', {
                                loop : true,
                                autoplay: {
                                    delay: 2000,
                                    disableOnInteraction: false
                                },
                                navigation: {
                                    nextEl: '.swiper-button-next',
                                    prevEl: '.swiper-button-prev'
                                },
                                pagination: {
                                    el: '.swiper-pagination',
                                    clickable :true
                                }
                            });
                        }
                    },500);
                }
            });
        },
        getMainProducts: function(){
            var _self = this;
            $.getJSON(spiServer + "/supplier/home/products",{supplierId: this.params.id}).done(function (r) {
                if (r.code === 0 && r.data) {
                    if(_self.userId && _self.userId>0){
                        var productIdArr = [];
                        r.data.map(function (item) {
                            // 0公开价格 1不公开价格 2仅对会员机构公开
                            if (item.priceFlag !== 1) {
                                productIdArr.push(item.id)
                            }
                        });
                        setProductPrice(r.data, productIdArr.join(","), _self.userId, function () {});
                    }
                    _self.mainProducts = r.data;
                }
            });
        },
        getProductLists: function(){
            var _self = this;
            $.getJSON(spiServer + "/search/query/product/supplier",this.params, function (r) {
                if (r.code === 0 && r.data) {
                    var result = JSON.parse(r.data);
                    _self.listRecord = result.total;
                    var resultData = setSearchProductList(result.items, _self.userId,2);
                    _self.productLists = _self.productLists.concat(resultData);
                }
                _self.requestFlag = true;
            })
        },
        keyupSearch: function(event) {
            // 搜索输入框 按回车键搜索
            var keyCode = event.keyCode || event.which;
            if(keyCode === 13){
                this.supplierSearch();
            }
        },
        supplierSearch: function(){
            if (this.params.keyword.length < 2) {
                $.confirm({
                    useBootstrap: false,
                    boxWidth: (isPC?'300px':'70%'),
                    title:'提示',
                    content:'请至少输入两个关键字！',
                    closeIcon: true,
                    animation: 'opacity',
                    closeAnimation: 'opacity',
                    animateFromElement: false,
                    buttons: {
                        close: {
                            text: '确定',
                            btnClass: 'btn-confirm'
                        }
                    }
                });
                return false;
            }
            this.productLists = [];
            this.getProductLists();
            var floorScroll = $('#supProduct').offset().top;
            $('html,body').animate({scrollTop: floorScroll},600);
        },
        upgrade: function () {
            location.href='/upgrade.html';
        }
    },
    created: function () {
        this.params.id = getUrlParam("id") ? getUrlParam("id") * 1 : 0;
        if(this.params.id !== 0) {
            this.getShopInfo();
            this.getBanners();
        }
    },
    mounted: function () {
        if(this.params.id !== 0) {
            this.getMainProducts();
            this.getProductLists();
        }
         var userInfo = localStorage.getItem('userInfo');
            if(userInfo){
                this.userId = JSON.parse(userInfo).userId;
            }
        var _self = this;
        $('.supplierInfo').slide({
            mainCell:".tabCon"
            ,titCell:".tabTit span"
            ,trigger: "click"
        });
        //上垃加载更多
        $(window).on('scroll', function(){
            var scrollTop = $(this).scrollTop();
            var scrollHeight = $(document).height();
            var windowHeight = window.innerHeight+$(".new-footer").height();
            if (scrollTop + windowHeight >= scrollHeight) {
                //此处是滚动条到底部时候触发的事件，在这里写要加载的数据，或者是拉动滚动条的操作
                var totalPage = Math.ceil(_self.listRecord / _self.params.size)?Math.ceil(_self.listRecord / _self.params.size):1;
                var next = _self.params.num+1;
                if(next <= totalPage){
                    if (_self.requestFlag){
                        _self.params.num = next;
                        // 获取列表数据
                        _self.getProductLists();
                    }
                    _self.requestFlag = false;
                }
            }
        });
        $('body').on("click", '.showSearch', function(){
            $('.supplierTit .search').show();
        })
    }
});
