<template>
  <div class="top_list apaas_scroll" @mouseenter="mouseenter" @mouseleave="mouseleave">
    <ul>
      <li class="item_list" v-for="(option, index) in options" :key="index">
        <div class="item_index">
          <span v-text="'TOP' + (index + 1)"></span>
        </div>
        <div class="item_logo">
          <el-avatar shape="square" :size="28" fit="cover" :src="option.cover" />
        </div>
        <div class="item_detail">
          <p class="item_info">
            <span
              class="text_clip is_link"
              @click="getDetail(option.service_id, option.data_service_type1)"
            >
              {{ option.service_name }}
              <i class="el-icon-arrow-right"></i>
            </span>
            <span
              v-text="helper.numberFormat(option.request_count, 2) + (option.request_count > 10000 ? '万次' : '次')"
            ></span>
          </p>
          <el-progress
            class="item_progress"
            :percentage="getPercent(option.request_count)"
            :show-text="false"
            color="#e56600"
          ></el-progress>
        </div>
      </li>
    </ul>
  </div>
</template>

<script>
import helper from "@/services/helper";
export default {
  props: {
    options: {
      type: Array,
      default: () => []
    },
    targetValue: {
      type: Number,
      default: () => 0
    }
  },
  data() {
    return {
      timer: null,
      helper
    };
  },
  watch: {
    text() {
      this.autoScroll();
    }
  },
  methods: {
    getPercent(value) {
      if (value == 0) {
        return 0;
      } else {
        return Math.min((value / this.targetValue) * 100, 100);
      }
    },
    autoScroll() {
      if (this.timer) {
        clearInterval(this.timer);
      }

      this.$el.scrollTop = 0;

      this.timer = setInterval(() => {
        let itemHeight = 50;
        let curScrollTop = this.$el.scrollTop + itemHeight;

        if (curScrollTop >= this.$el.scrollHeight - this.$el.clientHeight + itemHeight) {
          curScrollTop = 0;
        }

        this.$el.scrollTop = Math.min(curScrollTop, this.$el.scrollHeight - this.$el.clientHeight);
      }, 1500);
    },
    mouseenter() {
      if (this.timer) {
        clearInterval(this.timer);
      }
    },
    mouseleave() {
      this.autoScroll();
    },
    getDetail(id, type) {
      switch (type) {
        case 5:
          this.$router.push(`/shop/sjfwDetail/${id}`);
          break;
        case 6:
          this.$router.push(`/shop/skfwDetail/${id}`);
          break;
        case 21:
          this.$router.push(`/shop/zhfwDetail/${id}`);
          break;
        case 124:
          this.$router.push(`/yygl/0/0/detail/${id.id}?source=${id.source}`);
          break;
        default:
          this.$message({
            message: "查看详情失败",
            type: "warning"
          });
          break;
      }
    }
  },
  mounted() {
    this.autoScroll();
    window.addEventListener("resize", () => {
      this.autoScroll();
    });
  },
  destroyed() {
    if (this.timer) {
      clearInterval(this.timer);
    }
  }
};
</script>

<style scoped>
.top_list {
  overflow-x: hidden;
  overflow-y: auto;
}
.item_list {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  margin-bottom: 18px;
}
.item_index {
  width: 35px;
  flex-shrink: 0;
  margin-right: 10px;
  font-size: 12px;
  color: #58617a;
}
.item_logo {
  margin-right: 10px;
  flex-shrink: 0;
  font-size: 0;
  border: 2px solid #e3e5ef;
  border-radius: 6px;
  overflow: hidden;
}
.item_info {
  flex-grow: 1;
  display: flex;
  justify-content: flex-start;
  align-items: center;
  font-size: 14px;
  line-height: 20px;
  color: #58617a;
}
.item_detail {
  flex-grow: 1;
}
.item_info > span:first-child {
  width: 100px;
  flex-grow: 1;
  margin-right: 10px;
}
.item_info > span:first-child > i {
  font-weight: bold;
  font-size: 12px;
  color: #8e96ab;
}
.item_info > span:nth-child(2) {
  flex-shrink: 0;
  white-space: nowrap;
  color: #0d1847;
}
.item_progress {
  margin-top: 6px;
}
.is_link {
  cursor: pointer;
}
</style>
