;
var addFormPage = new Vue({
    el: '#addFormPage',
    data: {
        loginLoading:false,
        userId:0,
        form: {
            userId:'',
            userContact: '',//联系人
            userMobile: '',//联系方式
            provinceName: '',//省
            cityName: '',//市
            townName: '',//区
            userAddress: '',//详细地址
            instrumentName: '',//仪器名称
            manufacturer: '',//生产厂家
            instrumentImages: '',//图片
            problemDescription: ''//问题描述
        },
        imagesList:[],
        rule:{
            name: '^[a-zA-Z\\u4e00-\\u9fa5]{2,}$',
            phone: '^\\d{6,12}$'
        },
        formData:new FormData(),
        ProvinceList:[],
        cityArray:[],
        townArray:[],
        addressfrom: {
            province: '',//省id
            city: '',
            town: ''
        },
    },
    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;}
                _self.loginLoading = true;
                RepairApi.AddApplyMaintenance(_self.form,function (response) {
                    if(response.code === 0){
                        CAIMEI.dialog('维修申请提交成功！',true,function () {
                            _self.loginLoading = false;
                            window.location.href="/user/repair/list.html";
                        });
                    } else {
                        CAIMEI.Alert(response.msg,'确定',false);
                        _self.loginLoading = false;
                    }
                });
            });
        },
        ChangeProvince:function (event) {//选择省份
            var _self = this;
            var province_ =  document.getElementById('province');
            _self.addressfrom.province = event.target.value;
            _self.form.provinceName = province_.options[province_.selectedIndex].text;
            if(_self.addressfrom.province==''){//省为请选择时市区初始化
                _self.cityArray=[];
                _self.townArray=[];
            }else {
                _self.GetCtiyFn();
            }
        },
        ChangeGetcity:function(event){//加载市
            var _self = this;
            var ctiy_ =  document.getElementById('cCity');
            _self.addressfrom.city = event.target.value;
            _self.form.cityName = ctiy_.options[ctiy_.selectedIndex].text;
            if(_self.addressfrom.city==''){
                _self.townArray=[];
            }else {
                _self.GetTownFn();
            }
        },
        ChangeGetcTown:function(event){//选择地区
            var _self = this;
            var town_ =  document.getElementById('cTown');
            _self.form.townName = town_.options[town_.selectedIndex].text;
        },
        GetProvinceFn: function(){//获取全部省份
            var _self = this;
            PublicApi.GetProvince({},function(response){
                if(response.code == 0){
                    _self.ProvinceList =response.data;
                }else{
                    CAIMEI.Alert(response.msg,'确定',false);
                }
            })
        },
        GetCtiyFn: function(){//获取市
            var _self = this;
            PublicApi.GetCity({ provinceId: _self.addressfrom.province },function(response){
                if(response.code == 0 ){
                    _self.cityArray = response.data;
                }else{
                    CAIMEI.Alert(response.msg,'确定',false);
                }
            })
        },
        GetTownFn: function(){//获取区
            var _self = this;
            PublicApi.GetTown({ cityId: _self.addressfrom.city },function(response){
                if(response.code == 0 ){
                    _self.townArray = response.data;
                }else{
                    CAIMEI.Alert(response.msg,'确定',false);
                }
            })
        },
        uploadGoodsImagesFn: function(event){//上传图片
            var _self = this;
            var inputDOM = _self.$refs.goodsImages;
            var file = inputDOM.files;
            _self.formData.append('file', file[0]);
            PublicApi.uploadimg(_self.formData,function(response){
                _self.imagesList.push(response.data);
                _self.form.instrumentImages =  _self.imagesList.toString()+',';
                event.target.value = '';
                console.log(_self.imagesList)
            });
        },
        showViewerImageFn: function(index){//预览上传图片
            var DomEven = '#uploadGoodsImages'+index;
            var ViewerDom = document.querySelector(DomEven);
            this.viewer = new Viewer(ViewerDom, {url:'data-image'});
        },
        removeGoodsImagesFn: function(index){//删除商品图片
            var _self = this;
            _self.imagesList.splice(index,1);
            _self.form.instrumentImages =  _self.imagesList.toString()+',';
        }
    },
    created: function () {

    },
    mounted: function () {
        var _self = this;
        if(globalUserData){
            _self.userId = globalUserData.userId;
            _self.form.userId = _self.userId;
            _self.GetProvinceFn();
        }
        $('.navLayout').find('.navList').removeClass("on").find('.con').hide().find('a').removeClass("on");
        $('.navLayout').find('.navList').eq(0).addClass("on").find('.con').show().find('a').eq(1).addClass("on");

    }
});
