<template>
	<view class="page" :style="{'min-height':(h)+'px','padding-top':mt+'px'}">
		<c-nav-bar title="核销记录"></c-nav-bar>
		<view class="top">
			<view class="time-container">
				<view class="time" @click="showStartDate=true">
					<u-icon name="calendar" :label="orderTimeStart || '核销时间-开始'" labelPos="right" labelColor="#666" color="#666" space="7px"
						size="25px"></u-icon>
					<u-icon name="arrow-down" color="#999" size="20px" style="margin-left: 20rpx;"></u-icon>
				</view>
				<view class="time" @click="showEndDate=true">
					<u-icon name="calendar" :label="orderTimeEnd || '核销时间-结束'" labelPos="right" labelColor="#666" color="#666" space="7px"
						size="25px"></u-icon>
					<u-icon name="arrow-down" color="#999" size="20px" style="margin-left: 20rpx;"></u-icon>
				</view>
			</view>
			<!-- <view class="right">共{{totalNum}}单 收入¥{{totalAmount}}</view> -->
		</view>
		<view class="list">
			<block v-if="list.length>0">
					<view class="item" v-for="(item,index) in list" :key="index">
						<!-- <image :src="item.productImg|delArr"></image> -->
						
							<view class="price" style="font-size: bold;">{{item.productName}}</view>
							<view style="margin-top: 10rpx;">核销时间：{{item.orderTime}}</view>
							<view style="margin-top: 10rpx;">优惠金额：￥{{item.orderAmount}}</view>
							<view style="margin-top: 10rpx;">客户姓名：{{item.guestName}}</view>
							<view style="margin-top: 10rpx;">客户电话：{{item.guestPhone}}</view>
					</view>
					
					<!-- 加载更多 -->
					<view class="load-more" v-if="loading">
						<u-loading-icon size="24" color="#007A69"></u-loading-icon>
						<text style="margin-left: 10rpx;">加载中...</text>
					</view>
					
					<!-- 没有更多数据 -->
					<view class="no-more" v-else-if="!hasMore && list.length > 0">
						没有更多数据了
					</view>
				</block>
				<block v-else>
					<NoData />
				</block>
		</view>
		<u-datetime-picker :immediateChange="true" @confirm="confirmStartDate" @cancel="showStartDate=false" :show="showStartDate" v-model="startDate" visibleItemCount="10" mode="date"></u-datetime-picker>
		<u-datetime-picker :immediateChange="true" @confirm="confirmEndDate" @cancel="showEndDate=false" :show="showEndDate" v-model="endDate" visibleItemCount="10" mode="date"></u-datetime-picker>
	</view>
</template>

