<template>
  <div class="home" @click="autoplay" @touchstart="autoplay" @touchend='huadong'>
    <audio
      v-if="audio"
      class="audio"
      id="audio1"
      :src="audio"
      preload
      autoplay
      controls
      loop
      ref="musicBg"
    ></audio>
    <div
      class="content"
      v-if="fixIcon.length > 0"
      :style="{ height: isMobile ? '100%' : '80%' }"
    >
                    <h3 id="shoujititle"  v-html="data.title"></h3>

      <div
        v-if="!isMobile && videoLength"
        @click="slideto('slidePrev')"
        class="swiper-button-prev"
      >
      <img src="../assets/images/previous.png" alt="">
      </div>
      <div class="mb-intro" v-if="active === 'title' && isMobile">
        <p v-html="data.title"></p>
        <p v-html="data.content"></p>
      </div>
      <swiper v-else class="warpper" ref="mySwiper" :options="swiperOptions">
        <swiper-slide v-for="(item, i) in data[active]" :key="i">
          <div class="slide">
            <img
              style="cursor: pointer"
              v-viewer
              v-if="active === 'images'"
              :src="fixUrl(item)"
              alt=""
            />
            <video
              v-else-if="active === 'video'"
              :src="fixUrl(item.url)"
              controls
              autoplay
            ></video>

            <!-- :src="fixUrl(item)" -->
            <iframe
              @click="colseParent()"
              v-else
              :src="fixUrl(myFim[i])"
              frameborder="0"
            ></iframe>
          </div>
        </swiper-slide>
        <div class="swiper-pagination" slot="pagination"></div>
      </swiper>
      <div
        v-if="!isMobile &&videoLength"
        @click="slideto('slideNext')"
        class="swiper-button-next"
      >
        <img src="../assets/images/next.png" alt="">
      </div>
    </div>
    <ul class="iconarr" v-if="fixIcon.length > 1">
      <li
        :class="{ active: item.id === active }"
        @click="active = item.id"
        v-for="(item, i) in fixIcon"
        :key="i"
      >
        <img :src="require(`@/assets/images/${item.img}.png`)" alt="" />
        <span>{{ item.name }}</span>
      </li>
    </ul>
    <div
      class="intro"
      :class="{ ismtop: fixIcon.length <= 0, mcenter:!active }"
      v-if="!isMobile || (isMobile && fixIcon.length <= 0)"
    >
      <h3 v-html="data.title"></h3>
      <p v-html="data.content"></p>
    </div>
  </div>
</template>

<script>
import { Swiper, SwiperSlide } from "vue-awesome-swiper";
import "swiper/css/swiper.css";
import browser from "@/utils/browser";

let iconArr = [
  { name: "图片", id: "images", img: "img-icon", display: false },
  { name: "视频", id: "video", img: "video-icon", display: false },
  { name: "网页", id: "iframe", img: "iframe-icon", display: false },
  { name: "模型", id: "model", img: "model-icon", display: false },
];

browser.mobile &&
  iconArr.push({ name: "介绍", id: "title", img: "txt-icon", display: false });

