var registerPage = new Vue({
    el: "#registerPage",
    data: {
        loginLoading:false,
        showTabContentIndex:2,
        showSupplierConfirm:true,
        showClass:'',
        mobilCount:0,
        mobileCodeText:'获取验证码',
        mobilTime:null,
        isMobileDisabled:false,
        isCodeModel:false,
        codeImagePath:'',
        codeImageToken:'',
        imageCode:'',
        supplierUser: {
            bindMobile:'',
            mobileCode:'',
            password:'',
            passWordConfirm:'',
            linkMan:'',
            email:'',
            name:'',
            sname:'',
            socialCreditCode:'',
            businessLicenseImage:'',
            provinceID :'',
            cityID : '',
            townID : '',
            address:'',
            firstShopType:'2',
            secondShopType: '',
            mainpro:'',
            isAgreed:0,
        },
        isAgree:false,
        isShowAgree:false,
        mentuzCampNullList:[
            {value:'1',name:'产品'},
            {value:'2',name:'仪器'},
            {value:'3',name:'服务'}
        ],
        userMainPros: [],
        formData:new FormData(),
        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: {
        supplierRegister: 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.supplierUser.passWordConfirm!=_self.supplierUser.password){
                    CAIMEI.dialog('两次密码输入不一致',false,function () {});
                    return;
                }
                _self.loginLoading = true;
                UserApi.SupplierRgister(_self.supplierUser,function (response) {
                    if(response.code === 0){
                        CAIMEI.Alert('您的资料已提交成功，系统将在1-2个工作日内进行审核~','知道了',true,function () {
                            window.location.href = '/index.html';
                            _self.loginLoading = false;
                        });
                    } 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.supplierUser.bindMobile,
                    imgCode:_self.imageCode,
                    token:_self.codeImageToken,
                    activateCodeType:3,
                    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;
                }
            })
        },
        refreshCodeImage:function(){
            this.getMobileImage()
        },
        closeCodeModel:function(){
            this.isCodeModel = false;
        },
        GetProvinceFn: function(){//获取全部省份
            var _this = this;
            PublicApi.GetProvince({},function(response){
                if(response.code == 0){
                    _this.ProvinceList =response.data;
                }else{
                    CAIMEI.Alert(response.msg,'确定',false);
                }
            })
        },
        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.supplierUser.businessLicenseImage = response.data;
                event.target.value = '';
            });
        },
        deleteBusinessImage:function(){//清楚营业执照图片
            this.supplierUser.businessLicenseImage = '';
        },
        ChangeProvince:function () {//选择省份
            var _this = this;
            _this.addressfrom.province = event.target.value;
            _this.supplierUser.provinceID = event.target.value;
            if(_this.addressfrom.province==''){//省为请选择时市区初始化
                _this.cityArray=[];
                _this.townArray=[];
                _this.supplierUser.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.supplierUser.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.supplierUser.townID = event.target.value;
                    console.log(_this.supplierUser.townID)
                }else{
                    CAIMEI.Alert(response.msg,'确定',false);
                }
            })
        },
        mainProAdd: function(){//添加主营内容
            var _self = this;
            if (_self.mainProEdit.length>0) {
                var obj = {name:_self.mainProEdit};
                if(_self.checkedArray(_self.mainProEdit,_self.mentuzCampNullList)){
                    CAIMEI.dialog('主营内容已存在!',false);
                }else {
                    _self.mentuzCampNullList.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);
        },
        showSupplier:function () {//同意
            var _self = this;
            _self.showSupplierConfirm = false;
        },
        checkedIsAgreeFn:function () {//勾选协议
            var _self = this;
            _self.isAgree = !_self.isAgree;
            if (_self.isAgree){
                _self.isShowAgree = false;
                _self.supplierUser.isAgreed = 1;
            }else{
                _self.isShowAgree = true;
                _self.supplierUser.isAgreed = 0;
            }
        }
    },
    created: function () {

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