Vue.config.devtools = true;
var v = new Vue({
    el:".container",
    data:{
        isLogin: 0,
        memToken: '',
        memHeadImg: '',
        userJf:-1,
        goodList:[],
        start:1,
        size:10,
        finished:false,
        loading:false,//出现了 上拉到底出发了两次请求接口的情况  加一个变量控制一下
        integralType: "",
        integralCount: "",
    },
    created:function (){
        if(!checkLogin()){
            // goLogin();
            return false;
        }
        if($.cookie("memToken")){
            this.memToken = $.cookie("memToken");
            this.isLogin=1;
        }
        if($.cookie("memHeadImg")){
            this.memHeadImg = $.cookie("memHeadImg");
        }
        this.findUserGoodList();
        this.findUserIntegral();
    },
    mounted:function(){
        // window.addEventListener("scroll", this.scrollBottom, true);
        setTimeout(() => {
            $(".loading-item").fadeOut(200);
        },500)
    },
    methods: {
        showOrder (i) {
          this.goodList[i].flag = !this.goodList[i].flag;
        },
        gotoCommodityDetail (id){
            if(!checkLogin()){
                // goLogin();
                return false;
            }
            window.location.href = serverHttp + "/user/commodityDetail?from=H5&memToken="+this.memToken+"&id="+id;
        },
        findUserIntegral (){
            var that = this;
            $.ajax({
                type : 'POST',
                url : serverHttp+"/user/findUserIntegral",
                data : {
                    memToken:that.memToken
                },
                success : function(data) {
                    if (data.code == 200) {
                        that.userJf = data.data;
                    }
                },
                error : function(data) {
                }
            });
        },
        findUserGoodList (){
            var that = this;
            if (that.loading) {
                return ;
            }
            that.loading = true;
            if(that.start == 1){
                that.list = []
            }
            $.ajax({
                type : 'POST',
                url : serverHttp+"/user/findUserGoodList",
                data : {
                    memToken:that.memToken,
                    start:that.start,
                    size:that.size
                },
                success:function (data) {
                    if (data.code == 200) {
                        that.goodList = that.goodList.concat(data.data);
                        // 请求的数据未加载完成时
                        that.start = that.start + 1;
                        that.loading = false;
                    } else if (data.code == 10005) {
                        that.finished = true;
                    }
                }
            })
        },
        scrollBottom() {
            const that = this;
            // 滚动到页面底部时
            const el = document.getElementById("list");
            const elHeight = $("#list").offset()['top'];//元素距离顶部距离 该计算可能会有一些偏差
            const windowHeight = window.screen.height*750/window.screen.width;
            const scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
            const contentHeight = el.clientHeight;
            const toBottom = contentHeight - windowHeight - scrollTop-elHeight;
            console.log(contentHeight,windowHeight,scrollTop,elHeight,toBottom)
            if (toBottom < 10 && !that.finished) {
                that.findUserGoodList();
            }
        },
    }
})
