;
var helpSuggestion = new Vue({
    el: "#dashboard",
    data: {
        btnLoading: false,
        isRequset:true,
        listQuery:{
            userId: '',
            pageNum:1,
            pageSize:10,
        },
        noMore:false,
        listRecord: 0,
        pageInput: '1',
        degree:'',
        isCheckedAll:false,
        messagesList:[],//消息列表
        check:false,
        checklist:[],
        msgId:'',
    },
     computed: {
        pageTotal: function () {
            var total = Math.ceil(this.listRecord / this.listQuery.pageSize);
            return total > 0 ? total : 1;
        },
        showPageBtn: function () {
            var total = Math.ceil(this.listRecord / this.listQuery.pageSize);
            total = total > 0 ? total : 1;
            var index = this.listQuery.pageNum, 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) {
                this.listQuery.pageNum = pageNum;
                this. GetMyPcMymessageInfo();
            }
        },
        checkNum: function () {//输入跳转分页
            if (this.pageInput > this.pageTotal) {
                this.pageInput = this.pageTotal;
            } else if (this.pageInput < 1) {
                this.pageInput = 1;
            }
        },

        GetMyPcMymessageInfo:function(){
            var _self = this;
            UserApi.GetMymessageList(_self.listQuery,function (response) {
                if(response.code == 0){
                    _self.isRequset = false;
                     if (response.data.results.length>0){
                      var data = response.data;
                         _self.messagesList = data.results;
                         _self.listRecord =  data.totalRecord;
                         _self.requestFlag = true;
                     }else {
                     _self.requestFlag = false;
                      _self.listRecord =  data.totalRecord;
                     }
                }else{
                    CAIMEI.Alert(response.msg, '确定', false);
                     _self.requestFlag = false;
                }
            })
        },
        checkedItemFn:function(item){ //单选
            var _self = this;
                    if(event.target.checked){
                         _self.checklist.push(item.messageID);
                         _self.msgId = _self.checklist.toString()+','
                    }else {
                      _self.checklist.splice(_self.checklist.indexOf(item.messageID), 1);
                      _self.msgId = _self.checklist.toString()+','
                    }
                    if(_self.checklist.length==_self.messagesList.length){
                        _self.isCheckedAll=true;
                    }else {
                       _self.isCheckedAll=false;
                    }

        },
          checkedAllFn: function () { //全選
            var _self = this;
            _self.isCheckedAll = !_self.isCheckedAll;
                if (_self.isCheckedAll) {
                    _self.checklist = [];
                    _self.messagesList.forEach(function (item) {
                             item.check = true;
                             _self.checklist.push(item.messageID);
                             _self.msgId = _self.checklist.toString()+','
                        })
                }else{
                     _self.messagesList.forEach(function(item){
                          item.check = false;
                    });
                    _self.checklist = [];
                    _self.msgId = ''
                }
        },
        deleteMessageFn: function(){//删除消息
             var _this =this;
             if(_this.messagesList==''){
                 CAIMEI.Alert('暂无消息', '确定', false);
             }else if(_this.checklist.length==0) {
                 CAIMEI.Alert('未选择任何消息', '确定', false);
             }else {
                PublicApi.deleteMessage({messageIds:_this.msgId},function (res) {
                    if (res.code==0){
                      CAIMEI.dialog('刪除成功');
                       location.reload()
                    }else {
                      CAIMEI.Alert(res.msg, '确定', false);
                    }
                })
             }
        },
        signMessageFn: function(){//标记为已读
          var _this =this;
            console.log(_this.checklist);
            console.log(_this.msgId);
            if(_this.messagesList==''){
             CAIMEI.Alert('暂无消息', '确定', false);
            }else if(_this.checklist.length==0){
              CAIMEI.Alert('未选择任何消息', '确定', false);
            }else {
                     PublicApi.markAsRead({messageIds:_this.msgId},function (res) {
                    if(res.code==0){
                     CAIMEI.dialog('已读成功');
                     location.reload()
                    }else {
                     CAIMEI.Alert(res.msg, '确定', false);
                    }
                })
            }

        },
        showBadge:function(n){//处理标签
            var num ='';
            if(n>100){num = 99}else{num = n;}
            return num;
        },
        toFixedFn:function(text){//处理小数点后两位数
            return Number(text).toFixed(2);
        },

    },
    mounted: function () {
        var _self=this;
        if(globalUserData){
            this.listQuery.userId = globalUserData.userId;
            this.GetMyPcMymessageInfo();
        }
        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.listQuery.pageSize)?Math.ceil(_self.listRecord / _self.listQuery.pageSize):1;
                    var next = _self.listQuery.pageNum+1;
                    if(next <= totalPage){
                        if (_self.requestFlag){
                            _self.listQuery.pageNum = next;
                            // 获取列表数据
                            _self.GetMyPcMymessageInfo();
                        }
                        _self.requestFlag = false;
                    }else{
                        //到底了
                        _self.noMore = true;
                        $('footer').removeClass("noneImportant");
                    }
                }
            });
        }
    }
});
