<template>
	<view class="all">
		<view class="top padding-v-30 padding-c-30">
			<view class="zhiya padding-b-10"> {{ $t("myple.u1") }} </view>
			<view class="zhiyashu">
				<text class="shu">{{navList[tabCurrentIndex].sum|getMoneyStyle}}</text>
				<text class="wen padding-l-10">USDT</text>
			</view>
		</view>
		<view class="navbar flex">
			<view v-for="(item, index) in navList" :key="index" class="nav-item flex-center"
				:class="{ current: tabCurrentIndex === index }" @click="tabClick(index)">{{ item.text }}</view>
		</view>
		<view class="title-box flex">
			<view class="title">
				<text>{{ $t("myple.u4") }}</text>
			</view>
			<view class="title">
				<text>{{ $t("myple.u5") }}</text>
			</view>
			<view class="title">
				<text>{{ $t("myple.u6") }}</text>
			</view>
			<view class="title">
				<text>{{ $t("myple.u7") }}</text>
			</view>
			<!-- <view class="title">
				<text>{{ $t("myple.u8") }}</text>
			</view> -->
		</view>
		<swiper :current="tabCurrentIndex" :style="{ height: height }" class="swiper-box" duration="300"
			@change="changeTab">
			<swiper-item class="tab-content padding-b-30" v-for="(tabItem, tabIndex) in navList" :key="tabIndex">
				<scroll-view scroll-y="true" class="list-scroll-content" @scrolltolower="loadData">
					<!-- 空白页 -->
					<empty v-if="
							tabItem.loaded === true &&
							tabItem.orderList.length === 0
						"></empty>
					<!-- 订单列表 -->
					<view>
						<!-- <view class="order-item flex" > -->
						<view class="order-item">
							<view class="all-list" v-for="(item,ind) in navList[tabCurrentIndex].orderList" :key="ind">
								<view class="list flex padding-v-20">
									<view class="list-item">
										<view class="item"> {{item.num}} </view>
										<view class="item"> USDT </view>
									</view>
									<view class="list-item">
										<view class="item"> {{item.income}} </view>
										<view class="item"> USDT </view>
									</view>
									<view class="list-item">
										<view class="item">
											{{item.day}}天
										</view>
									</view>
									<view class="list-item">
										<view class="l-item">{{item.add_time[0]}} </view>
										<view class="l-item">
											{{item.add_time[1]}}
										</view>
									</view>
									<!-- <view class="list-item">
										<view class="l-item">{{item.send_time[0]}}</view>
										<view class="l-item">
											{{item.send_time[1]}}
										</view>
									</view> -->
								</view>
							</view>
							<!-- <view class="money">
								<view>{{ (item.pm == 0 ? '-' : '+') + item.number * 1 }}</view>
							</view> -->
						</view>
					</view>
				</scroll-view>
			</swiper-item>
		</swiper>
	</view>
</template>

