<template>
	<view class="topupDetail">
		<view class="firstLine">
			<view class="left" @click="show = true">
				<view>{{currentDate}}</view>
				<view class="arrowImg" :style="'background-image: url('+ImgUrl + 'user/arrowDown.png'+')'"></view>
			</view>
			<view class="right">
				<view class="font28">¥</view>
				<view class="font40">{{itemAmount(expenditure||0)}}</view>
			</view>
		</view>
		<scroll-view scroll-y="true" class="scrollY">
			<view class="pannels" v-if="list.length>0">
				<view class="item" v-for="(item,index) in list" :key="index" @tap="rechargeRecordDetai(item)">
					<view class="p1">
						<view>{{item.optionName||''}}</view>
						<view class="num">{{itemAmount(item.amount||0)}}</view>
					</view>
					<view class="p2">{{itemTimeFormat(item.payTime)}}</view>
				</view>
			</view>
			<view v-if='list.length==0' class="noCostInfo">
				<image :src="ImgUrl+'xzf/noData-01.png'" mode='widthFix' />
				<p>没有更多数据~</p>
			</view>
			<view class="bottomTip" v-if="list.length>0">— 已展示全部数据 —</view>
		</scroll-view>
		<view class="popup" v-if="show">
			<van-datetime-picker :value="currentDate" type="year-month" title="选择年月" :min-date="minDate"
				position="bottom" :max-date="maxDate" :formatter="formatter" @confirm="onConfirm" @cancel="onCancel" />
		</view>
		<view class="answeringQuestion" @click="goToPage(1)">
			为什么有待支付订单？
		</view>
	</view>
</template>

<script>
	import moment from "moment";
	import {
		Dialog,
		Toast
	} from 'vant';
	import config from '../common/config.js'
	import {
		requestGet,
		requestPost
	} from '../common/request.js'
	import {
		DatetimePicker
	} from 'vant';
	export default {
		data() {
			return {
				ImgUrl: this.$ImgUrl,
				minDate: new Date(2022, 10, 0),
				maxDate: new Date(2999, 10, 1),
				currentDate: '',
				currentDate_: '',
				show: false,
				list: [],
				// 支出 单位分
				expenditure: 0,
				// 收入 单位分
				revenue: 0
			}
		},
		onLoad(options) {
			this.currentDate = moment(new Date()).format('YYYY年M月');
			this.currentDate_ = moment(new Date()).format('YYYY-MM');
			this.QueryRechargeRecord();
		},
		computed: {
			itemAmount() {
				return function(Amount) {
					if (Amount) {
						return (Amount / 100).toFixed(2)
					} else {
						return "0.00"
					}
				}
			},
			itemTimeFormat() {
				return function(time) {
					if (time) {
						return moment(time).format('MM-DD HH:mm:ss')
					} else {
						return time
					}
				}
			},
		},
		methods: {
			// 充值详情
			async rechargeRecordDetai(item) {
				try {
					uni.showLoading({
						title: "请稍后",
						// #ifndef MP-ALIPAY
						mask: true,
						// #endif
					})
					await new Promise((res, rej) => {
						setTimeout(() => {
							res()
						}, 200);
					})
					uni.setStorageSync('rechargeDetail', item)
					uni.hideLoading()
					uni.navigateTo({
						url: `../rechargeDetail/rechargeDetail`
					})
				} catch (error) {
					uni.showToast({
						icon: "error",
						title: error.message
					})
				}
			},
			// 跳转页面
			async goToPage(value) {
				switch (value) {
					case 1:
						uni.navigateTo({
							url: '../OrderClearSuspicions/OrderClearSuspicions'
						})
						break;
					case 2:
						break;
					case 3:
						break;
					default:
						break;
				}
			},
			// 充值明细列表
			async QueryRechargeRecord() {
				uni.showLoading({
					title: "请稍后",
					// #ifndef MP-ALIPAY
					mask: true,
					// #endif
				})
				await new Promise((res, rej) => {
					setTimeout(() => {
						res()
					}, 200);
				})
				const data = await requestPost('/cgi-xpay/app/h5/QueryRechargeRecord', {
					accountId: sessionStorage.getItem('accountId') * 1,
					month: this.currentDate_
				})
				uni.hideLoading()
				if (data['code'] != 200) {
					await Dialog({
						title: '温馨提示',
						message: data['message'],
						confirmButtonText: "知道了"
					})
					return
				}
				// amount	integer金额 单位分 有正负值
				// balance	integer余额 单位分
				// id	integer
				// recordName	string记录名称
				// recordTime	string操作时间
				this.list = data.data['list'] || []
				this.expenditure = data.data['expenditure'] || 0
				// this.list = Array(10).fill(1).map((v, index) => {
				// 	return {
				// 		amount: 100, //	integer金额 单位分
				// 		className: '班级', //	string班级
				// 		gradeName: '年级', //	string年级
				// 		id: index + 1, //	integer
				// 		optionName: '线上充值', //	string操作名称 示例：线上充值
				// 		outTradeNo: '易订单号', //	string交易订单号
				// 		payTime: '2023-09-10 08:22:56', //	string消费时间
				// 		serviceFee: '0.02', //	string服务费 格式￥0.02
				// 		tradeNo: 'd3456345', //	string支付订单号
				// 	}
				// })
			},
			formatter(type, val) {
				if (type === 'year') {
					return `${val}年`;
				} else if (type === 'month') {
					return `${val}月`;
				}
				return val;
			},
			onConfirm(val) {
				this.currentDate = moment(val).format('YYYY年M月')
				this.currentDate_ = moment(val).format('YYYY-MM');
				this.show = false
				this.QueryRechargeRecord()
			},
			onCancel() {
				this.show = false
			}
		}
	}
