Vue.config.devtools = true;
var v = new Vue({
    el: ".index-box",
    data: {
        cata: [
            {name: "积分商城", active: 1, id: 1},
            {name: "积分明细", active: 0, id: 2},
            {name: "积分任务", active: 0, id: 3},
            {name: "积分榜", active: 0, id: 4},
            {name: "兑换记录", active: 0, id: 5},
        ],
        info: {},
        userLevel:'',
        isLogin: 0,
        memToken: '',
        memHeadImg: '',
        userJf: -1,
        showLevelInfo: false,
        pageLoaded: true,
        goodMall: [],//积分商城
        totalPoints: 0,
        userTaskInfo: [],//积分明细
        userTaskList: [],//任务记录
        ranking: [],//排行榜
        goodList: [],// 兑换记录
        start: 1,
        size: 12,
        total: 0,
        open: -1,
    },
    created: function () {
        if (!checkWebLogin()) {
            // $('.navlogorebox').trigger('click')
            return false;
        }
        // if(!checkLogin()){
        //     goLogin();
        //     return false;
        // }
        // if($.cookie("memToken")){
        //     this.memToken = $.cookie("memToken");
        //     this.isLogin=1;
        // }
        // if($.cookie("memHeadImg")){
        //     this.memHeadImg = $.cookie("memHeadImg");
        // }
        var jf = sessionStorage.getItem('jf');
        if (jf) {
            this.cata = [
                {name: "积分商城", active: 0, id: 1},
                {name: "积分明细", active: 0, id: 2},
                {name: "积分任务", active: 0, id: 3},
                {name: "排行榜", active: 1, id: 4},
                {name: "兑换记录", active: 0, id: 5},
            ]
            this.getUserInfo();
            this.findUserjfRanking();
            sessionStorage.removeItem('jf');
        } else {
            this.getUserInfo();
            this.findGoodMallList()
        }

        // this.findUserIntegral();
    },
    methods: {
        handleSizeChange(val) {
            this.page = val
            this.findGoodMallList()
        },
        handleCurrentChange(val) {
            this.start = val
            this.findGoodMallList()
        },
        findGoodMallList() {
            var that = this;
            $.ajax({
                type: 'POST',
                url: "/user/findGoodMallListWeb",
                data: {jfPage: this.start, jfSize: this.size},
                success: function (res) {
                    if (res.code == 200) {
                        that.goodMall = res.data.data;
                        that.goodMall.forEach(function(item, i){
                            if(item.img){
                                if(item.img.indexOf('http') < 0){
                                    item.img = 'http://files.qkyxzx.com/' + item.img;
                                }
                            }
                        })
                        that.total = res.data.total
                        that.totalPoints = res.data.totalPoints;
                    }
                }
            })
        },
        webcoursePrefectureList(item) {
            const type = item.zq;
            if (type == 1) {
                //普通课程
                window.location.href = "/course/courseDetails?from=web&id=" + item.id;
            } else {
                //专区
                window.location.href = "/course/coursePrefectureList?from=web&id=" + item.id;
            }
        },
        getUserInfo() {
            var that = this;
            $.ajax({
                type: 'POST',
                url: "/user/getUserInfoWeb",
                success: (res)=> {
                    if (res.code == 200) {
                        that.info = res.data;
                        this.pageLoaded = false;
                        var integral = res.data.memTotalPoints;
                        if (integral >= 0 && integral <= 3000) {
                            that.userLevel = '/static/web/img/yizhe@2x.png';
                        } else if (integral > 3000 && integral <= 6000) {
                            that.userLevel = '/static/web/img/yiling@2x.png';
                        } else if (integral > 6000 && integral <= 9000) {
                            that.userLevel = '/static/web/img/yizun@2x.png';
                        } else if (integral > 9000 && integral <= 12000) {
                            that.userLevel = '/static/web/img/yisheng@2x.png';
                        } else if (integral > 12000 && integral <= 15000) {
                            that.userLevel = '/static/web/img/yishen@2x.png';
                        } else {
                            that.userLevel = '/static/web/img/yixian@2x.png';
                        }
                    }
                }
            })
        },
        singDay() {
            if (!checkWebLogin()) {
                // $('.navlogorebox').trigger('click')
                return false;
            }
            var that = this;
            $.ajax({
                type: 'POST',
                url: "/user/singDayWeb",
                data: {
                    dayPoints: that.info.dayPoints
                },
                success: function (res) {
                    if (res.code == 200 || res.data == 2) {
                        playMusic("签到成功", that.info.dayPoints)
                        that.getUserInfo();
                    } else {
                        layer.msg("签到失败");
                    }
                }
            })
        },
        showLevel() {
            this.showLevelInfo = !this.showLevelInfo;
        },
        change() {
            var that = this;
            $("#fileInput").click();
        },
        chooseTab(i) {
            if (!checkWebLogin()) {
                // $('.navlogorebox').trigger('click')
                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 == 0 && that.goodMall.length == 0) {
                this.findGoodMallList()
            }
            if (i == 1) {
                that.findUserTaskInfo();
            }
            if (i == 2 && that.userTaskList.length == 0) {
                that.findUserTaskList();
            }
            if (i == 3 && that.ranking.length == 0) {
                that.findUserjfRanking();
            }
            if (i == 4) {
                that.findUserGoodList();
            }
        },
        findUserTaskInfo() {
            var that = this;
            $.ajax({
                type: 'POST',
                url: "/user/findUserTaskInfoWeb",
                success: function (res) {
                    if (res.code == 200) {
                        that.userTaskInfo = res.data;
                    }
                }
            })
        },
        findUserTaskList() {
            var that = this;
            $.ajax({
                type: 'POST',
                url: "/user/findUserTaskListWeb",
                success: function (res) {
                    if (res.code == 200) {
                        that.userTaskList = res.data;
                    }
                }
            })
        },
        findUserjfRanking() {
            var that = this;
            $.ajax({
                type: 'POST',
                url: " /user/findUserjfRankingWeb",
                success: function (res) {
                    if (res.code == 200) {
                        that.ranking = res.data;
                    }
                }
            })
        },
        findUserGoodList() {
            var that = this;
            $.ajax({
                type: 'POST',
                url: "/user/findUserGoodListWeb",
                data: {
                    start: that.start,
                    size: that.size
                },
                success: function (data) {
                    if (data.code == 200) {
                        that.goodList = [];
                        that.goodList = that.goodList.concat(data.data);
                        that.goodList.forEach(function (item, i) {
                            item.show = false;
                        })
                    } else if (data.code == 10005) {
                        // layer.msg(data.message)
                    }
                }
            })
        },
        buyRecord() {
            var that = this;
            console.log(this.memToken);
            window.location.href = serverHttp + "/user/buyRecord?from=H5&memToken=" + that.memToken;
        },
        pointsMall() {
            var that = this;
            window.location.href = serverHttp + "/user/pointsMall?from=H5&memToken=" + that.memToken;
        },

        certificationSm() {
            var that = this;
            window.location.href = serverHttp + "/user/certificationSm?from=H5&memToken=" + that.memToken;
        },
        certificationYs() {
            var that = this;
            window.location.href = serverHttp + "/user/certificationYs?from=H5&memToken=" + that.memToken;
        },
        userAccountNumber() {
            var that = this;
            window.location.href = serverHttp + "/user/userAccountNumber?from=H5&memToken=" + that.memToken;
        },
        editPass() {
            var that = this;
            window.location.href = serverHttp + "/user/editPass?from=H5&memToken=" + that.memToken;
        },
        callWe() {
            var that = this;
            window.location.href = serverHttp + "/user/callWe?from=H5&memToken=" + that.memToken;
        },
        aboutWe() {
            var that = this;
            window.location.href = serverHttp + "/user/aboutWe?from=H5&memToken=" + that.memToken;
        },
        logOut() {
            var that = this;
            window.location.href = serverHttp + "/login/logOut?from=H5&memToken=" + that.memToken;
        },

        gotoUserStudyRecord() {
            var that = this;
            window.location.href = serverHttp + "/user/userStudyRecord?from=H5&memToken=" + that.memToken;
        },
        showDetail(i) {
            this.open = i;
        },
        hideDetail() {
            this.open = -1;
        },
        chooseImg() {
            //上传图片https://files.imevos.com/upload/headimgs/4d1d21aebda441c9924daed3fefee8e3.jpg
            var formData = new FormData();
            formData.append('file', document.getElementById('fileInput').files[0]);
            formData.append('memToken', this.memToken);
            formData.append('type', 1);
            $.ajax({
                url: serverHttp + "/user/uploadHearderImg",
                type: 'post',
                data: formData,
                dataType: "json",
                async: false,
                cache: false,
                contentType: false,
                processData: false,
                success: function (data) {
                    if (data.code == 200) {
                        layer.msg("上传成功");
                    } else if (data.code == 10003) {
                        layer.msg("为查询到用户");
                    } else if (data.code == 10010) {
                        layer.msg("图片上传失败");
                    }
                },
                error: function (returndata) {
                }
            });
        },
        gotoCommodityDetail(id) {
            sessionStorage.setItem('commodityId', id);
            window.location.href = "/user/pointsMall?from=web";
        }
    }
})
