Vue.config.devtools = true;
var v = new Vue({
    el:".container",
    data:{
        userName:"",
        oldUserName:"",
        headerImg:"",
        phone:"",
        oldPhone:"",
        code:"",
        isLogin: 0,
        memToken: '',
        memHeadImg: '',
        userJf:-1,
        integralType: "",
        integralCount: "",
    },
    created:function (){
        var that = this;
        if(!checkLogin()){
            // goLogin();
            return false;
        }
        if($.cookie("memToken")){
            that.memToken = $.cookie("memToken");
            that.isLogin=1;
        }
        that.getUserInfo();
        that.findUserIntegral();
    },
    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) {
                }
            });
        },
        getUserInfo (){
            var that = this;
            $.ajax({
                type : 'POST',
                url : serverHttp+"/user/getUserInfo",
                data : {
                    memToken:that.memToken
                },
                success:function (res) {
                    console.log(res);
                    if (res.code == 200) {
                        that.userName = res.data.memNickname;
                        that.oldUserName = res.data.memNickname;
                        that.headerImg = res.data.memHeadimg;
                        that.phone = res.data.memMobile;
                        that.oldPhone = res.data.memMobile;
                    }
                }
            })
        },
        change () {
            var that = this;
            $("#fileInput").click();
        },
        sendCode(){
            if (this.oldPhone == this.phone) {
                alert('手机号没有更改，不需要验证码验证');
                return;
            }
            if (this.phone == "") {
                alert('请填写手机号');
                return;
            }
            if (!this.isMobile(this.phone)) {
                alert('请填写正确的手机号码');
                return;
            }
            this.sendYzm(this.phone);
        },
        sendYzm(memUserId) {
            var that = this;
            $.ajax({
                type : 'Get',
                url : serverHttp+"/login/sendMobileYzm",
                data:{
                    phone:memUserId
                },
                success : function(res) {
                    if (res.code == 200) {
                        $("#sendcode").hide();
                        $("#editDjstime").show();
                        that.timerMobileValCode(59, "sendcode", "editDjstime", "editWaittime");
                    }
                },
                error : function(data) {
                    alert("验证码发送失败");
                }
            });
        },
        timerMobileValCode(intDiff, sendBefoBtnId, sendafterBtnId,
            waitsecondId) {
            mobileInt = window.setInterval(function () {
                var second = 60;
                if (intDiff > 0) {
                    second = intDiff;
                } else if (intDiff == 0) {
                    $("#" + sendafterBtnId).hide();
                    $("#" + sendBefoBtnId).show();
                    window.clearInterval(mobileInt);
                }
                if (second <= 9)
                    second = '0' + second;
                $('#' + waitsecondId).html(second);
                intDiff--;
            }, 1000);
        },

        gotoUserCenter () {
            window.location.href = serverHttp + "/user/personalCenter?from=H5&memToken="+this.memToken;
        },
        updateUserInfo (){
            if(!checkLogin()){
                // goLogin();
                return false;
            }
            var getName = this.userName;
            if (this.oldUserName == this.userName){
                getName = '';
            }

            if (this.phone == "") {
                alert("请填写手机号码");
                return;
            }
            var getPhone = this.phone;
            if (this.oldPhone == this.phone) {
                getPhone = '';
            }
            if (this.oldPhone != this.phone && this.code == ""){
                alert('请输入验证码');
                return;
            }
            if (!this.isMobile(this.phone)) {
                alert("请填写正确的手机号码");
                return;
            }

            var that = this;
            $.ajax({
                type : 'POST',
                url : serverHttp+"/user/updateUserInfo",
                data : {
                    memToken:that.memToken,
                    userName:getName,
                    phone:getPhone,
                    headerImg:that.headerImg,
                    valCode:that.code,
                },
                success:function (res) {
                    if (res.code == 200) {
                        alert("设置成功");
                        if (that.oldPhone != that.phone) {
                            localStorage.removeItem('isVip')
                            window.location.href = serverHttp + "/login/logOut?from=H5&memToken=" + that.memToken;
                        }else{
                            window.location.href = serverHttp + "/user/personalCenter?from=H5";
                        }
                    }else{
                        alert(res.message);
                    }
                }
            })
        },
        isMobile(mobile){
            var matchrs = mobile.match(/^((13[0-9])|(16[0-9])|(14[5,7])|(15[^4,\D])|(17[0,6,7,8])|(18[0-9]))\d{8}$/);
            return matchrs
        },
        chooseImg (){
            var that = this;
            //上传图片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',2);
            $.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) {
                        that.headerImg = data.data;
                    } else if (data.code == 10003) {
                        alert("为查询到用户");
                    } else if (data.code == 10010) {
                        alert("图片上传失败");
                    }
                },
                error:function (returndata){
                }
            });
        }
    }
})
