Vue.config.devtools = true;
var v = new Vue({
    el:".container",
    data:{
        cata:[
            {name:"积分商城",active:1,id:1},
            {name:"积分明细",active:0,id:2},
            {name:"做任务",active:0,id:3},
            {name:"积分榜",active:0,id:4},
        ],
        isLogin: 0,
        memToken: '',
        memHeadImg: '',
        userJf:-1,
        userTaskInfo:[],//积分明细
        userTaskList:[],//任务记录
        ranking:[],//积分排行
        goodMall:[],
        totalPoints:0,
        start: 1,
        size: 12,
        total: 0,
        loading:false,//出现了 上拉到底出发了两次请求接口的情况  加一个变量控制一下
        finished: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.findGoodMallList();
        this.findUserTaskInfo();
        this.findUserIntegral();
        window.addEventListener("scroll", this.scrollBottom, true);
        if (typeof (fromlogin) != "undefined" && fromlogin == "1") {
            jifen();
        }
    },
    mounted:function (){
        setTimeout(() => {
            $(".loading-item").fadeOut(200);
        },500)
    },
    methods: {
        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) {
                }
            });
        },
        findGoodMallList (){
            var that = this;
            if (that.loading) {
                return ;
            }
            that.loading = true;
            if(that.start == 1){
                that.goodMall = []
            }
            $.ajax({
                type: 'POST',
                url: "/user/findGoodMallList",
                data: {jfPage: this.start, jfSize: this.size},
                success: function (res) {
                    if(that.start === res.data.pageSize){
                        that.finished = true;
                    }
                    if (res.code == 200) {
                        res.data.data.forEach(function(item, i){
                            if(item.img){
                                if(item.img.indexOf('http') < 0){
                                    item.img = 'http://files.qkyxzx.com/' + item.img;
                                }
                            }
                        })
                        that.goodMall = that.goodMall.concat(res.data.data);
                        that.total = res.data.total
                        that.totalPoints = res.data.totalPoints;
                        // 请求的数据未加载完成时
                        that.start = that.start + 1;
                        that.loading = false;
                    }
                }
            })
            // $.ajax({
            //     type : 'POST',
            //     url : serverHttp+"/user/findGoodMallList",
            //     data : {
            //         memToken:that.memToken
            //     },
            //     success:function (res) {
            //         if (res.code == 200) {
            //             // that.goodMall = res.data.list;
            //             that.totalPoints = res.data.totalPoints;
            //         }
            //     }
            // })

        },
        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;
            //316为
            const toBottom = contentHeight - windowHeight - scrollTop-elHeight;
            if (toBottom < 10 && !that.finished) {
                that.findGoodMallList();
            }
        },
        findUserTaskInfo (){
            var that = this;
            $.ajax({
                type : 'POST',
                url : serverHttp+"/user/findUserTaskInfo",
                data : {
                    memToken:that.memToken
                },
                success:function (res) {
                    if (res.code == 200) {
                        that.userTaskInfo = res.data;
                    }
                }
            })
        },
        findUserTaskList (){
            var that = this;
            $.ajax({
                type : 'POST',
                url : serverHttp+"/user/findUserTaskList",
                data : {
                    memToken:that.memToken
                },
                success:function (res) {
                    if (res.code == 200) {
                        that.userTaskList = res.data;
                    }
                }
            })
        },
        findUserjfRanking (){
            var that = this;
            $.ajax({
                type : 'POST',
                url : serverHttp+"/user/findUserjfRanking",
                data : {
                    memToken:that.memToken
                },
                success:function (res) {
                    if (res.code == 200) {
                        that.ranking = res.data;
                    }
                }
            })
        },
        chooseTab (i){
            if(!checkLogin()){
                // goLogin();
                return false;
            }
            const that = this;
            for (var item of that.cata) {
                if (item.active == 1) {
                    item.active = 0
                }
            }
            that.cata[i].active = 1;
            if (i == 1 && that.userTaskInfo.length == 0) {
                that.findUserTaskInfo();
            }
            if (i == 2 && that.userTaskList.length == 0) {
                that.findUserTaskList();
            }
            if (i == 3 && that.ranking.length == 0) {
                that.findUserjfRanking();
            }
        },
        gotoBuyRecord (){
            if(!checkLogin()){
                // goLogin();
                return false;
            }
            window.location.href = serverHttp + "/user/userExchange?from=H5&memToken="+this.memToken;
        },
        gotoCommodityDetail (id){
            if(!checkLogin()){
                // goLogin();
                return false;
            }
            window.location.href = serverHttp + "/user/commodityDetail?from=H5&memToken="+this.memToken+"&id="+id;
        },
        gotoCourseDetail (id){
            if(!checkLogin()){
                // goLogin();
                return false;
            }
            window.location.href = serverHttp + "/course/courseDetails?from=H5&memToken="+this.memToken+"&id="+id;
        },
    }
})