export default {
  name: "Home",
  data() {
    return {
      //右侧按钮节流
      myTimer:null,
      myFim:[],
      videoLength:true,
      loadAuto: false,
      audio: "",
      m: this.$route.query.m,
      id: this.$route.query.id,
      isMobile: browser.mobile,
      isAndriod: browser.android,
      swiperOptions: browser.mobile
        ? {
            pagination: {
              el: ".swiper-pagination",
              clickable: true,
            },
          }
        : {
            slidesPerView: 3,
            spaceBetween: 0,
            centeredSlides: true,
            pagination: {
              el: ".swiper-pagination",
              clickable: true,
            },
          },
      data: {},
      iconArr,
      active: "",
    };
  },
  watch: {
    active(newVal) {
      if (!newVal) {
        return;
      }
      if (!this.$refs.musicBg) {
        return;
      }
      setTimeout(() => {
        if (newVal == "video") {
          if (!this.$refs.musicBg.paused) {
            this.$refs.musicBg.pause();
          }
        } else {
          if (this.$refs.musicBg.paused) {
            this.$refs.musicBg.play(); // 暂停
          }
        }
      }, 500);
    },
  },
  computed: {
    swiper() {
      return this.$refs.mySwiper.$swiper;
    },
    fixIcon() {
      let arr = this.iconArr.filter((item) => !!item.display);
      return arr;
    },
  },
  components: {
    Swiper,
    SwiperSlide,
  },
  methods: {
    huadong(){
      // console.log(666);
            //点击的时候才加载，防止一次运行过多iframe标签导致页面崩溃
      let myFim =document.querySelectorAll('.swiper-wrapper .swiper-slide')
      let myFimCente =document.querySelectorAll('.swiper-wrapper .swiper-slide-active iframe')
      console.log(myFim,myFimCente);
      myFim.forEach((v,i)=>{
        //拿到当前在屏幕中间的iframe
        if(v.className.includes('swiper-slide-active') && i>1) {
          // console.log(myFimCente[0].src);
          if(myFimCente[0].src.includes('undefined')){
            myFimCente[0].src =this.fixUrl(this.data.model[i])
          }
          }
      })
    },
    autoplay() {
      if (this.loadAuto) {
        return;
      }
      try {
        this.$refs.musicBg.play();
        this.loadAuto = true;
      } catch (error) {
        error;
      }
    },
    async getData() {
      // http://192.168.0.44:8110
      let url = `/data/${
        this.id
      }/hot/js/data.js?time=${Math.random()}`;
      let result = (await this.$http.get(url)).data;
      this.data = result[this.m];


    //进页面只让iframe加载2个
      // console.log(9999999,this.data.model);
      if(this.data.model){
        this.myFim[0]=this.data.model[0]
        this.myFim[1]=this.data.model[1]
      }
      // this.myFim=this.data.model


      if (!this.data) {
        return alert("热点解析错误");
      }
 
      this.audio = this.data["backgroundMusic"];

      if (!this.data.content && this.isMobile) {
        this.iconArr.pop();
      }
      this.iconArr.forEach((item) => {
        if (this.data[item.id]) {
          this.active = !this.active ? item.id : this.active;
          item.display = true;
        }
      });
    },

    colseParent() {
      if (this.isMobile) {
          window.parent.document.getElementById("closepop").click();
      }
    },
    fixUrl(item) {
      return 'http://192.168.0.44:8110'+item;
    },
    slideto(action) {
        if(this.myTimer!==null) return
        this.myTimer=setTimeout(() => {
          this.swiper[action]();
          this.myTimer=null
        }, 500);

      
      //点击的时候才加载，防止一次运行过多iframe标签导致页面崩溃
      let myFim =document.querySelectorAll('.swiper-wrapper .swiper-slide')
      let myFimCente =document.querySelectorAll('.swiper-wrapper .swiper-slide-next iframe')
      myFim.forEach((v,i)=>{
        //拿到当前在屏幕中间的iframe
        if(v.className.includes('swiper-slide-next') && i>1) {
          // console.log(myFimCente[0].src);
          if(myFimCente[0].src.includes('undefined')){
            myFimCente[0].src =this.fixUrl(this.data.model[i])
          }
          }
      })
    },
  },
  mounted() {
    // 进页面获取视频个数
    setTimeout(() => {
      let videos =document.querySelectorAll('.slide video')
      let Imgs =document.querySelectorAll('.slide img')
      let iframes = document.querySelectorAll('.slide iframe')
      // console.log(11111,iframes);
      if(videos.length===1 || Imgs.length===1 ||iframes.length===1) this.videoLength = false
      
    }, 100);
    this.getData();
    document.addEventListener(
      "WeixinJSBridgeReady",
      () => {
        this.autoplay();
      },
      false
    );
  },
};
</script>

<style lang="less" scoped>
.audio {
  width: 90%;
  max-width: 400px;
  height: 50px;
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%,-50%);
}

.mb-intro {
  color: #cf7b0e;
  padding: 10px;
  position: fixed;
  top: 50%;
  transform: translateY(-50%);
  left: 0;
  > p {
    line-height: 1.5;
    letter-spacing: 1px;
    &:first-of-type {
      font-weight: bold;
      font-size: 20px;
    }
  }
}

