var articleList = new Vue({
    el: '#articleList',
    data: {
        loginStatus: false,
        listLoading: true,
        requestFlag: true,
        noMore: false,
        params: {
            size: 8,
            num: 1,
            typeId: '',
            labelId: '',
            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;
            }
        },
        getArticleList: function (url, params) {
            var _self = this;
            $.getJSON(url, params, function (r) {
                if (r.code === 0 && r.data) {
                    var result = JSON.parse(r.data);
                    _self.listRecord = result.total;
                    var resultData = [];
                    result.items.map(function (item) {
                        resultData.push({
                            id: item.a_id,
                            title: item.a_title,
                            image: item.a_image,
                            intro: item.a_intro,
                            typeId: item.a_type_id,
                            type: item.a_type_text,
                            labelIds: item.a_label_ids.replace(/[^[\d|\S]]*/g, ',').split(","),
                            labels: item.a_label_text.replace(/[^[\d|\S]]*/g, ',').split(","),
                            pv: item.a_pv,
                            publisher: item.a_publisher,
                            publishDate: item.a_publish_date
                        });
                    });
                    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 : 8;
            this.params.num = getUrlParam("pageNum") ? getUrlParam("pageNum") * 1 : 1;
        }else{
            this.params.size = 8;
            this.params.num = 1;
        }
        this.params.keyword = getUrlParam("keyword")?getUrlParam("keyword"):'';
        // 判断登录状态
        if (globalUserData.token) {
            this.loginStatus = true;
        }
    },
    mounted: function () {
        this.params.typeId = $("#typeId").val()*1;
        this.params.labelId = $("#labelId").val()*1;
        if(!this.params.typeId){$(".content").addClass("sea-top")}
        var requestUrl = '';
        var requestParams = {};
        if (this.params.typeId) {
            requestUrl = spiServer+"/search/query/article/type";
            requestParams = {
                id: this.params.typeId,
                pageSize: this.params.size,
                pageNum: this.params.num
            };
        } else if (this.params.labelId) {
            requestUrl = spiServer+"/search/query/article/label";
            requestParams = {
                id: this.params.labelId,
                pageSize: this.params.size,
                pageNum: this.params.num
            };
        } else if(this.params.keyword){
            requestUrl = spiServer+"/search/query/article";
            requestParams = {
                keyword: this.params.keyword,
                pageSize: this.params.size,
                pageNum: this.params.num
            };
            $("header").find("#topSearch").remove();
        }
        if (requestUrl) {
            this.getArticleList(requestUrl, requestParams);
        }
        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;
                            requestParams.pageNum = next;
                            // 获取列表数据
                            _self.getArticleList(requestUrl, requestParams);
                        }
                        _self.requestFlag = false;
                    }else{
                        //到底了
                        _self.noMore = true;
                        $('footer').removeClass("noneImportant");
                    }
                }
            });
        }
        $("body").on("click", ".header .hotWord", function(){
            $("#mHotWord").show();
        }).on("click", "#mHotWord .close,#mHotWord", function(){
            $("#mHotWord").hide();
        }).on("click", "#mHotWord .tags", function(e){
            e.stopPropagation();
        })

    }
});
