var repairForm = new Vue({
    el: '#repairForm',
    data: {
        postData:{
            userId:'',
            provinceName:'',
            cityName:'',
            townName:'',
            userAddress:'',
            userContact:'',
            userMobile:'',
            instrumentName:'',
            problemDescription:'',
            manufacturer:'',
            instrumentImages:''
        },
            townID:'',
            provinceID: '',//省id
            cityID: '',
        rule:{
            name: '^[a-zA-Z\\u4e00-\\u9fa5]{2,}$',
            phone: '^\\d{6,12}$'
        },
        provinceArray:[],
        cityArray: [],//市
        townArray: [],//区
        productImagesList:[],
        formData:new FormData(),
    },
    computed: {

    },
    methods: {
        // 失去焦点校验
        blurHandle: function(event) {
            var el = event.currentTarget;
            verifyHandle(el);
        },
        // 提交数据
        submitData: function(){
            var _self = this;
            var pass = verifyForm();
            if (_self.loginLoading) { return false; }
            this.$nextTick(function() {
                 if (!pass) {return false;}
                  console.log(_self.postData)
                 UserApi.Maintenance(_self.postData,function (res) {
                        if(res.code==0){
                         CAIMEI.dialog('提交成功, 客服会在1-2个工作日内与您联系，请保持电话畅通');
                          window.location.href="/user/repair/list.html";
                        }else {
                         CAIMEI.Alert(res.msg, '确定', false);
                        }
                 })
            })
            console.log(JSON.stringify(this.postData));
        },
         province:function () {
           var _this = this;
           var donClass = '#cProvince';
           var domProvince =  document.querySelector(donClass);
           _this.postData.provinceName = domProvince.options[domProvince.selectedIndex].text;
           _this.provinceID =event.target.value;
            if(_this.provinceID==''){//省为请选择时市区初始化
                _this.cityArray=[];
                _this.townArray=[];
                _this.postData.townName=''
            }else {
              _this.getcity();
            }
        },
        uploadImageFn: function(event){//图片上传
            var _this = this;
            var inputDOM = _this.$refs.product;
            var file = inputDOM.files;
            _this.formData.append('file', file[0]);
            PublicApi.uploadimg(_this.formData,function(response){
               _this.productImagesList.push(response.data);
               _this.postData.instrumentImages =_this.productImagesList.toString()+',';
                event.target.value = '';
            });
        },
         removeImagesFn: function(index){//删除图片
            var _this = this;
            _this.productImagesList.splice(index,1);
            _this.postData.instrumentImages =_this.productImagesList.toString()+',';
         },
         getcity:function(event){//加载市
           var _this = this;
           var donClass = '#cityname';
           var domCityname =  document.querySelector(donClass);
           _this.postData.cityName = domCityname.options[domCityname.selectedIndex].text;
            PublicApi.GetCity({ provinceId: _this.provinceID },function(response){
                 if(response.code == 0 ){
                    _this.cityArray = response.data;
                     _this.cityID = event.target.value;
                     if(_this.cityID==''){
                           _this.townArray=[];
                     }else {
                         _this.getcTown();
                     }
                }
            })
        },
         getcTown:function(event){
            var _this = this;
            var donClass = '#townname';
           var domTownname =  document.querySelector(donClass);
           _this.postData.townName = domTownname.options[domTownname.selectedIndex].text;
            PublicApi.GetTown({cityId:_this.cityID},function(response){
                if(response.code == 0 ){
                    _this.townArray = response.data;
                    _this.townID = event.target.value;
                }else{
                    CAIMEI.Alert(response.msg, '确定', false);
                }
            })
        },
    },
    created: function () {

    },
    mounted: function () {
        var _self = this;
        if(globalUserData){
            _self.userId = globalUserData.userId;
        }
        PublicApi.GetProvince({},function(response){ //省份
            if(response.code == 0){
                _self.provinceArray = response.data
            }else{
                CAIMEI.Alert(response.msg, '确定', false);
            }
        })
    }
});
