<template>
	<view class="container">
		<!-- 顶部筛选标签 -->
		<view class="filter-tabs">
			<view class="tab-item" :class="{ active: activeTab === 'all' }" @click="switchTab('all')">
				全部
			</view>
			<view class="tab-item" :class="{ active: activeTab === 'recharge' }" @click="switchTab('recharge')">
				充值
			</view>
			<view class="tab-item" :class="{ active: activeTab === 'monthly' }" @click="switchTab('monthly')">
				包月
			</view>
			<view class="tab-item" :class="{ active: activeTab === 'season' }" @click="switchTab('season')">
				包季
			</view>
			<view class="tab-item" :class="{ active: activeTab === 'yearly' }" @click="switchTab('yearly')">
				包年
			</view>
		</view>

		<!-- 订单列表 -->
		<view class="order-list">
			<view class="order-item" v-for="order in filteredOrders" :key="order.id">
				<!-- 订单头部 -->
				<view class="order-header">
					<view class="order-info">
						<text class="order-number">订单编号: {{ order.orderNumber }}</text>
						<view class="order-type-tag">复制</view>
					</view>
					<view class="order-status" :class="order.status">
						{{ order.statusText }}
					</view>
				</view>

				<!-- 订单详情 -->
				<view class="order-details">
					<view class="detail-row">
						<text class="label">用户名称:</text>
						<text class="value">{{ order.userName }}</text>
					</view>
					<view class="detail-row">
						<text class="label">用户ID:</text>
						<text class="value">{{ order.userId }}</text>
					</view>
					<view class="detail-row">
						<text class="label">订单类型:</text>
						<text class="value">{{ order.orderType }}</text>
					</view>
					<view class="detail-row">
						<text class="label">订单金额:</text>
						<text class="value price">¥{{ order.amount }}</text>
					</view>
					<view class="detail-row" v-if="order.discount > 0">
						<text class="label">折扣金额:</text>
						<text class="value discount">-¥{{ order.discount }}</text>
					</view>
					<view class="detail-row">
						<text class="label">实付金额:</text>
						<text class="value final-price">¥{{ order.finalAmount }}</text>
					</view>
					<view class="detail-row">
						<text class="label">{{ order.status === 'pending' ? '创建时间:' : '支付时间:' }}</text>
						<text class="value">{{ order.payTime }}</text>
					</view>
				</view>

				<!-- 订单操作 -->
				<view class="order-actions">
					<div>
						<div class="countdown-text" v-if="order.status === 'pending'">支付剩余时间: {{ order.countdown }}
						</div>
					</div>
					<div class="buttonBox">
						<template v-if="order.status === 'pending'">
							<uv-button type="default" size="small" @click="cancelOrder(order)">
								取消订单
							</uv-button>
							<uv-button type="error" size="small" @click="payOrder(order)">
								去支付
							</uv-button>
						</template>
						<template v-else>

							<uv-button type="default" size="small" @click="deleteOrder(order)">
								删除订单
							</uv-button>
							<uv-button type="primary" color="#3F70FF" size="small" @click="viewOrderDetail(order)">
								查看详情
							</uv-button>
						</template>
					</div>
				</view>

				<!-- 待支付订单底部 -->
				<!-- 				<view v-if="order.status === 'pending'" class="pending-footer">
					<view class="countdown">
						<text class="countdown-text">支付剩余时间: {{ order.countdown }}</text>
					</view>
					<view class="pending-actions">
						<uv-button 
							type="default" 
							size="large"
							@click="cancelOrder(order)"
						>
							取消订单
						</uv-button>
						<uv-button 
							type="error" 
							size="large"
							@click="payOrder(order)"
						>
							去支付
						</uv-button>
					</view>
				</view> -->
			</view>
		</view>

		<!-- 空状态 -->
		<view v-if="filteredOrders.length === 0" class="empty-state">
			<u-icon name="file-text" size="60" color="#ccc"></u-icon>
			<text class="empty-text">暂无订单记录</text>
		</view>
	</view>
