<template>
  <view class="order-container">
    <!-- 状态切换 -->
    <view class="tab-bar">
      <view 
        v-for="(tab, index) in OrderStatusTabs" 
        :key="index"
        class="tab-item"
        :class="{ active: currentTab === index }"
        @click="switchTab(index)"
      >
        {{ tab.text }}
      </view>
    </view>

    <!-- 订单列表 -->
    <view class="order-list">
      <view class="order-item" v-for="(order, index) in orderList" :key="index">
        <view class="order-header">
          <text class="order-no">订单号：{{ order.orderNo }}</text>
          <text class="order-status" :class="OrderStatusMap[order.status]?.class">{{ OrderStatusMap[order.status]?.text }}</text>
          <navigator v-if="order.status === 1" :url="`/pages/return/index?deviceId=${order.deviceId}&orderId=${order.orderNo}`" class="return-btn">归还设备</navigator>
        </view>
        <view class="order-content">
          <view class="device-info">
            <text class="device-name">共享风扇</text>
            <text class="device-id">设备号：{{ order.deviceId }}</text>
          </view>
          <view class="time-info">
            <view class="time-item">
              <text class="label">开始时间：</text>
              <text class="value">{{ order.startTime }}</text>
            </view>
            <view class="time-item">
              <text class="label">结束时间：</text>
              <text class="value">{{ order.endTime || '-' }}</text>
            </view>
          </view>
          <view class="price-info">
            <text class="amount">￥{{ order.amount }}</text>
          </view>
        </view>
      </view>
    </view>

    <!-- 无数据提示 -->
    <view class="empty-tip" v-if="orderList.length === 0">
      <view class="empty-icon"></view>
      <text>暂无订单记录</text>
    </view>
  </view>
</template>

<script>
import {getOrderList, queryById} from '../../config/user.js'
import { OrderStatusMap, OrderStatusTabs } from '../../constants/orderStatus.js'

export default {
  data() {
    return {
      currentTab: 0,
      OrderStatusMap,
      OrderStatusTabs,
      orderList: []
    }
  },
  async onLoad(options) {
    // 如果有传入orderId参数，说明是从设备租借页面跳转过来的
    if (options && options.orderId) {
      try {
        // 获取特定订单信息
        const res = await queryById(options.orderId);
        if (res.code === 200 && res.data) {
          // 将获取到的订单添加到列表中
          const orderData = res.data;
          // 格式化订单数据
          const formattedOrder = {
            orderNo: orderData.orderId,
            status: orderData.orderStatus,
            deviceId: orderData.deviceNo,
            startTime: orderData.createTime,
            endTime: orderData.endTime || '',
            amount: orderData.amount || '0.00'
          };
          
          // 将订单添加到列表开头
          this.orderList = [formattedOrder, ...this.orderList];
          
          // 根据订单状态切换到对应标签
          const tabIndex = this.OrderStatusTabs.findIndex(tab => 
            tab.status.includes(orderData.orderStatus)
          );
          if (tabIndex !== -1) {
            this.switchTab(tabIndex);
          }
        }
      } catch (error) {
        console.error('获取订单详情失败:', error);
      }
    }
    
    // 获取订单列表
    await this.getOrderList();
  },
  methods: {
    async getOrderList(statusList = []) {
      try {
        const res = await getOrderList(statusList);
        if (res.code === 200 && res.data && res.data.records) {
          // 处理订单列表数据
          this.orderList = res.data.records.map(item => ({
            orderNo: item.orderId,
            status: item.orderStatus,
            deviceId: item.deviceNo,
            startTime: item.createTime,
            endTime: item.endTime || '',
            amount: item.amount || '0.00'
          }));
        }
      } catch (error) {
        console.error('获取订单列表失败:', error);
        uni.showToast({
          title: '获取订单列表失败',
          icon: 'none'
        });
      }
    },
    async switchTab(index) {
      this.currentTab = index;
      // 根据状态获取订单列表
      const statusList = this.OrderStatusTabs[index].status;
      await this.getOrderList(statusList);
    }
  }
}
</script>

<style lang="scss" scoped>
.order-container {
  min-height: 100vh;
  background: #f8f8f8;

  .tab-bar {
    display: flex;
    background: #fff;
    padding: 20rpx 0;
    position: sticky;
    top: 0;
    z-index: 100;
    box-shadow: 0 2rpx 10rpx rgba(0,0,0,0.05);

    .tab-item {
      flex: 1;
      text-align: center;
      font-size: 28rpx;
      color: #666;
      position: relative;
      padding: 20rpx 0;

      &.active {
        color: #1976D2;
        font-weight: 500;

        &::after {
          content: '';
          position: absolute;
          bottom: 0;
          left: 50%;
          transform: translateX(-50%);
          width: 40rpx;
          height: 4rpx;
          background: #1976D2;
          border-radius: 2rpx;
        }
      }
    }
  }

  .order-list {
    padding: 20rpx;

    .order-item {
      background: #fff;
      border-radius: 20rpx;
      margin-bottom: 20rpx;
      padding: 30rpx;
      box-shadow: 0 4rpx 16rpx rgba(0,0,0,0.04);

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

        .order-no {
          font-size: 26rpx;
          color: #666;
        }

        .order-status {
          font-size: 26rpx;
          
          &.status-waiting {
            color: #FF9800;
          }

          &.status-progress {
            color: #2196F3;
          }

          &.status-success {
            color: #4CAF50;
          }

          &.status-using {
            color: #1976D2;
          }

          &.status-failed {
            color: #F44336;
          }

          &.status-cancelled {
            color: #9E9E9E;
          }

          &.status-finished {
            color: #4CAF50;
          }
        }
      }

      .order-content {
        padding-top: 20rpx;

        .device-info {
          margin-bottom: 20rpx;

          .device-name {
            font-size: 32rpx;
            color: #333;
            font-weight: 500;
            margin-right: 20rpx;
          }

          .device-id {
            font-size: 26rpx;
            color: #999;
          }
        }

        .time-info {
          .time-item {
            font-size: 26rpx;
            color: #666;
            margin-bottom: 10rpx;

            .label {
              color: #999;
            }
          }
        }

        .price-info {
          text-align: right;
          margin-top: 20rpx;

          .amount {
            font-size: 36rpx;
            color: #FF9800;
            font-weight: 500;
          }
        }
      }
    }
  }

  .empty-tip {
    padding: 100rpx 0;
    text-align: center;
    color: #999;
    font-size: 28rpx;

    .empty-icon {
      width: 200rpx;
      height: 200rpx;
      margin: 0 auto 20rpx;
      background: #f0f0f0;
      border-radius: 50%;
    }
  }
}
</style>