index.vue 29.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983
<!--
 * @Date: 2022-09-19 14:11:06
 * @LastEditors: hookehuyr hookehuyr@gmail.com
 * @LastEditTime: 2025-07-03 21:14:00
 * @FilePath: /jgdl/src/pages/myOrders/index.vue
 * @Description: 订单管理页面
-->
<template>
  <view class="order-management-page">
    <nut-sticky>
      <!-- 买车/卖车切换 -->
      <view id="mode-toggle" class="view-mode-toggle">
        <view class="toggle-container">
          <view class="toggle-option" :class="{ active: viewMode === 'bought' }" @click="setViewMode('bought')">
            我买的车
          </view>
          <view class="toggle-option" :class="{ active: viewMode === 'sold' }" @click="setViewMode('sold')">
            我卖的车
          </view>
        </view>
      </view>

      <!-- 状态筛选标签 -->
      <view id="status-tabs" class="status-tabs">
        <view class="tab-item" :class="{ active: activeTab === 'all' }" @click="setActiveTab('all')">
          全部
        </view>
        <view v-if="viewMode === 'bought'" class="tab-item" :class="{ active: activeTab === 'pending' }"
          @click="setActiveTab('pending')">
          待支付
        </view>
        <view class="tab-item" :class="{ active: activeTab === 'completed' }" @click="setActiveTab('completed')">
          已完成
        </view>
        <view class="tab-item" :class="{ active: activeTab === 'cancelled' }" @click="setActiveTab('cancelled')">
          已取消
        </view>
      </view>
    </nut-sticky>

    <!-- 订单列表 -->
    <view class="order-list">
      <!-- 滚动列表 -->
      <scroll-view class="order-scroll-view" :style="scrollStyle" :scroll-y="true" @scrolltolower="loadMore"
        @scroll="scroll" :lower-threshold="50" :enable-flex="false">
        <!-- 空状态 -->
        <view v-if="filteredOrders.length === 0" class="empty-state">
          <text class="empty-text">暂无订单</text>
        </view>

        <!-- 订单卡片 -->
        <view v-else>
          <view v-for="order in filteredOrders" :key="order.id" class="order-card">
            <!-- 订单头部信息 -->
            <view class="order-header">
              <text class="order-date">{{ order.date }}</text>
              <text class="order-status" :class="getStatusClass(order.status)">
                {{ getStatusText(order.status) }}
              </text>
            </view>

            <!-- 车辆信息 -->
            <nut-row :gutter="12" class="vehicle-info">
              <nut-col :span="6">
                <image :src="order.vehicle.imageUrl" :alt="order.vehicle.name" class="vehicle-image"
                  mode="aspectFill" />
              </nut-col>
              <nut-col :span="18">
                <view class="vehicle-details">
                  <text class="vehicle-name">{{ order.vehicle.name }}</text>
                  <text class="vehicle-specs">
                    {{ order.vehicle.year }} | {{ order.vehicle.mileage }}
                  </text>
                  <text class="vehicle-battery">{{ order.vehicle.batteryCapacity }}</text>
                  <text class="vehicle-price">¥{{ order.vehicle.price }}</text>
                </view>
              </nut-col>
            </nut-row>

            <!-- 操作按钮 -->
            <view class="order-actions">
              <!-- 买车模式:待支付状态 -->
              <template v-if="viewMode === 'bought' && order.status === 'pending'">
                <nut-button type="primary" size="small" @click="handlePayment(order)">
                  去支付
                </nut-button>
              </template>

              <!-- 已完成状态 -->
              <template v-if="order.status === 'completed'">
                <nut-button type="default" size="small" @click="viewOrderDetail(order.id)">
                  查看详情
                </nut-button>
                <nut-button type="primary" size="small" @click="rateOrder(order.id)" class="ml-2">
                  {{ order.review ? '查看评价' : '评价' }}
                </nut-button>
              </template>

              <!-- 已取消状态 -->
              <template v-if="order.status === 'cancelled'">
                <nut-button type="default" size="small" @click="deleteOrder(order.id)"
                  :loading="deletingOrderId === order.id" :disabled="deletingOrderId === order.id">
                  {{ deletingOrderId === order.id ? '删除中...' : '删除订单' }}
                </nut-button>
              </template>
            </view>
          </view>
        </view>

        <!-- 加载更多指示器 -->
        <view v-if="loading" class="loading-container">
          <text class="loading-text">加载中...</text>
        </view>

        <!-- 没有更多数据 -->
        <view v-if="!hasMore && filteredOrders.length > 0" class="no-more">
          <text class="no-more-text">没有更多订单了</text>
        </view>
      </scroll-view>
    </view>

    <!-- 支付组件 -->
    <payCard :visible="show_pay" :data="payData" @close="onPayClose" />

    <!-- 评价弹窗 -->
    <nut-popup v-model:visible="showRatePopup" position="right" :style="{ width: '100%', height: '100%' }" closeable
      close-icon-position="top-right" @close="closeRatePopup">
      <view class="rate-popup">
        <view class="rate-header">
          <text class="rate-title">{{ isReadOnlyMode ? '查看评价' : '商品评价' }}</text>
        </view>

        <view class="rate-content">
          <!-- 商品信息展示 -->
          <view class="product-info">
            <image :src="currentRateOrder?.vehicle?.imageUrl" class="product-image" mode="aspectFill" />
            <view class="product-details">
              <text class="product-name">{{ currentRateOrder?.vehicle?.name }}</text>
              <text class="product-specs">{{ currentRateOrder?.vehicle?.year }} · {{ currentRateOrder?.vehicle?.mileage
                }}</text>
              <text class="product-price">¥{{ currentRateOrder?.vehicle?.price }}</text>
            </view>
          </view>

          <!-- 评分组件 -->
          <view class="rate-score-section">
            <text class="score-label">{{ isReadOnlyMode ? '评分' : '请给商品评分' }}</text>
            <nut-rate v-model="rateScore" :readonly="isReadOnlyMode" :size="isReadOnlyMode ? '20' : '24'"
              active-color="#ff6b35" void-color="#e5e5e5" class="rate-stars" />
            <text v-if="isReadOnlyMode" class="score-text">{{ rateScore }}/5分</text>
          </view>

          <!-- 评价输入框 -->
          <view class="rate-input-section">
            <text class="input-label">{{ isReadOnlyMode ? '评价内容' : '请输入评价内容' }}</text>
            <view class="border border-gray-100">
              <nut-textarea v-model="rateContent" :placeholder="isReadOnlyMode ? '' : '请输入您的评价内容...'" :max-length="200"
                :rows="4" :show-word-limit="!isReadOnlyMode" :readonly="isReadOnlyMode"
                :class="{ 'readonly': isReadOnlyMode }" />
            </view>
            <text v-if="isReadOnlyMode && currentRateOrder?.review?.date" class="review-date">
              评价时间:{{ currentRateOrder?.review?.date }}
            </text>
          </view>
        </view>

        <!-- 提交按钮 -->
        <view class="rate-footer" v-if="!isReadOnlyMode">
          <nut-button type="primary" size="large" @click="submitRate" :loading="submittingRate" block>
            提交评价
          </nut-button>
        </view>
      </view>
    </nut-popup>

    <!-- 订单详情弹窗 -->
    <nut-popup v-model:visible="showOrderDetailPopup" position="right" :style="{ width: '100%', height: '100%' }"
      closeable close-icon-position="top-right" @close="closeOrderDetailPopup">
      <view class="order-detail-popup">
        <view class="detail-header">
          <text class="detail-title">订单详情</text>
        </view>

        <view class="detail-content">
          <!-- 订单基本信息 -->
          <view class="detail-section">
            <text class="section-title">订单信息</text>
            <view class="info-row">
              <text class="info-label">订单编号</text>
              <text class="info-value">{{ currentOrderDetail?.id }}</text>
            </view>
            <view class="info-row">
              <text class="info-label">下单时间</text>
              <text class="info-value">{{ currentOrderDetail?.date }}</text>
            </view>
            <view class="info-row">
              <text class="info-label">订单状态</text>
              <text class="info-value" :class="getStatusClass(currentOrderDetail?.status)">
                {{ getStatusText(currentOrderDetail?.status) }}
              </text>
            </view>
            <view class="info-row">
              <text class="info-label">订单金额</text>
              <text class="info-value price">¥{{ currentOrderDetail?.price }}</text>
            </view>
          </view>

          <!-- 商品信息 -->
          <view class="detail-section">
            <text class="section-title">商品信息</text>
            <view class="product-detail-info">
              <image :src="currentOrderDetail?.vehicle?.imageUrl" class="product-detail-image" mode="aspectFill" />
              <view class="product-detail-content">
                <text class="product-detail-name">{{ currentOrderDetail?.vehicle?.name }}</text>
                <text class="product-detail-specs">{{ currentOrderDetail?.vehicle?.year }} · {{
                  currentOrderDetail?.vehicle?.mileage }}</text>
                <text class="product-detail-battery">{{ currentOrderDetail?.vehicle?.batteryCapacity }}</text>
                <text class="product-detail-price">¥{{ currentOrderDetail?.vehicle?.price }}</text>
              </view>
            </view>
          </view>

          <!-- 交易信息 -->
          <view class="detail-section">
            <text class="section-title">交易信息</text>
            <view class="info-row">
              <text class="info-label">{{ viewMode === 'bought' ? '卖家' : '买家' }}</text>
              <text class="info-value">{{ viewMode === 'bought' ? '张先生' : '李女士' }}</text>
            </view>
            <view class="info-row">
              <text class="info-label">联系电话</text>
              <text class="info-value">{{ viewMode === 'bought' ? '138****5678' : '139****1234' }}</text>
            </view>
            <view class="info-row">
              <text class="info-label">交易地点</text>
              <text class="info-value">北京市朝阳区望京SOHO</text>
            </view>
          </view>

          <!-- 评价信息(如果有) -->
          <view class="detail-section" v-if="currentOrderDetail?.review">
            <text class="section-title">评价信息</text>
            <view class="review-info">
              <view class="review-rating">
                <text class="rating-label">评分:</text>
                <nut-rate :model-value="currentOrderDetail?.review?.rating" readonly size="20" active-color="#ff6b35"
                  void-color="#e5e5e5" />
                <text class="rating-text">{{ currentOrderDetail?.review?.rating }}/5分</text>
              </view>
              <view class="review-content">
                <text class="content-label">评价内容:</text>
                <text class="content-text">{{ currentOrderDetail?.review?.content }}</text>
              </view>
              <view class="review-time">
                <text class="time-text">评价时间:{{ currentOrderDetail?.review?.date }}</text>
              </view>
            </view>
          </view>
        </view>

        <!-- 关闭按钮 -->
        <view class="detail-footer">
          <nut-button type="primary" size="large" @click="closeOrderDetailPopup" block>
            关闭
          </nut-button>
        </view>
      </view>
    </nut-popup>
  </view>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import Taro from '@tarojs/taro'