</script>

<style lang="scss">
	.topupDetail {
		width: 100%;
		height: 100%;
		min-height: 100vh;
		background: #F1F1F3;
		font-family: PingFangSC-Regular, PingFang SC;

		.noCostInfo {
			width: 500rpx;
			margin: 10vh auto;
			text-align: center;
			color: #484C54;
			font-size: 30rpx;

			image {
				width: 500rpx;
				height: 255rpx;
				margin-bottom: 3vh;
			}
		}

		.answeringQuestion {
			height: 45rpx;
			font-size: 32rpx;
			font-family: PingFangSC-Regular, PingFang SC;
			font-weight: 400;
			color: #858585;
			line-height: 45rpx;
			text-align: center;
			margin-top: 30rpx;
			padding-bottom: 50rpx;
		}

		.firstLine {
			display: inline-flex;
			justify-content: space-between;
			height: 88rpx;
			line-height: 88rpx;
			width: calc(100% - 130rpx);
			padding: 0 38rpx 0 92rpx;
			background: #FFFFFF;
			border-top: 1rpx solid #E5E5E5;
			border-bottom: 1rpx solid #E5E5E5;
			font-size: 30rpx;
			color: #333333;

			.left,
			.right {
				display: inline-flex;
				height: 100%;

				view {
					align-self: center;
				}

				.arrowImg {
					margin-left: 12rpx;
					width: 17rpx;
					height: 11rpx;
					background-size: cover;
					background-position: center center;
				}

				.font28 {
					font-size: 28rpx;
				}

				.font40 {
					font-size: 40rpx;
					margin-left: 15rpx;
				}
			}
		}

		.scrollY {
			// position: fixed;
			// height: calc(100vh - 91rpx - calc(44px + env(safe-area-inset-top)));
			// bottom: 0;
			// left: 0;
			// right: 0;
		}

		.popup {
			position: fixed;
			bottom: 0;
			left: 0;
			right: 0;
		}

		.pannels {
			width: 100%;

			background-color: white;
			display: flex;
			flex-direction: column;

			.item {
				margin-left: 32rpx;
				width: calc(100% - 32rpx);
				border-bottom: 1px solid #E5E5E5;
				display: flex;
				flex-direction: column;

				.p1 {
					margin-top: 26rpx;
					margin-bottom: 6rpx;
					width: calc(100% - 40rpx);
					height: 45rpx;
					line-height: 45rpx;
					display: flex;
					justify-content: space-between;
					color: #333333;
					font-size: 32rpx;
					padding-right: 40rpx;

					.num {
						color: #E02020;
					}
				}

				.p2 {
					width: calc(100% - 40rpx);
					height: 37rpx;
					font-size: 26rpx;
					color: rgba(0, 0, 0, 0.35);
					line-height: 37rpx;
					padding-right: 40rpx;
					margin-bottom: 24rpx;
				}
			}
		}

		.bottomTip {
			margin-top: 32rpx;
			padding-bottom: 32rpx;
			width: 100%;
			height: 33rpx;
			font-size: 24rpx;
			color: rgba(0, 0, 0, 0.25);
			line-height: 33rpx;
			text-align: center;
		}
	}
</style>