var supplierList = new Vue({
    el: "#supplierList",
    data: {
        searchFlag: false,
        listLoading: true,
        requestFlag: true,
        noMore: false,
        params: {
            size: 0,
            num: 0,
            keyword: ""
        },
        listData: [],
        listRecord: 0,
        pageInput: ''
    },
    computed: {
        pageTotal: function () {
            var total = Math.ceil(this.listRecord / this.params.size);
            return total > 0 ? total : 1;
        },
        showPageBtn: function () {
            var total = Math.ceil(this.listRecord / this.params.size);
            total = total > 0 ? total : 1;
            var index = this.params.num, arr = [];
            if (total <= 6) {
                for (var i = 1; i <= total; i++) {
                    arr.push(i);
                }
                return arr;
            }
            if (index <= 3) return [1, 2, 3, 4, 5, 0, total];
            if (index >= total - 2) return [1, 0, total - 4, total - 3, total - 2, total - 1, total];
            return [1, 0, index - 2, index - 1, index, index + 1, index + 2, 0, total];
        }
    },
    methods: {
        toPagination: function (pageNum) {
            if (pageNum <= this.pageTotal) {
                var params = {pageNum: pageNum};
                window.location.href = updateUrlParam(params);
            }
        },
        checkNum: function () {
            if (this.pageInput > this.pageTotal) {
                this.pageInput = this.pageTotal;
            } else if (this.pageInput < 1) {
                this.pageInput = 1;
            }
        },
        getListByKeyword: function () {
            var _self = this;
            $.getJSON(spiServer+"/search/query/supplier", {
                keyword: this.params.keyword,
                pageSize: this.params.size,
                pageNum: this.params.num
            }, function (r) {
                if (r.code === 0 && r.data) {
                    var result = JSON.parse(r.data);
                    _self.listRecord = result.total;
                    var resultData = [];
                    result.items.map(function (su) {
                        if(su.products && su.products.length>0) {
                            var productList = setSearchProductList(su.products, 0);
                        }
                        resultData.push({
                            id: su.s_id,
                            name: su.s_name,
                            license: su.s_license,
                            logo: su.s_logo,
                            businessScope: su.s_business,
                            address: su.s_address,
                            productList: productList
                        });
                    });
                    if(isPC){
                        _self.listData = resultData;
                    }else{
                        _self.listData = _self.listData.concat(resultData);
                    }
                }
                _self.listLoading = false;
                _self.requestFlag = true;
            })
        },

    },
    created: function () {
        if(isPC){
            this.params.size = getUrlParam("pageSize") ? getUrlParam("pageSize") * 1 : 10;
            this.params.num = getUrlParam("pageNum") ? getUrlParam("pageNum") * 1 : 1;
        }else{
            this.params.size = 10;
            this.params.num = 1;
        }
        this.params.keyword = getUrlParam("keyword") ? getUrlParam("keyword") : "";
        // 搜索框赋值
        $('#topSearch').find('[data-select]').attr("data-select", 1).text("供应商");
        $('#topSearch').find('.jqSelect').find('select').val(1);
        $('#topSearch').find('.keyword').val(this.params.keyword);
        // 获取列表数据
        this.getListByKeyword();
    },
    mounted: function () {
        var _self = this;
        if(!isPC){
            $('footer').addClass("noneImportant");
            //移动端上垃加载更多
            $(window).on('scroll', function(){
                var scrollTop = $(this).scrollTop();
                var scrollHeight = $(document).height();
                var windowHeight = window.innerHeight;
                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.getListByKeyword();
                        }
                        _self.requestFlag = false;
                    }else{
                        //到底了
                        _self.noMore = true;
                        $('footer').removeClass("noneImportant");
                    }
                }
            });
        }
    }
});
