<template>
	<view class="container clearfix">
		<tui-skeleton
			v-if="skeletonShow"
			backgroundColor="#fafafa"
			borderRadius="10rpx"
			:isLoading="true"
			:loadingType="5"
		></tui-skeleton>
		<view v-else>
			<view v-if="!isEmpty" class="address-list" :style="{'height': scrollHeight + 'px'}">
				<scroll-view scroll-y="true" :style="{'height': scrollHeight + 'px'}">
					<view class="list" v-for="(item, index) in addressList" :key="index" @click="checkAddress(item)">
						<view class="wrapper">
							<view class="u-box">
								<text class="name">{{item.receiver}}</text>
								<text class="mobile">{{item.mobile}}</text>
							</view>
							<view class="u-box b-b">
								<text class="address">收货地址：{{item.province}}{{item.city}}{{item.town}}{{item.address}}</text>
							</view>
							<view class="u-box b-t">
								<view v-if="item.defaultFlag == 1" class="tag-left">
									<view class="tag">默认地址</view>
								</view>
								<view v-else class="tag-left"></view>
								<view class="tag-right">
									<view class="t-b"  @click.stop="handleDeleteAddress(item.addressId)">
										<text class="iconfont icon-a-shanchu"></text>
										<text class="txt">删除</text>
									</view>
									<view class="t-b"  @click.stop="addAddress('edit',item)">
										<text class="iconfont icon-bianji"></text>
										<text class="txt">编辑</text>
									</view>
									
								</view>
							</view>
						</view>
					</view>
				</scroll-view>
				<view class="add-btn" @click="addAddress('add')" :style="{bottom :isIphoneX ? '68rpx' : '34rpx'}">添加新地址</view>
			</view>
			<view v-else  class="empty-container">
				<image class="empty-container-image" :src="staticUrl + 'icon_address@2x.png'" mode="aspectFit"></image>
				<view class="txt">您还没有收货地址</view>
				<view class="txt">点击底部按钮添加收货地址吧~~</view>
				<view class="login-btn" @click="addAddress('add')">添加新地址</view>
			</view>
		</view>
		<!-- 弹窗提示 -->
		<tui-modal
			:show="modal"
			@click="handleClick"
			@cancel="modal = false"
			:content="'确定删除该地址？'"
			:button="modalButton"
			color="#333"
			:size="32"
			shape="circle"
			:maskClosable="false"
		>
		</tui-modal>
	</view>
</template>

<script>
	import authorize from '@/common/config/authorize.js'	
	export default {
		data() {
			return {
				isIphoneX:this.$store.state.isIphoneX,
				skeletonShow:true,
				isSelect:false,
				isEmpty:false,
				listQuery:{
					userId:0,
					pageNum:1,
					pageSize:10
				},
				delParams:{//删除地址参数
					userId:0,
					adressId:0
				},
				addressList: [],
				hasNextPage:false,
				allowDataStatus:true,
				wrapperHeight:'100%',
				scrollHeight:'',
				currPage:'',//当前页面
				prevPage:'',//上一个页面
				modal:false,
				modalButton: [
					{
						text: '取消',
						type: 'gray',
						plain: true //是否空心
					},
					{
						text: '确认',
						customStyle: {
							color: '#fff',
							bgColor: '#F3B574'
						},
						plain: false
					}
				],
			}
		},
		onLoad(option){	
			if(option.type=='select'){this.isSelect = true;}
			this.setScrollHeight();
		},
		methods: {
			setScrollHeight() {	// 窗口高度 - 底部距离
				setTimeout(()=> {
					const query = wx.createSelectorQuery().in(this);
					query.selectAll('.add-btn').boundingClientRect();
					query.exec(res => {
						if(res[0][0]){
							let winHeight = this.$api.getWindowHeight(),
								eleTop = res[0][0].top - 1;
								this.scrollHeight =  eleTop;
						}
					})
				}, 500)
			},
			//初始化地址列表数据
			async getQueryAddressList(){	
				try{
					this.listQuery.pageNum = 1;
					const res = await this.UserService.QueryAddressList(this.listQuery)
					let data = res.data
					console.log('data',data)
					if(data.list&&data.list.length > 0){
						this.isEmpty = false
						this.hasNextPage = data.hasNextPage
						this.addressList = data.list
					}else{
						this.isEmpty = true
					}
					this.skeletonShow = false
				}catch(error){
					this.$util.msg(error.msg,2000)
				}
			},
			async getOnReachBottomData(){// 上滑加载分页
				try{
					this.listQuery.pageNum+=1
					const res = await this.UserService.QueryAddressList(this.listQuery)
					let data = res.data
					if(data.list&&data.list.length > 0){
						this.isEmpty = false
						this.hasNextPage = data.hasNextPage
						this.addressList = this.addressList.concat(data.list) 
					}else{
						this.isEmpty = true
					}
				}catch(error){
					this.$util.msg(error.msg,2000)
				}
			},
			checkAddress(item){//选择地址
				//是否需要返回地址(从订单确认页跳过来选收货地址)
				if(!this.isSelect){return ;}
				uni.setStorageSync('selectAddress',item)
				var pages = getCurrentPages();
				var prevPage = pages[pages.length - 2];  //上一个页面	
					prevPage.setData({select:'select'})
				uni.navigateBack();
			},
			addAddress(type,item){
				uni.navigateTo({
					url: `/pages/user/address/addressManage?type=${type}&data=${JSON.stringify(item)}`
				})
			},
			// 显示提示
			handleDeleteAddress(addressId){
				this.modal = true
				this.delParams.addressId = addressId
			},
			//删除收货地址
			async deleteAddress(params){
				try{
					const res =this.UserService.DeleteAddress(params)
					this.$util.msg('删除成功',2000,true,'success')
					setTimeout(()=>{
						this.addressList = [];
						this.getQueryAddressList();
					},2000)
				}catch(error){
					this.$util.msg(error.msg,2000);
				}
			},
			// 确认删除
			handleClick(e) {
				//确认删除
				if (e.index == 1) {
					this.deleteAddress(this.delParams)
				}
				this.modal = false
			},
		},
		onReachBottom() {
			if(this.hasNextPage) {
				this.getOnReachBottomData();
			}
		},
		onShow() {
			this.$api.getStorage().then((resolve) =>{
				this.listQuery.userId = this.delParams.userId = resolve.userId ? resolve.userId : 0
				this.listQuery.pageNum = 1;
				this.addressList = [];
				this.getQueryAddressList();
				var pages = getCurrentPages();
				var prevPage = pages[pages.length - 2];  //上一个页面	
					// prevPage.setData({select:''})
			})
		}
	}
