Vue.config.devtools = true;
function getQueryVariable(variable)
{
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i=0;i<vars.length;i++) {
        var pair = vars[i].split("=");
        if(pair[0] == variable){return pair[1];}
    }
    return(false);
}
var v = new Vue({
    el: ".container",
    data: {
        finished:false,
        loading:false,//出现了 上拉到底出发了两次请求接口的情况  加一个变量控制一下
        list: {},
        integralType:"",
        integralCount:"",
        time:'',
        memToken:'',
        //获取链接数据
        id:'',
        m:'',
        isSubmit:'',
        //页面展示的金额
        money:''
    },
    created:function (){
        var that = this;
        if($.cookie("memToken")){
            this.memToken = $.cookie("memToken");
            this.isLogin=1;
        }
        if($.cookie("memHeadImg")){
            this.memHeadImg = $.cookie("memHeadImg");
        }
        window.addEventListener("scroll", this.scrollBottom, true);
        if (!checkLogin()) {
            // goLogin();
            return false;
        }
        this.id = getQueryVariable("id");
        if(getQueryVariable("m")){
            this.m = getQueryVariable("m");
        }
        if(getQueryVariable("money")){
            this.m = getQueryVariable("money");
        }
        if(this.m == 'a'){
            this.money = 1000;
        }else if(this.m == 'b'){
            this.money = 1500;
        }else if(this.m == 'c'){
            this.money = 2000;
        }else if(this.m == 'd'){
            this.money = 2500;
        }else if(this.m == 'e'){
            this.money = 3000;
        }else{
            this.money = this.m
        }


        this.getIndexData();
        this.getDate();
    },
    mounted: function (){
        setTimeout(() => {
            $(".loading-item").fadeOut(200);
        },500)
    },
    methods: {
        //获取数据
        getIndexData() {
            var that = this;
            if (that.loading) {
                return ;
            }
            that.loading = true;
            $.ajax({
                type: 'post',
                url: serverHttp + "/signatrue/checkSign",
                data: {
                    memToken: that.memToken,
                    conferenceId: that.id
                },
                success: function (data) {
                    if(data.code == 200 && data.data){
                        if(data.data.isSubmit == 1){
                            $("input").attr("readonly","readonly");
                        }else{
                            $("input").removeAttr("readonly");
                        }
                        that.list = data.data;
                        that.list.memCard = that.memToken;
                        that.list.conferenceId = that.id;
                        that.list.m = that.m;
                        //日期展示
                        if(that.list.signTime){
                            that.time = that.list.signTime;
                        }else{
                            that.list.signTime = that.time;
                        }
                        that.submitInfo();
                    }
                }
            });
        },
        // 提交
        submitInfo(){
            var that = this;
            $.ajax({
                type: 'post',
                url: serverHttp + "/signatrue/addSign",
                data: that.list,
                success: function (data) {
                    if(data.code == 200){

                    }
                }
            });
        },
        allSubmit(){
            var that = this;
            if (that.isEmptyStr(that.list.partyB) ||
                !that.list.partyB ||
                that.isEmptyStr(that.list.identityId) ||
                !that.list.identityId ||
                that.isEmptyStr(that.list.courseMatter) ||
                !that.list.courseMatter ||
                that.isEmptyStr(that.list.courseTime) ||
                !that.list.courseTime ||
                that.isEmptyStr(that.list.courseSite) ||
                !that.list.courseSite ||
                that.isEmptyStr(that.list.bankProvince) ||
                !that.list.bankProvince ||
                that.isEmptyStr(that.list.bankOfDepositName) ||
                !that.list.bankOfDepositName ||
                that.isEmptyStr(that.list.bankId) ||
                !that.list.bankId ||
                that.isEmptyStr(that.list.phone) ||
                !that.list.phone ||
                that.isEmptyStr(that.list.bankName) ||
                !that.list.bankName ||
                that.isEmptyStr(that.list.signImg) ||
                !that.list.signImg) {
                alert("请完善所有信息");
                return false;
            }
            if (!that.isIdentityCard(that.list.identityId.trim())) {
                alert("身份证不合法请检查");
                return false;
            }
            that.list.isSubmit = 1;
            $.ajax({
                type: 'post',
                url: serverHttp + "/signatrue/addSign",
                data: that.list,
                success: function (data) {
                    if(data.code == 200){
                        alert("提交成功");
                        $("input").attr("readonly","readonly");
                    }else{
                        alert(data.message);
                    }
                }
            });
        },
        goSignName(){
            var that = this;
            if(that.list.isSubmit == 1){
                return false;
            }
            window.location.href = serverHttp + "/signatrue/sign?from=H5&id=" + that.id;
        },
        getDate(){
            var myDate = new Date();
            var month = myDate.getMonth() + 1;
            if(month<10){
                month =  '0'+ month
            }
            var day = myDate.getDate();
            if(day<10){
                day =  '0'+ day
            }
            this.time =  myDate.getFullYear()+'-'+month+'-'+day;
        },
        isEmptyStr (str) {
            if ( str == "" ) return true;
            var regu = "^[ ]+$";
            var re = new RegExp(regu);
            return re.test(str);
        },
        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;
        }
    }
})
