<!-- 原项目丢了 省事直接cv一下老项目代码 -->
<template>
  <div class="wrapper" ref="wrapper">
    <div id="reportdtl" v-if="isshow" :style="{backgroundColor:reportInfo.CanvasColor}">

      <div>
        <div class="html-head-img-box" v-if="reportInfo && !!reportInfo.NeedSplice && reportInfo.HeadImg">
          <img :src="reportInfo.HeadImg" alt="" style="display:block;width:100%">
          <div class="head-layout-item" v-for="item in headImgStyle" :key="item.value"
          :style="{fontFamily:item.family,fontSize:(item.size*2)+'px',fontWeight:item.weight,textAlign:item.align,color:item.color,
              width:item.width,height:item.height,left:item.left,top:item.top
          }">
              {{ layoutBaseInfo[item.value] }}
          </div>
        </div>
        <div class="no-layout-img-box" v-if="reportInfo && !!reportInfo.NeedSplice && (!reportInfo.HeadImg) && (!reportInfo.EndImg)">
          <header>
            <span>{{reportInfo.Title}}</span>

            <img class="logo" :src="configInfo.ReportLogo" alt="" v-if="configInfo.ReportLogo"/>
          </header>
          <div style=" box-sizing:border-box; color:#666; font-size:24px; overflow:hidden;">
            <span>{{ reportInfo.Author}}</span>
            <span style="float:right;" v-if="[2,6].includes(reportInfo.State)">{{reportInfo.PublishTime}}</span>
          </div>
        </div>

        <div id="playcon" v-if="reportInfo.VideoUrl">
          <div>
            <img
              @click.stop="$refs.audioRef.playaudiopause()"
              v-if="
                $store.state.isplaying &&
                reportInfo.VideoUrl ==
                  $store.state.audiolist[$store.state.playcount].video_url
              "
              src="~@/assets/img/playImg.png"
              style="width: 2rem; height: 2rem; float: left; margin-right: 0.4rem"
            />
            <img
              @click.stop="playaudio(reportInfo)"
              v-else
              src="~@/assets/img/pauseImg.png"
              style="width: 2rem; height: 2rem; float: left; margin-right: 0.4rem"
            />
            <p
              style="
                font-size: 0.64rem;
                line-height: 1rem;
                color: #333;
                white-space: nowrap;
                overflow: hidden;
                text-overflow: ellipsis;
              "
            >
              {{ reportInfo.VideoName }}
            </p>
            <p style="font-size: 0.56rem; line-height: 1rem; color: #878787">
              {{ parseInt(reportInfo.VideoPlaySeconds / 60) }}分{{
                parseInt(reportInfo.VideoPlaySeconds % 60)
              }}秒
            </p>
          </div>
        </div>

        <div class="abstract" v-if="reportInfo && !!reportInfo.NeedSplice">
          <div v-if="reportInfo.Abstract">摘要：{{ reportInfo.Abstract }}</div>
          <div style="color: #666; margintop: 1.2rem" v-if="configInfo.Disclaimer">
            *注：请务必阅读&nbsp;<strong
              style="fontweight: normal; cursor: pointer; color: #3e8ce6"
              @click="showTips"
              >免责声明</strong
            >
          </div>
        </div>

        <!-- 章节报告 -->
        <div class="chapter-wrap" v-if="reportInfo.HasChapter===1">
          <div class="chapter-item" v-for="item in reportInfo.ChapterList" :key="item.ReportChapterId">
            <div style="margin-bottom:10px">
              <span class="type" v-if="item.TypeName">{{item.TypeName}}</span>
              <span class="title">{{item.Title}}</span>
            </div>
            <div id="resetcss" style="overflow:hidden;" v-html="item.Content"></div>
          </div>
        </div>
        <div id="resetcss" style="overflow:hidden;" v-html="reportInfo.Content" v-else></div>

        <div class="html-end-img-box" v-if="reportInfo && !!reportInfo.NeedSplice && reportInfo.EndImg">
          <img :src="reportInfo.EndImg" alt="" style="display:block;width:100%">
          <div class="head-layout-item" v-for="item in endImgStyle" :key="item.value"
          :style="{fontFamily:item.family,fontSize:(item.size*2)+'px',fontWeight:item.weight,textAlign:item.align,color:item.color,
              width:item.width,height:item.height,left:item.left,top:item.top
          }">
              {{ layoutBaseInfo[item.value] }}
          </div>
        </div>
        <div id="tipsAlert">
          <div
            style="
              width: 100%;
              max-width:1200px;
              margin:0 auto;
              padding: 0.8rem 0.66rem 2.2rem;
              box-sizing: border-box;
              font: 0.52rem/0.8rem 'PingFang-SC-Regular';
              color: #666;
              background: #fff;
              overflow: scroll;
              position: relative;
              border-radius: 0.2rem;
            "
          >
            <h1 style="font: 0.64rem 'PingFang-SC-Regular'; color: #333; text-align: center; margin-bottom: 0.34rem">免责声明</h1>
            
            <div class="disclaimer" v-html="configInfo.Disclaimer"></div>
            <p @click="hideTips" style="position: absolute; bottom: 0; left: 0; right: 0; font-size: 0.56rem; line-height: 1.6rem; color: #2680eb; text-align: center; border-top: 1px solid #eaeaea">
              知道了
            </p>
          </div>
        </div>
      </div>
    </div>
    <div style="width: 30px; position: fixed; right: 0.6rem; bottom: 2.4rem; z-index: 100">
      <img src="~@/assets/img/returntop.png" @click="goTop" style="width: 30px; margin-bottom: 0.2rem" />
    </div>


    <AudioPlay ref="audioRef"/>
  </div>
