<template>
  <view class="order-detail-container">
    <!-- 顶部导航栏 -->
    <view class="nav-bar" :style="{paddingTop: statusBarHeight + 'px', height: (statusBarHeight + 44) + 'px'}">
      <uni-icons type="left" @tap="goBack" size="24" color="#222" />
      <text class="nav-title">订单详情</text>
      <view class="nav-icons">
        <!-- <uni-icons type="more" size="24" color="#222" /> -->
      </view>
    </view>

    <view class="main-content" :style="{ paddingTop: (statusBarHeight + 44 + 30) + 'px' }">
      <!-- 回收信息卡片（高保真，参考流程卡片） -->
      <view class="info-card process-card">
        <view v-if="order.cancelReason" class="cancel-reason">{{ order.cancelReason }}</view>
        <view class="info-card-header">
          <view class="info-title-wrap">
          <text class="info-title">回收信息</text>
            <view v-if="order.statusText === '不包邮'" class="tag-nobaoyou">不包邮</view>
          </view>
          <view class="status-tag" :class="order.statusClass" style="margin-left:auto;">{{ order.statusLabel }}</view>
        </view>
        <!-- 步骤条（横向卡片式） -->
        <view class="steps-bar">
          <view v-for="(step, idx) in steps" :key="step.text" :class="['step-item', {active: idx === currentStep}]">
            <image :src="step.icon" class="step-icon" />
            <view class="step-label" :class="{active: idx === currentStep}">
              <text class="step-num">{{ ['①','②','③','④'][idx] }}</text>
            <text class="step-text">{{ step.text }}</text>
            </view>
          </view>
        </view>
        <!-- 订单基础信息（左对齐，分隔线，按钮、箭头、标签等细节） -->
        <view class="base-info">
          <view v-for="(item, i) in baseInfo" :key="item.label" class="base-info-row">
            <view class="base-label-wrap">
            <text class="base-label">{{ item.label }}</text>
            </view>
            <view class="base-value-wrap">
              <text class="base-value">{{ item.value }}</text>
              <text v-if="item.copy" class="copy-btn" @tap="copyText(item.value)">复制</text>
            <uni-icons v-if="item.arrow" type="right" size="18" color="#bbb" />
            </view>
            <view v-if="i < baseInfo.length-1" class="divider"></view>
          </view>
        </view>
      </view>

      <!-- 订单明细卡片 -->
      <view class="info-card detail-card">
        <view class="card-header-bg">
          <view class="card-title">订单详情</view>
        </view>
        <view class="detail-content">
          <template v-if="order.status === 2">
            <view class="detail-row">
              <text class="detail-label">订单编号</text>
              <text class="detail-value">{{ order.orderNo }}</text>
            </view>
            <view class="detail-row">
              <text class="detail-label">合格结算</text>
              <text class="detail-value orange">¥ {{ order.settleAmount || order.estimate }}</text>
            </view>
            <view class="detail-row">
              <text class="detail-label">运费扣除</text>
              <text class="detail-value">¥ {{ order.freight || 0 }}</text>
            </view>
            <view class="detail-row total-row">
              <text class="detail-label">结算金额</text>
              <text class="detail-value total">¥ {{ order.settleAmount || order.estimate }}</text>
            </view>
            <view class="divider"></view>
          </template>
        <view v-for="item in order.items" :key="item.name" class="goods-row">
          <image :src="item.img" class="goods-img" />
          <view class="goods-info">
            <text class="goods-name">{{ item.name }}</text>
            <text class="goods-desc">{{ item.desc }}</text>
            <view class="goods-price-row">
              <text class="goods-price">¥ {{ item.price }} <text class="goods-unit">/件</text></text>
              <text class="goods-count">x{{ item.count }}</text>
              <text class="goods-total">¥{{ item.total }}</text>
            </view>
          </view>
          </view>
        </view>
      </view>

      <!-- 质检结果卡片 -->
      <view v-if="order.status === 2" class="inspect-card">
        <view class="inspect-header">
          <text class="inspect-title">质检结果</text>
        </view>
        <view class="inspect-content">
          <view v-for="(item, i) in qcInfo" :key="item.label" class="inspect-row">
            <text class="inspect-label">{{ item.label }}</text>
            <text class="inspect-value">{{ item.value }}</text>
            <view v-if="i < qcInfo.length-1" class="inspect-divider"></view>
          </view>
          <view class="inspect-btn" @tap="goToInspection">点此查看质检报告详情</view>
        </view>
      </view>
    </view>

    <!-- 底部操作按钮 -->
    <view v-if="order.status === 0" class="footer-btns">
      <button class="btn-outline">驳回</button>
      <button class="btn-main">通过</button>
    </view>
    <view v-else-if="order.status === 1" class="footer-btns">
      <button class="btn-main" @tap="goToInspect">开始质检</button>
    </view>
  </view>
