<template>
  <div>
    <div class="container-cygx" @copy="copyMonitor" v-if="haveData == 1" :class="reportInfo.IsResearch ? 'no-cv' : ''">
      <canvas id="tutorial" ref="tutorial"></canvas>
      <topbar class="top-box-bar" showText="弘则研究" />
      <div class="search">
        <div class="search-box" @click="btnSearch">
          <Icon name="search" color="#8D8D8D" />
          <span>搜索您想要的纪要</span>
        </div>
      </div>
      <div class="z-index-content">
        <div class="content-top">
          <div class="report-title">{{ reportInfo.Title }}</div>
          <div class="report-text">
            <div class="report_desc">
              <span class="author">{{ reportInfo.Department }}</span>
              <span>{{ reportInfo.PublishDate }}</span>
            </div>
            <div class="seller-list" v-if="!reportInfo.IsResearch">
              <span>联系人:</span>
              <span v-for="(item, index) in reportInfo.SellerList" :key="index"> {{ item.SellerName }}({{ item.SellerMobile }})&nbsp;&nbsp; </span>
            </div>
            <div class="seller-list" v-else>
              <span>作者:{{ reportInfo.SellerAndMobile }} </span>
            </div>
            <div>注：请务必阅读<span class="tip" @click="showTips = true"> &nbsp;免责声明</span></div>
            <div class="container-abstract">&nbsp;&nbsp;摘要:&nbsp;{{ reportInfo.Abstract }}</div>
          </div>
        </div>
        <div class="detail-report">
          <div id="report-content" v-html="reportInfo.Body"></div>
        </div>
      </div>
      <div class="btn-returntop">
        <img src="~@/assets/cygx/returntop.png" @click="scrolltop" style="width: 40px" />
      </div>
      <dlg :showTips="showTips" :reportInfo="reportInfo" @hideDlg="showTips = false" />
    </div>
    <div v-else-if="haveData !== 0" class="nodata">
      <img src="@/assets/cygx/noauth.png" mode="" class="nodata_ico" />
      <div v-if="haveData == 2">
        <p>您暂无权限查看报告</p>
        <p>若想查看可联系海通销售开通试用权限</p>
      </div>
      <div v-else>
        <p>您的试用权限已到期</p>
        <p>若想继续试用，请联系海通销售</p>
      </div>
    </div>
    <topbar v-if="haveData != 1" class="top-haveData-bar" showText="弘则研究" />
  </div>
</template>

<script setup>
import { reactive, onMounted, toRefs, ref, onUnmounted } from "vue";
import { useRouter, useRoute } from "vue-router";
import { RaiApi } from "@/api/htgj/api.js";
import { Icon, Dialog, Toast } from "vant";
import dlg from "../cygx/dlg.vue";
import store from "@/store";
import topbar from "./topbar.vue";
const router = useRouter();
const route = useRoute();
const state = reactive({
  reportInfo: {},
});
const rerportId = ref(null);
const haveData = ref(1);
const fileLink = ref(false);
const showTips = ref(false);
const tutorial = ref(null);
/* 访谈接口 */
const interviewApi = () => {
  RaiApi.applyRpt({
    ArticleId: Number(rerportId.value),
  }).then((res) => {
    if (res.Ret === 200) {
      state.reportInfo.IsInterviewApply = !state.reportInfo.IsInterviewApply;
      state.reportInfo.InterviewApplyStatus = state.reportInfo.IsInterviewApply ? "待邀请" : "";
      Toast(res.Msg);
    }
  });
};

const scrolltop = () => {
  document.body.scrollTop = document.documentElement.scrollTop = 0;
};

/* 复制 */
const copyMonitor = () => {
  RaiApi.pageHistoryCopy({
    DetailId: rerportId.value + "",
  }).then((res) => {});
};

/* 打水印 */
const waterMark = (text) => {
  let canvas = document.createElement("canvas");
  let ctx = canvas.getContext("2d");
  ctx.font = "20px Arial";
  ctx.rotate((-45 * Math.PI) / 200);
  ctx.fillText(text, 30, 200);
  ctx.fillText(text, -40, 100);

  // 将canvas的内容转换为base64编码
  let data = canvas.toDataURL("image/png");

  // 将容器的的背景图片设置为生成的base64图片,并平铺
  tutorial.value.style.background = "url(" + data + ") repeat";
};

//点击回到搜索页面
const btnSearch = () => {
  router.push("/htgj/search");
};

const timeState = reactive({
  setIntervalTiem: null,
  readTiem: 0,
});

/* 获取报告详情 */
const getReport = (id) => {
  RaiApi.reportDtl({
    ArticleId: id,
  }).then((res) => {
    if (res.Ret === 200) {
      haveData.value = res.Data.HasPermission || 0;
      if (res.Data.HasPermission === 1) {
        //有访问权限
        state.reportInfo = res.Data.Detail;
        fileLink.value = res.Data.Detail.FileLink;
        if (state.reportInfo.IsResearch || state.reportInfo.IsBelongSummary) {
          waterMark(res.Data.CompanyName);
        }
        $(document).on("click", "#report-content img", function (event) {
          let imgArray = [];
          let src_tag = $(this).attr("src");
          let parent_tag = $(this).parent();
          if (src_tag && !parent_tag.attr("href")) {
            $("#report-content img").each(function (index, el) {
              let itemSrc = $(this).attr("src");
              imgArray.push(itemSrc);
            });
            wx.previewImage({ current: src_tag, urls: imgArray });
          }
        });
        timeState.readTiem = 0;
        timeState.setIntervalTiem = setInterval(() => {
          timeState.readTiem++;
        }, 1000);
      }
    } else {
      haveData.value = 0;
      Dialog.alert({
        title: "提示",
        message: res.Msg,
        confirmButtonColor: "#3385FF",
      }).then((res) => {
        router.back();
      });
    }
  });
};

