<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="search-btn" @tap="searchData">查询</view>
		</view>
		<view class="statistics-container">
			<view class="statistics-summary">
				<view class="summary-item">
					<view class="summary-label">总核销单</view>
					<view class="summary-value">{{totalNum}}</view>
				</view>
				<view class="summary-item">
					<view class="summary-label">总优惠金额</view>
					<view class="summary-amount">￥{{totalAmount}}</view>
				</view>
			</view>
			<view class="statistics-title">产品统计</view>
			<view class="statistics-card" v-for="(item,index) in checkInfo" :key="index">
				<view class="statistics-header" @tap="toggleExpand(item.productName)">
					<view class="statistics-product">{{item.productName}}</view>
					
				</view>
				<view class="statistics-info">
					<view class="statistics-item">
						<view class="statistics-label">核销数量</view>
						<view class="statistics-value">{{item.num}}</view>
					</view>
					<view class="statistics-item">
						<view class="statistics-label">优惠金额</view>
						<view class="statistics-amount">￥{{item.totalAmount}}</view>
					</view>
				</view>
			</view>
		</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>
	export default {
		data() {
			return {
				dateStr: '请选择时间段',
				showStartDate: false,
				showEndDate: false,
				startDate: Number(new Date()) - 7 * 24 * 60 * 60 * 1000,
				endDate: Number(new Date()),
				orderTimeStart: '',
				orderTimeEnd: '',
				merchantId: uni.getStorageSync('merchantId'),
				totalAmount: 0,
				totalNum: 0,
				checkInfo:[],
				expandedItems: {}, // 存储展开状态
				h: 0,
				mt: 0
			}
		},
		onLoad() {
			this.initHeight();
			this.getStatisticsData();
		},
		methods: {
			initHeight() {
				uni.getSystemInfo({
					success: (res) => {
						this.h = res.windowHeight;
						this.mt = res.statusBarHeight + 44;
					}
				});
			},
			getStatisticsData() {
				const params = {
					merchantId: this.merchantId
				};
				
				// 添加时间段筛选参数
				if (this.orderTimeStart) {
					params.orderTimeStart = this.orderTimeStart;
				}
				if (this.orderTimeEnd) {
					params.orderTimeEnd = this.orderTimeEnd;
				}
				
				// 获取统计数据
				this.$api.get('/scenic/merchant/offline/order/count', params).then(res => {
					if(res.data.code==0){
						this.checkInfo = res.data.data;
						// 计算总计
						this.calculateTotal();
					}
				}).catch(() => {
					uni.showToast({
						title: '获取统计数据失败',
						icon: 'none'
					});
				});
			},
			calculateTotal() {
				this.totalNum = 0;
				this.totalAmount = 0;
				this.checkInfo.forEach(item => {
					this.totalNum += item.num || 0;
					this.totalAmount += parseFloat(item.totalAmount) || 0;
				});
				// 保留两位小数
				this.totalAmount = this.totalAmount.toFixed(2);
			},
			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;
			},
			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;
			},
			searchData() {
				this.getStatisticsData();
			},
			toggleExpand(productName) {
				this.expandedItems[productName] = !this.expandedItems[productName];
			}
		}
	}
</script>

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

	.top {
		padding: 20rpx 30rpx;
		background-color: #FFFFFF;
		margin-bottom: 20rpx;
		box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.05);
		
		.time-container {
			display: flex;
			gap: 30rpx;
			margin-bottom: 20rpx;
		}

		.time {
			display: flex;
			align-items: center;
			gap: 0 10rpx;
			flex: 1;
		}

		.search-btn {
			background-color: #007A69;
			color: #FFFFFF;
			font-size: 28rpx;
			text-align: center;
			padding: 15rpx 0;
			border-radius: 10rpx;
			margin-top: 10rpx;
			cursor: pointer;
			transition: background-color 0.3s ease;
		}
	}

	/* 统计数据样式 */
	.statistics-container {
		padding: 0 30rpx 30rpx;
	}
	
	.statistics-summary {
		background: #FFFFFF;
		border-radius: 16rpx;
		padding: 30rpx;
		margin-bottom: 24rpx;
		box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.08);
		display: flex;
		justify-content: space-around;
		align-items: center;
	}
	
	.summary-item {
		text-align: center;
		flex: 1;
	}
	
	.summary-label {
		font-size: 24rpx;
		color: #666666;
		margin-bottom: 12rpx;
	}
	
	.summary-value {
		font-size: 36rpx;
		font-weight: bold;
		color: #1E3A62;
	}
	
	.summary-amount {
		font-size: 36rpx;
		font-weight: bold;
		color: #FF6B6B;
	}
	
	.statistics-title {
		font-size: 32rpx;
		font-weight: bold;
		color: #333333;
		margin-bottom: 20rpx;
		padding-left: 10rpx;
		border-left: 6rpx solid #1E3A62;
	}
	
	.statistics-card {
		background: #FFFFFF;
		border-radius: 16rpx;
		padding: 30rpx;
		margin-bottom: 20rpx;
		box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.08);
		transition: transform 0.3s ease, box-shadow 0.3s ease;
	}
	
	.statistics-card:hover {
		transform: translateY(-2rpx);
		box-shadow: 0 8rpx 16rpx rgba(0, 0, 0, 0.12);
	}
	
	.statistics-header {
		display: flex;
		justify-content: space-between;
		align-items: center;
		cursor: pointer;
		padding-right: 10rpx;
	}
	
	.statistics-product {
		font-size: 28rpx;
		font-weight: 600;
		color: #333333;
		margin-bottom: 24rpx;
		padding-bottom: 16rpx;
		border-bottom: 1rpx solid #F0F0F0;
		flex: 1;
	}
	
	.statistics-info {
		display: flex;
		justify-content: space-between;
		flex-wrap: nowrap;
		gap: 40rpx;
	}
	
	.statistics-item {
		flex: 1;
		min-width: 150rpx;
		max-width: calc(50% - 20rpx);
	}
	
	.statistics-label {
		font-size: 24rpx;
		color: #666666;
		margin-bottom: 8rpx;
	}
	
	.statistics-value {
		font-size: 32rpx;
		font-weight: bold;
		color: #1E3A62;
	}
	
	.statistics-amount {
		font-size: 32rpx;
		font-weight: bold;
		color: #FF6B6B;
	}
</style>