<template>
	<view class="container">
		<view class="header">
			<text class="back-link" @click="goBack">⬅ 返回</text>
			<view class="header-main">
				<text class="title">我的快递</text>
				<text class="count">共 {{ orders.length }} 个包裹</text>
			</view>
		</view>

		<view class="tabs">
			<view class="tab-item" :class="{active: currentTab === 'all'}" @click="currentTab = 'all'">全部</view>
			<view class="tab-item" :class="{active: currentTab === 'active'}" @click="currentTab = 'active'">进行中</view>
			<view class="tab-item" :class="{active: currentTab === 'completed'}" @click="currentTab = 'completed'">已签收</view>
		</view>

		<scroll-view class="order-list" scroll-y="true">
			<view v-for="(order, index) in filteredOrders" :key="order.order_no" class="order-card" @click="goDetail(order.order_no)">
				<view class="card-header">
					<view class="carrier-tag">{{ order.carrier }}</view>
					<text class="status-text" :class="order.status">{{ getStatusText(order.status) }}</text>
				</view>
				
				<view class="card-body">
					<view class="pkg-icon">📦</view>
					<view class="pkg-info">
						<text class="latest-msg">{{ getDisplayMessage(order.current_status_text || '', order.status) }}</text>
						<text class="order-meta">订单号: {{ order.order_no }}</text>
					</view>
					<text class="arrow">></text>
				</view>

				<view class="card-footer">
					<text class="time">下单时间: {{ order.created_at }}</text>
				</view>
			</view>

			<view v-if="filteredOrders.length === 0" class="empty-state">
				<text class="empty-icon">📂</text>
				<text class="empty-text">暂无相关包裹信息</text>
			</view>
		</scroll-view>
	</view>
</template>

<script uts>
	import { mockService, MockOrder } from './mock-service.uts'
	import { getCurrentUser } from '@/utils/store.uts'

	export default {
		data() {
			return {
				currentTab: 'all',
				orders: [] as MockOrder[]
			}
		},
		onShow() {
			this.loadData()
		},
		computed: {
			filteredOrders(): MockOrder[] {
				if (this.currentTab === 'all') {
					return this.orders
				}
				if (this.currentTab === 'completed') {
					return this.orders.filter((o: MockOrder): boolean => o.status === 'DELIVERED')
				} else {
					return this.orders.filter((o: MockOrder): boolean => o.status !== 'DELIVERED')
				}
			}
		},
		methods: {
			goBack() {
				uni.navigateBack()
			},
			async loadData() {
				// Ensure user mapping is ready before querying orders
				try {
					await getCurrentUser()
				} catch (e) {
					console.warn('getCurrentUser failed:', e)
				}

				const data = await mockService.getMockOrders()
				this.orders = [...data]
			},
			getStatusText(status: string): string {
				return mockService.getStatusText(status)
			},
			getDisplayMessage(text: string, status: string): string {
				return mockService.getDisplayMessage(text, status)
			},
			goDetail(orderNo: string) {
				uni.navigateTo({
					url: `/pages/mall/delivery/test/consumer-logistics-detail?order_no=${orderNo}`
				})
			}
		}
	}
</script>

<style scoped>
	.container {
		background-color: #f7f8fa;
		min-height: 100vh;
		padding: 30rpx;
	}
	.header {
		margin-bottom: 30rpx;
		display: flex;
		flex-direction: row;
		justify-content: flex-start;
		align-items: center;
		gap: 20rpx;
	}
	.back-link {
		font-size: 28rpx;
		color: #007AFF;
		cursor: pointer;
	}
	.header-main {
		flex: 1;
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		align-items: flex-end;
	}
	.title {
		font-size: 40rpx;
		font-weight: bold;
		color: #1a1a1a;
	}
	.count {
		font-size: 24rpx;
		color: #999;
	}
	.tabs {
		display: flex;
		margin-bottom: 30rpx;
		gap: 40rpx;
	}
	.tab-item {
		font-size: 30rpx;
		color: #666;
		padding-bottom: 10rpx;
	}
	.tab-item.active {
		color: #007AFF;
		font-weight: bold;
		border-bottom: 4rpx solid #007AFF;
	}
	.order-card {
		background-color: #fff;
		border-radius: 20rpx;
		padding: 24rpx;
		margin-bottom: 24rpx;
		box-shadow: 0 2rpx 10rpx rgba(0,0,0,0.02);
	}
	.card-header {
		display: flex;
		justify-content: space-between;
		margin-bottom: 20rpx;
	}
	.carrier-tag {
		background-color: #f0f7ff;
		color: #007AFF;
		font-size: 20rpx;
		padding: 4rpx 12rpx;
		border-radius: 6rpx;
	}
	.status-text.DELIVERED { color: #52c41a; }
	.status-text.SHIPPED { color: #007AFF; }
	.status-text.OUT_FOR_DELIVERY { color: #faad14; }
	.status-text.EXCEPTION { color: #ff4d4f; }
	.status-text.PENDING { color: #999; }
	.status-text.DELIVERED { color: #999; }
	.status-text.OUT_FOR_DELIVERY { color: #27ae60; }
	.status-text.SHIPPED { color: #007AFF; }
	
	.card-body {
		display: flex;
		align-items: center;
		gap: 20rpx;
		padding: 10rpx 0;
	}
	.pkg-icon {
		font-size: 50rpx;
		background-color: #f9f9f9;
		width: 90rpx;
		height: 90rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		border-radius: 16rpx;
	}
	.pkg-info {
		flex: 1;
	}
	.latest-msg {
		font-size: 26rpx;
		color: #333;
		line-height: 1.4;
		display: -webkit-box;
		-webkit-line-clamp: 1;
		-webkit-box-orient: vertical;
		overflow: hidden;
		margin-bottom: 8rpx;
	}
	.order-meta {
		font-size: 22rpx;
		color: #999;
	}
	.arrow {
		color: #ccc;
		font-size: 30rpx;
	}
	.card-footer {
		margin-top: 20rpx;
		padding-top: 20rpx;
		border-top: 1rpx solid #f2f2f2;
	}
	.time {
		font-size: 22rpx;
		color: #bbb;
	}
	.empty-state {
		padding-top: 200rpx;
		text-align: center;
	}
	.empty-icon {
		font-size: 100rpx;
		display: block;
		margin-bottom: 20rpx;
	}
	.empty-text {
		color: #999;
		font-size: 28rpx;
	}
</style>