// import { deleteOrderAPI, submitReviewAPI } from '@/api/orders' // 预留真实API调用
import './index.less'
import { $ } from '@tarojs/extend'
import payCard from '@/components/payCard.vue'
// NutUI组件已全局注册,无需单独导入Rate

const scrollStyle = ref({
  height: ''
})

// 页面状态
const activeTab = ref('all')
const viewMode = ref('bought')
const loading = ref(false)
const hasMore = ref(true)

const show_pay = ref(false)
const payData = ref({
  id: '',
  price: 0,
  remain_time: 0
})

// 评价相关状态
const showRatePopup = ref(false)
const currentRateOrder = ref(null)
const rateContent = ref('')
const rateScore = ref(5)
const submittingRate = ref(false)
const isReadOnlyMode = ref(false)

// 订单详情相关状态
const showOrderDetailPopup = ref(false)
const currentOrderDetail = ref(null)

// 模拟订单数据 - 我买的车
const boughtOrders = ref([
  {
    id: 'B001',
    price: 3999,
    date: '2023-11-15 14:30',
    status: 'pending',
    vehicle: {
      name: '小牛 U1 Pro',
      year: '2022年',
      mileage: '续航120km',
      batteryCapacity: '电池容量:1.5kWh',
      price: 3999,
      imageUrl: 'https://images.unsplash.com/photo-1558981285-6f0c94958bb6?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=60'
    }
  },
  {
    id: 'B002',
    price: 5299,
    date: '2023-10-28 09:15',
    status: 'completed',
    vehicle: {
      name: '雅迪 DE2',
      year: '2023年',
      mileage: '续航80km',
      batteryCapacity: '电池容量:1.2kWh',
      price: 5299,
      imageUrl: 'https://images.unsplash.com/photo-1571068316344-75bc76f77890?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=60'
    },
    review: {
      rating: 4,
      content: '车子质量不错,续航也够用,就是充电时间有点长。',
      date: '2023-10-30 16:20'
    }
  },
  {
    id: 'B003',
    price: 2899,
    date: '2023-09-20 11:30',
    status: 'cancelled',
    vehicle: {
      name: '台铃 小狮子',
      year: '2021年',
      mileage: '续航65km',
      batteryCapacity: '电池容量:1.0kWh',
      price: 2899,
      imageUrl: 'https://images.unsplash.com/photo-1544191696-15693072b5a5?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=60'
    }
  },
  {
    id: 'B004',
    price: 4599,
    date: '2023-08-15 15:45',
    status: 'completed',
    vehicle: {
      name: '九号 E80C',
      year: '2023年',
      mileage: '续航80km',
      batteryCapacity: '电池容量:1.44kWh',
      price: 4599,
      imageUrl: 'https://images.unsplash.com/photo-1558618666-fcd25c85cd64?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=60'
    },
    review: {
      rating: 5,
      content: '非常满意!车况很好,卖家服务态度也很棒,推荐!',
      date: '2023-08-18 10:30'
    }
  },
  {
    id: 'B005',
    price: 1899,
    date: '2023-07-22 13:20',
    status: 'pending',
    vehicle: {
      name: '绿源 MH5',
      year: '2020年',
      mileage: '续航50km',
      batteryCapacity: '电池容量:0.9kWh',
      price: 1899,
      imageUrl: 'https://images.unsplash.com/photo-1571068316344-75bc76f77890?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=60'
    }
  }
])