</template>

<script>
import { getShareInfo,reportDetail } from "@/api/api.js";
import _ from "lodash";
import {wxShare,injectSdk} from "../utils/wxShare.js";
import AudioPlay from '../report/audioPlay.vue';
export default {
  components: { AudioPlay },
  data() {
    return {
      id: this.$route.query.code || "",
      isshow: false,
      /**
       * NeedSplice 0-不需要 1-需要
       */
      reportInfo: {},
      reportlist: [],

      totalContent: [],
      realContent: [],
      page_no: 0,
      pageSize: 20, //默认初始加载50个p标签
      total_page: 0,
      headImgStyle:null,//版头style
      endImgStyle:null,//版尾style
      layoutBaseInfo:{
          研报标题:'',
          研报作者:'',
          创建时间:''
      },

      configInfo: {}
    };
  },
  created() {
    wx.ready(function(){
      wx.hideOptionMenu();
    });
  },
  mounted() {
    // injectSdk().then(res => {
      this.getShareData()
    // })
    window.addEventListener("scroll", this.loadMoreHandle);
    window.addEventListener('resize',this.pageResize)
  },

  destroyed() {
    window.removeEventListener("scroll", this.loadMoreHandle);
    window.removeEventListener('resize',this.pageResize)
  },

  methods: {
    async getShareData() {
      let res = await getShareInfo({ ReportCode: this.id })
      if(res.Ret !== 200) return 
      
      wxShare(document.location.href,res.Data.H5ShareName||document.title,res.Data.H5ShareTitle,res.Data.H5ReportShareImg,0);
      this.getreportdetail()
    },
    pageResize(){
      const el=document.getElementById('reportdtl')
      const windowWid=document.body.clientWidth
      if(windowWid<1200){
        el.style.transform=`scale(${windowWid/1200})`
        // el.style.
      }
    },

    playaudio(item) {
      //子组件先存后播
      console.log(item);
      if (
        this.$store.state.audiolist.length > 0 &&
        item.VideoUrl == this.$store.state.audiolist[0].VideoUrl
      ) {
        // this.$parent.playaudioplay();
        this.$refs.audioRef.playaudioplay();
      } else {
        this.$refs.audioRef.setisshowaudio(
          [
            {
              video_url: item.VideoUrl,
              video_name: item.VideoName,
              video_play_seconds: item.VideoPlaySeconds,
            },
          ],
          0
        );
      }
    },

    showTips() {
      //免责声明显示
      $("#tipsAlert").animate({ top: 0 });
    },
    hideTips() {
      //免责声明收起
      $("#tipsAlert").animate({ top: "-9999px" });
    },
    goTop() {
      document.body.scrollTop = document.documentElement.scrollTop = 0;
    },

    async getreportdetail() {
      const { Data, Ret } = await reportDetail({ ReportCode: this.id });
      document.title = (Data && Data.H5ShareName) || "研报";
      if (Ret !== 200) return;
      // console.log('data',Data)
      // let H5ShareName = Data.H5ShareName;
      // let H5ReportShareImg = Data.H5ReportShareImg;
      // let wxShareTitle = !!Data.Hz
      //   ? "【第" + Data.Report.Stage + "期|FICC" + "】" + Data.Report.Title + "(" + Data.Report.CreateTime.substring(5, 7) + Data.Report.CreateTime.substring(8, 10) + ")"
      //   : Data.Report.Title + "(" + Data.Report.CreateTime.substring(5, 7) + Data.Report.CreateTime.substring(8, 10) + ")";
      this.reportInfo = Data.Report || {};
      this.configInfo = {
        ...Data,
        Report:null
      };

      this.headImgStyle=this.reportInfo.HeadStyle?JSON.parse(this.reportInfo.HeadStyle):[]
      this.endImgStyle=this.reportInfo.EndStyle?JSON.parse(this.reportInfo.EndStyle):[]
      this.layoutBaseInfo['研报标题']=this.reportInfo.Title
      this.layoutBaseInfo['研报作者']=this.reportInfo.Author
      // 已发布已通过的报告才显示发布时间
      this.layoutBaseInfo['创建时间']=[2,6].includes(this.reportInfo.State)?this.reportInfo.PublishTime:''
      
      this.isshow = true;
      this.$nextTick(()=>{
        this.pageResize()
      })

      // wxShare(document.location.href, H5ShareName || document.title, wxShareTitle, H5ReportShareImg, 0);

      // this.splitContentHandle();

      $(document).on("click", "#resetcss img", function (event) {
        let imgArray = [];
        let curImageSrc = $(this).attr("src");
        let oParent = $(this).parent();
        if (curImageSrc && !oParent.attr("href")) {
          $("#resetcss img").each(function (index, el) {
            let itemSrc = $(this).attr("src");
            imgArray.push(itemSrc);
          });
          wx.previewImage({ current: curImageSrc, urls: imgArray });
        }
      });
    },

    /*内容分割*/
    splitContentHandle() {
      let content='';
      if(this.reportInfo.HasChapter===1) {
        this.reportInfo.ChapterList.forEach(item => {
          content+=item.Content
        })
      }else {
        content = this.reportInfo.Content;
      }

      const regex = new RegExp(`<div[^>]*class="?report-drag-item-wrap"?>`);
      const arr = content.split(regex);
      this.totalContent = arr.map((_) => _ + "<div class='report-drag-item-wrap'>");
      this.realContent = this.totalContent.slice(0, this.pageSize);
      this.total_page = parseInt(this.totalContent.length / this.pageSize) + 1;
      // console.log(this.realContent, this.totalContent);
    },

    /* 加载下一页内容 */
    loadContent() {
      this.realContent = this.realContent.concat(this.totalContent.slice(this.page_no * this.pageSize, (this.page_no + 1) * this.pageSize));
    },
    loadMoreHandle: _.throttle(function () {
      if (this.page_no >= this.total_page) return;

      const scrollTop = document.documentElement.scrollTop || document.body.scrollTop; // 滚动的高度
      const clientHeight = document.documentElement.clientHeight || document.body.clientHeight; // 可视高度
      const scrollHeight = document.body.scrollHeight; // 总高度

      if (scrollHeight - scrollTop - clientHeight < 500) {
        console.log("触底");
        this.page_no = this.page_no + 1;
        this.loadContent();
      }
    }, 300),
  },
};
</script>

