<template>
	<view>
		<cu-custom bgColor="bg-white" :isBack="true">
			<block slot="content">
				我的地址
			</block>
			<block slot="right">
				<view class="" style="color: #000000; font-size: 26rpx; margin-right: 40rpx;" @tap="jumpAddress()">
					新增地址
				</view>
			</block>
		</cu-custom>
		<view class="" style="margin-top: 15rpx;">
		</view>
		<view class="" v-if="myAddresses.length<1&& outAddressList.length<1">
			<image src="../../../static/nomore_icon.png" mode="" style="width: 370rpx;height: 370rpx;position: absolute;top:calc(50% - 185rpx) ;left:calc(50% - 185rpx);" v-if="!emptyLoading">
			</image>
            <view class="flex flex-direction align-center justify-center" style="width:100%;margin-top:calc(50% - 185rpx);" v-if="emptyLoading">
                <image src="../../../static/empty-loading.png" mode="" style="width: 256rpx;height: 158rpx;"></image>
                <view style="color:#3A3A3A;font-size;34rpx;margin-top:46rpx;">加载中...</view>
            </view>
		</view>
		<view class="" v-else>
			<view class="padding " v-for="(item,index) in myAddresses" :key="index" @click="chooseAddress(item)"
				style="border-bottom: #F3F3F3 1rpx solid; background-color: #FFFFFF;">
				<view class="flex justify-between align-center">
					<view class="flex-direction">
						<view class="" style="font-size: 31rpx;color: #111111;font-weight: bold;">
							{{item.address+item.detailed}}
						</view>
						<view class="flex align-center" style="color: #999999;margin-top: 10rpx;">
							<view class="">
								{{item.name}}
							</view>
							<view class="" style="margin-left: 30rpx;">
								{{item.phone}}
							</view>
						</view>
					</view>
					<view @click.stop="jumpUpdateAddress(item)" class="margin-right">
						<image src="../../../static/edit_2x.png" mode="" style="width: 32rpx; height: 32rpx;"></image>
					</view>
				</view>
			</view>
			<view class="" v-if="outAddressList.length>0">
				<view class="padding-sm color-99">
					以下地址超出服务范围
				</view>
				<view class="padding " v-for="(item,index) in outAddressList" :key="index"
					style="border-bottom: #F3F3F3 1rpx solid; background-color: #FFFFFF;">
					<view class="flex justify-between align-center">
						<view class="flex-direction">
							<view class="" style="font-size: 31rpx;color: #111111;font-weight: bold;">
								{{item.address+item.detailed}}
							</view>
							<view class="flex align-center" style="color: #999999;margin-top: 10rpx;">
								<view class="">
									{{item.name}}
								</view>
								<view class="" style="margin-left: 30rpx;">
									{{item.phone}}
								</view>
							</view>
						</view>
						<view @click.stop="jumpUpdateAddress(item)">
							<image src="../../../static/edit_2x.png" mode="" style="width: 32rpx; height: 32rpx;">
							</image>
						</view>
					</view>
				</view>
			</view>
		</view>


	</view>
</template>

<script>
	export default {

		data() {
			return {
				///1.地址列表
				myAddresses: [],
				outAddressList: [],
				flag: true,
				merchantId: '',
                emptyLoading: true,
			}
		},
		onShow() {
			this.getAddressList();
		},
		onLoad(options) {
			if (options.flag) {
				this.flag = options.flag
				// console.log(this.flag, '111111')
			}
			if (options.merchantId) {
				this.merchantId = options.merchantId
			}
			// console.log(options)

		},
		methods: {
			chooseAddress(item) {
				// console.log(this.flag, item)
				if (this.flag == true) {

					//判断手机号与姓名是否相等
					//相等跳转到编辑页面
					if(item.name == item.phone){
						
						this.jumpUpdateAddress(item);
						setTimeout(()=>{
							this.$api.msg('联系人姓名不符合规范,请修改后重新选择!')
						},500)
						
					}else{
						uni.setStorageSync('address', item)
						uni.navigateBack({
							delta: 1
						})
					}
				} else {
					return
				}

			},
			///1.获取地址列表
			getAddressList() {
				const param = {
					memberId: uni.getStorageSync('userInfo').memberId, //会员id
				}
				if (this.merchantId) {
					param.merchantId = this.merchantId
				}
				this.$u.api.addressList(param).then(res => {
					// console.log(res, 'resresres', param)
					if (res.code == 200) {
						this.myAddresses = res.data.addressList
						this.outAddressList = res.data.outAddressList
						if (this.myAddresses == null) {
							this.myAddresses = []
						}
						if (this.outAddressList == null) {
							this.outAddressList = []
						}
					}
				}).finally(()=>{
                    this.emptyLoading = false
                });

			},
			///2.跳转到地址编辑
			jumpUpdateAddress(obj) {
				uni.navigateTo({
					url: "/pages/mine/minePage/updateAddress?addressInfo=" + JSON.stringify(obj),
				});
			},
			jumpAddress() {
				uni.navigateTo({
					url: "/pages/mine/minePage/updateAddress"
				});
			},

		}
	}
</script>

<style>

</style>