// 模拟订单数据 - 我卖的车
const soldOrders = ref([
  {
    id: 'S001',
    price: 3200,
    date: '2023-11-10 16:45',
    status: 'completed',
    vehicle: {
      name: '爱玛 小蜜豆',
      year: '2021年',
      mileage: '续航60km',
      batteryCapacity: '电池容量:0.8kWh',
      price: 3200,
      imageUrl: 'https://images.unsplash.com/photo-1544191696-15693072b5a5?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=60'
    },
    review: {
      rating: 5,
      content: '买家很爽快,交易很顺利,车子也很满意!',
      date: '2023-11-12 14:20'
    }
  },
  {
    id: 'S002',
    price: 2800,
    date: '2023-10-05 10:30',
    status: 'pending',
    vehicle: {
      name: '小刀 长征版',
      year: '2022年',
      mileage: '续航70km',
      batteryCapacity: '电池容量:1.1kWh',
      price: 2800,
      imageUrl: 'https://images.unsplash.com/photo-1558618666-fcd25c85cd64?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=60'
    }
  },
  {
    id: 'S003',
    price: 4200,
    date: '2023-09-18 14:15',
    status: 'cancelled',
    vehicle: {
      name: '立马 威风',
      year: '2023年',
      mileage: '续航90km',
      batteryCapacity: '电池容量:1.6kWh',
      price: 4200,
      imageUrl: 'https://images.unsplash.com/photo-1558981285-6f0c94958bb6?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=60'
    }
  },
  {
    id: 'S004',
    price: 1650,
    date: '2023-08-28 09:45',
    status: 'completed',
    vehicle: {
      name: '新日 XC1',
      year: '2020年',
      mileage: '续航45km',
      batteryCapacity: '电池容量:0.7kWh',
      price: 1650,
      imageUrl: 'https://images.unsplash.com/photo-1571068316344-75bc76f77890?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=60'
    }
  },
  {
    id: 'S005',
    price: 3800,
    date: '2023-07-12 16:20',
    status: 'completed',
    vehicle: {
      name: '哈啰 A80',
      year: '2022年',
      mileage: '续航85km',
      batteryCapacity: '电池容量:1.3kWh',
      price: 3800,
      imageUrl: 'https://images.unsplash.com/photo-1544191696-15693072b5a5?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=60'
    },
    review: {
      rating: 4,
      content: '车子成色不错,买家也很好沟通,交易愉快!',
      date: '2023-07-15 11:30'
    }
  }
])