</template>

<script setup>
	import {
		computed,
		onMounted,
		ref
	} from "vue";
	import {
		onShow
	} from "@dcloudio/uni-app";
	import {
		useAuthStore,
		useUserStore
	} from "@/store";
	import {
		regions
	} from '/static/json/regions.js'
	import HeaderCustom from '/components/HeaderCustom/HeaderCustom.vue'
	import RPC from "@/utils/request";

	const authStore = useAuthStore()
	const userStore = useUserStore()

	// 响应式数据
	const activeTab = ref('all')
	const orders = ref([{
			id: 1,
			orderNumber: 'ORD20230515001',
			typeLabel: '续费',
			status: 'paid',
			statusText: '已支付',
			userName: '张三',
			userId: 'U12345678',
			orderType: '包年会员',
			amount: '1200.00',
			discount: 200.00,
			finalAmount: '1000.00',
			payTime: '2025-05-15 10:30',
			type: 'yearly'
		},
		{
			id: 2,
			orderNumber: 'ORD20230515002',
			typeLabel: '续费',
			status: 'paid',
			statusText: '已支付',
			userName: '李四',
			userId: 'U12345666',
			orderType: '充值',
			amount: '500.00',
			discount: 0,
			finalAmount: '500.00',
			payTime: '2025-06-15 11:30',
			type: 'recharge'
		},
		{
			id: 3,
			orderNumber: 'ORD20230515332',
			typeLabel: '续费',
			status: 'pending',
			statusText: '待支付',
			userName: '小客',
			userId: 'U12345555',
			orderType: '包月会员',
			amount: '120.00',
			discount: 12.00,
			finalAmount: '108.00',
			payTime: '2025-08-12 15:20',
			countdown: '30分钟',
			type: 'monthly'
		}
	])

	// 计算过滤后的订单
	const filteredOrders = computed(() => {
		if (activeTab.value === 'all') {
			return orders.value
		}
		return orders.value.filter(order => order.type === activeTab.value)
	})

	// 切换标签
	const switchTab = (tab) => {
		activeTab.value = tab
	}

	// 查看订单详情
	const viewOrderDetail = (order) => {
		uni.navigateTo({
			url: `/pagesA/order-detail/order-detail?id=${order.id}`
		})
	}

	// 取消订单
	const cancelOrder = (order) => {
		uni.showModal({
			title: '确认取消',
			content: '确定要取消这个订单吗？',
			success: async (res) => {
				if (res.confirm) {
					try {
						// 调用取消订单API
						// await RPC.post('/api/order/cancel', { orderId: order.id })

						// 更新订单状态
						const index = orders.value.findIndex(item => item.id === order.id)
						if (index !== -1) {
							orders.value[index].status = 'cancelled'
							orders.value[index].statusText = '已取消'
						}

						uni.showToast({
							title: '订单已取消',
							icon: 'success'
						})
					} catch (error) {
						console.error('取消订单失败:', error)
						uni.showToast({
							title: '取消失败，请重试',
							icon: 'none'
						})
					}
				}
			}
		})
	}

	// 删除订单
	const deleteOrder = (order) => {
		uni.showModal({
			title: '确认删除',
			content: '确定要删除这个订单吗？删除后无法恢复。',
			success: async (res) => {
				if (res.confirm) {
					try {
						// 调用删除订单API
						// await RPC.post('/api/order/delete', { orderId: order.id })

						// 从列表中移除订单
						const index = orders.value.findIndex(item => item.id === order.id)
						if (index !== -1) {
							orders.value.splice(index, 1)
						}

						uni.showToast({
							title: '订单已删除',
							icon: 'success'
						})
					} catch (error) {
						console.error('删除订单失败:', error)
						uni.showToast({
							title: '删除失败，请重试',
							icon: 'none'
						})
					}
				}
			}
		})
	}

	// 支付订单
	const payOrder = (order) => {
		uni.navigateTo({
			url: `/pages/payment/payment?orderId=${order.id}`
		})
	}

	// 加载订单数据
	const loadOrders = async () => {
		try {
			// 调用获取订单列表API
			// const result = await RPC.get('/api/orders')
			// orders.value = result.data
		} catch (error) {
			console.error('加载订单失败:', error)
			uni.showToast({
				title: '加载失败，请重试',
				icon: 'none'
			})
		}
	}

	onMounted(() => {
		loadOrders()
	})

	onShow(() => {
		// 页面显示时刷新订单列表
		loadOrders()
	})
