;
var orderPage = new Vue({
    el: "#orderPage",
    data: {
        isRequset:true,
        isShwoAddModel:false,
        isDefaultFlag:false,
        userId: 0,
        addressList:[],//收货地址
        provinceOptions:[],
        cityOptions:[],
        townOptions:[],
        addressForm: {
            id: '',
            name: '',
            mobile: '',
            townId: '',
            cityId: '',
            provinceId: '',
            desc: '',
            default: ''
        },
        rule:{
            name: '^[a-zA-Z\\u4e00-\\u9fa5]{2,}$',
            mobile: '^\\d{6,12}$'
        }
    },

    methods: {
        blurHandle: function(event) {
            var el = event.currentTarget;
            verifyHandle(el);
        },
        GetQueryListData:function(){//查询d地址列表
            var _self = this;
            OrderApi.GetAddressList({userID:_self.userId,pageNum:1,pageSize:20},function (response) {
                if(response.code == 0){
                    var data = response.data;
                    if(data.results && data.results.length>0) {
                        _self.addressList = [];
                        _self.addressList = data.results;
                        _self.addressLength =  _self.addressList.length;
                    }else{
                        _self.addressList = [];
                        _self.addressList = data.results;
                    }
                    _self.isRequset = false;
                }else{
                    CAIMEI.Alert(response.msg, '确定', false);
                }
            })
        },
        // 获取全部省份
        getProvinceOptions: function(){
            var _self = this;
            PublicApi.GetProvince({},function(r){
                if(r.code === 0 && r.data){
                    _self.provinceOptions =r.data;
                }else{
                    CAIMEI.Alert(r.msg,'确定',false);
                }
            });
        },
        getCityOptions: function(provinceId,callback){
            var _self = this;
            if(provinceId && provinceId>0){
                _self.cityOptions = [];
                _self.townOptions = [];
                _self.addressForm.cityId = '';
                _self.addressForm.townId = '';
                PublicApi.GetCity({ provinceId: provinceId },function(r){
                    if(r.code === 0 && r.data){
                        _self.cityOptions = r.data;
                        if(callback){callback();}
                    }else{
                        CAIMEI.Alert(r.msg,'确定',false);
                    }
                });
            }
        },
        getTownOptions: function(cityId, callback){
            var _self = this;
            if(cityId && cityId>0){
                _self.townOptions = [];
                _self.addressForm.townId = '';
                PublicApi.GetTown({cityId: cityId},function(r){
                    if(r.code === 0 && r.data){
                        _self.townOptions = r.data;
                        if(callback){callback();}
                    }else{
                        CAIMEI.Alert(r.msg,'确定',false);
                    }
                })
            }
        },
        // 列表直接设为默认地址
        setDefaultAddress: function(addressId){
            var _self = this;
            OrderApi.DefaultAddress({addressId:addressId,userId:_self.userId},function (r) {
                if(r.code === 0 ){
                    // 重新获取地址列表
                    _self.GetQueryListData();
                }else{
                    CAIMEI.Alert(r.msg,'确定',false);
                }
            });
        },
        deleteAddress :function(addressID){//列表删除单个地址
            var _self = this;
            CAIMEI.Modal('确定要删除该地址？','取消','确定',function () {
                OrderApi.DeleteAddress({addressID:addressID,userID:_self.userId},function (response) {
                    if(response.code == 0 ){ //删除成功
                        CAIMEI.dialog('删除成功',true,function () {
                            _self.GetQueryListData();
                        });
                    }else{
                        CAIMEI.Alert(response.msg,'确定',false);
                    }
                })
            });
        },
        createAddress: function(){
            this.isShwoAddModel = true;
            this.addressForm.id = 0;
            this.addressForm.name = '';
            this.addressForm.mobile = '';
            this.addressForm.default = false;
            this.addressForm.desc = '';
            this.addressForm.provinceId = '';
            this.addressForm.cityId = '';
            this.addressForm.townId = '';
            if(!isPC){fixedBody();}
        },
        editAddress :function(item) {//编辑地址
            this.isShwoAddModel = true;
            this.addressForm.id = item.addressID;
            this.addressForm.name = item.shouHuoRen;
            this.addressForm.mobile = item.mobile;
            this.addressForm.default = (item.defaultFlag*1===1);
            this.addressForm.desc = item.address;
            this.addressForm.provinceId = item.provinceID;
            var _self = this;
            this.getCityOptions(item.provinceID, function(){
                _self.addressForm.cityId = item.cityID;
                _self.getTownOptions(item.cityID,function(){
                    _self.addressForm.townId = item.townID;
                });
            });
            if(!isPC){fixedBody();}
        },
        hideAddressForm: function(){
            this.isShwoAddModel = false;
            if(!isPC){looseBody();}
        },
        saveAddressForm: function(){
            var _self = this;
            var pass = verifyForm();
            if (_self.isConDisabled) { return false; }
            this.$nextTick(function() {
                if (!pass) {return false;}
                if(!_self.addressForm.townId){
                    CAIMEI.dialog('请选择所在地区');
                    return false;
                };
                _self.isConDisabled = true;
                var params = {
                    userID:_self.userId, //用户id ,只在新增收货地址时传
                    shouHuoRen:_self.addressForm.name, //收货人
                    mobile:_self.addressForm.mobile,   //手机
                    townID:_self.addressForm.townId,   //区ID
                    address:_self.addressForm.desc, //地址
                    defaultFlag:_self.addressForm.default?1:0 //是否默认收货地址(0 不是默认,1 默认)
                };
                if(_self.addressForm.id){
                    // 编辑
                    params.addressID = _self.addressForm.id;//编辑保存地址ID
                }
                _self.saveAddress(params);
            });
        },
        saveAddress: function(params){
            var _self = this;
            OrderApi.AddSaveAddress(params,function(r){
                if(r.code === 0){
                    CAIMEI.dialog('保存成功');
                    _self.isShwoAddModel = false;
                    _self.isConDisabled = false;
                    // 重新获取地址列表
                    _self.GetQueryListData();
                }else{
                    CAIMEI.Alert(r.msg,'确定',false);
                }
            });
        }
    },
    created: function() {
        this.getProvinceOptions();
        if(globalUserData){
            this.userId = globalUserData.userId;
            this.GetQueryListData();
        }
    },
    mounted: function () {
        $('.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(2).addClass("on");

    }
});
