var isPC = ($(window).width()>768);
var fleaMarketList = new Vue({
     el: "#fleaMarketList",
     data:{
         currentId:1,
         currentID2:0,
         isShow:false,
         requestFlag: true,
         showflag:false,
         mainflag:false,
         userID:JSON.parse(localStorage.getItem('userInfo')),
         tabTitleName:'二手仪器',
         tabTitle: [
                    {value:'1',name:'二手仪器'},
                    {value:'2',name:'临期产品'},
                    {value:'3',name:'其他'}
                   ],
          tabList : [
                    {value:'0',name:'全部'},
                    {value:'1',name:'轻光电'},
                    {value:'2',name:'重光电'},
                    {value:'3',name:'耗材配件'},
               ],
          listQuery:{
                    pageNum:1,
                    pageSize:12,
                },
          tabchildList:[],
          productsList:[],
          tabchild:{},
          listRecord: 0,
          pageInput: '1',
          params:{},
          isScroll:false,
          noMore:false,
          hasNextPage:false
     },
     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) {
          var _this = this;
           _this.productsList = [];
            if (pageNum <= this.pageTotal) {
                _this.listQuery.pageNum = pageNum;
                _this.gettabList()
            }
        },
        checkNum: function () {
            if (this.pageInput > this.pageTotal) {
                this.pageInput = this.pageTotal;
            } else if (this.pageInput < 1) {
                this.pageInput = 1;
            }
        },
         handle: function (index) {//一級分类
               var _this = this;
               _this.currentId = index;
               _this.tabTitleName = '二手仪器';
               if ( _this.currentId == '1'){
                    _this.mainflag = true;
               }else {
                    _this.mainflag=false;
                    _this.currentID2 = 0;
               }
               _this.listQuery.pageNum = 1;
               _this.gettabList();
          },
         handleChild:function(item){ //二级分类
               var _this = this;
               _this.currentID2 = item.value;
               if(!isPC){
                   _this.tabTitleName = item.name;
                   if (_this.currentID2==item.value){
                       _this.mainflag=false;
                   }
               }
               _this.listQuery.pageNum = 1;
               _this.gettabList();
          },
           gettabList:function (more) {
                var _this = this;
                _this.returnedTarget();
                var params = Object.assign({secondHandType: _this.currentId,instrumentType:_this.currentID2}, _this.listQuery);
                SecondApi.SeconHandProductList(params,function (res) {
                     if (res.code == 0) {
                           var data = res.data;
                           if(data.results.length == 0){
                               _this.tabchildList = [];
                               _this.showflag = true;
                               _this.listRecord = data.totalRecord;
                           }else {
                               _this.showflag = false;
                               _this.isShow = true;
                               _this.hasNextPage = data.hasNextPage;
                               _this.listRecord = data.totalRecord;
                               if(isPC){
                                   _this.tabchildList = data.results;
                               }else{
                                   if(more){
                                       _this.isScroll = false;
                                       _this.tabchildList = _this.tabchildList.concat(data.results);
                                   }else{
                                       _this.tabchildList = data.results;
                                   }
                               }
                           }
                         }else{
                             CAIMEI.Alert(response.msg, '确定', false);
                         }
                    _this.requestFlag = true;
                })
          },
           returnedTarget:function(){//对象合并 IE 兼容方法
            if (typeof Object.assign != 'function') {
                Object.assign = function(target) {
                    'use strict';
                    if (target == null) {
                        throw new TypeError('Cannot convert undefined or null to object');
                    }
                    target = Object(target);
                    for (var index = 1; index < arguments.length; index++) {
                        var source = arguments[index];
                        if (source != null) {
                            for (var key in source) {
                                if (Object.prototype.hasOwnProperty.call(source, key)) {
                                    target[key] = source[key];
                                }
                            }
                        }
                    }
                    return target;
                };
            }
        }
     },
     mounted:function () {
          var _this = this;
          _this.gettabList();
          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(_this.listRecord / _this.listQuery.pageSize)?Math.ceil(_this.listRecord / _this.listQuery.pageSize):1;
                    var next = _this.listQuery.pageNum+1;
                    if(next <= totalPage){
                        if (_this.requestFlag){
                            _this.listQuery.pageNum = next;
                            _this.isScroll = true;
                            setTimeout(function(){
                                _this.gettabList(true);
                            },500)

                        }
                        _this.requestFlag = false;
                    }else{
                        //到底了
                        _this.noMore = true;
                        $('footer').removeClass("noneImportant");
                    }
                }
            });
        }
      },
});
