<template>
  <!-- 轮播图片的 -->
  <div v-if="isType === '1'" @mouseenter="closeLoop" @mouseleave="loopSlider" class="box">
    <div class="xtx-slider1">
      <!-- 图片列表 -->
      <ul class="slider-body">
        <!--
          fade: 当fade类名存在 当前图片就显示 不存在就不显示
         -->
        <li class="slider-item" @click="imgShowData(item.id)" :class="{ fade: curIndex === i }" v-for="(item, i) in data"
          :key="i">
          <img class="img-box" :src="item.imgUrl" alt="" />
          <div class="bg-box">
            <span class="text-nav-data">{{ item.title }}</span>
          </div>
        </li>
      </ul>
      <!-- 圆圈切换按钮 -->
      <div class="nav-box">
        <div class="slider-indicator">
          <span @click="curIndex = index" :class="{ active: curIndex === index }" v-for="(item, index) in data"
            :key="index">{{ index + 1 }}</span>
        </div>
        <div class="more" @click="moreData()"><span class="more-title">....</span></div>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, onMounted, } from 'vue'
import { useRouter } from 'vue-router'

export default {
  name: 'XtxSlider',

  props: {
    data: {
      type: Array,
      default: () => []
    },
    //  控制播放
    isAutoPlay: {
      type: Boolean,
      default: true
    },
    // 传入看是文字的还是图片的
    isType: {
      type: String,
      default: () => '1'
    },
  },

  setup(props) {
    const router = useRouter();
    // 当前要显示图片的索引
    const curIndex = ref(0)

    // 自动播放实现
    const timeId = ref(null)
    const loopSlider = () => {
      if (!props.isAutoPlay) return
      timeId.value = setInterval(() => {
        curIndex.value++
        if (curIndex.value > props.data.length - 1) {
          curIndex.value = 0
        }
      }, 2500)
    }
    onMounted(() => {
      loopSlider()
    })

    //  鼠标移入暂停
    const closeLoop = () => {
      clearInterval(timeId.value)
    }
    // 跳转详情
    const imgShowData = (val) => {
      //定义一个路径
      let href = router.resolve({
        path: "News/pictureDetails",
        query: { id: val },
      }).href;
      //打开新的页面 到 href 这个页面
      window.open(href, "_blank");
    }
    // 查看更多
    const moreData = () => {
      // console.log(1221);
      //定义一个路径
      let href = router.resolve({
        path: "/News/imgesDel",
        // query: { type: "ttxmbt" },
      }).href;
      //打开新的页面 到 href 这个页面
      window.open(href, "_blank");
    }

    return {
      curIndex,
      closeLoop,
      loopSlider,
      imgShowData,
      moreData
    }
  }
}
</script>

<style scoped lang='less'>
.xtx-slider1 {
  width: 100%;
  height: 100%;
  min-width: 576px;
  min-height: 300px;
  position: relative;
}

.xtx-slider2 {
  width: 100%;
  height: 100%;
  min-width: 576px;
  min-height: 300px;
  position: relative;
}

.xtx-slider {
  width: 100%;
  height: 100%;
  min-width: 576px;
  min-height: 300px;
  position: relative;
}

.title-conter {
  position: absolute;
  left: 4px;
  bottom: 6px;
  z-index: 2;
  width: 99%;
  text-align: left;
}

.slider {
  &-body {
    width: 100%;
    height: 100%;
  }

  &-item {
    width: 100%;
    height: 100%;
    position: absolute;
    left: 0;
    top: 0;
    opacity: 0;
    transition: opacity 0.5s linear;

    &.fade {
      opacity: 1;
      z-index: 1;
    }

    img {
      width: 100%;
      height: 100%;
    }
  }

  &-indicator {
    position: absolute;
    right: 8%;
    bottom: 6px;
    z-index: 10;
    width: 13%;
    text-align: right;

    span {
      line-height: 20px;
      display: inline-block;
      width: 16px;
      height: 19px;
      background: #fff;
      border-radius: 4px;
      cursor: pointer;
      text-align: center;

      ~span {
        margin-left: 12px;
      }

      &.active {
        color: #fff;
        background: rgb(0, 157, 255);
      }
    }
  }
}

.box {
  width: 96%;
  height: 300px
}

.slider-item {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.slider-text {
  font-size: 30px;
  width: 92%;
  text-align: center;
  margin: auto;
  line-height: 100px;
  color: crimson;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  cursor: pointer;
}

.scroll-btn {
  width: 42px;
  padding-top: 20px;
  cursor: pointer;
}

.img-box {
  cursor: pointer;
}

.more {
  position: absolute;
  right: 4px;
  bottom: 6px;
  font-size: 20px;
  font-weight: bold;
  width: 30px;
  height: 20px;
  background: #fff;
  cursor: pointer;
  z-index: 20;
  border-radius: 4px;
}

.more-title {
  display: block;
  line-height: 10px;
  padding-left: 4px;
}

.title-box-data {
  color: #fff;
}

.nav-box {
  position: absolute;
  right: 0px;
  bottom: 0px;
  width: 100%;
  height: 42px;
  z-index: 100;
}

.text-nav-data {
  width: 72%;
  padding-left: 12px;
  white-space: nowrap;
  /* 确保文本在一行内显示 */
  overflow: hidden;
  /* 隐藏超出容器的内容 */
  text-overflow: ellipsis;
  /* 使用省略号表示被截断的文本 */
  position: absolute;
  bottom: 8px;
  left: 0;
  color: #fff !important;
  z-index: 100;
}

.bg-box {
  z-index: 1;
  width: 100%;
  height: 38px;
  position: absolute;
  bottom: 0;
  left: 0;
  background-color: rgb(51 51 51 / 60%);
}
</style>