</template>

<script>
import pullRefreshMixin from '../mixins/pullRefreshMixin.js'
export default {
  mixins: [pullRefreshMixin],
  data() {
    return {
      statusBarHeight: 0,
      order: {
        status: 3, // 0:已预约, 1:待质检, 2:已结款, 3:已驳回, 4:已取消
        statusText: '已结款',
        statusLabel: '已结款',
        statusClass: 'settled',
        cancelReason: '', // 如有取消原因
        estimate: '73.6~75.8',
        qualityTime: '2025-03-20 11:00',
        orderNo: 'RE82738127861525',
        userName: '周小艺',
        userPhone: '13877881234',
        address: '海南省海口市秀英区秀英街道5单元183室',
        items: [
          { name: '羽绒服', desc: '允许脏破烂，160码以上', price: 8, count: 8, total: 64, img: '/static/coat1.png' },
          { name: '品牌羽绒服', desc: '允许脏破烂，160码以上', price: 10, count: 8, total: 8, img: '/static/coat2.png' }
        ]
      },
      steps: [],
      currentStep: 3, // 第4步高亮
      baseInfo: [
        // { label: '取消原因', value: this.order.cancelReason, show: !!this.order.cancelReason },
        { label: '订单编号', value: 'RE82738127861525', copy: true },
        { label: '用户名', value: '周小艺', arrow: true },
        { label: '用户电话', value: this.maskPhone('13877881234'), copy: true },
        { label: '取件地址', value: '海南省海口市秀英区秀英街道5单元183室' },
        { label: '质检完成时间', value: '2025-03-20 11:00' }
      ],
      qcInfo: [
        { label: '质检数量', value: '9 件' },
        { label: '质检合格', value: '7 件' },
        { label: '质量问题', value: '2 件' },
        { label: '不可回收', value: '0 件' },
        { label: '订单重量', value: '2.85 kg' }
      ]
    }
  },
  onLoad() {
    this.statusBarHeight = uni.getSystemInfoSync().statusBarHeight
    // 接收从订单列表页面传递过来的订单详情数据
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.on('orderDetail', (data) => {
      this.order = data
      this.currentStep = this.getCurrentStep(data.status)
      console.log(this.order);
    })
    this.getAreaList();
  },
  methods: {
    async onRefresh() {
      await this.getAreaList()
      // 如有订单详情接口，也可在此处await
    },
    goBack() {
      uni.navigateBack()
    },
    copyText(text) {
      uni.setClipboardData({ data: text })
    },
    maskPhone(phone) {
      if (!phone) return '';
      return phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2');
    },
    getCurrentStep(status) {
      // 0:已预约, 1:待质检, 2:已结款, 3:已驳回, 4:已取消
      switch (status) {
        case 0: // 已预约
          return 0;
        case 1: // 待质检
        case 3: // 已驳回
          return 2;
        case 2: // 已结款
          return 3;
        case 4: // 已取消
          return 1;
        default:
          return 0;
      }
    },
    goToInspect() {
      uni.navigateTo({
        url: '/pages/manager/inspect'
      })
    },
    getAreaList() {
      this.$api('getAreaList', {}, (res) => {
        if (res.code == 200 && Array.isArray(res.result)) {
          // 按sort升序排序
          const sorted = res.result.slice().sort((a, b) => a.sort - b.sort)
          this.steps = sorted.map(item => ({ text: item.title, icon: item.image }))
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
$order-card-radius: 32px;
$order-card-padding: 40px 28px;
.order-detail-container {
  background: #f8f8f8;
  min-height: 100vh;
  font-family: 'PingFang SC', 'Microsoft YaHei', Arial, sans-serif;
  display: flex;
  flex-direction: column;
  align-items: center;
  .nav-bar {
    position: fixed;
    top: 0; left: 0; right: 0;
    z-index: 100;
    background: #fff;
    display: flex;
    align-items: center;
    justify-content: space-between;
    .nav-title { flex: 1; text-align: center; font-size: 18px; font-weight: bold; }
    .nav-icons { display: flex; align-items: center; gap: 16px; }
  }
  .main-content {
    width: 100vw;
    min-width: 0;
    box-sizing: border-box;
    padding: 0 16px 60px 16px;
    display: flex;
    flex-direction: column;
    align-items: center;
  }
  .info-card {
    width: 100%;
    max-width: 375px;
    min-width: 0;
    box-sizing: border-box;
    margin: 0 auto 28px auto;
    padding: 16px 8px;
    background: #fff;
    border-radius: 20px;
    box-shadow: 0 2px 12px rgba(0,0,0,0.04);
    // color: blue;
    &.process-card {
      position: relative;
      .status-tag {
        position: absolute;
        right: 18px;
        top: 18px;
        font-size: 13px;
        border-radius: 12px;
        padding: 2px 10px;
        font-weight: 500;
        background: #e6f9e6;
        color: #1ecb1e;
        height: 22px;
        line-height: 18px;
        display: flex;
        align-items: center;
        &.orange {
          background: #fff7e6;
          color: #ffb400;
          font-size: 14px;
          border-radius: 12px;
          padding: 2px 14px;
          font-weight: 400;
          height: 22px;
          display: flex;
          align-items: center;
        }
        &.gray {
          background: #f5f5f5;
          color: #444;
          font-size: 14px;
          border-radius: 12px;
          padding: 2px 14px;
          font-weight: 400;
          height: 22px;
          display: flex;
          align-items: center;
        }
        &.blue {
          background: #eaf6ff;
          color: #2a9cfb;
          font-size: 14px;
          border-radius: 12px;
          padding: 2px 14px;
          font-weight: 400;
          height: 22px;
          display: flex;
          align-items: center;
        }
      }
      .status-tag.red {
        background: #fff0f0;
        color: #ff4d4f;
      }
    }
    .info-card-header {
      display: flex; align-items: center; justify-content: flex-start;
      .info-title-wrap {
        display: flex;
        align-items: center;
        .info-title { font-size: 18px; font-weight: bold; color: #222; }
        .tag-nobaoyou {
          margin-left: 8px;
          background: #fff0f0;
          color: #ff4d4f;
          font-size: 14px;
          border-radius: 12px;
          padding: 2px 14px;
          font-weight: 400;
          height: 22px;
          display: flex;
          align-items: center;
        }
      }
      .status-tag { margin-left: auto; }
    }
    .steps-bar {
      display: flex;
      justify-content: flex-start;
      align-items: flex-start;
      margin: 24px 0 24px 0;
      gap: 14px;
      .step-item {
        background: #fff8ea;
        border-radius: 16px;
        width: 76px;
        height: 76px;
        display: flex;
        flex-direction: column;
        align-items: center;
        box-shadow: 0 2px 8px rgba(255, 156, 0, 0.04);
        overflow: hidden;
        position: relative;
        .step-icon { width: 44px; height: 44px; margin: 6px 0 0 0; }
        .step-label {
          width: 100%; height: 28px; background: transparent; position: absolute; left: 0; bottom: 0; border-radius: 0 0 12px 12px; display: flex; align-items: center; justify-content: center; transition: background 0.2s; overflow: hidden;
          line-height: 1;
          .step-num, .step-text {
            font-size: 11px;
            color: #9b9b9b;
            font-weight: 600;
            line-height: 1;
            vertical-align: middle;
          }
          .step-num { margin-right: 2px; }
        }
        &.active .step-label { background: linear-gradient(90deg, #ffd01e 0%, #ff8917 100%); }
        &.active .step-num, &.active .step-text { color: #fff; font-weight: bold; }
      }
    }
    .base-info {
      margin-top: 18px;
      .base-info-row {
        display: flex;
        align-items: flex-start;
        padding: 0 0 0 0;
        position: relative;
        min-height: 36px;
        justify-content: space-between;
        .base-label-wrap { min-width: 80px; text-align: left;  color: #8b8b8b; font-size: 13px;}
        .base-value-wrap { display: flex; align-items: center; flex: 1; justify-content: flex-end; }
        .base-value { color: #222; font-size: 15px; font-weight: 600; word-break: break-all; text-align: right; }
        .copy-btn { color: #ffb400; font-size: 13px; margin-left: 6px; font-weight: 400; padding: 1px 6px; border-radius: 6px; transition: background 0.2s; }
        .copy-btn:active { background: #fff7e6; }
        .divider { position: absolute; left: 16px; right: 16px; bottom: -1px; height: 1px; background: #f0f0f0; }
        uni-icons { margin-left: 6px; }
      }
      .base-info-row .base-value-wrap { flex-wrap: wrap; }
      .base-info-row .base-value { white-space: pre-line; line-height: 1.5; }
    }
    &.detail-card {
      position: relative;
      padding: 0;
      background: transparent;
      .card-header-bg {
        height: 56px;
        background: linear-gradient(180deg, #fff7e6 0%, #fff 100%);
        border-radius: 20px 20px 0 0;
        display: flex;
        align-items: flex-end;
        padding-left: 20px;
        padding-top: 12px;
      }
      .detail-content {
        background: #fff;
        border-radius: 0 0 20px 20px;
        padding: 20px 16px 16px 16px;
      }
      .detail-row {
        display: flex;
        align-items: center;
        min-height: 32px;
        .detail-label {
          color: #bcbcbc;
          font-size: 14px;
          width: 90px;
          flex-shrink: 0;
        }
        .detail-value {
          color: #222;
          font-size: 15px;
          font-weight: 500;
          text-align: left;
          margin-left: 0;
          flex: 1;
        }
        &.total-row .detail-value.total {
          color: #ffb400;
          font-size: 20px;
          font-weight: bold;
          text-align: right;
          flex: unset;
          margin-left: auto;
          min-width: 100px;
        }
        .orange { color: #ffb400; }
      }
      .divider { height: 1px; background: #f0f0f0; margin: 16px 0; }
      .goods-row { display: flex; align-items: flex-start; margin-bottom: 18px; }
      .goods-img { width: 48px; height: 48px; border-radius: 12px; margin-right: 12px; }
      .goods-info { flex: 1; }
      .goods-name { font-size: 15px; font-weight: bold; color: #222; margin-bottom: 2px; }
      .goods-desc { font-size: 13px; color: #bbb; margin-bottom: 8px; }
      .goods-price-row { display: flex; align-items: center; }
      .goods-price { color: #ffb400; font-size: 15px; font-weight: bold; margin-right: 8px; }
      .goods-unit { font-size: 13px; color: #bbb; }
      .goods-count { color: #bbb; font-size: 13px; margin-left: 8px; }
      .goods-total { color: #222; font-size: 15px; font-weight: bold; margin-left: auto; }
    }
  }
  .footer-btns {
    position: fixed; left: 0; right: 0; bottom: 0; background: #fff; display: flex; gap: 16px; padding: 16px 24px 32px 24px; z-index: 101;
    .btn-outline {
      flex: 1;
      height: 40px;
      border-radius: 16px;
      border: 1px solid #ffe09a;
      color: #ffb400;
      background: #fff0d2;
      font-size: 15px;
      font-weight: 500;
      box-shadow: none;
      padding: 0 18px;
    }
    .btn-main {
      flex: 1;
      height: 40px;
      border-radius: 16px;
      background: linear-gradient(90deg, #ffbe3d 0%, #ffac04 100%);
      color: #fff;
      border: none;
      font-size: 15px;
      font-weight: 500;
      box-shadow: none;
      padding: 0 18px;
    }
  }
  .cancel-reason {
    color: #bbb;
    font-size: 13px;
    margin-bottom: 10px;
    padding-left: 2px;
  }
  .inspect-card {
    max-width: 335px;
    width: 100%;
    margin: 0 auto 28px auto;
    border-radius: 24px;
    overflow: hidden;
    .inspect-header {
      height: 60px;
      background: linear-gradient(180deg, #fff7e6 0%, #fffbe6 100%);
      border-radius: 24px 24px 0 0;
      display: flex;
      align-items: flex-end;
      padding-left: 24px;
    }
    .inspect-title {
      font-size: 20px;
      font-weight: bold;
      color: #222;
      line-height: 60px;
    }
    .inspect-content {
      background: #fff;
      border-radius: 0 0 24px 24px;
      padding: 24px 24px 20px 24px;
    }
    .inspect-row {
      display: flex; align-items: center; justify-content: space-between;
      min-height: 40px;
      .inspect-label { color: #bcbcbc; font-size: 16px; }
      .inspect-value { color: #222; font-size: 16px; font-weight: 500; }
      .inspect-divider { height: 1px; background: #f0f0f0; margin: 0; width: 100%; position: absolute; left: 0; bottom: 0; }
      position: relative;
    }
    .inspect-btn {
      width: 100%;
      margin: 24px 0 0 0;
      height: 48px;
      border-radius: 24px;
      border: 1.5px solid #ffb400;
      color: #ffb400;
      background: #fff0d2;
      font-size: 18px;
      font-weight: 500;
      box-shadow: none;
      text-align: center;
      line-height: 48px;
    }
  }
  .card-title {
    font-size: 20px;
    font-weight: bold;
    color: #222;
    margin-bottom: 12px;
  }
}
</style> 