<template>
  <div class="timeline-wrapper">
    <!-- <button class="arrow left" :disabled="index <= 0" @click="index--">
      &lt;
    </button> -->

    <div class="timeline-box" :style="{
      height: '100%'
      // height: type === 'normal' ? '100%' : '50vh'
    }">
      <div class="line"
        :style="{ '--i': type === 'normal' ? ' 50%' : '100%', '--translateY': type === 'normal' ? ' -50%' : '-100%', }">
      </div>
      <div v-for="(item, i) in showList" :key="item[idKey]" class="node" :style="leftOffset(i)">
        <div class="node" :style="leftOffset(i)">
          <!-- 圆环 -->
          <div class="dot" :class="linePos(i, flip)" :style="{
            marginTop: linePos(i, flip) === 'down' ? '-5px' : '-5px'
          }"></div>

          <div class="time" :style="{
            marginTop: linePos(i, flip) === 'down' ? '-50px' : '10px'
          }" v-if="type === 'normal'">
            {{ item.sanctionDate }}
          </div>
          <!-- 卡片：放到线右侧 -->
          <div class="card" :class="[cardPos(i, flip), 'right-side']" @click="$emit('click-card', item)" :style="{
            marginTop: linePos(i, flip) === 'down' ? '100px' : '-40px',
            marginLeft: type === 'normal' ? '150px' : '0px'
          }">
            <div class="time" :style="{
              marginLeft: 0
            }" v-if="type !== 'normal'">
              {{ item.sanctionDate }}
            </div>
            <div class="title">
              {{ item.title }}
              <!-- <img class="item-header-icon" src="@/assets/images/icon/copy.png" style="cursor: pointer;"></img> -->
            </div>
            <el-tooltip class="box-item" effect="dark" :content="item.content" placement="top">
              <div class="content">
                {{ item.content }}
              </div>
            </el-tooltip>

          </div>
        </div>
      </div>
    </div>

    <!-- <button class="arrow right" :disabled="index >= total - 5" @click="index++">
      &gt;
    </button> -->
  </div>
</template>

<script>
export default {
  name: 'TimeLine',
  props: {
    data: {                // 父组件传入的数组
      type: Array,
      required: true
    },
    textKey: {             // 要显示的文本字段
      type: String,
      default: 'text'
    },
    idKey: {               // 唯一标识字段
      type: String,
      default: 'id'
    },
    type: {                // 父组件传入的数组
      type: String,
      default: 'normal'
    },
  },
  data() {
    return { index: 0 };
  },
  computed: {
    total() {
      return this.data.length;
    },
    showList() {
      return this.data.slice(this.index, this.index + 5);
    },
    flip() { return this.index % 2 === 1; }
  },
  methods: {
    leftOffset(i) {
      return this.type === 'normal' ? { left: `${(i * 100) / 4}%`, top: '50%' } : { left: `${(i * 100) / 4}%`, top: '100%' }
    },


    /* 上下层翻转（保留上次逻辑） */
    cardPos(i, flip = false) {
      return this.type === 'normal' ? ((i % 2) ^ flip ? 'down' : 'up') : ('up')
    },
    /* 线延伸方向 = 卡片出现方向 */
    linePos(i, flip = false) {
      return this.type === 'normal' ? this.cardPos(i, flip) : this.cardPos(i, flip) // up / down
    }
  }
};
</script>

<style scoped>
/* 样式与之前完全一致，不再重复 */
.timeline-wrapper {
  font-family: Microsoft YaHei;
  display: flex;
  align-items: center;
  width: 100%;
  position: relative;
  padding: 0 40px;
  height: 100%;
}

.arrow {
  position: absolute;
  top: 50%;
  /* 左右切换按钮 */
  width: 24px;
  height: 48px;
  font-size: 24px;
  border-color: #E7F3FF;
  border: 0;
  background: #E7F3FF;
  cursor: pointer;
  z-index: 10;
  color: #3E84D1;
}

.arrow:disabled {
  color: #c0c4cc;
  cursor: not-allowed;
}

.left {
  left: 0;
  border-radius: 4px 0px 0px 4px;

}

.right {
  right: 0;
  border-radius: 0px 4px 4px 0px;
}

.timeline-box {
  flex: 1;
  /* height: 100%; */
  position: relative;
}

.line {
  position: absolute;
  left: 0;
  right: 0;
  top: var(--i);
  height: 6px;
  background-image: url("@/assets/images/bg/timeLine-bg.jpg");
  transform: translateY(var(--translateY));
  background-size: auto 100%;
}



.node {
  position: absolute;

  transform: translate(-50%, -50%);
  z-index: 2;
}

/* ===== 圆环基础 ===== */
.dot {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  border: 3px solid #409eff;
  background: #fff;
  position: relative;
  margin: 0 auto;
  z-index: 2;
}

/* ===== 延伸线 ===== */
.dot::after {
  content: '';
  position: absolute;
  left: 50%;
  transform: translateX(-1px);
  /* 居中细线 */
  width: 1px;
  background: #409eff;
}

/* 向上节点：线往下伸 */
.dot.up::after {

  bottom: 100%;
  height: 180px;
  /* 圆环底部 → 卡片顶 */
}

/* 向下节点：线往上伸 */
.dot.down::after {

  top: 100%;
  height: 180px;
}



.card {
  position: absolute;
  height: 180px;
  width: 15vw;
  padding: 8px 12px;
  text-align: left;
  cursor: pointer;
  font-size: 14px;
  margin-left: 150px;
}

.time {
  width: 150px;
  margin-left: 150px;
  color: rgba(5, 95, 194, 1);
  font-family: Microsoft YaHei;
  font-size: 20px;
  font-weight: 700;
  line-height: 26px;
  letter-spacing: 0px;
  text-align: justify;
}

.title {
  color: rgba(59, 65, 75, 1);
  font-family: Microsoft YaHei;
  font-size: 20px;
  font-weight: 700;
  line-height: 26px;
  letter-spacing: 0px;
  text-align: justify;
}

.content {
  margin-top: 10px;
  color: rgba(95, 101, 108, 1);
  font-family: Microsoft YaHei;
  font-size: 16px;
  font-weight: 400;
  line-height: 24px;
  letter-spacing: 0px;
  text-align: justify;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 4;
  overflow: hidden;
  -webkit-box-orient: vertical;
}

.card.up {
  bottom: 80px;
  margin-bottom: 20px;
  margin-left: 160px;
}

.card.down {
  top: -50px;
  margin-top: 95px;
  margin-left: 160px;
}

.box-item {
  color: rgba(255, 255, 255, 1);

  font-family: Microsoft YaHei;
  font-style: Regular;
  font-size: 16px;
  font-weight: 400;
  line-height: 30px;
  letter-spacing: 0px;
  text-align: left;
}

:deep(.el-popper.is-dark) {
  color: rgba(255, 255, 255, 1);

  font-family: Microsoft YaHei !important;
  font-style: Regular;
  font-size: 16px;
  font-weight: 400;
  line-height: 30px;
  letter-spacing: 0px;
  text-align: left;
}
</style>
