<template>
	<view class="pages">
		<u-navbar
			title="收货地址"
			@leftClick="leftClick"
			:autoBack="true"
			 :safeAreaInsetTop="true"
		>
		</u-navbar>
		<view class="page-wrap">
			<view class="full-btn" @click="newAddr">新增收货地址</view>
			<view v-if="dataList.length>0" class="addr" @click="addrClick(item)" v-for="(item,index) in dataList" :key="item.id">
				<u-swipe-action>
				<u-swipe-action-item :show="item.show" :index="index" @click.stop="swipeClick(index)" :options="options">
				<view class="u-flex">
				<view class="icon-wrap" v-if="item.ifDefault==1">
					<u-icon size="36rpx" :name="staticUrl+'/img/arrows.png'"></u-icon>
				</view>
				<view class="info u-flex-1">
					<view class="up">
						<text>{{item.receiveName}}</text>
						<text>{{item.receivePhone}}</text>
						<text class="default" v-if="item.ifDefault==1">默认</text>
					</view>
					<view class="down">{{item.receiveAdress}}{{item.address}}</view>
				</view>
				<u-icon name="edit-pen" color="#333" size="20" @click.native.stop="editAddr(item)"></u-icon>
				</view>
				</u-swipe-action-item>
				</u-swipe-action>
			</view>
			<u-empty v-else mode="list"></u-empty>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				staticUrl:this.$commonConfig.staticUrl,
				dataList:[],
				options:[
					{
						text: '删除',
						style: {
							backgroundColor: '#FF3C3F',
							'padding-left':'10px'
						}
					}
				],
				fromPage:'',
				backUrl:'',
			}
		},
		onShow() {	
			this.getAddrList();
		},
		onLoad(page) {
			console.log('page',page);
			this.fromPage = page.from;
			this.backUrl = page.backUrl;
		},
		methods: {
			leftClick(e){
				console.log('leftClick',e);
			},
			newAddr(){
				uni.$u.route('/center/addrForm', {type: 'add',backUrl:this.backUrl});
			},
			editAddr(item){
				uni.$u.route('/center/addrForm', {type: 'edit',id:item.id});
			},
			getAddrList(){
				this.$u.api.addrList().then(res=>{
					this.dataList = res.data.rows;
					}).catch(err=>{
					console.log('getAddrList',err.data);
				})
			},
			swipeClick(index){
				let that = this;
				// console.log('swipeClick',this.dataList[index]);
				let item = this.dataList[index];
				uni.showModal({
					title: '温馨提示',
					content: '确定要删除吗？',
					success: res => {
						if (res.confirm) {
							that.deleteAddr(item.id);
						}
					},
					complete:()=>{}
				})
			},
			deleteAddr(id){
				this.$u.api.delAddr({id:id}).then(res=>{
					this.getAddrList()
					}).catch(err=>{
					console.log('deleteAddr',err.data);
				})
			},
			addrClick(item){
				console.log('selectAddr',item);
				if(this.fromPage == 'submitorder'){
					let pages = getCurrentPages();             //获取所有页面栈实例列表
					let nowPage = pages[ pages.length - 1];    //当前页页面实例
					let prevPage = pages[ pages.length - 2 ];  //上一页页面实例
					prevPage.$vm.fromPage = 'addrlist';
					prevPage.$vm.receive.id = item.id;         // 修改上一页data里面的content参数值
					uni.navigateBack()
				}
			}
		}
	}
</script>
<style>
	page{
		background-color: #F5F5F5;
	}
</style>
<style lang="scss" scoped>
.addr{
	background-color: #fff;
	padding: 30rpx;
	margin-bottom: 20rpx;
	.info{
		margin-left: 30rpx;
	}
	.up{
		margin-bottom: 10rpx;
		font-size: 32rpx;
		font-weight: 600;
		text{margin-right: 20rpx;}
		.default{
			padding: 5rpx 16rpx;
			border-radius: 4rpx;
			color: #FF3C3F;
			background-color: #FFECEC;
			font-size: 22rpx
		}
	}
	.down{
		font-size: 26rpx;
		color: #999;
	}
}
</style>