<template>
	<view class="reconciliation-container">
		
		

		<!-- 日期选择区域 -->
		<view class="date-selector">
			<view class="date-row">
				<view class="date-item">
					<text class="date-label">开始日期</text>
					<view class="date-picker" @click="showStartDatePicker">
						<text class="date-text">{{ startDate }}</text>
					</view>
				</view>
				<view class="date-item">
					<text class="date-label">结束日期</text>
					<view class="date-picker" @click="showEndDatePicker">
						<text class="date-text">{{ endDate }}</text>
					</view>
				</view>
				<view class="date-item">
					<text class="date-label blue">按日期筛选</text>
					<view class="refresh-btn" @click="refreshData">
						<text class="refresh-text">重置</text>
					</view>
				</view>
			</view>
		</view>

		<!-- 一级市场统计 -->
		<view class="stats-section">
			<view class="stats-title green">一级市场已确认</view>
			<view class="stats-row">
				<view class="stats-item">
					<text class="stats-label">金额：</text>
					<text class="stats-value">{{ primaryConfirmed.amount }}</text>
				</view>
				<view class="stats-item">
					<text class="stats-label">数量：</text>
					<text class="stats-value">{{ primaryConfirmed.quantity }}</text>
				</view>
			</view>
		</view>

		<view class="stats-section">
			<view class="stats-title red">一级市场待确认</view>
			<view class="stats-row">
				<view class="stats-item">
					<text class="stats-label">金额：</text>
					<text class="stats-value">{{ primaryPending.amount }}</text>
				</view>
				<view class="stats-item">
					<text class="stats-label">数量：</text>
					<text class="stats-value">{{ primaryPending.quantity }}</text>
				</view>
			</view>
		</view>

		<!-- 二级市场买单统计 -->
		<view class="stats-section">
			<view class="stats-title green">二级市场买单已确认</view>
			<view class="stats-row">
				<view class="stats-item">
					<text class="stats-label">金额：</text>
					<text class="stats-value">{{ secondaryBuyConfirmed.amount }}</text>
				</view>
				<view class="stats-item">
					<text class="stats-label">数量：</text>
					<text class="stats-value">{{ secondaryBuyConfirmed.quantity }}</text>
				</view>
			</view>
		</view>

		<view class="stats-section">
			<view class="stats-title red">二级市场买单待确认</view>
			<view class="stats-row">
				<view class="stats-item">
					<text class="stats-label">金额：</text>
					<text class="stats-value">{{ secondaryBuyPending.amount }}</text>
				</view>
				<view class="stats-item">
					<text class="stats-label">数量：</text>
					<text class="stats-value">{{ secondaryBuyPending.quantity }}</text>
				</view>
			</view>
		</view>

		<!-- 二级市场卖单统计 -->
		<view class="stats-section">
			<view class="stats-title green">二级市场卖单已确认</view>
			<view class="stats-row">
				<view class="stats-item">
					<text class="stats-label">金额：</text>
					<text class="stats-value">{{ secondarySellConfirmed.amount }}</text>
				</view>
				<view class="stats-item">
					<text class="stats-label">数量：</text>
					<text class="stats-value">{{ secondarySellConfirmed.quantity }}</text>
				</view>
			</view>
		</view>

		<view class="stats-section">
			<view class="stats-title red">二级市场卖单待确认</view>
			<view class="stats-row">
				<view class="stats-item">
					<text class="stats-label">金额：</text>
					<text class="stats-value">{{ secondarySellPending.amount }}</text>
				</view>
				<view class="stats-item">
					<text class="stats-label">数量：</text>
					<text class="stats-value">{{ secondarySellPending.quantity }}</text>
				</view>
			</view>
		</view>

		<!-- 仓库商品统计 -->
		<view class="warehouse-section">
			<view class="warehouse-title red">仓库商品不参与日期筛选</view>
			<view class="warehouse-row">
				<view class="warehouse-item">
					<view class="warehouse-status green">寄售中</view>
					<view class="warehouse-stats">
						<text class="stats-label">金额：</text>
						<text class="stats-value">{{ warehouseActive.amount }}</text>
					</view>
					<view class="warehouse-stats">
						<text class="stats-label">数量：</text>
						<text class="stats-value">{{ warehouseActive.quantity }}</text>
					</view>
				</view>
				<view class="warehouse-item">
					<view class="warehouse-status gray">未寄售</view>
					<view class="warehouse-stats">
						<text class="stats-label">金额：</text>
						<text class="stats-value">{{ warehouseInactive.amount }}</text>
					</view>
					<view class="warehouse-stats">
						<text class="stats-label">数量：</text>
						<text class="stats-value">{{ warehouseInactive.quantity }}</text>
					</view>
				</view>
			</view>
		</view>

		<!-- 积分变更统计 -->
		<view class="score-section">
			<view class="score-left">
				<view class="score-title">积分变更</view>
				<view class="score-stats">
					<text class="score-label">增加积分：</text>
					<text class="score-value">{{ scoreChange.increase }}</text>
				</view>
				<view class="score-stats">
					<text class="score-label">减少积分：</text>
					<text class="score-value">{{ scoreChange.decrease }}</text>
				</view>
			</view>
			<view class="score-right" @click="goToScoreDetail">
				<view class="score-icon">📋</view>
				<text class="score-link">积分变更记录</text>
			</view>
		</view>

		
		<uni-datetime-picker
			ref="startPicker"
			type="date"
			v-model="startDate"
			@change="onStartDateChange"
			:clear-icon="false"
		/>
		
		<uni-datetime-picker
			ref="endPicker"
			type="date"
			v-model="endDate"
			@change="onEndDateChange"
			:clear-icon="false"
		/>
	</view>
