<!-- 地址列表 -->
<template>
	<view class="container">
    <global-loading />
		<view v-if="addresListlist.length" class="pad-bot-140 addAddress">
			<view class="addAddress-content flex-row-plus" v-for="(item, index) in addresListlist" :key="index">
				<!--				<view class="address-hesd">{{item.username1}}</view>-->
				<view class="address-detail" @click="itemTap(item)">
					<view class="userName">
						<text class="fs28 font-color-252744">{{item.receiveName}}</text>
						<text class="fs28 font-color-90919C mar-left-30">{{item.receivePhone}}</text>
					</view>
					<view class="defaultAD-box">
						<text class="def" v-if="item.ifDefault">{{$t('common.default')}}</text>
						<text class="lable font-color-999" v-else-if="item.label!=''">{{item.label}}</text>
						<text class="user-address font-color-999">{{item.receiveAdress}}{{item.address}}</text>
					</view>
				</view>
				<view class="editIcon" @click="editAdress(index, item)"></view>
			</view>
		</view>
		<view v-else class="emptyAddress-box">
			<image class="emptyAddress" src="http://36.138.125.206:8081/ceres-local-file/static/img/noAddress.png"></image>
			<text>{{$t('common.addressempty')}}</text>
		</view>
		<!-- #ifdef MP-WEIXIN -->
		<view class="wxAddressNBox">
			<view class="flex-items btnBox flex-sp-between">
				<view class="wxAddress btn flex-items flex-center" @click="wxAddFn">
					<image src="http://36.138.125.206:8081/ceres-local-file/static/images/weixin2x.png"></image>
					<text>微信导入</text>
				</view>
				<view class="addAddressBtn btn" @click="addAddressClick">{{$t('common.add')}}</view>
			</view>
		</view>
		<!-- #endif -->
		<!-- #ifdef H5 || APP-PLUS || MP-ALIPAY -->
		<view class="addAddress-box">
			<view class="addAddress" @click="addAddressClick">{{$t('common.add')}}</view>
		</view>
		<!-- #endif -->
	</view>
</template>

<script>
	import {
		hidden
	} from "../../utils/hidden";
	const NET = require('../../utils/request')
	const API = require('../../config/api')
	export default {
		data() {
			return {
				addresList: [],
				headWord: '',
				editAddress: [],
				type: 0,
				addresListlist: [],
				page: 1, //当前页
				pageSize: 20, //每页记录数
				loadingType: 0,
				addData: {},
			}
		},
		onLoad(options) {
			this.type = options.type
		},
		onShow(options) {
			this.addresListlist = []
			this.page = 1
			this.getAddressData()
		},
		onBackPress(e) {
			if (e.from === 'navigateBack') {
				return false;
			}
			this.back();
			return true;
		},
		onReachBottom() {
			if (this.loadingType == 1) {
				uni.stopPullDownRefresh()
			} else {
				this.page = this.page + 1
				this.getAddressData()
			}
		},
		methods: {
			back() {
				if (this.type == 1 || this.type == 2 ) {
					uni.navigateTo({
						url: '../../pages_category_page1/orderModule/orderConfirm?type=' + this.type
					})
				} else {
					uni.switchTab({
						url: '../../pages/tabbar/user/index'
					});
				}
			},
			addAddressClick() {
				if (this.type ==  1 || this.type == 2) {
					uni.navigateTo({
						url: 'addAddress?type=1&ordertype=1'
					})
				} else {
					uni.navigateTo({
						url: 'addAddress?type=1'
					})
				}

			},
			getAddressData() {
				// uni.showLoading({
				// 	mask: true,
				// 	title: '加载中...',
				// })
				NET.request(API.QueryMemberAddres, {
					page: this.page,
					pageSize: this.pageSize
				}, "GET").then(res => {
					this.addresList = res.data
					if (res.data.list.length == 0) {
						this.loadingType = 1
						this.page = this.page
					}
					for (let i = 0; i < res.data.list.length; i++) {
						this.addresList.list[i].username1 = this.addresList.list[i].receiveName.slice(0, 1)
					}
					this.addresListlist = this.addresListlist.concat(res.data.list)
					this.addresListlist.forEach((item) => {
						item.receivePhone = hidden(item.receivePhone, 3, 4)
					})
					uni.hideLoading();
				}).catch(err => {
					uni.hideLoading();
				})
			},
			editAdress(id, item) {
				uni.setStorageSync("editAddress", JSON.stringify(this.addresList.list[id]))
				uni.navigateTo({
					url: 'addAddress?type=2&receiveId=' + item.receiveId
				})
			},
			itemTap(item) {
				if (this.type == 1 ||  this.type == 2) {
					uni.setStorageSync('receiveItem', item)
					uni.navigateTo({
						url: '../../pages_category_page1/orderModule/orderConfirm?receiveId=' + item
							.receiveId +'&type='+ this.type
					})
				}
			},
			// 微信导入
			wxAddFn() {
				let self = this
				uni.chooseAddress({
					success(res) {
						self.addData['username'] = res.userName
						self.addData['phone'] = res.telNumber
						self.addData['ssqText'] = `${res.provinceName}-${res.cityName}-${res.countyName}`
						self.addData['defaultRegion'] = self.addData.ssqText.split("-")
						self.addData['address'] = res.detailInfo
						self.addData['province'] = res.provinceName
						self.addData['city'] = res.cityName
						// uni.showLoading({
						// 	mask: true,
						// 	title: '导入中...',
						// })
						setTimeout(() => {
							uni.hideLoading();
							uni.navigateTo({
								url: 'addAddress?type=3&wxAddressData=' + JSON.stringify(self
									.addData)
							})
						}, 500);
					},
          fail:(res)=>{
          }
				})
			}
		}
	}
