<template>
	<view>
		<!-- 头部 -->
		<headContent borderBottom>
			<template #left>
				<reverse-back />
			</template>
			<template #right>
				<view class="head-icon" @click.stop="selectCurrency()">
					<text class="iconfont">&#xe671;</text>
				</view>
			</template>
		</headContent>
		<view class="content" :style="{'height':PageContentHeight + 'px'}">
			<view class="pages-title">
				地址管理
			</view>
			<view class="currency">
				<text class="currency-lable">{{ currencyInfo ? currencyInfo.name : '' }}</text>
				<u-switch space="4" v-model="value11" activeColor="#05C175" inactiveColor="rgb(226, 226, 226)"
					size="30"></u-switch>
			</view>
			<scroll-view class="scroll-view" scroll-y :style="{'height': scrollHeight}">
				<template v-for="(item , index) in addressList">
					<view class="address-item">
						<view class="address-title">
							<text class="title">{{ item.notes }}</text>
							<text class="del-address" @click.stop="delAddress(item)">删除</text>
						</view>
						<view class="address-currency">
							{{ currencyInfo ? `${currencyInfo.name}_` : '' }}{{ item.type }}
						</view>
						<view class="address-text">
							{{ item.address }}
						</view>
					</view>
				</template>
			</scroll-view>
			<view class="add-address">
				<view class="add-btn" @click.stop="addBtn()">
					添加提币地址
				</view>
			</view>
		</view>
		<confirm-popup ref="confirmPopupRef" content="确定删除？" @confirm="confirmDel" />
	</view>
</template>
<script>
	import {
		mapGetters
	} from 'vuex'
	import {
		Way_getBiTypeList
	} from "@/utils/common-request.js"
	import {
		Api_getAddress,
		Api_delAddress
	} from "@/api/index.js"
	export default {
		name: 'addressList',
		data() {
			return {
				value11: false,
				scrollHeight: 0,
				currencyInfo: null,
				addressList: [],
				getBiStatus: false,
				delItem: {},
			};
		},
		computed: {
			...mapGetters([
				'PageContentHeight'
			])
		},
		watch: {
			PageContentHeight: {
				handler(newH) {
					this.scrollHeight = newH - uni.upx2px(120) - uni.upx2px(80) - uni.upx2px(160) + 'px'
				},
				immediate: true
			},
			currencyInfo: {
				handler(newC, oldC) {
					if (newC && newC.id !== oldC?.id) {
						this.getAddress();
						this.$setStorageSync('currency', newC);
					} else {
						if (this.getBiStatus) {
							return false
						};
						this.getBiStatus = true;
						Way_getBiTypeList().then(res => {
							if (res && res.length > 0) {
								this.currencyInfo = res[0];
								this.getBiStatus = false
							}
						}).catch(err => {
							this.currencyInfo = []
						})
					}
				},
				deep: true,
				immediate: true
			},
		},
		onLoad() {
			this.currencyInfo = this.$getStorageSync('currency');
		},
		onShow() {

		},
		mounted() {

		},
		methods: {
			confirmDel() {
				uni.showLoading({
					title: ''
				})
				Api_delAddress({
					address_id: this.delItem?.id
				}).then(res => {
					this.getAddress()
				}).catch(err => {

				}).finally(() => {
					setTimeout(() => {
						uni.hideLoading()
					}, 200)
				})
			},
			// 删除
			delAddress(item) {
				this.delItem = item
				this.$nextTick(() => {
					this.$refs.confirmPopupRef.open();
				})
			},
			selectCurrency() {
				uni.navigateTo({
					url: `/pages/content/select-currency?type=address`
				})
			},
			addBtn() {
				uni.navigateTo({
					url: '/pages/content/add-address',
					success: res => {
						// 通过eventChannel向被打开页面传送数据
						res.eventChannel.emit('setCurrency', this.currencyInfo)
					}
				})
			},
			getAddress() {
				Api_getAddress({
					currency: this.currencyInfo?.id
				}).then(res => {
					this.addressList = res
					console.log('getAddress = ', res)
				}).catch(err => {

				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		width: 100%;

		.pages-title {
			height: 120rpx;
			font-size: 60rpx;
			font-weight: bold;
			line-height: 130rpx;
			padding: 0 $pages-padding;
		}

		.currency {
			width: 100%;
			height: 80rpx;
			padding: 0 $pages-padding;
			display: flex;
			justify-content: space-between;

			.currency-lable {
				font-size: 28rpx;
			}
		}

		.scroll-view {
			width: 100%;
			padding: 0 $pages-padding;

			.address-item {
				width: 100%;
				background-color: #f6f6f6;
				padding: 36rpx 20rpx;
				border-radius: 10rpx;
				margin-bottom: 20rpx;

				.address-title {
					width: 100%;
					border-bottom: 1rpx solid $border-color4;
					padding-bottom: 30rpx;
					display: flex;
					justify-content: space-between;

					.title {
						flex: 1;
						font-size: 30rpx;
					}

					.del-address {
						flex-shrink: 0;
						font-size: 26rpx;
						color: #d7b213;
					}
				}

				.address-currency {
					padding: 20rpx 0 16rpx;
					font-size: 24rpx;
					color: #aaa8a8;
				}

				.address-text {
					font-size: 22rpx;
					color: #aaa8a8;
				}
			}

			// <view class="address-item">
			// 	<view class="address-title">
			// 		<text class="title">发个按时打卡可是大家撒低级</text>
			// 		<text class="del-address">删除</text>
			// 	</view>
			// 	<view class="address-currency">
			// 		USDT_TRC20
			// 	</view>
			// 	<view class="address-text">
			// 		的军事打击跨国黄金时代进口的韩国
			// 	</view>
			// </view>


		}

		.add-address {
			width: 100%;
			height: 160rpx;
			padding: 40rpx $pages-padding;

			.add-btn {
				width: 100%;
				height: 100%;
				text-align: center;
				line-height: 80rpx;
				background-color: $Theme-Color;
				font-size: 32rpx;
				color: #fff;
				border-radius: 10rpx;
			}
		}
	}
</style>