<template>
  <view class="order-history-container">
    <!-- 顶部导航栏 -->
    <view class="page-header">
      <!-- 左上角：返回主页按钮（箭头+文字 垂直排列） -->
      <view class="nav-left" @click="goBackToHome">
        <text class="nav-icon">←</text>
      </view>

      <!-- 页面标题居中 -->
      <text class="page-title">历史订单</text>

      <!-- 右上角留空 -->
      <view class="nav-right"></view>
    </view>

    <!-- 主要内容区域 -->
    <view class="content-wrapper">
      <!-- 订单列表 -->
      <view v-if="orderList.length > 0" class="order-list">
        <view v-for="order in orderList" :key="order.id" class="order-item">
          <view class="order-header">
            <text class="order-id">订单号: {{ order.order_no }}</text>
            <text class="order-status" :class="getOrderStatusClass(order.status)">{{ getOrderStatusText(order.status) }}</text>
          </view>

          <view class="order-addresses">
            <view class="address-item">
              <text class="address-icon">📍</text>
              <view class="address-info">
                <text class="address-label">取货地址</text>
                <text class="address-text">{{ order.pickup_address.detail }}</text>
                <text class="contact-info">联系人: {{ order.pickup_contact.name }} {{ order.pickup_contact.phone }}</text>
              </view>
            </view>

            <view class="address-line"></view>

            <view class="address-item">
              <text class="address-icon">🏠</text>
              <view class="address-info">
                <text class="address-label">收货地址</text>
                <text class="address-text">{{ order.delivery_address.detail }}</text>
                <text class="contact-info">联系人: {{ order.delivery_contact.name }} {{ order.delivery_contact.phone }}</text>
              </view>
            </view>
          </view>

          <view class="order-details">
            <text class="order-info">配送费: ¥{{ order.delivery_fee }}</text>
            <text class="order-info">预计距离: {{ order.distance }}km</text>
            <text class="order-info">预计时间: {{ order.estimated_time }}分钟</text>
          </view>

          <view class="order-actions">
            <button class="action-btn primary" @click="viewOrderDetail(order.id, order.status)">查看详情</button>
          </view>
        </view>
      </view>

      <!-- 无数据时显示 -->
      <view v-else class="no-data">
        <text class="no-data-text">暂无历史订单</text>
      </view>
    </view>
  </view>
</template>

<script lang="uts">
import type { DeliveryTaskType } from '@/types/mall-types.uts'

export default {
  data() {
    return {
      // 模拟历史订单数据
      orderList: [] as Array<DeliveryTaskType>
    }
  },

  onLoad() {
    this.loadOrderHistory()
  },

  onShow() {
    // 页面每次显示时都检查是否有新的已完成订单
    this.checkForNewCompletedOrder()
  },

  methods: {
    // 检查是否有新的已完成订单
    checkForNewCompletedOrder() {
      const completedOrderFromStorage = uni.getStorageSync('completed_order_for_history')
      if (completedOrderFromStorage) {
        // 如果有，将其添加到订单列表的开头
        // 检查是否已经存在于列表中，避免重复添加
        const exists = this.orderList.some(order => order.id === completedOrderFromStorage.id)
        if (!exists) {
          this.orderList.unshift(completedOrderFromStorage)
        }
        // 清除本地存储，防止下次进入页面时重复添加
        uni.removeStorageSync('completed_order_for_history')
      }
    },

    // 加载历史订单
    loadOrderHistory() {
      // TODO: 调用API获取历史订单列表
      this.orderList = [
        {
          id: '1',
          order_no: 'D202501081234',
          status: 4, // 已接取
          pickup_address: {
            detail: '深圳公司',
            area: '购物公园'
          },
          delivery_address: {
            detail: '梅州',
            area: '海岸城'
          },
          pickup_contact: {
            name: '商家联系人',
            phone: '138****6567'
          },
          delivery_contact: {
            name: '张先生',
            phone: '139****9786'
          },
          delivery_fee: 12.0,
          distance: 8.2,
          estimated_time: 25,
          created_at: '2025-01-08T15:00:00Z'
        },
        {
          id: '2',
          order_no: 'D202501081235',
          status: 5, // 已完成
          pickup_address: {
            detail: '福田区购物公园',
            area: '购物公园'
          },
          delivery_address: {
            detail: '南山区海岸城',
            area: '海岸城'
          },
          pickup_contact: {
            name: '商家联系人',
            phone: '138****5678'
          },
          delivery_contact: {
            name: '张先生',
            phone: '139****1234'
          },
          delivery_fee: 12.0,
          distance: 8.2,
          estimated_time: 25,
          created_at: '2025-01-08T15:00:00Z'
        },
        {
          id: '3',
          order_no: 'D202501081236',
          status: 1, // 取货中
          pickup_address: {
            detail: '罗湖区东门步行街',
            area: '罗湖'
          },
          delivery_address: {
            detail: '福田区市民中心',
            area: '福田'
          },
          pickup_contact: {
            name: '商家联系人',
            phone: '138****5678'
          },
          delivery_contact: {
            name: '李先生',
            phone: '139****5678'
          },
          delivery_fee: 10.0,
          distance: 5.0,
          estimated_time: 15,
          created_at: '2025-01-08T16:00:00Z'
        }
      ]

      // 检查是否有新完成的订单（在加载初始数据后）
      this.checkForNewCompletedOrder()
    },

    // 获取订单状态样式
    getOrderStatusClass(status: number): string {
      switch (status) {
        case 1: return 'status-pending'
        case 2: return 'status-accepted'
        case 3: return 'status-picking'
        case 4: return 'status-picked' // 已取货
        case 5: return 'status-delivered' // 已送达
        default: return 'status-default'
      }
    },

    // 获取订单状态文本
    getOrderStatusText(status: number): string {
      switch (status) {
        case 1: return '待接取'
        case 2: return '已接取'
        case 3: return '取货中'
        case 4: return '已取货'
        case 5: return '已完成'
        default: return '未知状态'
      }
    },

    // 查看订单详情
    viewOrderDetail(orderId: string, status: number) {
      uni.navigateTo({
        url: `/pages/mall/delivery/order-detail?id=${orderId}&status=${status}`
      })
    },

    // 返回主页
    goBackToHome() {
      uni.reLaunch({
        url: '/pages/mall/delivery/index'
      })
    }
  }
}
</script>