<script>
	import {
		getLock
	} from "@/api/mypledge.js";
	import empty from "@/components/empty";
	import {
		getTime
	} from '@/utils/rocessor.js';
	import {
		getMoneyStyle
	} from "@/utils/rocessor.js";
	export default {
		filters: {
			getMoneyStyle,
		},
		components: {
			empty,
		},
		onReady(res) {
			var _this = this;
			uni.getSystemInfo({
				success: resu => {
					const query = uni.createSelectorQuery();
					query.select('.swiper-box').boundingClientRect();
					query.exec(function(res) {
						_this.height = resu.windowHeight - res[0].top + 'px';
					});
				},
				fail: res => {}
			});
		},
		data() {
			return {
				height: "",
				tabCurrentIndex: 0,
				navList: [{
						state: 1,
						text: this.$t('myple.u2'),
						loadingType: "more",
						orderList: [],
						page: 1, //当前页数
						limit: 10, //每次信息条数
						loaded: false,
						sum:''
					},
					{
						state: 2,
						text: this.$t('myple.u3'),
						loadingType: "more",
						orderList: [],
						page: 1, //当前页数
						limit: 10, //每次信息条数
						loaded: false,
						sum:''
					},
				],
			};
		},
		onLoad(options) {
			uni.setNavigationBarTitle({
				title: this.$t("tab.a1"),
			});
		},
		onShow() {
			this.loadData();
		},
		methods: {
			getTime,
			toBack() {
				uni.switchTab({
					url: "/pages/pledge/pledge",
				});
			},
			// 页面跳转
			navto(e) {
				uni.navigateTo({
					url: e,
				});
			},
			//获取收入支出信息
			async loadData(source) {
				const that = this;
				//这里是将订单挂载到tab列表下
				let index = that.tabCurrentIndex;
				let navItem = that.navList[index];
				let state = navItem.state;
				if (source === 'tabChange' && navItem.loaded === true) {
					//tab切换只有第一次需要加载数据
					return;
				}
				if (navItem.loadingType === 'loading') {
					//防止重复加载
					return;
				}
				if (navItem.loadingType === 'noMore') {
					//防止重复加载
					return;
				}
				// 修改当前对象状态为加载中
				navItem.loadingType = 'loading';

				getLock({
						status: state,
						page: navItem.page,
						limit: navItem.limit
					})
					.then(({
						data
					}) => {
						// 保存我的总金额
						navItem.sum = data.sum;

						let arr = data.list.map(e => {
							e.add_time = that.getTime(e.add_time).split(" ")
							e.send_time = that.getTime(e.send_time).split(" ")
							e.income = Number(e.income);
							e.num = Number(e.num);
							return e;
						});
						navItem.orderList = navItem.orderList.concat(arr);
						if (navItem.limit == data.length) {
							navItem.page++;
							//判断是否还有数据， 有改为 more， 没有改为noMore
							navItem.loadingType = 'more';
							return;
						} else {
							//判断是否还有数据， 有改为 more， 没有改为noMore
							navItem.loadingType = 'noMore';
						}
						uni.hideLoading();
						that.$set(navItem, 'loaded', true);
					})
					.catch(e => {
						console.log(e);
					});
			},
			//swiper 切换
			changeTab(e) {
				this.tabCurrentIndex = e.target.current;
				this.loadData("tabChange");
			},
			//顶部tab点击
			tabClick(index) {
				this.tabCurrentIndex = index;
			},
		},
	};
</script>

<style lang="scss">
	.all {
		line-height: 1;
	}

	.top {
		margin: 0 30rpx;
		height: 138rpx;
		background-color: #191a1f;
		font-weight: bold;
		color: #ffffff;
		border-radius: 20rpx;

		.zhiya {
			font-size: $font-sm;
			color: #757c8f;
		}

		.zhiyashu {
			.shu {
				font-size: $font-lg;
			}

			.wen {
				font-size: $font-base;
			}
		}
	}

	.swiper-box {
		margin: 0 30rpx;
	}

	.navbar {
		margin: 20rpx 30rpx 0 30rpx;
		border-top-right-radius: 20rpx;
		border-top-left-radius: 20rpx;
		height: 88rpx;
		padding: 0 5px;
		background: #191a1f;
		border-bottom: 1px solid rgba(255, 255, 255, 0.3);

		.nav-item {
			flex: 1;
			height: 100%;
			font-size: 15px;
			color: #999999;
			position: relative;

			&.current {
				color: #fff;

				&:after {
					content: "";
					position: absolute;
					left: 50%;
					bottom: 0;
					transform: translateX(-50%);
					width: 44px;
					height: 0;
					border-bottom: 2px solid $color-yellow;
				}
			}
		}
	}

	.tab-content {
		.list-scroll-content {
			background: #191a1f;
			border-bottom-right-radius: 20rpx;
			border-bottom-left-radius: 20rpx;
			height: 100%;
			overflow:hidden;
		}
	}

	// 列表样式
	.title-box {
		padding: 20rpx 0;
		background: #191a1f;
		margin: 0 30rpx;
		justify-content: space-around;
		border-bottom: 1px solid rgba(255, 255, 255, 0.3);

		.title {
			width: 20%;
			font-size: 22rpx;
			color: $font-color-light;
			text-align: center;
		}
	}

	.all-list {
		.list {
			border-bottom: 1px solid rgba(255, 255, 255, 0.3);
			justify-content: space-around;

			.list-item {
				color: #ffffff;
				width: 20%;
				text-align: center;
				flex-shrink: 0;

				.item {
					font-size: 22rpx;
					font-weight: bold;
				}

				.l-item {
					font-size: 18rpx;
				}

			}
		}

	}
</style>