/**
 * 根据当前视图模式和筛选条件获取过滤后的订单列表
 */
const filteredOrders = computed(() => {
  const orders = viewMode.value === 'bought' ? boughtOrders.value : soldOrders.value

  if (activeTab.value === 'all') {
    return orders
  }

  return orders.filter(order => {
    if (activeTab.value === 'pending') return order.status === 'pending'
    if (activeTab.value === 'completed') return order.status === 'completed'
    if (activeTab.value === 'cancelled') return order.status === 'cancelled'
    return true
  })
})

/**
 * 设置视图模式(买车/卖车)
 */
const setViewMode = (mode) => {
  viewMode.value = mode
  // 重置状态筛选标签到全部
  activeTab.value = 'all'
  resetListState()
  // 重置加载更多的状态
  hasMore.value = true
}

/**
 * 设置活跃的状态标签
 */
const setActiveTab = (tab) => {
  activeTab.value = tab
  // 重置列表加载状态
  resetListState()
}

/**
 * 重置列表状态
 */
const resetListState = () => {
  loading.value = false
  hasMore.value = true
  // 重置加载索引,但不重置已加载的数据
  // 这样切换标签时不会重复加载数据,但切换视图模式时会重置
}

/**
 * 滚动事件处理
 */
const scroll = (e) => {
  // 可以在这里处理滚动事件
}

