var registerPage = new Vue({
    el: "#registerPage",
    data: {
        loginLoading: false,
        showTabContentIndex:1,
        showSupplierConfirm:true,
        advisoryStatus: false,
        showClass:'',
        mobilCount:0,
        mobileCodeText:'获取验证码',
        mobilTime:null,
        isMobileDisabled:false,
        isCodeModel:false,
        codeImagePath:'https://dss2.bdstatic.com/8_V1bjqh_Q23odCf/pacific/1819816496.jpg',
        codeImageToken:'',
        imageCode:'',
        advisory:{
            name: '',
            phone: ''
        },
        clubUser: {
            name:'',
            phone:'',
            code:'',
            password:'',
            password2:''
        },
        clubUpgradeUser: {
            userID:'',
            clubID:'',
            contractEmail1:'',
            name:'',
            sname:'',
            socialCreditCode:'',
            mainPros:[],
            businessLicenseImage:'',
            headpic:'',
            medicalPracticeLicenseImg:'',
            firstClubType:'',
            secondClubType:0,
            department:'',
            provinceID :'',
            cityID : '',
            townID : '',
        },
        userMainPros:[],
        formData:new FormData(),
        isAgreed:0,
        isAgree:false,
        isShowAgree:false,
        mentuzCampNullList:[
            {value:'1',name:'整形'},
            {value:'2',name:'轻医美'},
            {value:'3',name:'皮肤科'},
        ],
        medicaCampNullList: [
            {name:'美容'},
            {name:'美体'},
            {name:'美发'},
            {name:'皮肤管理'},
            {name:'光电'},
            {name:'综合类'},
            {name:'中医养生'},
            {name:'spa'},
        ],
        mainPros: ['产品','仪器','服务','整形'],
        mainProEdit: '',
        ProvinceList:[],
        cityArray:[],
        townArray:[],
        addressfrom: {
            province: '',//省id
            city: '',
            town: ''
        },
        rule:{
            name: '^[a-zA-Z\\u4e00-\\u9fa5]{2,}$',
            phone: '^\\d{6,12}$',
            code: '^\\d{6}$',
            license: '^[a-zA-Z0-9_]{18}$',
            email: '^(([^<>()\\[\\]\\\\.,;:\\s@&quot;]+(\\.[^<>()\\[\\]\\\\.,;:\\s@&quot;]+)*)|(&quot;.+&quot;))@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}])|(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,}))$',
            password: "^[^\\u4e00-\\u9fa5]{8,16}$"
        }
    },
    computed: {

    },
    methods: {
        clubRegister: function(){ //机构注册提交
            var _self = this;
            var pass = verifyForm();
            if (_self.loginLoading) { return false; }
            this.$nextTick(function() {
                if (!pass) {return false;}
                if (!_self.isAgree){
                    _self.isShowAgree = true;
                    return;
                }
                if(_self.clubUser.password2!=_self.clubUser.password){
                    CAIMEI.dialog('两次密码输入不一致',false,function () {});
                    return;
                }
                var params = {
                        userName : _self.clubUser.name,
                        bindMobile : _self.clubUser.phone,
                        password : _self.clubUser.password,
                        passWordConfirm : _self.clubUser.password2,
                        activationCode : _self.clubUser.code,
                        isAgreed :  _self.isAgreed,
                };
                _self.loginLoading = true;
                UserApi.ClubRegister(params,function (response) {
                    if(response.code === 0){
                        CAIMEI.dialog('注册成功！',true,function () {
                            _self.showTabContentIndex = 2;
                            _self.loginLoading = false;
                            _self.clubLogin();
                        });
                    } else {
                        CAIMEI.Alert(response.msg,'确定',false);
                        _self.loginLoading = false;
                    }
                });
            });
        },
        getMobileImage:function() {//获取图形验证码
            var _self = this;
            var pass = verifyCheack('.massageBtn');
            if (_self.isMobileDisabled) { return false; }
            this.$nextTick(function() {
                if (!pass) {
                    return false;
                }
                PublicApi.getImgVerifyCode({platformType: 0},function(response){
                    if(response.code == 0){
                        _self.imageCode = '';
                        _self.codeImagePath = response.data.baseImage;
                        _self.codeImageToken = response.data.token;
                        _self.isCodeModel = true;
                    }else{
                        CAIMEI.Alert(response.msg,'确定',false);
                    }
                })
            });
        },
        getMobileCodeFn:function(){//机构注册获取注册短信验证码
            var _self = this;
            var params = {
                    mobile:_self.clubUser.phone,
                    imgCode:_self.imageCode,
                    token:_self.codeImageToken,
                    activateCodeType:2,
                    platformType:0
                };
            _self.isMobileDisabled = true;
            UserApi.getRegisterMobileCode(params,function (response) {
                if(response.code == 0){
                    _self.isCodeModel = false;
                    CAIMEI.dialog('验证短信已发送',false,function () {});
                    var TIME_COUNT = 60;
                    if (!_self.mobilTime) {
                        _self.mobilCount = TIME_COUNT;
                        _self.isMobileDisabled = true;
                        _self.mobilTime = setInterval(function(){
                        if (_self.mobilCount > 1 && _self.mobilCount <= TIME_COUNT) {
                            _self.mobilCount--;
                            _self.mobileCodeText = _self.mobilCount +'s重新发送';
                        } else {
                            _self.isMobileDisabled = false;
                            clearInterval(_self.mobilTime);
                            _self.mobilTime = null;
                            _self.mobileCodeText = '获取验证码';
                        }
                    },1000)
                    }
                }else{
                    CAIMEI.Alert(response.msg,'确定',false);
                    _self.isMobileDisabled = false;
                    _self.isCodeModel = false;
                }
            })
        },
        clubUpgrade:function() {//升级会员机构
            var _self = this;
            var pass = verifyForm();
            if (_self.loginLoading) { return false; }
            this.$nextTick(function() {
                if (!pass) {return false;}
                _self.clubUpgradeUser.mainPros = _self.userMainPros.join("/");
                _self.loginLoading = true;
                UserApi.ClubUpgrade(_self.clubUpgradeUser,function (response) {
                    if(response.code === 0){
                        CAIMEI.Alert('您的升级申请已提交审核','确定',true,function () {
                            _self.clubLoginIndex();
                            _self.loginLoading = false;
                        });
                    } else {
                        CAIMEI.Alert(response.msg,'确定',false);
                        _self.loginLoading = false;
                    }
                });
            });
        },
        clubLogin: function () { // 登录
            var _self = this;
            var params =  {mobileOrEmail: _self.clubUser.phone,password: _self.clubUser.password,source: 'www'};
            UserApi.PostLoginAccount(params,function(response){
                if(response.code === 0){
                    _self.clubUpgradeUser.userID =  response.data.userID;
                    _self.clubUpgradeUser.clubID =  response.data.clubID;
                    _self.userData = {
                        account: response.data.account,
                        email: response.data.email,
                        phone: response.data.bindMobile,
                        name: response.data.name,
                        userName: response.data.userName,
                        userId: response.data.userID,
                        spId: response.data.serviceProviderID,
                        clubId: response.data.clubID,
                        shopId: response.data.shopID,
                        identity: response.data.userIdentity,
                        permission: response.data.userPermission,
                        token: response.data.token
                    };
                    _self.setStorages( _self.userData);
                } else {// 登录失败
                    alertInfo(response.msg);
                }
            });
        },
        refreshCodeImage:function(){
            this.getMobileImage()
        },
        closeCodeModel:function(){
            this.isCodeModel = false;
        },
        clubLoginIndex:function(){// 跳过升级直接去个人中心
            window.location.href = '/user/dashboard.html';
        },
        mainProAdd: function(){
            var _self = this;
            if (_self.mainProEdit.length>0) {
                if (_self.mainPros.indexOf(_self.mainProEdit)<0){
                    var obj = {name:_self.mainProEdit};
                    if(_self.clubUpgradeUser.firstClubType === '0'){
                        if(_self.checkedArray(_self.mainProEdit,_self.mentuzCampNullList)){
                            CAIMEI.dialog('主营内容已存在!',false);
                        }else {
                            _self.mentuzCampNullList.push(obj);
                        }
                    }else{
                        if(_self.checkedArray(_self.mainProEdit,_self.medicaCampNullList)){
                            CAIMEI.dialog('主营内容已存在!',false);
                        }else {
                            _self.medicaCampNullList.push(obj);
                        }
                    }
                }
            }
        },
        checkedArray:function(content,arr){//判断主营内容是否已存在
            for(var i in arr){
                if(arr[i].name == content){
                    return true;
                }
            }
            return false;
        },
        showPassword: function(event) {//密码
            var el = event.currentTarget;
            var pwdEle = $(el).siblings('input');
            if(pwdEle.attr('type') === 'password') {
                $(el).addClass('on');
                pwdEle.attr('type','text');
            } else {
                $(el).removeClass('on');
                pwdEle.attr('type','password');
            }
        },
        blurHandle: function(event) { // 失去焦点校验
            var el = event.currentTarget;
            verifyHandle(el);
        },
        postAdvisory: function(){//机构入驻咨询提交
            var _self = this;
            $('.advisoryInfo').find("[needverify]:visible").each(function (index, ele) {
                if (!$(ele).siblings('.checked').hasClass("show")) {
                    verifyHandle(ele);
                }
            });
            var pass = ($('.errTips.show:visible').length === 0);
            if (_self.advisoryStatus) { return false; }
            this.$nextTick(function(){
                if (!pass) { return false; }
                _self.advisoryStatus = true;
                UserApi.quickConsultation({name: _self.advisory.name,mobile: _self.advisory.phone},function (response) {
                    _self.advisoryStatus = false;
                    if(response.code === 0){
                        alertInfo("您的信息已提交成功,采美工作人员会第一时间联系您");
                    } else {
                        alertInfo(response.msg);
                    }
                });
            });
        },
        uploadBusinessLicenseImageFn: function(event){//上传营业执照
            var _this = this;
            var inputDOM = _this.$refs.businessLicenseImage;
            var file = inputDOM.files;
            _this.formData.append('file', file[0]);
            PublicApi.uploadimg(_this.formData,function(response){
                _this.clubUpgradeUser.businessLicenseImage = response.data;
                event.target.value = '';
            });
        },
        uploadMentuzImageFn: function(event){//上传营业执照
            var _this = this;
            var inputDOM = _this.$refs.businessMentuImage;
            var file = inputDOM.files;
            _this.formData.append('file', file[0]);
            PublicApi.uploadimg(_this.formData,function(response){
                _this.clubUpgradeUser.headpic = response.data;
                event.target.value = '';
            });
        },
        uploadMedicaImageFn: function(event){//上传资质证书
            var _this = this;
            var inputDOM = _this.$refs.businessMedicaImage;
            var file = inputDOM.files;
            _this.formData.append('file', file[0]);
            PublicApi.uploadimg(_this.formData,function(response){
                _this.clubUpgradeUser.medicalPracticeLicenseImg = response.data;
                event.target.value = '';
            });
        },
        deleteBusinessImage:function(){//清楚营业执照图片
            this.clubUpgradeUser.businessLicenseImage = '';
        },
        deleteMentuzImage:function(){//清楚门头照图片
            this.clubUpgradeUser.headpic = '';
        },
        deleteMedicalImage:function(){//清楚资质照图片
            this.clubUpgradeUser.medicalPracticeLicenseImg = '';
        },
        setStorages:function(data){//存储本地数据
            localStorage.setItem('userInfo',JSON.stringify(data));
        },
        showVadvisory:function(){
            var _self = this;
            _self.showClass = 'active';
        },
        closeVadvisory:function () {
            var _self = this;
            _self.showClass = '';
        },
        checkedIsAgreeFn:function () {
            var _self = this;
            _self.isAgree = !_self.isAgree;
            if (_self.isAgree){
                _self.isShowAgree = false;
                _self.isAgreed = 1;
            }else{
                _self.isShowAgree = true;
                _self.isAgreed = 0;
            }
        },
        GetProvinceFn: function(){//获取全部省份
            var _this = this;
            PublicApi.GetProvince({},function(response){
                if(response.code == 0){
                    _this.ProvinceList =response.data;
                }else{
                    CAIMEI.Alert(response.msg,'确定',false);
                }
            })
        },
        ChangeProvince:function () {//选择省份
            var _this = this;
            _this.addressfrom.province = event.target.value;
            _this.clubUpgradeUser.provinceID = event.target.value;
            if(_this.addressfrom.province==''){//省为请选择时市区初始化
                _this.cityArray=[];
                _this.townArray=[];
                _this.clubUpgradeUser.townID=''
            }else {
                _this.ChangeGetcity();
            }
        },
        ChangeGetcity:function(event){//加载市
            var _this = this;
            PublicApi.GetCity({ provinceId: _this.addressfrom.province },function(response){
                if(response.code == 0 ){
                    _this.cityArray = response.data;
                    _this.addressfrom.city = event.target.value;
                    _this.clubUpgradeUser.cityID = event.target.value;
                    if(_this.addressfrom.city==''){
                        _this.townArray=[];
                    }else {
                        _this.ChangeGetcTown();
                    }
                }else{
                    CAIMEI.Alert(response.msg,'确定',false);
                }
            })
        },
        ChangeGetcTown:function(event){//选择地区
            var _this = this;
            PublicApi.GetTown({ cityId: _this.addressfrom.city },function(response){
                if(response.code == 0 ){
                    _this.townArray = response.data;
                    _this.clubUpgradeUser.townID = event.target.value;
                    console.log(_this.clubUpgradeUser.townID)
                }else{
                    CAIMEI.Alert(response.msg,'确定',false);
                }
            })
        },
        showViewerImageFn:function(){//预览营业执照图片
            var viewer = new Viewer(document.getElementById('businessLicenseImage'));
        },
        showViewerImageFn1:function(){//预览门头照图片
            var viewer = new Viewer(document.getElementById('uploadMentuzImage'));
        },
        showViewerImageFn2:function(){//预览资质图片
            var viewer = new Viewer(document.getElementById('medicalPracticeLicenseImg'));
        },
    },
    created: function () {

    },
    mounted: function () {
        var _self = this;
        _self.GetProvinceFn();
    }
});
