<template>
  <view class="header">
    <view class="order" v-for="(item,index) in orderItem" :key="index">
      <!-- 取餐号/图片切换显示 -->
      <view class="order-number-container" v-if="item.pickupMethod !== 2 || !item.imageAddress">
        <view class="order-number">取餐号</view>
        <view class="order-number-value">#{{item?.id}}</view>
      </view>
      
      <!-- 跑腿订单图片显示 -->
      <view class="image-container" v-if="item.pickupMethod === 2 && item.imageAddress">
        <view class="Lookbackground">
          <image :src="'http://'+item.imageAddress" mode="aspectFit"></image>
        </view>
        <text class="preview-text" @click="previewImage(item.imageAddress)">点击查看大图</text>
      </view>

      <!-- 动态步骤条 -->
      <uni-steps 
        :options="item.pickupMethod === 2 ? stepsForDelivery : stepsForSelfPickup"
        :active="calculateActive(item)"
        class="step-item"
      />
    </view>
  </view>
</template>

<script setup>
import { ref, reactive,onMounted } from 'vue';
import { apiImageUrl } from '../../API/api';
import { onLoad, onUnload } from '@dcloudio/uni-app'; 
const active = ref(0)
const orderItem = ref([]);

onUnload(() => {
  uni.reLaunch({
    url: '/pages/index/index'
  });
});

// 步骤配置
const stepsForSelfPickup = reactive([
  { title: '备餐中' },
  { title: '已出餐' },
  { title: '已完成' }
])

const stepsForDelivery = reactive([
  { title: '备餐中' },
  { title: '已出餐' },
  { title: '送餐中' },
  { title: '已完成' }
])

// 计算激活步骤
const calculateActive = (item) => {
  if (item.pickupMethod === 2) {
    // 配送订单逻辑
    if (item.errandState === 4) return 3
    if (item.errandState === 3) return 2
    if (item.orderState === 4) return 1
    return 0
  } else {
    // 自取订单逻辑
    if (item.orderState === 4) return 1
    if (item.orderState > 4) return 2
    return 0
  }
}

// 图片预览
const previewImage = (address) => {
  uni.previewImage({
    urls: ['http://' + address],
    current: 0
  })
}

const getOrderStatus = () => {
  uni.request({
    url: apiImageUrl + '/api/orders/list/status',
    method: 'POST',
    header: {
      'Content-Type': 'application/json',
      'cookie': uni.getStorageSync("cookie") || ''
    },
    success(res) {
      orderItem.value = res.data.data.map(item => ({
        ...item,
        // 转换数字类型防止类型判断错误
        orderState: Number(item.orderState),
        errandState: Number(item.errandState)
      }))
    }
  })
}

onMounted(() => {
  getOrderStatus()
})
</script>

<style scoped>
/* 新增图片相关样式 */
.image-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-bottom: 20px;
}

.Lookbackground {
  width: 250px;
  height: 150px;
  background: #f5f5f5;
  border-radius: 8px;
  overflow: hidden;
  margin-bottom: 10px;
}

.Lookbackground image {
  width: 100%;
  height: 100%;
}

.preview-text {
  color: #4095e5;
  font-size: 14px;
  text-decoration: underline;
}

/* 原有样式保持不变 */
.order {
  padding: 20px;
  background-color: #fff;
  width: 85%;
  border-radius: 15px;
  margin: 20px auto;
}

.order-number-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-bottom: 20px;
}

.order-number {
  font-size: 24px;
  font-weight: bold;
  margin-bottom: 10px;
}

.order-number-value {
  font-size: 48px;
  font-weight: bold;
  color: #4095e5;
}
</style>