<template>
  <div class="image-carousel">
    <el-carousel style="height: 100%;" motion-blur indicator-position="none" @change="handleCarouselChange"
      :autoplay="false">
      <el-carousel-item v-for="(item, index) in listSxt" :key="index">
        <div class="video-grid" v-if="activeIndex == index">
          <div v-for="(items,indexs) in item" :key="indexs" class="video-cell">
            <div class="iconFont" @click.stop="showchenge(items)">
              <el-icon :size="20"><UploadFilled /></el-icon>
            </div>
            <WsIframe :sbbh="items.sbbh"  />
          </div>
        </div>
      </el-carousel-item>
    </el-carousel>

  </div>
</template>

<script setup>
import { ref, watch, computed, onMounted, onUnmounted, reactive,nextTick } from 'vue'
import WsIframe from '@/components/wsIframe/index.vue'
import { useRoute } from "vue-router"
import { sxtGetList } from '@/api/mosty-jcz.js'
import emitter from "@/utils/eventBus.js";
const activeIndex = ref(0)
const cdList=ref([])
const listSxt=ref([1])
const handleCarouselChange = (index) => {
  activeIndex.value = index
}

const getSxtGetList = (id) => {
  const jczid = {
    jczid: id
  }
  sxtGetList(jczid).then(res => {
   listSxt.value= Array.from({ length:Math.ceil(res.length / 12)==0?1:Math.ceil(res.length / 12) }, (_, row) => {
   return res.slice(row * 12, row * 12 + 12)
    }
    );
     cdList.value = listSxt.value[0].length < 4 ? listSxt.value[0]: listSxt.value[0].slice(0,4)
    emitter.emit("getSxtGetList", cdList.value )
  }).finally(()=>{
  })
}
const route = useRoute()
onMounted(() => {
  const id = route.query.id
  getSxtGetList(id)
})
const showchenge = (item) => {
  const index = cdList.value.indexOf(item);
if (index !== -1) {
  // 如果数字已存在，先移除它
  cdList.value.splice(index, 1);
}
// 添加到最前面
  cdList.value.unshift(item);

  emitter.emit("getSxtGetList", cdList.value )
}
</script>

<style lang="scss" scoped>
.image-carousel {
  width: 100%;
  height: 100%;
  position: relative;
  padding: 30px 20px;
  box-sizing: border-box;

  .carousel-container {
    width: 100%;
    height: 100%;
    position: relative;
    overflow: hidden;
  }

  .image-wrapper {
    display: flex;
    height: 49.5%;
    transition: transform 0.3s ease;
  }

  .image-item {
    width: 24%;
    margin: 0 1%;
    height: 100%;

    img {
      width: 100%;
      height: 100%;
      object-fit: cover;
    }
  }

  .control-button {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    cursor: pointer;
    transition: all 0.3s;
    color: #fff;
    z-index: 1;

    &.prev {
      left: 0;
    }

    &.next {
      right: 0;
    }
  }
}

.video-grid {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  /* 6列 */
  grid-template-rows: repeat(2, auto);
  /* 2行 */
  gap: 10px;
  height: 100%;
  overflow: auto;
}

::v-deep .el-carousel__container {
  height: 100% !important;
}

::v-deep .image-carousel {
  // padding: 0 !important;;
}

/* 自定义滚动条样式 */
::-webkit-scrollbar {
  width: 0.5em;
  height: 0.5em;
}

::-webkit-scrollbar-track {
  background: #000000;
}

::-webkit-scrollbar-thumb {
  background: #021b31;
  border-radius: 0.25em;
}

::-webkit-scrollbar-thumb:hover {
  background: #011733cd;
}
.video-cell{
  position: relative;
}
.iconFont{
  position: absolute; top: 10px; text-align: right;  width: 100%;
}
</style>
