<script setup lang="ts">
import dayjs from 'dayjs'
import { useOverflow } from '@/hooks/useOverflow'

const props = defineProps({
  data: Object
})
const simplexData: any = ref([])

watch(
  () => props.data,
  (newVal) => {
    if (newVal) {
      simplexData.value = newVal
    } else {
      simplexData.value = []
    }
  },
  {
    immediate: true,
    deep: true
  }
)

// 中间点 每两个节点之间加上26px  上边距离28px 下边距离54px  如果没有中间点则高度为56px
const getSimplexLineHeight = (index: number) => {
  if (index === 0) {
    return 56
  } else {
    return 28 + 56 + 26 * (index - 1)
  }
}
const getDateHeight = (index: number) => {
  return 42 + 26 * index
}

const formatDate = (date: string) => {
  return date ? dayjs(date).format('MMM-DD-YYYY') : '--'
}

const pathRef = ref()

const { isOverflow: isPathOverflow } = useOverflow(pathRef, props)
</script>

<template>
  <div class="simplex-content">
    <div
      class="detail-step-item"
      :class="{
        last: !stepItem.isArrival
      }"
      v-for="(stepItem, index) in simplexData"
      :key="stepItem.index"
    >
      <div class="data">
        <div class="left-step-icon">{{ stepItem.index }}</div>
        <div class="right-info">
          <div class="path">
            <div class="label">{{ stepItem.label }}</div>
            <el-tooltip v-if="isPathOverflow?.[index]" placement="top">
              <template #content>{{ stepItem.path }}</template>

              <div ref="pathRef" class="detail-path single-line-ellipsis">{{ stepItem.path }}</div>
            </el-tooltip>
            <div v-else ref="pathRef" class="detail-path single-line-ellipsis">
              {{ stepItem.path }}
            </div>
          </div>
          <div
            class="transport-info"
            :style="{ top: getDateHeight(dateIndex) + 'px' }"
            v-for="(dateItem, dateIndex) in stepItem.children"
            :key="dateIndex"
          >
            <div class="step-dot"></div>
            <div class="label">{{ dateItem.label }}</div>
            <div class="divider"></div>
            <div class="date">{{ formatDate(dateItem.date) }}</div>
          </div>
        </div>
      </div>
      <div
        class="line"
        v-if="stepItem.label !== 'Place of Delivery'"
        :style="{ height: getSimplexLineHeight(stepItem.children?.length || 0) + 'px' }"
      ></div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
// 单式样式
.simplex-content {
  width: 100%;
  padding: 24px 8px 9px 16px;
}
.detail-step-item {
  & > .data {
    display: flex;
    position: relative;
    height: 16px;

    .left-step-icon {
      width: 16px;
      height: 16px;
      margin-right: 8px;
      border-radius: 50%;
      background-color: var(--color-neutral-1);
      font-size: 10px;
      font-weight: 700;
      text-align: center;
      line-height: 16px;
      color: #fff;
    }
    .right-info {
      flex: 1;
      position: relative;
      display: flex;
      flex-direction: column;
      justify-content: flex-start;
      align-items: flex-start;
      gap: 8px;
      margin-top: -8px;
      .step-dot {
        width: 8px;
        height: 8px;
        border-radius: 50%;
        background-color: var(--color-neutral-1);
      }
      .path {
        flex: 1;
        display: flex;
        align-items: center;
        width: 100%;
        border-radius: 6px;
        .label,
        .detail-path {
          height: 32px;
          padding: 0 8px;
          line-height: 32px;
        }
        .label {
          // width: 96px;
          background-color: var(--color-neutral-1);
          color: #fff;
          font-weight: 500;
          font-size: 12px;
          border-radius: 3px 0 0 3px;
        }
        .detail-path {
          flex: 1;
          width: 252px;
          background-color: #fff;
          font-weight: 700;
          color: var(--color-neutral-1);
          border: 1px solid #bfc1c3;
          border-left: 0;
          border-radius: 0 3px 3px 0;
        }
      }
      .transport-info {
        position: absolute;
        top: 20px;
        left: -20px;
        display: flex;
        align-items: center;
        gap: 8px;
        width: calc(100% + 20px);
        .label {
          font-weight: 700;
        }
        .divider {
          flex: 1;
          height: 0px;
          margin-right: 8px;
          border-top: 1px dashed var(--color-neutral-3);
        }
        .date {
          font-size: 12px;
        }
      }
    }
  }
  & > .line {
    height: 72px;
    margin-left: 7px;
    border-left: 1px solid var(--color-neutral-1);
  }
  &.last {
    & > .data {
      .left-step-icon {
        background-color: #ccd1db;
      }
      .right-info {
        .step-dot {
          background-color: #ccd1db;
        }
        .path {
          .label {
            background-color: #ccd1db;

            font-weight: 600;
            color: var(--color-neutral-1);
          }
          .detail-path {
            border-color: #ccd1db;
          }
        }
      }
    }
    & > .line {
      border-left: 1px dashed var(--color-neutral-3);
    }
  }
}
.single-line-ellipsis {
  height: 18px;
  display: inline-block; /* 或者根据需要使用 inline-block */
  white-space: nowrap; /* 不换行 */
  overflow: hidden; /* 隐藏超出部分 */
  text-overflow: ellipsis; /* 超出部分显示省略号 */
}
</style>