onMounted(() => {
  if (route.query.id) {
    rerportId.value = route.query.id;
    getReport(rerportId.value);
  }
});
onUnmounted(() => {
  clearInterval(timeState.setIntervalTiem);
  if (!rerportId.value || haveData.value !== 1) return;
  RaiApi.addStopTime({
    ArticleId: Number(rerportId.value),
    StopTime: timeState.readTiem,
    OutType: 1,
    Source: "PC",
  }).then((res) => {});
});
const { reportInfo } = toRefs(state);
</script>

<style lang="scss">
.container-cygx {
  padding: 54px 34px 34px 34px;
  position: relative;
  z-index: 5;
  .z-index-content {
    position: relative;
    z-index: 5;
  }
  .fixed_cont {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    display: flex;
    justify-content: space-around;
    border-top: 1px solid #ddd;
    padding-bottom: calc(5px + constant(safe-area-inset-bottom));
    padding-bottom: calc(5px + env(safe-area-inset-bottom));
    background-color: #fff;
    z-index: 9;
    box-sizing: border-box;
    .handle-item {
      padding-top: 14px;
      text-align: center;
      line-height: 33px;
      font-size: 20px;
      color: #888888;
      width: 130px;
      img {
        width: 44px;
        height: 44px;
        padding: 0;
        margin: 0;
      }
      div {
        padding: 0;
        margin: 0;
      }
    }
  }
  .top-box-bar {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 60px;
    background-color: #fff;
    margin: 0;
    padding-top: 20px;
    z-index: 10;
    img {
      margin-top: 10px;
    }
  }
  .search {
    width: 100%;
    padding: 20px 20px;
    line-height: 70px;
    position: fixed;
    top: 60px;
    left: 0;
    background: #fff;
    z-index: 9;
    .search-box {
      display: flex;
      align-items: center;
      padding-left: 20px;
      width: 100%;
      height: 70px;
      border-radius: 35px;
      background: #f6f6f6;
      color: #8d8d8d;
      font-size: 24px;
    }
  }
  .content-top {
    margin-top: 115px;
    .report-title {
      font-size: 34px;
      font-weight: bold;
      color: #4a4a4a;
      margin: 60px 0 20px;
    }
    .report-text {
      color: #999999;
      font-size: 28px;
      .seller-list {
        margin: 20px 0;
      }
      .report_desc {
        display: flex;
        justify-content: space-between;
      }
      .tip {
        color: #3385ff;
      }
      .container-abstract {
        margin: 40px 0 20px;
        padding-bottom: 40px;
        border-bottom: 2px dashed #999;
        position: relative;
        line-height: 44px;
        color: #333;
        &::before {
          content: "";
          position: absolute;
          top: 0;
          left: 0;
          width: 8px;
          height: 44px;
          background: #2a65f5;
        }
      }
    }
  }
  .report-link {
    font-size: 28px;
    line-height: 80px;
  }
  .detail-report {
    padding-bottom: 130px;
    p,
    span {
      font-size: 28px !important;
    }
    img {
      width: 100% !important;
    }
    a {
      color: #333;
    }
    table {
      border-collapse: collapse;
      width: 100% !important;
      margin-left: 0 !important;
    }
    tr td,
    th {
      border: 1px solid #333;
    }
  }
  pre {
    width: 100%;
    overflow-y: auto;
    overflow-x: hidden;
    outline: none;
    border: 0;
    white-space: pre-wrap;
    word-break: normal;
  }
  .btn-returntop {
    position: fixed;
    right: 40px;
    bottom: 150px;
    width: 88px;
    height: 88px;
    z-index: 10;
  }
  .fixed_cont {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    display: flex;
    justify-content: space-around;
    border-top: 1px solid #ddd;
    padding-bottom: calc(5px + constant(safe-area-inset-bottom));
    padding-bottom: calc(5px + env(safe-area-inset-bottom));
    background-color: #fff;
    z-index: 9;
    box-sizing: border-box;
    .handle-item {
      padding-top: 14px;
      text-align: center;
      line-height: 33px;
      font-size: 20px;
      color: #888888;

      img {
        width: 44px;
        height: 44px;
        padding: 0;
        margin: 0;
      }
      div {
        padding: 0;
        margin: 0;
      }
    }
  }
}
.nodata {
  width: 100%;
  margin: 30% auto;
  text-align: center;
  img {
    width: 50%;
    margin-bottom: 30px;
  }
  p {
    margin-top: 20px;
  }
}
.no-cv {
  -webkit-user-select: none;
  -khtml-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}
.top-box-bar {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 40px;
  background-color: #fff;
  margin-top: 15px;
  z-index: 10;
}
#tutorial {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 1;
  width: 100%;
  height: 100%;
  opacity: 0.1;
  // transform: translateX(130px) rotate(-38deg);
}
.van-dialog--round-button .van-dialog__footer {
  border-top: 1px solid rgba(0, 0, 0, 0.1) !important;
  padding: 0 !important;
  .van-button--warning {
    color: #333 !important;
    border-right: 1px solid rgba(0, 0, 0, 0.1) !important;
  }
  .van-button--danger {
    color: #3385ff !important;
  }
  .van-button--warning,
  .van-button--danger {
    height: 80px !important;
    border-radius: 0 !important;
  }
  .van-goods-action-button--first {
    border-right: 1px solid rgba(0, 0, 0, 0.1) !important;
  }
}
.van-icon-search {
  font-size: 28px;
  margin-right: 10px;
}
.top-haveData-bar {
  position: absolute !important;
  width: 100%;
  margin-top: 20px;
  top: 0;
  left: 0;
  z-index: 9;
}
</style>
