<template>
	<view class="bill-list-page">
		<!-- 自定义导航栏 -->
		<customNavbar title="我的账单"></customNavbar>

		<!-- 筛选栏 -->
		<view class="filter-bar">
			<!-- 年份筛选点击区域 -->
			<view class="year-filter" @click="toggleYearPicker">
				<text class="year-text">{{ currentYear }}年</text>
				<u-icon name="arrow-down" size="24" color="#666"></u-icon>
			</view>

			<!-- 自定义年份选择器弹窗 -->
			<u-popup v-model="showYearPicker" mode="bottom" border-radius="20rpx" :closeable="true">
				<view class="year-picker-popup">
					<!-- 弹窗标题 -->
					<view class="popup-header">
						<text class="popup-title">选择年份</text>
					</view>

					<!-- 年份列表 -->
					<view class="year-list">
						<view class="year-item" v-for="year in years" :key="year"
							:class="{ active: currentYear === year }" @click="selectYear(year)">
							{{ year }}年
						</view>
					</view>
				</view>
			</u-popup>
		</view>

		<!-- 账单列表 -->
		<scroll-view scroll-y class="scroll-content" :scroll-top="0" :refresher-enabled="true"
			:refresher-triggered="isRefreshing" @refresherrefresh="refresh" @scrolltolower="loadMore">
			<view v-if="flowList.length > 0">
				<view class="bill-item" v-for="item in flowList" :key="item.id" @click="goDetail(item)">
					<!-- 左侧信息 -->
					<view class="bill-left">
						<text class="bill-name">{{ item.billName }}</text>
						<text class="bill-date" v-if="item.payTime">支付时间：{{ item.payTime }}</text>
						<text class="bill-date" v-if="!item.payTime">账单截止日期: {{ item.billEndDate }}</text>
						<text :class="['bill-status', (item.billStatus === '待缴费' ? 'unpay' : paid)]" >{{ item.billStatus }}</text>
					</view>

					<!-- 右侧金额 + 箭头 -->
					<view class="bill-right">
						<text class="amount">{{ formatMoney(item.billAmount) }}</text>
						<u-icon name="arrow-right" size="28" color="#ccc" />
					</view>
				</view>
			</view>

			<view v-else class="empty">
				<u-empty mode="list" text="暂无账单" />
			</view>

			<u-loadmore :status="loadStatus" />
		</scroll-view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				bills: [],
				flowList: [],
				isRefreshing: false,
				loadStatus: 'more', // u-loadmore 状态
				// 年份筛选相关
				currentYear: null,
				years: [],
				pageNo: 1,
				pageSize: 10,
				showYearPicker: false
			};
		},
		onLoad(options) {
			// 可以从 options 获取用户ID或合同ID
			let year = new Date().getFullYear();
			this.currentYear = year;
			this.years = [];
			for (let i = 5; i >= 1; i--) {
				this.years.push(year - i);
			}
			this.loadBills();
		},
		onReachBottom() {
			this.loadStatus = 'loading';
			// 获取数据
			this.loadBills();
		},
		onShow(){
			this.$checkToken(this.$getToken())
		},
		methods: {
			// 切换年份选择器显示
			toggleYearPicker() {
				this.showYearPicker = !this.showYearPicker;
			},

			// 选择年份
			selectYear(year) {
				this.currentYear = year;
				this.showYearPicker = false;
				// 重新加载账单数据
				this.loadBills();
			},

			loadBills() {
				let url = '/bill/pageQueryContractBill'
				this.$u.post(url, {
					pageNo: this.pageNo,
					pageSize: this.pageSize,
					year: this.currentYear,
					billStatus: '已缴费'
				},{
					WT: this.$getToken()
				}).then(result => {
					const data = result.data.result;
					this.bills = data;
					for (let i = 0; i < this.bills.length; i++) {
						let item = this.bills[i]
						this.flowList.push(item);
					}
					++this.pageNo
				}).catch(err => {
					console.log("获取合同账单信息失败：", err)
				})
			},

			refresh() {
				this.isRefreshing = true;
				setTimeout(() => {
					this.loadBills();
					this.isRefreshing = false;
				}, 1000);
			},

			loadMore() {
				// 可以调用接口分页加载更多数据
				this.loadStatus = 'noMore';
			},

			formatMoney(val) {
				if (!val && val !== 0) return '—';
				return '¥' + Number(val).toFixed(2);
			},

			goDetail(item) {
				uni.navigateTo({
					url: `/pages-biz/bill/billDetail?id=${item.id}`,
				});
			},
		},
	};
</script>

<style lang="scss" scoped>
	.bill-list-page {
		background: #f7f8fa;
		min-height: 100vh;
		padding-top: 160rpx;
		/* 给导航栏留空间 */
	}

	.filter-bar {
		display: flex;
		justify-content: space-between;
		align-items: center;
		background: #fff;
		padding: 24rpx 30rpx;
		margin: 20rpx;
		border-radius: 12rpx;
		box-shadow: 0 2rpx 6rpx rgba(0, 0, 0, 0.05);
	}

	/* 年份筛选 */
	.year-filter {
		display: flex;
		align-items: center;
		gap: 8rpx;
		font-size: 32rpx;
		color: #333;
		cursor: pointer;
	}

	/* 支出/收入切换 */
	.tab-bar {
		display: flex;
		gap: 20rpx;
	}

	.tab-item {
		padding: 12rpx 32rpx;
		font-size: 28rpx;
		border-radius: 24rpx;
		color: #333;
		background-color: #f5f5f5;
		transition: all 0.2s ease;

		&.active {
			color: #fff;
			background: #ff3b30;
		}
	}

	/* 自定义年份选择器样式 */
	.year-picker-popup {
		background: #fff;
		padding: 30rpx 0;
	}

	.popup-header {
		text-align: center;
		padding: 20rpx 0;
		border-bottom: 1rpx solid #f0f0f0;
	}

	.popup-title {
		font-size: 34rpx;
		font-weight: 600;
		color: #333;
	}

	.year-list {
		padding: 20rpx;
	}

	.year-item {
		font-size: 32rpx;
		color: #666;
		text-align: center;
		padding: 24rpx;
		border-radius: 12rpx;
		margin-bottom: 16rpx;
		background: #f5f5f5;
		transition: all 0.2s ease;

		&:last-child {
			margin-bottom: 0;
		}

		&.active {
			background: #ff3b30;
			color: #fff;
		}

		&:hover {
			background: #ff5252;
			color: #fff;
		}
	}

	.scroll-content {
		margin-top: 20rpx;
		padding-bottom: 20rpx;
	}

	.bill-item {
		display: flex;
		justify-content: space-between;
		background: #fff;
		margin: 0 20rpx 20rpx 20rpx;
		border-radius: 12rpx;
		padding: 20rpx;
		box-shadow: 0 2rpx 6rpx rgba(0, 0, 0, 0.05);
	}

	.bill-left {
		display: flex;
		flex-direction: column;

		.bill-name {
			font-size: 28rpx;
			color: #2D2B2C;
			font-weight: 500;
		}

        .bill-status{
			font-size: 30rpx;
			font-weight: 800;
			&.paid {
				color: red;
			}
				
			&.unpay {
				color: green;
			}
		}
		.bill-date,
		.bill-status {
			margin-top: 18rpx;
		}
	}

	.bill-right {
		display: flex;
		align-items: center;

		.amount {
			font-size: 28rpx;
			color: #F34038;
			margin-right: 10rpx;
		}
	}

	.empty {
		margin-top: 200rpx;
		text-align: center;
	}
</style>