// 模拟更多数据池 - 买车订单
const moreBoughtOrders = [
  {
    id: 'B006',
    price: 2299,
    date: '2023-06-18 14:30',
    status: 'completed',
    vehicle: {
      name: '小鸟 V1',
      year: '2019年',
      mileage: '续航55km',
      batteryCapacity: '电池容量:0.8kWh',
      price: 2299,
      imageUrl: 'https://images.unsplash.com/photo-1558981285-6f0c94958bb6?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=60'
    },
    review: {
      rating: 3,
      content: '车子还行,就是电池续航有点短,价格还算合理。',
      date: '2023-06-20 09:15'
    }
  },
  {
    id: 'B007',
    price: 3599,
    date: '2023-05-25 16:45',
    status: 'cancelled',
    vehicle: {
      name: '速珂 TC Max',
      year: '2022年',
      mileage: '续航100km',
      batteryCapacity: '电池容量:1.8kWh',
      price: 3599,
      imageUrl: 'https://images.unsplash.com/photo-1571068316344-75bc76f77890?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=60'
    }
  },
  {
    id: 'B008',
    price: 4899,
    date: '2023-04-12 11:20',
    status: 'pending',
    vehicle: {
      name: '小牛 NGT',
      year: '2023年',
      mileage: '续航130km',
      batteryCapacity: '电池容量:2.0kWh',
      price: 4899,
      imageUrl: 'https://images.unsplash.com/photo-1558618666-fcd25c85cd64?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=60'
    }
  }
]

// 模拟更多数据池 - 卖车订单
const moreSoldOrders = [
  {
    id: 'S006',
    price: 2100,
    date: '2023-06-08 13:15',
    status: 'completed',
    vehicle: {
      name: '绿佳 FDT',
      year: '2020年',
      mileage: '续航50km',
      batteryCapacity: '电池容量:0.9kWh',
      price: 2100,
      imageUrl: 'https://images.unsplash.com/photo-1544191696-15693072b5a5?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=60'
    }
  },
  {
    id: 'S007',
    price: 3500,
    date: '2023-05-20 10:30',
    status: 'pending',
    vehicle: {
      name: '雅迪 G5 Pro',
      year: '2022年',
      mileage: '续航85km',
      batteryCapacity: '电池容量:1.4kWh',
      price: 3500,
      imageUrl: 'https://images.unsplash.com/photo-1558981285-6f0c94958bb6?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=60'
    }
  },
  {
    id: 'S008',
    price: 1800,
    date: '2023-04-15 15:45',
    status: 'cancelled',
    vehicle: {
      name: '爱玛 麦',
      year: '2019年',
      mileage: '续航40km',
      batteryCapacity: '电池容量:0.6kWh',
      price: 1800,
      imageUrl: 'https://images.unsplash.com/photo-1571068316344-75bc76f77890?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=60'
    }
  }
]

// 记录已加载的数据索引
const loadedBoughtIndex = ref(0)
const loadedSoldIndex = ref(0)

/**
 * 加载更多数据
 */
