Vue.config.devtools = true;
var v = new Vue({
    el:".container",
    data:{
        userName:"",
        card:"",
        unitName:"",
        address:"",
        addressXx:"",
        sex:"",
        experience:"-1",
        isLogin: 0,
        memToken: '',
        memHeadImg: '',
        userJf:-1,
        id: '',
        type: '',
        attestation: null,
        experiences: [
            {id:1,name:"1-3年"},
            {id:2,name:"3-5年"},
            {id:3,name:"5-10年"},
            {id:4,name:"10-15年"},
        ],
        integralType:"实名认证成功",
        integralCount:"200",
        isNew: true
    },
    created:function (){
        if(!checkLogin()){
            // goLogin();
            return false;
        }
        if($.cookie("memToken")){
            this.memToken = $.cookie("memToken");
            this.isLogin=1;
        }
        if($.cookie("memHeadImg")){
            this.memHeadImg = $.cookie("memHeadImg");
        }
        var myobj = getparamcommon();
        if(myobj){
            this.id = myobj.id;
            this.type = myobj.type;
        }
        this.findUserInfoWithSm();
        this.findUserIntegral();
        this.getAttestation();
    },
    mounted:function (){
        setTimeout(() => {
            $(".loading-item").fadeOut(200);
        },500)
    },
    methods:{
        getAttestation() {
            var that = this;
            $.ajax({
                type : 'POST',
                url : serverHttp+"/popu/window/checkWindow",
                data : {
                    id: that.id,
                    idType: that.type
                },
                success : function(data) {
                    if (data.code == 200) {
                        that.attestation = data.data;
                    }
                },
                error : function(data) {
                }
            });
        },
        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) {
                }
            });
        },
        findUserInfoWithSm (){
            var that = this;
            $.ajax({
                type : 'POST',
                url : serverHttp+"/user/findUserAttestInfo",
                data : {
                    memToken:that.memToken
                },
                success : function(data) {
                    if (data.code == 200 && data.data && data.data.personalAuth) {
                        that.userName = data.data.personalAuth.memRealName;
                        that.card = data.data.personalAuth.memIdentityId;
                        that.sex = data.data.personalAuth.memSex;
                        if(data.data.personalAuth.unitName){
                            that.unitName = data.data.personalAuth.unitName;
                        }
                        if(data.data.personalAuth.memAddr){
                            that.address = data.data.personalAuth.memAddr;
                        }
                        if(data.data.personalAuth.memExp){
                            that.experience = data.data.personalAuth.memExp;
                        }
                        that.isNew = false;
                    }
                },
                error : function(data) {
                }
            });
        },
        saveUserInfoWithSm(){
            if(!checkLogin()){
                // goLogin();
                return false;
            }
            var that = this;
            if (that.isEmptyStr(that.userName.trim()) || that.isEmptyStr(that.card.trim()) || that.isEmptyStr(that.sex)) {
                alert("请完善必填信息");
                return false;
            }
            if (!that.isIdentityCard(that.card)) {
                alert("身份证不合法请检查");
                return false;
            }
            $.ajax({
                type : 'POST',
                url : serverHttp+"/user/saveUserInfoWithSmAndYs",
                data : {
                    memToken:that.memToken,
                    userName:that.userName.trim(),
                    card:that.card.trim(),
                    unitName:that.unitName.trim(),
                    address:that.address.trim(),
                    sex:that.sex,
                    experience:that.experience,
                    attestType: '2'
                },
                success:function (res) {
                    if (res.code == 200) {
                        if(res.data.points != 0) {
                            that.integralCount = res.data.points;
                            $(".integral-toast").show();
                            playMusic()
                            setTimeout(function () {
                                $(".integral-toast").fadeOut(200);
                            }, 2000);
                            sessionStorage.setItem('refresh', 'true');
                            setTimeout(function (){
                                if(that.attestation.question){
                                    window.location.href = serverHttp + "/live/liveQuestion?from=H5&memToken=&type=" + that.type + "&id=" + that.id;
                                }else{
                                    window.location.href=document.referrer;
                                }
                            },2000);
                        }else{
                            sessionStorage.setItem('refresh', 'true');
                            if(that.attestation.question){
                                window.location.href = serverHttp + "/live/liveQuestion?from=H5&memToken=&type=" + that.type + "&id=" + that.id;
                            }else{
                                window.location.href=document.referrer;
                            }
                        }

                    } else {
                        alert(res.message);
                    }
                }
            })
        },
        isCardID  (code) {
            var city = { 11: "北京", 12: "天津", 13: "河北", 14: "山西", 15: "内蒙古", 21: "辽宁", 22: "吉林", 23: "黑龙江 ", 31: "上海", 32: "江苏", 33: "浙江", 34: "安徽", 35: "福建", 36: "江西", 37: "山东", 41: "河南", 42: "湖北 ", 43: "湖南", 44: "广东", 45: "广西", 46: "海南", 50: "重庆", 51: "四川", 52: "贵州", 53: "云南", 54: "西藏 ", 61: "陕西", 62: "甘肃", 63: "青海", 64: "宁夏", 65: "新疆", 71: "中国台湾", 81: "中国香港", 82: "中国澳门", 91: "国外 " };
            if (!code || !/^\d{6}(18|19|20)?\d{2}(0[1-9]|1[12])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/i.test(code)) {
                return false;
            }
            if (!city[code.substr(0, 2)]) {
                return false;
            }
            if (code.length == 18) {
                sBirthday = code.substr(6, 4) + "-" + Number(code.substr(10, 2)) + "-" + Number(code.substr(12, 2));
                var d = new Date(sBirthday.replace(/-/g, "/"))
                if (sBirthday != (d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate())) {
                    return false;
                }
            }
            //18位身份证需要验证最后一位校验位
            if (code.length == 18) {
                code = code.split('');
                //∑(ai×Wi)(mod 11)
                //加权因子
                var factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
                //校验位
                var parity = [1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2];
                var sum = 0;
                var ai = 0;
                var wi = 0;
                for (var i = 0; i < 17; i++) {
                    ai = code[i];
                    wi = factor[i];
                    sum += ai * wi;
                }
                var last = parity[sum % 11];
                if (parity[sum % 11] != code[17]) {
                    return false;
                }
            }
            return true;
        },
        isIdentityCard(idcode){
            // 函数参数必须是字符串，因为⼆代⾝份证号码是⼗⼋位，⽽在中，⼗⼋位的数值会超出计算范围，造成不精确的结果，导致最后两位和计算的值javascript不⼀致，从⽽该函数出现错误。
            // 详情查看的数值范围javascript
            // 加权因⼦

            var weight_factor =[7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2];
            // 校验码

            var check_code =['1','0','X','9','8','7','6','5','4','3','2'];
            var code = idcode +"";
            // 最后⼀位
            var last = idcode[17];//
            var seventeen = code.substring(0,17);
            // ISO 7064:1983.MOD 11-2
            //  判断最后⼀位校验码是否正确

            var arr = seventeen.split("");
            var len = arr.length;
            var num =0;
            for(var i =0; i < len; i++){
                num = num + arr[i]* weight_factor[i];
            }
            //  获取余数

            var resisue = num %11;
            var last_no = check_code[resisue];
            //  格式的正则

            //   正则思路

            /*
            第⼀位不可能是
            0
            第⼆位到第六位可以是
            0-9
            第七位到第⼗位是年份，所以七⼋位为
            或者
            19
            20
            ⼗⼀位和⼗⼆位是⽉份，这两位是
            之间的数值
            01-12
            ⼗三位和⼗四位是⽇期，是从
            之间的数值
            01-31
            ⼗五，⼗六，⼗七都是数字
            0-9
            ⼗⼋位可能是数字
            ，也可能是
            0-9
            X
                */
            var idcard_patter = /^[1-9][0-9]{5}([1][9][0-9]{2}|[2][0][0|1][0-9])([0][1-9]|[1][0|1|2])([0][1-9]|[1|2][0-9]|[3][0|1])[0-9]{3}([0-9]|[X])$/;
            //   判断格式是否正确

            var format = idcard_patter.test(idcode);
            //   返回验证结果，校验码和格式同时正确才算是合法的⾝份证号码

            return last === last_no && format ?true:false;
        },
        isEmptyStr (str) {
            if ( str == "" ) return true;
            var regu = "^[ ]+$";
            var re = new RegExp(regu);
            return re.test(str);
        },
        gotoUserCenter () {
            window.location.href = serverHttp + "/user/personalCenter?from=H5&memToken="+this.memToken;
        }
    }
})