</script>

<style lang='scss'>
	page {
		height: auto;
	}
	page,.container{
		/* padding-bottom: 120upx; */
		background: #F7F7F7;
		border-top: 1px solid #EBEBEB;
	}
	.container{
		position: relative;
	}
	.address-list{
		width: 100%;
		box-sizing: border-box;
		padding: 24rpx;
	}
	.list{
		display: flex;
		align-items: center;
		width: 100%;
		height: auto;
		padding: 24rpx;
		background: #FFFFFF;
		position: relative;
		box-sizing: border-box;
		margin-bottom: 24rpx;
		border-radius: 16rpx;
	}
	.wrapper{
		display: flex;
		flex-direction: column;
		flex: 1;
	}
	.u-box.b-b{
		
	}
	.u-box.b-b{
		margin-bottom:24rpx;
	}
	.u-box.b-t{
		margin-bottom:0;
	}
	.u-box{
		display: flex;
		align-items: center;
		font-size: $font-size-28;
		color: $text-color;
		line-height: 40rpx;
		margin-bottom: 12rpx;
		.name{
			margin-right: 40rpx;
			font-weight: bold;
		}
		.mobile{
			font-weight: bold;
		}
		.tag-left{
			flex: 6;
			.tag{
				width: 120rpx;
				height: 40rpx;
				background: #FFF8EF;
				border-radius: 20rpx;
				font-size: $font-size-24;
				color: $color-system;
				line-height: 40rpx;
				text-align: center;
				padding: 0 6rpx;
			}
		}
		.tag-right{
			flex: 4;
			display: flex;
			text-align: right;
			.t-b{
				flex: 1;
				line-height: 40rpx;
				.txt{
					font-size: $font-size-24;
					color: $text-color;
					line-height: 40rpx;
				}
			}
			.icon-a-shanchu{
				color:#999999;
				margin-right: 8rpx;
				font-size: $font-size-36;
			}
			.icon-bianji{
				color: #999999;
				margin-right: 8rpx;
				font-size: $font-size-36;
			}
		}
		.address{
			font-size: $font-size-28;
			color: $text-color;
			line-height: 40rpx;
			-o-text-overflow: ellipsis;
			text-overflow: ellipsis;
			display: -webkit-box;
			word-break: break-all;
			-webkit-box-orient: vertical;
			-webkit-line-clamp: 2;
			overflow: hidden;
		}
	}
	.add-btn{
		position: fixed;
		left: 75rpx;
		right: 75rpx;
		bottom: 34rpx;
		z-index: 95;
		display: flex;
		align-items: center;
		justify-content: center;
		width: 600rpx;
		height: 88rpx;
		font-size: $font-size-28;
		line-height: 88rpx;
		color: #FFFFFF;
		text-align: center;
		background: $btn-confirm;
		border-radius: 44rpx;
	}
	.adds-btn{
		width: 600rpx;
		height: 88rpx;
		font-size: 28rpx;
		line-height: 88rpx;
		color: #FFFFFF;
		margin: 0 auto;
		text-align: center;
		background: #000000;
		border-radius: 44rpx;
	}
</style>