const loadMore = () => {
  if (loading.value || !hasMore.value) return

  loading.value = true

  // 模拟加载延迟
  setTimeout(() => {
    const currentOrders = viewMode.value === 'bought' ? boughtOrders : soldOrders
    const moreOrders = viewMode.value === 'bought' ? moreBoughtOrders : moreSoldOrders
    const loadedIndex = viewMode.value === 'bought' ? loadedBoughtIndex : loadedSoldIndex

    // 每次加载2条数据
    const batchSize = 2
    const startIndex = loadedIndex.value
    const endIndex = Math.min(startIndex + batchSize, moreOrders.length)

    if (startIndex < moreOrders.length) {
      // 添加新数据
      const newOrders = moreOrders.slice(startIndex, endIndex)
      currentOrders.value.push(...newOrders)

      // 更新已加载索引
      if (viewMode.value === 'bought') {
        loadedBoughtIndex.value = endIndex
      } else {
        loadedSoldIndex.value = endIndex
      }

      // 检查是否还有更多数据
      hasMore.value = endIndex < moreOrders.length
    } else {
      // 没有更多数据了
      hasMore.value = false
    }

    loading.value = false
  }, 1000)
}

/**
 * 获取订单状态文本
 */
const getStatusText = (status) => {
  switch (status) {
    case 'pending':
      return '待支付'
    case 'completed':
      return '已完成'
    case 'cancelled':
      return '已取消'
    default:
      return '未知状态'
  }
}

/**
 * 获取订单状态样式类
 */
const getStatusClass = (status) => {
  switch (status) {
    case 'pending':
      return 'status-pending'
    case 'completed':
      return 'status-completed'
    case 'cancelled':
      return 'status-cancelled'
    default:
      return ''
  }
}

// 页面导航相关方法可以在需要时添加

/**
 * 处理支付
 */
const handlePayment = ({ id, price }) => {
  onPay({
    id,
    remain_time: 1800, // 30分钟
    price
  })
}

/**
 * 发送订单支付信息到支付组件
 * @param {Object} payInfo - 支付信息
 * @param {string} payInfo.id - 订单ID
 * @param {number} payInfo.remain_time - 剩余时间
 * @param {number} payInfo.price - 价格
 */
const onPay = ({ id, remain_time, price }) => {
  show_pay.value = true
  payData.value.id = id
  payData.value.price = price
  payData.value.remain_time = remain_time
}

/**
 * 关闭支付弹框
 */
const onPayClose = () => {
  show_pay.value = false
}

/**
 * 查看订单详情
 */
const viewOrderDetail = (orderId) => {
  // 找到对应的订单
  const orders = viewMode.value === 'bought' ? boughtOrders.value : soldOrders.value
  const order = orders.find(o => o.id === orderId)

  if (order) {
    currentOrderDetail.value = order
    showOrderDetailPopup.value = true
  }
}

/**
 * 关闭订单详情弹窗
 */
const closeOrderDetailPopup = () => {
  showOrderDetailPopup.value = false
  currentOrderDetail.value = null
}

/**
 * 评价订单
 */
const rateOrder = (orderId) => {
  // 找到对应的订单
  const orders = viewMode.value === 'bought' ? boughtOrders.value : soldOrders.value
  const order = orders.find(o => o.id === orderId)

  if (order) {
    currentRateOrder.value = order

    // 检查是否已有评价
    if (order.review) {
      // 已评价,显示只读模式
      isReadOnlyMode.value = true
      rateContent.value = order.review.content
      rateScore.value = order.review.rating
    } else {
      // 未评价,显示编辑模式
      isReadOnlyMode.value = false
      rateContent.value = ''
      rateScore.value = 5
    }

    showRatePopup.value = true
  }
}

/**
 * 关闭评价弹窗
 */
const closeRatePopup = () => {
  showRatePopup.value = false
  currentRateOrder.value = null
  rateContent.value = ''
  rateScore.value = 5
  isReadOnlyMode.value = false
}

/**
 * 提交评价
 */