</script>

<style lang="scss">
	.container {
		padding: 0 24rpx;

		.addAddress {
			border-top: 2rpx solid #F3F4F5;
			padding-top: 30rpx;
		}

		.emptyAddress-box {
			display: flex;
			justify-content: center;
			flex-direction: column;
			align-items: center;
			margin-top: 50%;

			.emptyAddress {
				width: 186upx;
				height: 150upx;
			}

			text {
				margin-top: 40upx;
				color: #999999;
			}
		}

		.addAddress-box {
			position: fixed;
			bottom: 50upx;
			left: 30upx;

			.addAddress {
				width: 690upx;
				height: 100upx;
				color: #FFFFFF;
				text-align: center;
				background: #252744;
        border-radius: 50rpx;
			}
		}

		.wxAddressNBox {
			position: fixed;
			bottom: 50upx;
			width: 100%;
			left: 0;

			.btnBox {
				width: 100%;
				background: #FFFFFF;
				height: 120rpx;
				padding: 0 50rpx;

				.btn {
					width: 300rpx;
					background: #FFFFFF;
					height: 90rpx;
					line-height: 90rpx;
					border: 2rpx solid #E4E5E6;
					text-align: center;
				}

				.addAddressBtn {
					color: #FFEBC4;
					background: #333333;
					border: 2rpx solid #333333;
				}

				.wxAddress {
					image {
						width: 75rpx;
						height: 75rpx;
						margin-right: 20rpx;
					}
				}
			}
		}

		.addAddress-content {
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding-bottom: 40rpx;
			margin-bottom: 40rpx;
			border-bottom: 1rpx solid #D3D4DE;

			.address-detail {
        flex-grow: 1;
				.userName {
					margin-bottom: 15rpx;
				}
			}

			.lable {
				padding: 3rpx 10rpx;
        background-color: #F2F2F6;
				border-radius: 6rpx;
				font-size: 24rpx;
				font-weight: 400;
				//color: rgba(102, 102, 102, 1);
        color: #595B6B;
        text-align: center;
			}

			.def {
				padding: 3rpx 10rpx;
				font-size: 24rpx;
				font-weight: 400;
				color: #F54639 !important;
        background-color: #FCE4E2 !important;
        border-radius: 6rpx;
			}

			.user-address {
				font-size: 28rpx;
				margin-left: 10rpx;
        color: #90919C;
			}

			.address-hesd {
				height: 70upx;
				width: 70upx;
				background-color: #BBBBBB;
				color: #FFFFFF;
				border-radius: 50%;
				line-height: 70upx;
				text-align: center;
			}

			.defaultAD-box {
				width: 100%;

				.default-textBox {
					padding-right: 20upx;
					width: 80upx;
				}

				.default-content {
					width: 435upx;
				}

				.default-text {
					color: #C5AA7B;
					background-color: #FFE4CC;
					height: 36upx;
					width: 60upx;
					font-size: 26upx;
					border-radius: 4upx;
					align-items: center;
					line-height: 36upx;
				}
			}
			.editIcon {
				width: 40rpx;
				height: 41rpx;
				display: block;
				background: url("http://36.138.125.206:8081/ceres-local-file/image/address_edit.png") no-repeat center center;
				background-size: contain;
			}
		}
	}
</style>