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:{
        userName:"",
        unitName:"",
        department:"",
        sex:"",
        professional:'',
        professionalTwo:'',
        businessLicense:"",
        isLogin: 0,
        memToken: '',
        memHeadImg: '',
        source: '',
        userJf:-1,
        selectOneData: [],
        selectTwoData: [],
        zw:[],
        id: '',
        type: '',
        attestation: null,
        post:-1,
        integralType:"医师认证成功",
        integralCount:"500",
        isNew: true,
        province: '',
        city: '',
        cityList: [],
        provinceList: []
    },
    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;
        }
        if(getQueryVariable("source") == 1){
            this.source = getQueryVariable("source");
        }else{
            this.source = '0';
        }
        this.getCataList(51,1);
        this.getCataListZw(150,1);
        this.findUserInfoWithYs();
        this.findUserIntegral();
        this.getAttestation();
        this.getProvince();
    },
    mounted:function (){
        setTimeout(() => {
            $(".loading-item").fadeOut(200);
        },500)
    },
    methods:{
        getProvince() {
            var that = this;
            $.ajax({
                type : 'get',
                url : serverHttp+"/sys/getRegion",
                data:{
                    parentId:'10000000',
                },
                success : function(res) {
                    var provinceFirst = [{
                        code: '',
                        id: '',
                        levelId: "1",
                        name: "请选择",
                        parentId: ''
                    }]
                    that.provinceList = provinceFirst.concat(res.data);
                }
            });
        },
        getCity(val) {
            var that = this;
            that.city = ''
            that.cityList = []
            $.ajax({
                type : 'get',
                url : serverHttp+"/sys/getRegion",
                data:{
                    parentId:val,
                },
                success : function(res) {
                    var cityFirst = [{
                        code: '',
                        id: '',
                        levelId: "1",
                        name: "请选择",
                        parentId: ''
                    }]
                    that.cityList = cityFirst.concat(res.data);
                }
            });
        },
        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) {
                }
            });
        },
        professionalChange (type) {
            var that = this;
            if(type == 1){
                if(that.professional == -1){
                    that.selectTwoData=[];
                }else{
                    that.getCataList(that.professional,2);
                }
            }
        },
        getCataList (fId,type) {
            var that = this;
            $.ajax({
                type : 'POST',
                url : serverHttp+"/course/getCataList",
                data:{
                    fId:fId,
                    type:5,
                },
                success : function(res) {
                    if (res.code == 200) {
                        if(type == 1){
                            res.data[0].name = "请选择";
                            that.selectOneData = res.data;
                        }else{
                            that.selectTwoData=[];
                            that.selectTwoData[0] = {id: '', name: "请选择"};
                            that.selectTwoData = that.selectTwoData.concat(res.data);
                        }
                    }
                },
                error : function(data) {

                }
            });
        },
        getCataListZw (fId,type) {
            var that = this;
            $.ajax({
                type : 'POST',
                url : serverHttp+"/course/getCataList",
                data:{
                    fId:fId,
                    type:5,
                },
                success : function(res) {
                    res.data[0].name = "请选择";
                    if (res.code == 200) {
                        that.zw = res.data;
                    }
                },
                error : function(data) {

                }
            });
        },
        findUserInfoWithYs (){
            var that = this;
            $.ajax({
                type : 'POST',
                url : serverHttp+"/user/findUserAttestInfo",
                data : {
                    memToken:that.memToken
                },
                success : function(data) {
                    if (data.code == 200 && data.data) {
                        if(data.data.physicians){
                            that.professional = data.data.physicians.professional
                            that.businessLicense = data.data.physicians.num
                            that.professionalTwo = data.data.physicians.professionalTwo
                            that.post = data.data.physicians.post
                            that.isNew=false;
                        }
                        if(data.data.personalAuth){
                            that.sex = data.data.personalAuth.memSex
                            if(data.data.personalAuth.memRealName){
                                that.userName = data.data.personalAuth.memRealName;
                            }
                            if(data.data.personalAuth.unitName){
                                that.unitName = data.data.personalAuth.unitName;
                            }
                            if(data.data.personalAuth.department){
                                that.department = data.data.personalAuth.department;
                            }
                            if(data.data.personalAuth.province){
                                that.province = data.data.personalAuth.province;
                                that.getCity(data.data.personalAuth.province);
                                that.city = data.data.personalAuth.city;
                            }
                            that.isNew=false;
                        }
                    }
                },
                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) {
                }
            });
        },
        saveUserInfoWithYs(){
            if(!checkLogin()){
                // goLogin();
                return false;
            }
            var that = this;
            if (that.isEmptyStr(that.userName.trim()) ||
                that.isEmptyStr(that.sex) ||
                that.isEmptyStr(that.unitName.trim()) ||
                that.isEmptyStr(that.department.trim()) ||
                (that.source != 1 && that.isEmptyStr(that.businessLicense))) {
                alert("请完善必填信息");
                return false;
            }
            if(!that.isEmptyStr(that.businessLicense) && !checkyishi(that.businessLicense)){
                alert("执业编码填写错误");
                return false;
            }
            if(that.province.name == '请选择'){
                that.province = ''
            }
            if(that.city == '请选择'){
                that.city = ''
            }
            $.ajax({
                type : 'POST',
                url : serverHttp+"/user/saveUserInfoWithSmAndYs",
                data : {
                    memToken:that.memToken,
                    professional:that.professional,
                    businessLicense:that.businessLicense,
                    userName:that.userName.trim(),
                    unitName:that.unitName.trim(),
                    department:that.department.trim(),
                    sex:that.sex,
                    province: that.province,
                    city: that.city,
                    attestType: '3',
                    source: that.source
                },
                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);
                    }
                }
            })
        },
        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;
        }
    }
})