<style lang="scss">
body{
  line-height: normal;
}
.wrapper {
  position: relative;
  // overflow: hidden;
  height: 100%;
  #noaccess {
    width: 100%;
    height: 12rem;
    padding: 5rem 0.6rem 1rem;
    box-sizing: border-box;
    text-align: center;
    position: absolute;
    bottom: 0;
    background: url(~@/assets/img/accessbg.png) no-repeat;
    background-size: 100% auto;
    overflow: hidden;
    > a,
    button {
      width: 12rem;
      height: 2rem;
      background: url(~@/assets/img/newbtn600.png) no-repeat;
      background-size: 100% 100%;
      font-size: 0.48rem;
      color: #fff;
      border: none;
      margin-top: 1.5rem;
    }
  }
}
#reportdtl {
  font-size: 14px;
  background: #fff;
  overflow: auto;
  width: 1200px;
  padding: 20px 20px 20px 44px;
  margin: 0 auto;

  .top-header {
    background-image: url("@/assets/img/smart_report_bg01.png");
    background-size: cover;
    background-repeat: no-repeat;
    background-position: center;
    padding: 16px;
    color: #fff;
    .title {
      font-size: 16px;
      margin-bottom: 10px;
    }
    .flex {
      font-size: 12px;
      display: flex;
      justify-content: space-between;
    }
  }
  .html-head-img-box,.html-end-img-box{
    position: relative;
    overflow: hidden;
    .head-layout-item{
        position: absolute;
        overflow: hidden;
        box-sizing: border-box
    }
  }
  .no-layout-img-box{
    header{
        padding:10px 0; box-sizing:border-box; font-size:28px; font-weight:500; color:#333; background:#fff;
        display: flex;
        justify-content: space-between;
        .logo {
          flex-shrink: 0;
          height: fit-content;
        }
    }
  }
  .abstract{
    box-sizing:border-box; font-size:22px; line-height:36px;
    border-left: 0.2rem solid #007aff;
    margin:0.6rem 0;
    div{
        padding: 5px 20px;
        box-sizing: border-box;
        color: #333;
        border-radius: 10px;
    }
  }
  #playcon {
    margin-top: 10px;
    box-sizing: border-box;
    background: #fff;
    overflow: hidden;
    > div {
      width: 100%;
      height: 2.96rem;
      padding: 0.48rem 0.6rem;
      box-sizing: border-box;
      background: #f1f1f1;
      border-radius: 0.2rem;
    }
  }
  #resetcss {
    pre {
      white-space: pre-wrap;
    }
    .report-drag-item-wrap{
                padding: 6px;
                margin-bottom: 3px;
            }
  }
  // #element .element .element-content .main-container .content tbody td {
  //   padding: 0.01rem 0;
  //   word-break: break-all;
  // }
  #tipsAlert {
    width: 100%;
    height: 100%;
    padding: 15% 1rem;
    box-sizing: border-box;
    position: fixed;
    top: -99999px;
    left: 0;
    background: rgba(0, 0, 0, 0.6);
    z-index: 200;
    .disclaimer {
      font-size: 0.58rem;
    }
  }

  .chapter-wrap{
    .chapter-item{
			margin-bottom: 20px;
			padding-bottom: 20px;
			border-bottom: 1px dashed #ccc;
      &:last-child {
        border-bottom: none;
      }
			.type{
				font-size: 15px;
				color: #fff;
				padding: 5px 10px;
				background-color: #E6A23C;
				border-radius: 4px;
			}
			.title{
				font-size: 15px;
				margin-left: 10px;
				font-weight: bold;
			}
		}
  }
}
@media screen and (max-width:1200px){
  #reportdtl{
    position: absolute;
    transform-origin: 0 0;
  }
}
</style>
