<template>
	<view class="detail-container">
		<!-- 日期选择器 -->
		<view class="date-picker">
			<u-datetime-picker :value="selectedDate" mode="date" @confirm="handleDateChange"></u-datetime-picker>
		</view>

		<!-- 数据表格 -->
		<view class="data-table">
			<view class="table-header">
				<text v-for="(col, index) in columns" :key="index" class="header-cell">
					{{ col.title }}
				</text>
			</view>

			<view class="table-body">
				<view class="table-row" v-for="(item, rowIndex) in tableData" :key="rowIndex">
					<text class="body-cell" v-for="(col, colIndex) in columns" :key="colIndex">
						{{ item[col.key] }}
					</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				selectedDate: new Date().toISOString().slice(0, 10), // 默认当天
				originData: null, // 原始数据
				statType: '', // 统计类型
				columns: [], // 动态表头
				tableData: [] // 表格数据
			}
		},

		onLoad(options) {
			this.initData(options.data)
			this.generateTable()
		},

		methods: {
			// 初始化接收数据
			initData(encodedData) {
				try {
					const rawData = decodeURIComponent(encodedData)
					this.originData = JSON.parse(rawData)
					this.statType = this.originData.statType

					console.log(rawData)
				} catch (e) {
					uni.showToast({
						title: '数据加载失败',
						icon: 'none'
					})
				}
			},

			// 日期变化处理
			handleDateChange(e) {
				this.selectedDate = e.value
				this.generateTable()
			},

			// 生成表格数据
			generateTable() {
				// 根据类型生成不同列
				this.columns = this.getColumns()

				// 根据类型生成数据（示例逻辑）
				this.tableData = this.statType === 'quality' ?
					this.generateQualityData() :
					this.generateCommonData()
			},

			// 获取表头配置
			getColumns() {
				const baseColumns = [{
						title: '日期',
						key: 'date'
					},
					{
						title: '类型',
						key: 'type'
					}
				]

				switch (this.statType) {
					case 'quality':
						return [...baseColumns,
							{
								title: '问题数量',
								key: 'issueCount'
							},
							{
								title: '问题类型',
								key: 'issueType'
							}
						]

					case 'salary':
						return [...baseColumns,
							{
								title: '计件数量',
								key: 'quantity'
							},
							{
								title: '工资金额',
								key: 'amount'
							}
						]

					default: // production
						return [...baseColumns,
							{
								title: '报工数量',
								key: 'total'
							},
							{
								title: '合格数量',
								key: 'passed'
							}
						]
				}
			},

			// 生成质量数据（示例）
			generateQualityData() {
				return [{
					date: this.selectedDate,
					type: '当日统计',
					issueCount: this.originData.todayData.failedNum,
					issueType: '生产问题'
				}, {
					date: '本月累计',
					type: '月度统计',
					issueCount: this.originData.monthData.failedNum,
					issueType: '综合问题'
				}]
			},
			generateCommonData() {
				return [{
					date: this.selectedDate,
					type: '当日统计',
					issueCount: this.originData.todayData.failedNum,
					issueType: '生产问题'
				}, {
					date: '本月累计',
					type: '月度统计',
					issueCount: this.originData.monthData.failedNum,
					issueType: '综合问题'
				}]
			}
		}
	}
</script>

<style scoped lang="scss">
	.detail-container {
		padding: 20rpx;
	}

	.date-picker {
		margin: 20rpx 0;
		padding: 20rpx;
		background: #fff;
		border-radius: 8rpx;
	}

	.data-table {
		border: 1rpx solid #e4e7ed;
		border-radius: 8rpx;
		overflow: hidden;
	}

	.table-header,
	.table-row {
		display: flex;
		height: 80rpx;
		align-items: center;
	}

	.header-cell,
	.body-cell {
		flex: 1;
		text-align: center;
		padding: 10rpx;
	}

	.table-header {
		background-color: #f5f7fa;
		font-weight: bold;
	}

	.table-row:nth-child(even) {
		background-color: #fafafa;
	}

	.body-cell {
		color: #606266;
	}
</style>