</template>

<script>
import request from '@/utils/request.js'

export default {
	name: 'Reconciliation',
	data() {
		return {
			// 日期选择
			startDate: '',
			endDate: '',
			
			// 一级市场统计
			primaryConfirmed: {
				amount: 0,
				quantity: 0
			},
			primaryPending: {
				amount: 0,
				quantity: 0
			},
			
			// 二级市场买单统计
			secondaryBuyConfirmed: {
				amount: 0,
				quantity: 0
			},
			secondaryBuyPending: {
				amount: 0,
				quantity: 0
			},
			
			// 二级市场卖单统计
			secondarySellConfirmed: {
				amount: 0,
				quantity: 0
			},
			secondarySellPending: {
				amount: 0,
				quantity: 0
			},
			
			// 仓库商品统计
			warehouseActive: {
				amount: 0,
				quantity: 0
			},
			warehouseInactive: {
				amount: 0,
				quantity: 0
			},
			
			// 积分变更统计
			scoreChange: {
				increase: 0,
				decrease: 0
			}
		}
	},
	
	onLoad() {
		this.initDates()
		this.loadReconciliationData()
	},
	
	methods: {
		// 初始化日期（默认今天）
		initDates() {
			const today = new Date()
			const dateStr = today.getFullYear() + '-' + 
				String(today.getMonth() + 1).padStart(2, '0') + '-' + 
				String(today.getDate()).padStart(2, '0')
			this.startDate = dateStr
			this.endDate = dateStr
		},
		
		// 返回上一页
		goBack() {
			uni.navigateBack()
		},
		
		// 显示开始日期选择器
		showStartDatePicker() {
			this.$refs.startPicker.show()
		},
		
		// 显示结束日期选择器
		showEndDatePicker() {
			this.$refs.endPicker.show()
		},
		
		// 开始日期变更
		onStartDateChange(e) {
			this.startDate = e
			this.loadReconciliationData()
		},
		
		// 结束日期变更
		onEndDateChange(e) {
			this.endDate = e
			this.loadReconciliationData()
		},
		
		// 重置数据
		refreshData() {
			this.initDates()
			this.loadReconciliationData()
		},
		
		// 加载对账数据
		async loadReconciliationData() {
			try {
				uni.showLoading({
					title: '加载中...'
				})
				
				const params = {
					start_date: this.startDate,
					end_date: this.endDate
				}
				
				const response = await request.get('/back/user/reconciliation/stats',  params )
				
				if (response.success) {
					const data = response.data
					
					// 一级市场统计
					this.primaryConfirmed = data.primary_confirmed || { amount: 0, quantity: 0 }
					this.primaryPending = data.primary_pending || { amount: 0, quantity: 0 }
					
					// 二级市场买单统计
					this.secondaryBuyConfirmed = data.secondary_buy_confirmed || { amount: 0, quantity: 0 }
					this.secondaryBuyPending = data.secondary_buy_pending || { amount: 0, quantity: 0 }
					
					// 二级市场卖单统计
					this.secondarySellConfirmed = data.secondary_sell_confirmed || { amount: 0, quantity: 0 }
					this.secondarySellPending = data.secondary_sell_pending || { amount: 0, quantity: 0 }
					
					// 仓库商品统计
					this.warehouseActive = data.warehouse_active || { amount: 0, quantity: 0 }
					this.warehouseInactive = data.warehouse_inactive || { amount: 0, quantity: 0 }
					
					// 积分变更统计
					this.scoreChange = data.score_change || { increase: 0, decrease: 0 }
				} else {
					uni.showToast({
						title: response.message || '加载失败',
						icon: 'none'
					})
				}
			} catch (error) {
				console.error('加载对账数据失败:', error)
				uni.showToast({
					title: '网络错误',
					icon: 'none'
				})
			} finally {
				uni.hideLoading()
			}
		},
		
		// 跳转到积分明细页面
		goToScoreDetail() {
			uni.navigateTo({
				url: '/pages/scores/scores'
			})
		}
	}
}
</script>

