<template>
  <view class="container reportDetail-container">
    <block v-if="haveAuth === 1">
      <web-view :src="linkurl">
        <cover-view bindtap="searchViewTap" class="search-view">
          <cover-view class="search-view-box" @click="btnSearch">
            <cover-image class="search-icon" src="https://hzchart.oss-cn-shanghai.aliyuncs.com/cygx/czbk/search.png"></cover-image>
            <cover-view class="search-text">搜索您想要的纪要</cover-view>
          </cover-view>
        </cover-view>
        <cover-view class="footer-con">
          <cover-view class="handle-item" @click="applyHandle">
            <cover-image class="download-img" src="https://hzchart.oss-cn-shanghai.aliyuncs.com/cygx/czbk/apply_act.png" v-if="reportInfo.IsInterviewApply"></cover-image>
            <cover-image class="download-img" src="https://hzchart.oss-cn-shanghai.aliyuncs.com/cygx/czbk/apply_ico.png" v-else></cover-image>
            <cover-view>{{ reportInfo.IsInterviewApply ? "已申请访谈" : "申请访谈" }}</cover-view>
          </cover-view>
          <cover-view class="handle-item" @click="collectHandle">
            <cover-image class="download-img" src="https://hzchart.oss-cn-shanghai.aliyuncs.com/cygx/czbk/collect_act.png" v-if="reportInfo.IsCollect"></cover-image>
            <cover-image class="download-img" src="https://hzchart.oss-cn-shanghai.aliyuncs.com/cygx/czbk/collect_ico.png" v-else></cover-image>
            <cover-view>{{ reportInfo.IsCollect ? "已收藏" : "收藏" }}</cover-view>
          </cover-view>
          <cover-view class="handle-item" v-if="fileLink" @click="downloadFile">
            <cover-image class="download-img" src="https://hzchart.oss-cn-shanghai.aliyuncs.com/cygx/czbk/down_ico.png"></cover-image>
            <cover-view>下载</cover-view>
          </cover-view>
        </cover-view>
        <cover-view class="footer-free-charge free-charge" v-if="isShowFree">
          <cover-image class="img" @click="toggle" src="https://hzchart.oss-cn-shanghai.aliyuncs.com/cygx/czbk/free_icon.png"></cover-image>
          <cover-image @click="removeBton" class="remove-icon" src="https://hzchart.oss-cn-shanghai.aliyuncs.com/cygx/czbk/free_%20remove.png"></cover-image>
        </cover-view>
      </web-view>
    </block>
  </view>
</template>