<style scoped>
.order-history-container {
  background-color: #f5f5f5;
  min-height: 100vh;
  padding: 20rpx 30rpx;
}

.page-header {
  background-color: #fff;
  padding: 20rpx 30rpx;
  border-bottom: 1rpx solid #e9ecef;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.05);
  position: relative;
  min-height: 80rpx; /* 确保有足够空间放垂直排列的按钮和标题 */
}

.nav-left {
  position: absolute;
  top: 20rpx;
  left: 30rpx;
  display: flex;
  flex-direction: column;
  align-items: center;
  cursor: pointer;
  padding: 10rpx;
  border-radius: 8rpx;
  transition: background-color 0.2s ease;
}

.nav-left:hover {
  background-color: #f0f0f0; /* 悬停效果 */
}

.nav-left:active {
  background-color: #e0e0e0; /* 点击效果 */
}

.nav-icon {
  font-size: 36rpx;
  color: #333;
  margin-bottom: 5rpx;
}

.nav-title {
  font-size: 28rpx;
  color: #333;
  font-weight: 500;
  text-align: center;
}

.page-title {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
  text-align: center;
  margin-top: 20rpx; /* 与 nav-left 保持一定距离 */
}

.nav-right {
  /* 为了保持左右对齐，右侧需要一个占位元素 */
  width: 1rpx;
  height: 1rpx;
}

.content-wrapper {
  margin-top: 20rpx;
}

.order-list {
  display: flex;
  flex-direction: column;
  gap: 20rpx;
}

.order-item {
  background-color: #fff;
  border-radius: 16rpx;
  padding: 20rpx;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.05);
  border-left: 6rpx solid #74b9ff;
}

.order-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20rpx;
  padding-bottom: 15rpx;
  border-bottom: 1rpx solid #f8f9fa;
}

.order-id {
  font-size: 28rpx;
  font-weight: bold;
  color: #333;
}

.order-status {
  font-size: 24rpx;
  padding: 6rpx 12rpx;
  border-radius: 20rpx;
  font-weight: 500;
}

.status-pending {
  background-color: #E3F2FD;
  color: #1976D2;
}

.status-accepted {
  background-color: #FFF3E0;
  color: #F57C00;
}

.status-picking {
  background-color: #FFF3E0;
  color: #F57C00;
}

.status-picked { 
  background-color: #E8F5E8;
  color: #388E3C;
}

.status-delivered { 
  background-color: #E8F5E8;
  color: #388E3C;
}

.status-default {
  background-color: #F8F8F8;
  color: #666;
}

.order-addresses {
  margin-bottom: 20rpx;
}

.address-item {
  display: flex;
  align-items: flex-start;
  margin-bottom: 15rpx;
}

.address-icon {
  font-size: 28rpx;
  margin-right: 15rpx;
  margin-top: 5rpx;
}

.address-info {
  display: flex;
  flex-direction: column;
  flex: 1;
}

.address-label {
  font-size: 24rpx;
  color: #666;
  margin-bottom: 8rpx;
}

.address-text {
  font-size: 28rpx;
  color: #333;
  margin-bottom: 8rpx;
}

.contact-info {
  font-size: 24rpx;
  color: #666;
}

.address-line {
  width: 2rpx;
  height: 30rpx;
  background-color: #ddd;
  margin: 10rpx 0 10rpx 14rpx;
}

.order-details {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20rpx;
  padding: 15rpx;
  background-color: #f8f9fa;
  border-radius: 8rpx;
  font-size: 24rpx;
  color: #666;
}

.order-info {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  font-size: 24rpx;
  color: #666;
  margin: 0 5rpx;
}

.order-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10rpx;
}

.action-btn {
  flex: 1;
  height: 80rpx;
  border-radius: 8rpx;
  font-size: 28rpx;
  border: none;
  font-weight: 500;
  padding: 0 10rpx;
  box-sizing: border-box;
}

.primary {
  background-color: #4CAF50;
  color: #fff;
}

.secondary {
  background-color: #f0f0f0;
  color: #333;
  border: 1rpx solid #ddd;
}

.no-data {
  text-align: center;
  padding: 80rpx 30rpx;
  border-radius: 16rpx;
  background-color: #fff;
}

.no-data-text {
  font-size: 32rpx;
  color: #999;
  margin-bottom: 15rpx;
}
</style>