const submitRate = async () => {
  if (!rateContent.value.trim()) {
    Taro.showToast({
      title: '请输入评价内容',
      icon: 'error',
      duration: 2000
    })
    return
  }

  try {
    submittingRate.value = true

    // 真实的API调用(当前注释掉,使用模拟数据)
    // const response = await submitReviewAPI({
    //   orderId: currentRateOrder.value.id,
    //   rating: rateScore.value,
    //   comment: rateContent.value
    // })
    // if (response.success) {
    //   // API提交成功后的处理
    //   const currentOrders = viewMode.value === 'bought' ? boughtOrders : soldOrders
    //   const order = currentOrders.value.find(o => o.id === currentRateOrder.value.id)
    //   if (order) {
    //     order.review = {
    //       rating: rateScore.value,
    //       content: rateContent.value,
    //       date: new Date().toLocaleString('zh-CN')
    //     }
    //     order.status = 'completed'
    //   }
    //   Toast.success(response.message || '评价提交成功')
    //   closeRatePopup()
    // } else {
    //   throw new Error(response.message || '提交失败')
    // }

    // 模拟API调用延迟(开发阶段使用)
    await new Promise(resolve => setTimeout(resolve, 1500))

    // 更新本地数据
    const currentOrders = viewMode.value === 'bought' ? boughtOrders : soldOrders
    const order = currentOrders.value.find(o => o.id === currentRateOrder.value.id)
    if (order) {
      order.review = {
        rating: rateScore.value,
        content: rateContent.value,
        date: new Date().toLocaleString('zh-CN')
      }
      order.status = 'completed'
    }

    Taro.showToast({
      title: '评价提交成功',
      icon: 'success',
      duration: 2000
    })

    closeRatePopup()

  } catch (error) {
    // console.error('提交评价失败:', error)
    Taro.showToast({
      title: error.message || '提交失败,请重试',
      icon: 'error',
      duration: 2000
    })
  } finally {
    submittingRate.value = false
  }
}

// 删除订单相关状态
const deletingOrderId = ref('')

/**
 * 删除订单
 * @param {string} orderId - 订单ID
 */
const deleteOrder = async (orderId) => {
  try {
    const result = await Taro.showModal({
      title: '确认删除',
      content: '确定要删除这个订单吗?删除后无法恢复。',
      confirmText: '删除',
      cancelText: '取消'
    })

    if (result.confirm) {
      await performDeleteOrder(orderId)
    }
  } catch (error) {
     // 用户取消删除或其他错误
     // console.log('删除操作被取消或出错:', error)
   }
}

/**
 * 执行删除订单操作
 * @param {string} orderId - 订单ID
 */
const performDeleteOrder = async (orderId) => {
  // 设置删除状态,用于显示加载效果
  deletingOrderId.value = orderId

  try {
    // 真实的API调用(当前注释掉,使用模拟数据)
    // const response = await deleteOrderAPI({ orderId })
    // if (response.success) {
    //   // API删除成功后的处理
    //   const orders = viewMode.value === 'bought' ? boughtOrders : soldOrders
    //   const orderIndex = orders.value.findIndex(order => order.id === orderId)
    //   if (orderIndex !== -1) {
    //     orders.value.splice(orderIndex, 1)
    //   }
    //   Toast.success(response.message || '订单删除成功')
    // } else {
    //   throw new Error(response.message || '删除失败')
    // }

    // 模拟API调用延迟(开发阶段使用)
    await new Promise(resolve => setTimeout(resolve, 1000))

    // 从本地数据中移除订单
    const orders = viewMode.value === 'bought' ? boughtOrders : soldOrders
    const orderIndex = orders.value.findIndex(order => order.id === orderId)

    if (orderIndex !== -1) {
      orders.value.splice(orderIndex, 1)

      Taro.showToast({
        title: '订单删除成功',
        icon: 'success',
        duration: 2000
      })
    } else {
      throw new Error('订单不存在')
    }

  } catch (error) {
    // console.error('删除订单失败:', error)

    Taro.showToast({
      title: error.message || '删除失败,请重试',
      icon: 'error',
      duration: 2000
    })
  } finally {
    // 清除删除状态
    deletingOrderId.value = ''
  }
}

// 页面加载时的初始化
onMounted(async () => {
  // TODO: 加载订单数据
  // 设置滚动列表可视高度
  const windowHeight = wx.getWindowInfo().windowHeight;
  setTimeout(async () => {
    const headerHeight = await $('#mode-toggle').height();
    const navHeight = await $('#status-tabs').height();
    scrollStyle.value = {
      height: windowHeight - headerHeight - navHeight + 'px'
    }
  }, 500);
})
</script>

<script>
export default {
  name: "OrderManagementPage",
};
</script>

<style lang="less">
/* 样式已移至 index.less 文件 */
</style>