var productList = new Vue({
    el: "#productList",
    data: {
        searchFlag: false,
        listLoading: true,
        requestFlag: true,
        noMore: false,
        params: {
            size: 0,
            num: 0,
            keyword: "",
            sortField: "",
            sortType: 1, // 1降序，其他升序
            bid: "",
            sid: "",
            tid: ""
        },
        classify: [],
        listData: [], //priceflag 0公开价格 1不公开价格 2仅对会员机构公开,//userIdentity: 2-会员机构, 4-普通机构
        listRecord: 0,
        pageInput: '1',
        bigType: "",
        smallType: "",
        tinyType: "",
        userId: 0,
        shopID:0,
        userIdentity: '',
        userToken: ''
    },
    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);
            }
        },
        toSortList: function (sortField, sortType) {
            var params = {
                sortfield: sortField,
                sorttype: sortType
            };
            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/product", {
                keyword: this.params.keyword,
                pageSize: this.params.size,
                pageNum: this.params.num,
                sortField: this.params.sortField,
                sortType: this.params.sortType
            }, 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);
                    if(isPC){
                        _self.listData = resultData;
                    }else{
                        _self.listData = _self.listData.concat(resultData);
                    }
                    console.log(_self.listData)
                }
                _self.listLoading = false;
                _self.requestFlag = true;
            })
        },
        getCategorys: function () {
            var _self = this;
            $.getJSON(spiServer+"/product/classify").done(function (r) {
                if (r.code === 0 && r.data) {
                    _self.classify = r.data;
                    if(isPC){
                        setTimeout(function(){
                            $('#listClassify').slide({
                                mainCell:".clsCon"
                                ,titCell:".clsTab a"
                                ,trigger: "mouseover"
                            });
                        },500);
                        r.data.map(function(big){
                            if (_self.params.bid == big.bigTypeID){
                                _self.bigType = big.name;
                                if (_self.params.sid && _self.params.sid>0) {
                                    big.smalltypeList.map(function(small){
                                        if (_self.params.sid==small.smallTypeID){
                                            _self.smallType = small.name;
                                            if (_self.params.tid && _self.params.tid>0) {
                                                small.tinytypeList.map(function(tiny){
                                                    if (_self.params.tid==tiny.tinyTypeID){
                                                        _self.tinyType = tiny.name;
                                                    }
                                                })
                                            }
                                        }
                                    })
                                }
                            }
                        })
                    }
                }
            });
        },
        getListByCategory: function (path, categoryId) {
            var _self = this;
            $.getJSON(spiServer+"/search/query/product/"+ path ,{
                id: categoryId,
                pageSize: this.params.size,
                pageNum: this.params.num,
                sortField: this.params.sortField,
                sortType: this.params.sortType
            }, function (r) {
                if (r.code === 0 && r.data) {
                    var result = JSON.parse(r.data);
                    _self.listRecord = result.total;
                    console.log(_self.userId)
                    var resultData = setSearchProductList(result.items, _self.userId);
                    if(isPC){
                        _self.listData = resultData;
                    }else{
                        _self.listData = _self.listData.concat(resultData);
                    }
                    console.log(_self.listData)
                }
                _self.listLoading = false;
                _self.requestFlag = true;
            })
        }
    },
    created: function () {
        if(isPC){
            this.params.size = getUrlParam("pageSize") ? getUrlParam("pageSize") * 1 : 20;
            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", 0).text("产品");
        $('#topSearch').find('.jqSelect').find('select').val(0);
        $('#topSearch').find('.keyword').val(getUrlParam("keyword"));
        this.params.sortField = getUrlParam("sortField") ? getUrlParam("sortField") : "";
        this.params.sortType = getUrlParam("sortType") ? getUrlParam("sortType") * 1 : 1;
        this.searchFlag = (this.params.keyword !== "");
        if(globalUserData){
            this.userId = globalUserData.userId;
            this.userIdentity = globalUserData.identity;
            this.userToken = globalUserData.token;
        }
        var userInfo = localStorage.getItem('userInfo');
        if(userInfo){
            this.userId = JSON.parse(userInfo).userId;
            this.shopID = JSON.parse(userInfo).shopId;
        }
        if (this.searchFlag) {
            // 获取列表数据
            this.getListByKeyword();
        } else {
            // category=1000-1001-1005
            if (!getUrlParam("category")) {
                this.listLoading = false;
                return false;
            }
            var category = getUrlParam("category").split("-");
            this.params.bid = category[0];
            this.params.sid = category[1];
            this.params.tid = category[2];
            // 根据分类获取数据
            if (this.params.tid && this.params.tid>0) {
                this.getListByCategory("tinyType", this.params.tid);
            } else if (this.params.sid && this.params.sid>0){
                this.getListByCategory("smallType", this.params.sid);
            } else if (this.params.bid && this.params.bid>0) {
                this.getListByCategory("bigType", this.params.bid);
            } else {
                alert("URL参数异常！");
            }
            // 获取分类
            this.getCategorys();
        }
    },
    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;
                            if (_self.searchFlag) {
                                // 获取列表数据
                                _self.getListByKeyword();
                            } else {
                                // 根据分类获取数据
                                if (_self.params.tid && _self.params.tid>0) {
                                    _self.getListByCategory("tinyType", _self.params.tid);
                                } else if (_self.params.sid && _self.params.sid>0){
                                    _self.getListByCategory("smallType", _self.params.sid);
                                } else if (_self.params.bid && _self.params.bid>0) {
                                    _self.getListByCategory("bigType", _self.params.bid);
                                }
                            }
                        }
                        _self.requestFlag = false;
                    }else{
                        //到底了
                        _self.noMore = true;
                        $('footer').removeClass("noneImportant");
                    }
                }
            });
        }
    }
});