<script>
import { Report, User, Reports,FreeButton } from "@/config/api.js";
import freeCharge from "@/components/freeCharge";
let app = getApp({ allowDefault: true });
export default {
  data() {
    return {
      haveAuth: false,
      // haveFree:false,//是否付费
      access_token: "", //用户标识
      isIphoneX: false, //判断机型
      reportInfo: "",
      showNav: false,
      id: "",
      linkurl: "",
      isShowTip: false,
      sale_name: "", //可联系销售名称
      sale_number: "", //可联系销售电话
      industry: "", //行业
      readTiem: 0,
      setIntervalTiem: null,
      fileLink: false,
    };
  },
  components: {
    freeCharge,
  },
  methods: {
    /* 获取详情 */
    getDetail() {
      Report.reportDtl({
        ArticleId: Number(this.id),
      }).then((res) => {
        if (res.Ret === 200) {
          uni.setNavigationBarTitle({
            title: res.Data.Detail.IsSummary == 1 ? "纪要详情" : "报告详情",
          });
          this.haveAuth = res.Data.HasPermission;
          this.industry = res.Data.Detail.CategoryName;
          this.sale_name = res.Data.Detail.SellerName;
          this.sale_number = res.Data.Detail.SellerMobile;
          if (res.Data.HasPermission === 1) {
            //有访问权限
            this.reportInfo = res.Data.Detail;
            this.linkurl = res.Data.Detail.HttpUrl;
            this.fileLink = res.Data.Detail.FileLink;
            this.access_token = this.access_token || this.$db.get("access_token");
            this.$store.dispatch("statistics", { PageType: "ReportParticulars", DetailId: this.id });
          }
        }
      });
    },
    /* 无权限申请开通权限 */
    applyAuth() {
      /* 区分是否是潜在用户 */
      this.haveAuth === 2
        ? User.applyTry({
            ApplyMethod: 3,
            TryType: "Article",
            DetailId: Number(this.id),
          }).then((res) => {
            if (res.Ret === 200) {
              uni.navigateTo({
                url: "/pageMy/applyResult/applyResult",
              });
            }
          })
        : this.haveAuth === 4
        ? uni.navigateTo({
            url: "/pageMy/applyTrial/applyTrial?tryType=Article&detailId=" + this.id,
          })
        : uni.showModal({
            title: "",
            content: "您已经提交过申请了,请耐心等待",
            showCancel: false,
            confirmColor: "#365595",
            success: function (res) {},
          });
    },
    //销售的立即申请
    sellerApplyAuth() {
      User.applyTry({
        TryType: "Article",
        DetailId: Number(this.id),
      }).then((res) => {
        if (res.Ret === 200) {
          uni.showModal({
            title: "",
            content: "提交申请成功,请耐心等待",
            showCancel: false,
            confirmColor: "#365595",
            success: function (res) {
              this.backIndex();
            },
          });
        }
      });
    },
    // 返回首頁
    backIndex() {
      uni.navigateBack({
        fail() {
          uni.switchTab({
            url: "/pages/index/index",
          });
        },
      });
    },
    callPhone(num) {
      uni.makePhoneCall({
        phoneNumber: num,
      });
    },
    /* 申请访谈 */
    applyHandle() {
      // 申请访谈
      !this.reportInfo.IsInterviewApply &&
        this.$util.modal("", "专家访谈申请会提交给您的对口销售,销售会线下与您取得联系,确定申请吗?", () => {
          this.interviewApi();
        });
      // 取消申请访谈 区分状态 '待邀请','待访谈','已完成','已取消'
      if (this.reportInfo.IsInterviewApply) {
        this.$util.modal(
          "",
          this.reportInfo.InterviewApplyStatus == "待访谈"
            ? "当前无法取消访谈,若有疑问,请联系对口销售"
            : this.reportInfo.InterviewApplyStatus == "待邀请"
            ? "您要取消此次访谈申请吗?"
            : "该访谈已完成",
          () => {
            this.reportInfo.InterviewApplyStatus == "待邀请"
              ? this.interviewApi()
              : this.reportInfo.InterviewApplyStatus == "待访谈"
              ? uni.makePhoneCall({
                  phoneNumber: "18767183922",
                })
              : "";
          }
        );
      }
    },
    /* 访谈接口 */
    interviewApi() {
      Report.applyRpt({
        ArticleId: Number(this.id),
      }).then((res) => {
        this.reportInfo.IsInterviewApply = !this.reportInfo.IsInterviewApply;
      });
    },
    /* 收藏 */
    collectHandle() {
      Report.collectRpt({
        ArticleId: Number(this.id),
      }).then((res) => {
        this.reportInfo.IsCollect = !this.reportInfo.IsCollect;
        this.$util.toast(res.Msg);
      });
    },
    /* 错误 */
    errorDetails() {
      uni.showModal({
        confirmText: "知道了",
        showCancel: false,
        confirmColor: "#3385FF",
        content: "网络不好,请刷新重试",
        success: (res) => {
          uni.navigateBack({
            fail() {
              uni.switchTab({
                url: "/pages/index/index",
              });
            },
          });
        },
      });
    },
    async downloadFile() {
      const res = await Report.articlePdfwatermark({
        ArticleId: Number(this.id),
      });
      if (res.Ret === 200) {
        uni.navigateTo({
          url: "/pageMy/downloadFile/downloadFile?url=" + res.Data.FileLink,
        });
      }
    },
    btnSearch() {
      uni.navigateTo({ url: "/pageMy/search/search" });
    },
    //跳转到免费送月卡页面
    toggle() {
      uni.navigateTo({
        url: "/pageMy/freeTrial/freeTrial",
      });
    },
    //隐藏当天的按钮
    async removeBton() {
      const res = await FreeButton.userFreeButtonUpdate();
    },
  },
  async onShow() {
    this.readTiem = 0;
    this.setIntervalTiem = setInterval(() => {
      this.readTiem++;
    }, 1000);
    let page = getCurrentPages(); //查看路径
    if (page.length === 1) {
      await this.$store.dispatch("checkHandle");
      if (!this.$store.state.isAuth && !this.$store.state.isBind) this.getDetail();
    } else {
      // 跳转入口
      this.getDetail();
    }
  },
  onLoad(option) {
    // /* 兼容iphonex底部 */
    uni.getSystemInfo({
      success: (res) => {
        let ua = res.model;
        if (ua.search("iPhone X") != -1 || ua.search("iPhone 11") != -1 || ua.search("iPhone 11 Pro Max") != -1) {
          this.isIphoneX = true;
        }
      },
    });
    this.id = option.id ? option.id : "";
    if (!this.id && !+this.id > 0) {
      this.errorDetails();
    }
  },
  /**
   * 用户点击分享
   */
  onShareAppMessage: function (res) {
    if (this.id) {
      return {
        title: this.isHorzMobile ? "好友向您推荐此内容,上传名片享查研观向免费月卡!" : this.reportInfo.Title,
        path: "/pageMy/reportDetail/reportDetail?id=" + this.id,
        success: (res) => {},
        fail: (err) => {},
      };
    }
  },
  onHide() {
    //页面退出
    if (this.id && +this.id > 0) {
      clearInterval(this.setIntervalTiem);
      Reports.addStopTime({
        ArticleId: Number(this.id),
        StopTime: this.readTiem,
        OutType: 2,
        Source: "MOBILE",
      }).then((res) => {});
    }
  },
  onUnload() {
    if (this.id && +this.id > 0) {
      //页面返回
      clearInterval(this.setIntervalTiem);
      Reports.addStopTime({
        ArticleId: Number(this.id),
        StopTime: this.readTiem,
        OutType: 1,
        Source: "MOBILE",
      }).then((res) => {});
    }
  },
};
</script>