.home {
  width: 100%;
  height: 100%;
  position: relative;
  .ismtop {
    padding-top: 40px !important;
  }
  
  .content {
    #shoujititle{
      display: none;
    }
    width: 100%;
    height: 80%;
    .warpper {
      width: 100%;
      height: 100%;
      .slide {
        font-size: 0;
        img,
        video,
        iframe {
          max-height: 570px;
          border-radius: 14px;
          box-shadow: 0 0 16px rgba(0, 0, 0, 0.6);
        }
       
        iframe {
          height: 570px;
          width: 1000px;
        }
      }
    }
  }

  .iconarr {
    list-style: none;
    display: flex;
    justify-content: flex-end;
    margin-right: 30px;
    li {
      display: flex;
      align-items: center;
      justify-content: center;
      color: #fff;
      list-style: none;
      font-size: 14px;
      width: 90px;
      height: 32px;
      line-height: 32px;
      cursor: pointer;
      border-radius: 10px;
      border: solid 1px #fff;
      margin-right: 10px;
      span {
        margin-left: 4px;
      }
    }
    .active {
      background: #19bbed;
      border: none;
    }
  }
  .intro {
    position: absolute;
    top: 80px;
    max-height: 20vh;
    overflow: auto;
    width: 100%;
    color: #d27a11;
    margin: 0 auto;
    > h3 {
      text-align: center;
      font-size: 20px;
      font-weight: 600;
      color: #d27a11;
    }
    > p {
      line-height: 1.5;
      margin-top: 10px;
      font-size: 16px;
      text-indent: 32px;
    }
  }
  .mcenter{
    position: absolute;
    transform: translate(-50%,-50%);
    top: 40%;
    left: 50%;
    max-height: none;
  }
}

@media screen and (max-width: 1400px) {
  .home {
    .content {
      .warpper {
        .slide {
          img,
          video,
          iframe {
            max-height: 500px;
          }
          img{
            // max-height: 80vh;
            // width: auto;
            background-size: cover;
          }
          iframe {
            height: 500px;
          }
        }
      }
    }
  }
}

@media screen and (max-width: 1000px) {
  #shoujititle{
    display: block !important;
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    top: 80px;
    font-size: 20px;
    color: #cf7b0e;
  }
  .intro h3 {
    bottom: 100px !important;
  }
  .swiper-pagination-fraction, .swiper-pagination-custom, .swiper-container-horizontal > .swiper-pagination-bullets{
    bottom: 240px;
  }
  .home {
    background: url('../assets/images/Mbac.png') no-repeat center;
    background-size: cover;
    #audio1{
      display: block !important;
    }
    .content {
      .warpper {
        .slide {
          width: 100%;
          img,
          video,
          iframe {
            max-height: none;
            width: 100%;
            border-radius: 0;
          }
          img{
            // max-height: 80vh;
            // width: auto;
            background-size: cover;
          }
          iframe {
            width: 100%;
            height: 55vh;
          }
        }
      }
    }
    .iconarr {
      list-style: none;
      margin-right: 0px;
      position: fixed;
      bottom: 10px;
      right: 0px;
      z-index: 10000;
      li {
        width: 70px;
      }
    }
  }
}

@media only screen and (max-width: 906px) and (orientation: landscape) {
  .home {
    .content {
      .warpper {
        .slide {
          width: 100%;
          img,
          video,
          iframe {
            max-width: 70%;
            max-height: 80vh;
          }
          iframe {
            width: 100%;
            max-width: unset;
            height: 100vh;
          }
        }
      }
    }
  }
}
</style>

<style>
.slide {
  position: absolute;
  top: 135px;
}
.swiper-container {
  width: 100%;
  height: 100%;
  bottom: -115px;
}

.swiper-pagination-bullet {
  background-color: #fff;
  opacity: 0.8;
}
.swiper-pagination-bullet-active {
  background-color: #cf7b0e;
}
.swiper-slide {
  text-align: center;
  font-size: 18px;
  display: flex;
  justify-content: center;
  align-items: center;
  transition: 300ms;
  transform: scale(0.8);
  position: relative;
  opacity: 0.5;
}

.swiper-slide-active,
.swiper-slide-duplicate-active {
  transform: scale(1);
  opacity: 1;
  z-index: 999;
}

.swiper-button-prev,
.swiper-button-next {
  /* background: rgba(0, 0, 0, 0.4); */
  /* padding: 10px 20px; */
  color: transparent !important;
    width: 80px;
  height: 80px;
/* position: relative; */
}
.swiper-button-prev img,
.swiper-button-next img {
  position: absolute;
  width: 100%;
  height: 100%;
}
.swiper-button-prev {
  left: 22px;
}
.swiper-button-next {
  right: 22px;
}

@media screen and (max-width: 500px) {
  .swiper-slide {
    width: 100%;
  }
  .slide{
    top: 40px;
  }
}
</style>