<template>
	<view class="AddaddressStyle" :style="{'height':contentHeight}">
		<!-- <view class="top">
			 <input class="uni-input" placeholder="自动获得焦点" style="width: 70%;"/>
			 <view style="display: flex;align-items: center;justify-content: center;border: 1px solid #F4AAA6;width:160rpx ;height: 60rpx;">
				 <Text style='color: #EA5A22;'>自动识别</Text>
			 </view>
		</view> -->
		<view class="addcenter">
			<view class="addcenter_item2">
				<Text>收货人</Text>
				<input class="uni-input inputM" v-model="msg.Name" />
			</view>
			<view style="width: 100%;height: 1px;background: #f5f5f5;"/>
		
			<view class="addcenter_item2">
				<Text>联系电话</Text>
				<input class="uni-input inputM" v-model="msg.Mobile" />
			</view>
			<view style="width: 100%;height: 1px;background: #f5f5f5;"/>
			<!-- TODO 动态获取地址数据 -->
			<view class="addcenter_item">
				<Text>所在地区</Text>
				<view style="display: flex;flex-direction: row;align-items: center;" @click="addressShow = true">
					<Text :style="{'color':address!=''? '#303133':'#9D9C9B'}">{{address!=''? address :'请选择'}}</Text>
					<u-icon name="arrow" color="#B2B2B2" size="30" ></u-icon>
					<pickerAddress v-model="addressShow" @confirm="addresspick" />
				</view>
			</view>
			<view style="width: 100%;height: 1px;background: #f5f5f5;"/>
			 <!-- TODO 定位地址 -->
			<!-- <view class="addcenter_item">
				<Text>定位地址</Text>
				<view style="display: flex;flex-direction: row;align-items: center;">
					<Text style='#303133'>{{msg.address}}</Text>
					<u-icon name="arrow" color="#B2B2B2" size="30" ></u-icon>
				</view>
			</view> -->
			<view style="width: 100%;height: 1px;background: #f5f5f5;"/>
			<view class="addcenter_item2">
				<Text>详细地址</Text>
				<input class="uni-input inputM" v-model="msg.Address" />
			</view>
		</view>
		<view class="btn" @click="preserve" :style="{'background':mainColor}">
			<Text>保存地址</Text>
		</view>
		
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	    
		import pickerAddress from './liudx-pickerAddress/index.vue'

	export default {
		components: {
		            pickerAddress
		        },
		data(){
			return{
				pageTitle:'收货地址',
				contentHeight:0,
				mainColor:'',
				msg:{
					 Id:0,
					 Name:'',
					 Mobile:'',
					 Province:'',
					 City:'',
					 District:'',
					 Address:'',
				},
				address:'',
				Pickershwo:false,
				addressShow: false,
				
			}
		},
		created(){
			 this.contentHeight = this.$utils.calcContentHeight(-40)+'px';
			 this.mainColor = this.$uiConfig.mainColor;
			 uni.setNavigationBarTitle({
			   title: this.pageTitle,
			 });
		},
		onLoad: function (option) { //option为object类型，会序列化上个页面传递的参数
			if(option.AddressId!=undefined){
				this.getAddressInfo(option.AddressId)
				
			}
		},
		methods:{
			preserve(){
				 let pages = getCurrentPages(); // 当前页面
				 let beforePage = pages[pages.length - 2]; // 前一个页面
				uni.showNavigationBarLoading();
				this.request2(
				  {
				  	url: '/api/AppletUser/SetUserShippingAddress',
				  	data: this.msg
				  
				  },
				
				  res => {
					  this.$refs.uToast.show({
					    title: "保存成功"
					  });
					  setTimeout(()=>{
						 uni.navigateBack({
						     success: function() {
						         beforePage.onLoad(); // 执行前一个页面的created方法
						     }
						 });
					  },1000)
				    
				    uni.hideNavigationBarLoading();
				  },
				  
				);
			},
			getAddressInfo(id){ //获取详细地址接口
				uni.showNavigationBarLoading();
				this.request2(
				  {
				  	url: '/api/AppletUser/GetUserShippingAddressInfo',
				  	data: {
						AddressId:id
					}
				  
				  },
				
				  res => {
					 
				    uni.hideNavigationBarLoading();
					this.msg.Id =res.data.Id;
					 this.msg.Name =res.data.Name;
					 this.msg.Mobile =res.data.Mobile;
					 this.msg.Province =res.data.Province;
					 this.msg.City =res.data.City;
					 this.msg.District =res.data.District;
					 this.msg.Address =res.data.Address;
					 this.address = res.data.DistrictAddress.split(" ").join(",");
				  },
				  
				);
			},
			 addresspick(obj) {
				 
				
				this.msg.Province = obj.province.ID;
				this.msg.City = obj.city.ID;
					
				if(obj.area!=undefined){
					this.msg.District = obj.area.ID;
					this.address = obj.province.Name+','+obj.city.Name+','+obj.area.Name;
				}else{
					this.msg.District = '';
					this.address = obj.province.Name+','+obj.city.Name
				}
				
			},
		}
	}
</script>

<style>
	.AddaddressStyle{
		background: #f3f4f6;
	}
	.AddaddressStyle .top{
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: space-between;
		padding: 20rpx;
		background: #fff;
	}
	.AddaddressStyle .addcenter{
		/* margin-top: 40rpx; */
		background: #FFFFFF;
	}
	.AddaddressStyle .addcenter_item{
		width: 100%;
		padding: 20rpx 28rpx;
		color: #303133;
		font-size: 28rpx;
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: space-between;

	}
	.AddaddressStyle .addcenter_item2{
		width: 100%;
		padding: 20rpx 28rpx;
		color: #303133;
		font-size: 28rpx;
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: space-between;
	
	}
	.AddaddressStyle .btn{
		width:94% ;
		height: 80rpx;
		
		display: flex;
		align-items: center;
		justify-content: center;
		color: #fff;
		margin-left: 3%;
		margin-top: 46rpx;
		border-radius: 40rpx;
	}
	.AddaddressStyle .inputM{
		margin-left: 20rpx;
		width: 70%;
		text-align: right;
	}
</style>
