<template>
	<view>
		<view class="ciycle">
			<view id="IntegralStatisticsTop" ref="IntegralStatisticsTop">
				<view class="innerTop">
					<view class="innerTopLeft">
						<view v-for="item in rangeSelect" :key="item.id"
							:class="activeRange == item.id ? 'activeTab' : 'defaultTab'" @click="selectedRange(item)">
							{{item.title}}
						</view>
					</view>
					<view class="innerTopRightImport">
						<view class="innerTopRight" @tap="showAndHiddenDatePicker">查看更多></view>
					</view>
				</view>
				<view class="innerBottom">
					<view class="innerBottomOfTop">
						<view class="innerTimer">
							{{fundTopDate}}
						</view>
					</view>
					<view class="IntegralImport">
						<view class="totalIntegral">
							当期收益(元)
						</view>
						<view class="totalIntegralNumber">
							{{currentIncome ? currentIncome :0}}
						</view>
					</view>
					<view class="innerBottomOfBottom">
						<view class="withdrawalMoneyBtn" @tap="jump('/my/wallet/index')">
							提现
						</view>
					</view>
				</view>
				<uni-datetime-picker type="date" ref="dataPicker" :clear-icon="true" return-type="string"
					@change="confirmSelectedDate" v-model="selectedDate" @maskClick="maskClick" />
			</view>
			<view id="IntegralStatisticsBottom" :style="{height:domNeedHeight == 0? 420 + 'px' : domNeedHeight + 'px'}">
				<view class="isbOptions" ref="isbOptions">
					<view v-for="item in bottomOptionsInfo" :key="item.id"
						:class="activeIntegral == item.id ?'bottomActiveTab optionsInfo':'bottomDefaultTab optionsInfo'"
						@tap="integralTypes(item)">
						{{item.title}}
					</view>
				</view>
				<view class="optionsDetail">
					<cc-nodata title="暂无数据" v-if="noDataScrollStatus"
						:style="{width:100+'%',height:domNeedHeight == 0? 420 + 'px' : domNeedHeight  + 'px'}"></cc-nodata>
					<!-- 储值积分 -->
					<view v-if="!noDataScrollStatus" v-for="(item,index) in fundDetail" :key="index"
						class="optionsDetailImport">
						<view class="storedIntegralTop">
							<view class="storedIntegralTopOfRight">
								<view class="">{{item.type == 1 ?"收入":"支出"}}</view>
								<view class="">{{item.money}}元</view>
							</view>
						</view>

						<view class="storedIntegralMiddle">
							<view class="">{{item.createTime}}</view>
							<view class="storedIntegralMiddleofRight">余额:{{item.balance}}</view>
						</view>

						<view class="storedIntegralBottom">备注: {{item.title}}</view>
					</view>
					<uniLoadMore color="#007AFF" :iconType="iconType" :status="loadingStatus" :marginBottom="20"
						:marginTop="20" v-if="isShowLoadMore" />
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import * as websocketUtils from 'utils/websocketUtils.js';
	import uniLoadMore from "@/uview-ui/components/u-loadmore/u-loadmore.vue";
	import navBar from "@/components/navBar/navBar.vue";
	import {
		watch
	} from "vue";
	export default {
		components: {
			uniLoadMore,
			navBar,
		},
		onUnload() {
			this.selectedDate = null;
			this.activeRange = null;
			this.activeIntegral = null;
			this.loadingStatus = null;
			this.currentPage = null;
			this.fundDetail = null;
			this.pageSize = null;
			this.noDataScrollStatus = null;
			this.iconType = null;
			this.rangeSelect = null;
			this.bottomOptionsInfo = null;
			this.fundTopDate = null;
			this.currentIncome = null;
			this.isShowLoadMore = true;
			this.userId = null;
		},
		onLaunch() {},
		onShow() {},
		onLoad() {
			this.selectedDate = this.getCurrentTimeStamp();
			this.userId = uni.getStorageSync("userId");
			// if (this.activeIntegral == 1) this.activeIntegral == "";
			// //获取下方积分明细(列表)
			this.getFundDataList({
				artificerId: this.userId,
				detailsType: this.activeIntegral,
				fundData: this.selectedDate,
				fundType: this.activeRange,
				limit: this.pageSize,
				page: this.currentPage,
			});
			console.log(this.noDataScrollStatus)
		},
		onReady() {
			//自适应改变列表高度
			this.selfAdaptionChangeElemtHeight();
		},
		//上拉触底
		onReachBottom(e) {
			console.log(e, "dwdw123123");
			this.currentPage += 1;
			this.isScroll = true;
			if (this.loadingStatus == "nomore") {
				return;
			} else {
				this.getFundDataList({
					artificerId: this.userId,
					detailsType: this.activeIntegral,
					fundData: this.selectedDate,
					fundType: this.activeRange,
					limit: this.pageSize,
					page: this.currentPage,
				}, true);
			}
		},
		//下拉刷新
		onPullDownRefresh(e) {
			this.fundDetail = [];
			this.currentPage = 1;
			this.activeIntegral = 1;
			this.selectedDate = this.getCurrentTimeStamp();
			// this.activeRange = 1;
			this.getFundDataList({
				artificerId: this.userId,
				detailsType: this.activeIntegral,
				fundData: this.selectedDate,
				fundType: this.activeRange,
				limit: this.pageSize,
				page: this.currentPage,
			});
		},
		watch: {
			// 监听数据源改变下拉刷新的状态
			fundDetail(newData, oldData) {
				if (newData.length >= 0) {
					uni.stopPullDownRefresh();
				}
			},
		},
		data() {
			return {
				domNeedHeight: 0, //元素的所需高度
				title: "资金明细",
				backgroundcolor: "linear-gradient(to bottom, #294856, #1a615b)",
				selectedDate: this.currentDate,
				activeRange: 1,
				activeIntegral: 1,
				loadingStatus: "loadmore",
				currentPage: 1,
				pageSize: 6,
				noDataScrollStatus: false,
				iconType: "circle",
				fundTopDate: null,
				currentIncome: null,
				isShowLoadMore: true,
				userId: null,
				rangeSelect: [{
					id: 1,
					title: "本期",
				}, {
					id: 2,
					title: "上期",
				}, {
					id: 3,
					title: "本月",
				}, {
					id: 4,
					title: "上月",
				}],
				bottomOptionsInfo: [{
					id: 1,
					title: "全部"
				}, {
					id: 2,
					title: "收入"
				}, {
					id: 3,
					title: "支出"
				}],
				fundDetail: [],
			}
		},
		methods: {
			//自适应改变列表高度
			selfAdaptionChangeElemtHeight() {
				let _this = this;
				try {
					uni.getSystemInfo({
						success: res => {
							if (res && res.windowHeight) {
								let query = uni.createSelectorQuery().in(this);
								query
									.select(".optionsDetail")
									.boundingClientRect((data) => {
										_this.domNeedHeight = res.windowHeight - data.top - 20;
									})
									.exec();
							}
						}
					});
				} catch (e) {
					//TODO handle the exception
				}
			},
			//获取下方(列表)
			getFundDataList(params, isScroll) {
				this.loadingStatus = "loading";
				let {
					artificerId,
					detailsType,
					fundData,
					fundType,
					limit,
					page,
				} = params;
				this.$Request.postXZX("/app/fundDetails/selectFundDetails", params).then((
					res) => {
					//日期在返回数据userMoneyDetailsList.records的外面,需要单独赋值
					if (res && res.data && res.data.startFundData) {
						this.fundTopDate = res.data?.startFundData + "---" + res.data?.endFundData;
					} else {
						this.fundTopDate = this.getCurrentTimeStamp() + "---" + this.getCurrentTimeStamp();
					}
					if (res && res.data && res.data.userMoneyDetailsList.records.length > 0) {
						//有数据时
						this.currentIncome = res.data?.income + "元";
						for (let i = 0; i < res.data?.userMoneyDetailsList.records.length; i++) {
							this.fundDetail.push(res.data?.userMoneyDetailsList.records[i]);
						}
						this.noDataScrollStatus = false;
						this.isShowLoadMore = true;
						if (this.fundDetail.length <= res.data.userMoneyDetailsList.total) {
							this.loadingStatus = "nomore";
						} else if (this.fundDetail.length > res.data.userMoneyDetailsList.total) {
							this.loadingStatus = "loadmore";
						}
					} else {
						//无数据时
						if (!isScroll) {
							//如果isScroll为null时说明不在滚动
							this.noDataScrollStatus = true;
							this.isShowLoadMore = false;
						} else {
							this.noDataScrollStatus = false;
						}
						this.loadingStatus = "nomore";
						// this.noDataScrollStatus = true;
					}
				})
				uni.stopPullDownRefresh();
			},
			//异步展开日期选择Picker
			showAndHiddenDatePicker() {
				this.$refs.dataPicker.show();
			},
			//日期选择确认事件
			confirmSelectedDate(e) {
				this.selectedDate = e;
				this.fundDetail = [];
				this.getFundDataList({
					artificerId: this.userId,
					detailsType: this.activeIntegral,
					fundData: this.selectedDate,
					fundType: this.activeRange,
					limit: this.pageSize,
					page: this.currentPage,
				});
			},
			//点击日期选择框遮罩层事件
			maskClick() {},
			//选择区间类型(本期/上期/本月/上月)
			selectedRange(item) {
				if (item && item.id) {
					if (item.id == this.activeRange) return;
					this.activeRange = item.id;
					this.currentPage = 1;
					this.isScroll = false;
					this.fundDetail = [];
					this.currentIncome = "";
					this.selectedDate = this.getCurrentTimeStamp();
					this.getFundDataList({
						artificerId: this.userId,
						detailsType: this.activeIntegral,
						fundData: this.selectedDate,
						fundType: this.activeRange,
						limit: this.pageSize,
						page: this.currentPage,
					});
				}
			},
			//选择积分类型(全部/收入/(提现/扣款))
			integralTypes(item) {
				if (item && item.id) {
					if (item.id == this.activeIntegral) return;
					this.activeIntegral = item.id;
					this.currentPage = 1;
					this.fundDetail = [];
					this.isScroll = true;
					this.loadingStatus = "loadmore";
					this.getFundDataList({
						artificerId: this.userId,
						detailsType: this.activeIntegral,
						fundData: this.selectedDate,
						fundType: this.activeRange,
						limit: this.pageSize,
						page: this.currentPage,
					});
				}
			},
			//获取当前时间及转换方法
			getCurrentTimeStamp() {
				let realDateTimeStamp = new Date();
				let year = realDateTimeStamp.getFullYear();
				let month = realDateTimeStamp.getMonth() + 1;
				let day = realDateTimeStamp.getDate();
				let currentDate = year + '-' + (month < 10 ? '0' + month : month) + '-' + (day < 10 ? '0' + day : day);
				return currentDate;
			},
			//跳转路由
			jump(url) {
				uni.navigateTo({
					url,
				})
			}
		}
	}
</script>

<style scoped>
	@import "./index.css";
</style>

<style scoped>
	page {
		background-color: #f1f1f1;
		height: 100%;
	}
</style>