<style scoped>
.reconciliation-container {
	min-height: 100vh;
	background-color: #f5f5f5;
}

/* 导航栏 */
.nav-bar {
	display: flex;
	align-items: center;
	justify-content: center;
	height: 88rpx;
	background: linear-gradient(135deg, #ff6b6b, #ff8e8e);
	color: white;
	position: relative;
}

.nav-back {
	position: absolute;
	left: 30rpx;
}

.nav-back-text {
	font-size: 32rpx;
	color: white;
}

.nav-title {
	font-size: 36rpx;
	font-weight: 600;
}

/* 日期选择区域 */
.date-selector {
	background: white;
	padding: 30rpx;
	margin-bottom: 20rpx;
}

.date-row {
	display: flex;
	justify-content: space-between;
	align-items: center;
}

.date-item {
	text-align: center;
}

.date-label {
	display: block;
	font-size: 28rpx;
	color: #333;
	margin-bottom: 10rpx;
}

.date-label.blue {
	color: #007aff;
}

.date-picker {
	padding: 10rpx 20rpx;
	border: 1rpx solid #ddd;
	border-radius: 8rpx;
	background: white;
}

.date-text {
	font-size: 26rpx;
	color: #333;
}

.refresh-btn {
	padding: 10rpx 20rpx;
	background: #007aff;
	border-radius: 8rpx;
}

.refresh-text {
	font-size: 26rpx;
	color: white;
}

/* 统计区域 */
.stats-section {
	background: white;
	margin: 0 30rpx 20rpx;
	padding: 30rpx;
	border-radius: 12rpx;
	box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.1);
}

.stats-title {
	font-size: 32rpx;
	font-weight: 600;
	margin-bottom: 20rpx;
	text-align: center;
}

.stats-title.green {
	color: #52c41a;
}

.stats-title.red {
	color: #ff4d4f;
}

.stats-row {
	display: flex;
	justify-content: space-around;
}

.stats-item {
	text-align: center;
}

.stats-label {
	font-size: 28rpx;
	color: #666;
}

.stats-value {
	font-size: 32rpx;
	font-weight: 600;
	color: #333;
	margin-left: 10rpx;
}

/* 仓库商品区域 */
.warehouse-section {
	background: white;
	margin: 0 30rpx 20rpx;
	padding: 30rpx;
	border-radius: 12rpx;
	box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.1);
}

.warehouse-title {
	font-size: 32rpx;
	font-weight: 600;
	color: #ff4d4f;
	text-align: center;
	margin-bottom: 30rpx;
}

.warehouse-row {
	display: flex;
	justify-content: space-between;
}

.warehouse-item {
	flex: 1;
	text-align: center;
	padding: 0 20rpx;
}

.warehouse-status {
	font-size: 28rpx;
	font-weight: 600;
	margin-bottom: 15rpx;
}

.warehouse-status.green {
	color: #52c41a;
}

.warehouse-status.gray {
	color: #999;
}

.warehouse-stats {
	margin-bottom: 10rpx;
}

/* 积分变更区域 */
.score-section {
	background: white;
	margin: 0 30rpx 40rpx;
	padding: 30rpx;
	border-radius: 12rpx;
	box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.1);
	display: flex;
	justify-content: space-between;
	align-items: center;
}

.score-left {
	flex: 1;
}

.score-title {
	font-size: 32rpx;
	font-weight: 600;
	color: #333;
	margin-bottom: 20rpx;
}

.score-stats {
	margin-bottom: 10rpx;
}

.score-label {
	font-size: 28rpx;
	color: #666;
}

.score-value {
	font-size: 32rpx;
	font-weight: 600;
	color: #333;
	margin-left: 10rpx;
}

.score-right {
	text-align: center;
	padding: 20rpx;
}

.score-icon {
	font-size: 60rpx;
	margin-bottom: 10rpx;
}

.score-link {
	font-size: 24rpx;
	color: #007aff;
}
</style>