</script>

<style lang="scss" scoped>
	.container {
		font-size: 14px;
		min-height: 100vh;
		padding-bottom: 50px;
		background-color: #f8f8f8;
	}

	.filter-tabs {
		display: flex;
		background: white;
		padding: 0 16px;
		border-bottom: 1px solid #e5e5e5;

		.tab-item {
			flex: 1;
			text-align: center;
			padding: 16px 0;
			color: #666;
			font-size: 14px;
			position: relative;

			&.active {
				color: #4285f4;
				font-weight: 500;

				&::after {
					content: '';
					position: absolute;
					bottom: 0;
					left: 50%;
					transform: translateX(-50%);
					width: 20px;
					height: 2px;
					background: #4285f4;
					border-radius: 1px;
				}
			}
		}
	}

	.order-list {
		padding: 16px;

		.order-item {
			background: white;
			border-radius: 12px;
			margin-bottom: 16px;
			overflow: hidden;

			.order-header {
				display: flex;
				justify-content: space-between;
				align-items: center;
				padding: 16px 16px 0;

				.order-info {
					display: flex;
					align-items: center;
					gap: 8px;

					.order-number {
						font-size: 14px;
						color: #333;
					}

					.order-type-tag {
						background: #f0f0f0;
						color: #666;
						padding: 2px 6px;
						border-radius: 4px;
						font-size: 12px;
					}
				}

				.order-status {
					padding: 4px 8px;
					border-radius: 4px;
					font-size: 12px;

					&.paid {
						background: #e8f5e8;
						color: #52c41a;
					}

					&.pending {
						background: #fff7e6;
						color: #fa8c16;
					}

					&.cancelled {
						background: #fff1f0;
						color: #ff4d4f;
					}
				}
			}

			.order-details {
				padding: 16px;

				.detail-row {
					display: flex;
					justify-content: space-between;
					align-items: center;
					margin-bottom: 8px;

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

					.label {
						color: #666;
						font-size: 14px;
					}

					.value {
						color: #333;
						font-size: 14px;

						&.price {
							color: #333;
							font-weight: 500;
						}

						&.discount {
							color: #ff4d4f;
						}

						&.final-price {
							color: #333;
							font-weight: bold;
						}
					}
				}
			}

			.order-actions {
				display: flex;
				justify-content: space-between;
				align-items: center;
				gap: 8px;
				padding: 0 16px 16px;
				border-top: 1px solid #f0f0f0;
				padding-top: 16px;

				.countdown-text {
					color: #ff4d4f;
					font-size: 12px;
				}

				.buttonBox {
					display: flex;
					align-items: center;
					gap: 10px;
					justify-content: flex-end;
				}
			}

			.pending-footer {
				border-top: 1px solid #f0f0f0;
				padding: 16px;

				.countdown {
					margin-bottom: 12px;

					.countdown-text {
						color: #ff4d4f;
						font-size: 12px;
					}
				}

				.pending-actions {
					display: flex;
					gap: 12px;

					.u-button {
						flex: 1;
					}
				}
			}
		}
	}

	.empty-state {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding: 80px 20px;

		.empty-text {
			color: #999;
			font-size: 14px;
			margin-top: 16px;
		}
	}
</style>