<script>
	import {
		encrypt,
		decrypt,
		tuomin
	} from '../../utils/aes.js'
	export default {
		data() {
			return {
				dateStr: '请选择时间段',
				showStartDate: false,
				showEndDate: false,
				k: '',
				list: [],
				startDate: Number(new Date()) - 7 * 24 * 60 * 60 * 1000,
				endDate: Number(new Date()),
				orderTimeStart: '',
				orderTimeEnd: '',
				merchantId: uni.getStorageSync('merchantId'),
				totalAmount: 0,
				totalNum: 0,
				page:1,
				limit:10,
				total: 0,
				loading: false,
				hasMore: true,
			}
		},
		filters: {
			delArr(val) {
				if (val) {
					return val.split(',')[0]
				}

			}
		},
		onLoad() {
			this.getdata();
		},
		onReachBottom() {
			// 触底加载更多
			if (!this.loading && this.hasMore) {
				this.page++;
				this.getdata();
			}
		},
		methods: {
			getdata() {
				// 设置加载状态
				this.loading = true;
				
				const params = {
					page: this.page,
					limit: this.limit,
					merchantId: this.merchantId
				};
				
				// 添加时间段筛选参数
				if (this.orderTimeStart) {
					params.orderTimeStart = this.orderTimeStart;
				}
				if (this.orderTimeEnd) {
					params.orderTimeEnd = this.orderTimeEnd;
				}
				this.$api.get('/scenic/merchant/offline/order/page', params).then(res => {  
					// 关闭加载状态
					this.loading = false;
					
					if (res.data.code == 0) {
						// this.totalAmount = res.data.data.orderAmount;
						// 处理数据累加
						if (this.page === 1) {
							this.list = res.data.data.list;
						} else {
							this.list = [...this.list, ...res.data.data.list];
						}
						
						// 更新总数
						this.totalNum = res.data.data.total || 0;
						
						// 判断是否还有更多数据
						this.hasMore = this.list.length < this.totalNum;
					}
					console.log(res)
				}).catch(() => {
					// 异常情况下关闭加载状态
					this.loading = false;
				})
			},
			confirmStartDate(e) {
				// 处理开始时间选择
				const date = new Date(e.value);
				const year = date.getFullYear();
				const month = ('0' + (date.getMonth() + 1)).slice(-2);
				const day = ('0' + date.getDate()).slice(-2);
				
				this.orderTimeStart = `${year}-${month}-${day}`;
				this.showStartDate = false;
				this.updateDateStr();
				// 重置分页参数
				this.page = 1;
				this.list = [];
				this.hasMore = true;
				this.getdata();
			},
			confirmEndDate(e) {
				// 处理结束时间选择
				const date = new Date(e.value);
				const year = date.getFullYear();
				const month = ('0' + (date.getMonth() + 1)).slice(-2);
				const day = ('0' + date.getDate()).slice(-2);
				
				this.orderTimeEnd = `${year}-${month}-${day}`;
				this.showEndDate = false;
				this.updateDateStr();
				// 重置分页参数
				this.page = 1;
				this.list = [];
				this.hasMore = true;
				this.getdata();
			},
			updateDateStr() {
				// 更新日期显示字符串
				if (this.orderTimeStart && this.orderTimeEnd) {
					this.dateStr = `${this.orderTimeStart} 至 ${this.orderTimeEnd}`;
				} else if (this.orderTimeStart) {
					this.dateStr = `开始时间: ${this.orderTimeStart}`;
				} else if (this.orderTimeEnd) {
					this.dateStr = `结束时间: ${this.orderTimeEnd}`;
				} else {
					this.dateStr = '请选择时间段';
				}
			},

		}
	}
</script>

<style lang="less" scoped>
	.page {
		box-sizing: border-box;
		background-color: #F5F5F5;
	}

	.top {
			padding: 24rpx 30rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
			background-color: #FFFFFF;
			box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.05);

			.time-container {
				display: flex;
				gap: 40rpx;
			}

			.time {
				display: flex;
				align-items: center;
				gap: 0 10rpx;
				padding: 12rpx 20rpx;
				background-color: #F9F9F9;
				border-radius: 12rpx;
			}

			.right {
				color: #999;
				font-size: 24rpx;

			}

		}

	.list {
		padding: 20rpx 30rpx;

		.item {
			background-color: #FFFFFF;
			border-radius: 16rpx;
			padding: 30rpx;
			margin-bottom: 20rpx;
			box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.05);
			transition: all 0.3s ease;
			
			&:hover {
				transform: translateY(-2rpx);
				box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.08);
			}

			.price {
				font-size: 34rpx;
				font-weight: bold;
				color: #333333;
				margin-bottom: 16rpx;
				line-height: 1.3;
			}
			
			view {
				font-size: 26rpx;
				color: #666666;
				line-height: 1.5;
				margin-bottom: 12rpx;
				
				&:last-child {
					margin-bottom: 0;
				}
			}
		}
	}
	
	.load-more {
		display: flex;
		justify-content: center;
		align-items: center;
		padding: 40rpx 0;
		color: #999;
		font-size: 28rpx;
		margin: 20rpx 30rpx;
		border-radius: 12rpx;
	}
	
	.no-more {
		display: flex;
		justify-content: center;
		align-items: center;
		padding: 40rpx 0;
		color: #999;
		font-size: 28rpx;
		margin: 20rpx 30rpx;
		border-radius: 12rpx;
	}
</style>