<template>
  <view v-if="showBlock" class="express-panel">
    <view class="section-title">物流信息</view>
    <scroll-view v-if="packageList.length > 1" class="pkg-tabs" scroll-x>
      <view
        v-for="(pkg, idx) in packageList"
        :key="idx"
        class="pkg-tab"
        :class="{ active: activePkg === idx }"
        @click="activePkg = idx"
      >
        包裹{{ pkg.package_no || idx + 1 }}
        <text v-if="pkg.warehouse_name" class="pkg-sub">{{ pkg.warehouse_name }}</text>
      </view>
    </scroll-view>
    <template v-if="currentDisplay.hasData">
      <view class="row"><text>物流公司</text><text>{{ currentDisplay.companyName }}</text></view>
      <view class="row"><text>运单号</text><text>{{ currentDisplay.expressNo }}</text></view>
      <view class="row"><text>最新状态</text><text class="highlight-text">{{ currentDisplay.stateText }}</text></view>
      <view v-if="currentDisplay.tracks.length" class="timeline">
        <view class="timeline-title">物流追踪</view>
        <view v-for="(t, idx) in currentDisplay.tracks" :key="idx" class="track-item">
          <view class="track-dot" :class="{ 'active': idx === 0 }" />
          <view class="track-body">
            <text class="track-status" :class="{ 'active': idx === 0 }">{{ t.status }}</text>
            <text class="track-detail">{{ t.detail }}</text>
            <text class="track-time">{{ t.time }}</text>
          </view>
        </view>
      </view>
    </template>
    <view v-else class="empty-express">
      {{ currentPkg && Number(currentPkg.is_send) === 0 ? '本包裹尚未发货' : '暂无物流信息' }}
    </view>
  </view>
</template>

<script>
import { mapExpressDetail } from '@/subPackages/sub_business_shared/common/display.js'
import businessMixin from '@/subPackages/sub_business_shared/common/businessMixin.js'

export default {
  mixins: [businessMixin],
  name: 'ExpressTimeline',
  props: {
    express: { type: Object, default: null },
    show: { type: Boolean, default: true },
  },
  data() {
    return { activePkg: 0 }
  },
  computed: {
    showBlock() {
      return this.show
    },
    packageList() {
      const pkgs = this.express && Array.isArray(this.express.packages) ? this.express.packages : []
      if (pkgs.length) return pkgs
      if (this.express && (this.express.express_no || this.express.detail)) {
        return [{ package_no: 1, warehouse_name: '', is_send: 1, express: this.express }]
      }
      return []
    },
    currentPkg() {
      return this.packageList[this.activePkg] || null
    },
    currentDisplay() {
      const pkg = this.currentPkg
      if (pkg && pkg.express) return mapExpressDetail(pkg.express)
      if (pkg && Number(pkg.is_send) === 0) {
        return { hasData: false, companyName: '', expressNo: '', stateText: '', tracks: [] }
      }
      return mapExpressDetail(this.express)
    },
  },
  watch: {
    express() {
      this.activePkg = 0
    },
  },
}
</script>

<style lang="scss" scoped>
$theme-primary: #6acdbb;
$color-text-title: #222B2A;
$color-text-body: #4E5969;
$color-text-muted: #86909C;
$color-border: #F2F3F5;

.express-panel {
  background: #ffffff;
  padding: 32rpx 40rpx;
  margin-bottom: 24rpx;
}

.section-title {
  font-size: 32rpx;
  font-weight: 700;
  color: $color-text-title;
  margin-bottom: 16rpx;
}

.pkg-tabs {
  white-space: nowrap;
  margin-bottom: 16rpx;
}

.pkg-tab {
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  padding: 12rpx 24rpx;
  margin-right: 12rpx;
  border-radius: 12rpx;
  background: #f5f5f5;
  font-size: 26rpx;
  color: $color-text-muted;

  &.active {
    background: rgba(106, 205, 187, 0.15);
    color: $theme-primary;
    font-weight: 600;
  }

  .pkg-sub {
    font-size: 20rpx;
    margin-top: 4rpx;
  }
}

.row {
  display: flex;
  justify-content: space-between;
  padding: 24rpx 0;
  font-size: 28rpx;
  border-bottom: 1rpx solid $color-border;
  gap: 24rpx;

  text:first-child {
    color: $color-text-muted;
  }

  text:last-child {
    color: $color-text-title;
    text-align: right;
    flex: 1;
    word-break: break-all;
  }

  .highlight-text {
    color: $theme-primary;
    font-weight: 500;
  }
}

.timeline-title {
  font-size: 28rpx;
  color: $color-text-title;
  font-weight: 600;
  margin: 32rpx 0 24rpx;
}

.track-item {
  display: flex;
  padding: 0;
  margin-bottom: 16rpx;
}

.track-dot {
  width: 16rpx;
  height: 16rpx;
  border-radius: 50%;
  background: $color-border;
  margin-top: 12rpx;
  flex-shrink: 0;
  border: 4rpx solid #ffffff;
  box-shadow: 0 0 0 2rpx $color-border;

  &.active {
    background: $theme-primary;
    box-shadow: 0 0 0 4rpx rgba(106, 205, 187, 0.2);
    border-color: #ffffff;
  }
}

.track-body {
  flex: 1;
  margin-left: 28rpx;
  padding-bottom: 32rpx;
  border-left: 2rpx solid $color-border;
  padding-left: 28rpx;
  margin-left: 8rpx;
}

.track-item:last-child .track-body {
  border-left-color: transparent;
  padding-bottom: 0;
}

.track-status {
  display: block;
  font-size: 28rpx;
  color: $color-text-body;
  font-weight: 500;
  margin-bottom: 8rpx;

  &.active {
    color: $theme-primary;
  }
}

.track-detail {
  display: block;
  font-size: 26rpx;
  color: $color-text-body;
  line-height: 1.5;
  margin-bottom: 12rpx;
}

.track-time {
  display: block;
  font-size: 24rpx;
  color: $color-text-muted;
}

.empty-express {
  text-align: center;
  color: $color-text-muted;
  font-size: 26rpx;
  padding: 40rpx 0;
}
</style>
