<template>
  <div class="time-axis-container">
    <i class="el-icon-arrow-right"></i>
    <ul class="time-axis-labels">
      <li
        v-for="(item, index) in timeList"
        :key="index"
        :class="{ active: index === currentTimeIndex && selected }"
        @click="handleTimeSelect(index)"
      >
        <i v-if="index === currentTimeIndex" class="circle-icon"></i>
        <span>{{ item.time }}</span>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      timeList: [],
      currentTimeIndex: 12, // 当前选中的时间索引
    };
  },
  props: {
    isScene: {
      type: Boolean,
      default: false,
    },
    selected: {
      type: Boolean,
      default: false,
    },
    timeData: {
      type: Array,
      default: () => {
        return [];
      },
    },
  },
  mounted() {
    if (this.timeData && this.timeData.length) {
      this.timeList = this.timeData.map((item) => {
        return {
          time: item,
        };
      });
    } else {
      this.generateTimeList();
    }
    if (this.isScene) {
      this.handleTimeSelect(0);
    } else {
      this.handleTimeSelect(this.timeList.length - 1);
    }
  },
  methods: {
    // 生成时间列表
    generateTimeList() {
      const interval = 5 * 60 * 1000; // 时间间隔为5分钟
      const currentTime = new Date();
      const startTime = new Date(currentTime.getTime() - 60 * 60 * 1000); // 当前时间往前推一个小时
      
      for (
        let i = startTime.getTime();
        i <= currentTime.getTime() - interval;
        i += interval
      ) {
        const time = new Date(i);
        const timeStamp = time.getTime();
        const hour = time
          .getHours()
          .toString()
          .padStart(2, "0");
        const minute = time
          .getMinutes()
          .toString()
          .padStart(2, "0");
        this.timeList.push({
          time: `${hour}:${minute}`,
          timeStamp: timeStamp,
        });
      }
    },

    // 处理时间选择
    handleTimeSelect(index) {
      if (!this.selected) {
        return;
      }
      this.currentTimeIndex = index;
      const timeStamp = this.timeList[index].timeStamp;
      // const time = new Date(
      //   new Date().getTime() -
      //     (this.timeList.length - index - 1) * 5 * 60 * 1000
      // ); //计算当前时间
      if (this.timeData.length) {
        this.$emit("time-selected", this.timeList[index].time);
      } else {
        this.$emit("time-selected", timeStamp);
      }
    },
  },
};
</script>

<style scoped lang="less">
.time-axis-container {
  position: relative;
  height: 45px;
  margin-left: 180px;
  border-top: 1px dashed #2d567b;
  i {
    position: absolute;
    right: 0;
    top: -6px;
  }
}

.time-axis-labels {
  list-style: none;
  margin: 0;
  padding: 0;
  height: 100%;
  display: flex;
}

.time-axis-labels li {
  position: relative;
  flex: 1;
  text-align: center;
  font-size: 14px;
  line-height: 50px;
  cursor: pointer;
  transition: background-color 0.3s ease;
}

.time-axis-labels li.active {
  color: #1890ff;
  .circle-icon {
    position: absolute;
    left: calc(50% - 5px);
    top: -6px;
    display: inline-block;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: #fff;
    border: 3px solid #1890ff;
  }
  /* background: rgba(122, 164, 255, 0.1); */
}
</style>
