jqMultipleShow("click", ".navList", ".tab", ".con");
var orderPage = new Vue({
    el: "#orderPage",
    data: {
        isRequset:true,
        isShwoAddModel:false,
        isDefaultFlag:false,
        isDefaultChecked:false,
        userId: 0,
        addressList:[],
        ProvinceList:[],
        cityArray:[],
        townArray:[],
        addressFrom: {
            name:'',
            province: '',
            city: '',
            town: ''
        },
        isAddType:'add',
        addressList:[],//收货地址
        addressData:{},//初始化地址信息
        hanldAddressData:{},//选择确定的地址信息
        addAddressData:{//初始化地址信息
            addressID:'',
            allAddress:'',
            userID: '',			//用户id ,只在新增收货地址时传
            shouHuoRen: '',		//收货人
            mobile:'',			//收货人手机
            townID:'',			//区ID
            address: '',	//地址
            defaultFlag:0		//是否默认收货地址(0 不是默认,1 默认)
        },
        addressLength:0
    },
    filters: {

    },
    computed: {

    },
    methods: {
        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);
                }
            })
        },
        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);
                    }
                })
            });
        },
        editAddress :function(item) {//编辑地址
            var _self = this;
            if (item.defaultFlag == '1') {
                _self.isDefaultFlag = true;
            } else {
                _self.isDefaultFlag = false;
            }
            _self.isAddType = 'edit';
            _self.isShwoAddModel = true;
            _self.addAddressData = item;
            _self.addAddressData.allAddress = item.province + '' + item.city + '' + item.town;
            _self.addressFrom.province = item.provinceID;
            _self.addressFrom.city = item.cityID;
            _self.addressFrom.town = item.townID;
            _self.GetProvinceFn();
            _self.ChangeGetcity();
            _self.ChangeGetcTown();
        },
        ConfirmAddressFn :function(){//保存地址
            var _self = this;
            if(_self.addAddressData.townID == ""){
                CAIMEI.dialog('请选择所在地区');
                return;
            };
            if(_self.addAddressData.address == ""){
                CAIMEI.dialog('请填写详细地址');
                return;
            };
            if(_self.addAddressData.shouHuoRen == ""){
                CAIMEI.dialog('请输入收货人姓名');
                return;
            };
            if(_self.addAddressData.mobile == ""){
                CAIMEI.dialog('请输入收货人手机号');
                return;
            };
            if( _self.isAddType == 'add'){
                var params = {
                    userID:_self.userId  ,		//用户id ,只在新增收货地址时传
                    shouHuoRen:_self.addAddressData.shouHuoRen ,//收货人
                    townID:_self.addAddressData.townID,			//区ID
                    address:_self.addAddressData.address,//地址
                    mobile:_self.addAddressData.mobile,			//手机
                    defaultFlag:_self.addAddressData.defaultFlag//是否默认收货地址(0 不是默认,1 默认)
                };
            }else{
                var params = {
                    addressID:_self.addAddressData.addressID,//编辑保存地址ID
                    userID:_self.userId  ,		//用户id ,只在新增收货地址时传
                    shouHuoRen:_self.addAddressData.shouHuoRen ,//收货人
                    townID:_self.addAddressData.townID,			//区ID
                    address:_self.addAddressData.address,//地址
                    mobile:_self.addAddressData.mobile,			//手机
                    defaultFlag:_self.addAddressData.defaultFlag//是否默认收货地址(0 不是默认,1 默认)
                };
            }
            _self.isConDisabled = true;
            OrderApi.AddSaveAddress(params,function(response){
                if(response.code == 0){
                    CAIMEI.dialog('保存成功',true,function () {
                        _self.GetQueryListData();
                        _self.isShwoAddModel = false;
                        _self.isConDisabled = false;
                    });
                }else{
                    CAIMEI.Alert(response.msg,'确定',false);
                }
            });
        },
        GetProvinceFn: function(){//获取全部省份
            var _self = this;
            PublicApi.GetProvince({},function(response){
                if(response.code == 0){
                    _self.ProvinceList =response.data;
                }else{
                    CAIMEI.Alert(response.msg,'确定',false);
                }
            })
        },
        ChangeProvince:function () {//选择省份
            var _self = this;
            _self.addressFrom.province = event.target.value;
            if(_self.addressFrom.province==''){//省为请选择时市区初始化
                _self.cityArray=[];
                _self.townArray=[];
                _self.addAddressData.townID=''
            }else {
                _self.ChangeGetcity();
            }
        },
        ChangeGetcity:function(event){//加载市
            var _self = this;
            PublicApi.GetCity({ provinceId: _self.addressFrom.province },function(response){
                if(response.code == 0 ){
                    _self.cityArray = response.data;
                    _self.addressFrom.city = event.target.value;
                    if(_self.addressFrom.city==''){
                        _self.townArray=[];
                    }else {
                        _self.ChangeGetcTown();
                    }
                }else{
                    CAIMEI.Alert(response.msg,'确定',false);
                }
            })
        },
        ChangeGetcTown:function(event){//选择地区
            var _self = this;
            PublicApi.GetTown({ cityId: _self.addressFrom.city },function(response){
                if(response.code == 0 ){
                    _self.townArray = response.data;
                    _self.addAddressData.townID = event.target.value;
                }else{
                    CAIMEI.Alert(response.msg,'确定',false);
                }
            })
        },
        settingDefaultFlag :function(){//添加&&编辑 保存 设置默认地址按钮
            var _self = this;
            _self.isDefaultFlag=! _self.isDefaultFlag;
            if(_self.isDefaultFlag){
                _self.addAddressData.defaultFlag = '1';
            }else{
                _self.addAddressData.defaultFlag = '0';
            }
        },
        ShowAddAddressFn :function(){//显示新增地址
            var _self = this;
            _self.isAddType = 'add';
            _self.isShwoAddModel = true;
            _self.addressData = {//初始化地址信息
                    addressID:'',
                    allAddress:'',
                    userID: '',			//用户id ,只在新增收货地址时传
                    shouHuoRen: '',		//收货人
                    mobile:'',			//收货人手机
                    townID:'',			//区ID
                    address: '',	    //地址
                    defaultFlag:0		//是否默认收货地址(0 不是默认,1 默认)
            };
            _self.GetProvinceFn();
        },
        CancelAddressFn:function(){
            var _self = this;
            _self.isShwoAddModel = false;
        }
    },
    mounted: function () {
        if(globalUserData){
            this.userId = globalUserData.userId;
            this.GetQueryListData();
        }
        $('.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");

    }
});