<style lang="scss" scoped>
.reportDetail-container {
  background-color: #fff;
  .search-view {
    position: fixed;
    width: 100%;
    height: 110rpx;
    padding: 15rpx 34rpx;
    z-index: 99999;
    font-size: 24rpx;
    box-sizing: border-box;
    background: #fff;
    .search-view-box {
      display: flex;
      align-items: center;
      background: #f6f6f6;
      border: 1px solid #e5e5e5;
      border-radius: 35rpx;
      height: 70rpx;
      z-index: 99999;
    }
    .search-icon {
      width: 26rpx;
      height: 26rpx;
      margin: 0 15rpx;
      z-index: 99999;
    }
  }
  .footer-con {
    position: fixed;
    z-index: 99999;
    bottom: 0;
    padding-bottom: constant(safe-area-inset-bottom);
    padding-bottom: env(safe-area-inset-bottom);
    left: 0;
    width: 100%;
    background-color: #ffffff;
    display: flex;
    justify-content: space-around;
    height: 100rpx;
    border-top: 1px solid #dddddd;
    .handle-item {
      align-items: center;
      display: flex;
      flex-direction: column;
      justify-content: center;
      position: relative;
      z-index: 99999;
      .download-img {
        width: 44rpx;
        height: 44rpx;
        margin-bottom: 4rpx;
      }
    }
  }
  .footer-free-charge {
    .img {
      width: 156rpx;
      height: 156rpx;
    }
    .remove-icon {
      width: 31rpx;
      height: 31rpx;
      position: absolute;
      right: 0;
      top: 0;
    }
  }
}
</style>