<template>
  <view class="container note-and-viewpoint" v-if="detailDataForm.HasPermission == 1">
    <zmm-watermark :watermark="watermarkText"></zmm-watermark>
    <view class="content-item">
      <view class="author-name">
        <view class="author-box">
          <view class="img-box">
            <image :src="detailDataForm.HeadImg" @click="goDetailPages"></image>
          </view>
          <text style="margin-left: 10rpx">{{ detailDataForm.NickName }}</text>
        </view>
        <view class="follow-content" @click="followAuthorHandler">{{ detailDataForm.IsFollowAuthor ? "取消关注" : "关注专栏" }}</view>
      </view>
      <view class="type-time">
        <view class="type"> {{ detailDataForm.Type == 1 ? "笔 记" : "观 点" }} </view>

        <view class="time">{{ detailDataForm.PublishTime }}</view>
      </view>
      <view class="title-item global_title"> {{ detailDataForm.Title }}</view>
      <view class="text-conten">
        <mp-html :content="isUserBindingPhoneNumber ? detailDataForm.Content : detailDataForm.Content.slice(0, 80)" />
      </view>
      <block v-if="isUserBindingPhoneNumber">
        <block v-if="detailDataForm.Docs && detailDataForm.Docs.length > 0">
          <view @click="goFilePages(item)" class="file-item text_oneLine" v-for="(item, index) in detailDataForm.Docs" :key="index">
            <image :src="item.DocIcon"></image>
            {{ item.DocName }}
          </view>
        </block>
        <view class="image-conten">
          <image v-for="key in dataProcessing(detailDataForm.ImgUrl)" :key="key" :src="key" @click="previewImageMediahandler(key)"></image>
        </view>
        <view class="lable-conten">
          <view class="item" v-for="key in dataProcessing(detailDataForm.Tags)" :key="key">{{ key }}</view>
        </view>
      </block>
    </view>
    <block v-if="isUserBindingPhoneNumber">
      <view class="statement-content" v-if="detailDataForm.Status == 3">
        <text>郑重声明：</text>
        本文为用户投稿，用户在平台中发表的所有资料、言论等仅代表个人或嘉宾观点，与本网站、任何公司与任何机构立场无关。本平台对文中陈述、观点判断保持中立，不对所包含内容及数据的真实性、准确性、可靠性或完整性提供任何明示或暗示的保证。
        股市波动与很多因素有关，任何用户或嘉宾的发言，都有其特定立场，投资决策是个人基于自己的研究分析所做的决定，本文章或会议目的在于事实、观点分享，不构成任何的投资建议。投资者应当自主进行投资决策，对投资者因依赖上述信息进行投资决策而导致的财产损失，本平台不承担法律责任。
        本文章或会议未经本平台和作者的书面许可，任何机构和个人不得以任何形式转发、转载、翻版、复制、刊登、发表、修改、仿制或引用文章或会议的全部或部分内容。本平台对任何第三方的未经授权行为所产生的的影响不承担任何责任，同时保持实施法律行动的权利。
      </view>
      <view class="collect-conten">
        <image @click="collectHandler(2)" v-if="detailDataForm.IsCollect == 1" src="https://hzchart.oss-cn-shanghai.aliyuncs.com/new_cygx/collected_icon.png"></image>
        <image @click="collectHandler(1)" v-else src="https://hzchart.oss-cn-shanghai.aliyuncs.com/new_cygx/collect_ico.png"></image>
        {{ detailDataForm.IsCollect == 1 ? "已收藏" : "收藏" }}
      </view>
    </block>
    <view v-else class="please-login" @click="pleaseGoLogin">请登录后查看更多内容</view>
    <Loading />
  </view>
  <view class="noauth-cont" v-else>
    <block v-if="isSpecialArticle_report">
      <image src="https://hzstatic.hzinsights.com/cygx/czbk/noauth.png" class="noauth-ico"></image>
      <view class="tip" v-if="detailDataForm.HasPermission === 3">
        需要升级行业套餐权限才可查看此报告，请联系对口销售
        <view class="btn-cont back-btn" @click="backIndex"> 返回 </view>
      </view>
    </block>
    <block v-else>
      <image src="https://hzstatic.hzinsights.com/cygx/czbk/noauth.png" class="noauth-ico"></image>
      <block v-if="detailDataForm.HasPermission !== 3">
        <view class="research-noauth">
          暂无 <text class="strong-text">买方研选</text> 权限
          <text style="margin-bottom: 30rpx"> 您可申请开通试用 </text>
        </view>
        <view class="btn-cont" @click="applyAuth">{{ "立即申请" }} </view>
      </block>
      <view class="tip" v-if="detailDataForm.HasPermission === 3">
        <view class="research-noauth">
          暂无 <text class="strong-text">买方研选</text> 权限
          <text> 点击提交申请，提醒对口销售为你开通试用 </text>
        </view>
      </view>
      <view v-if="detailDataForm.HasPermission === 3" class="btn-cont" @click="sellerApplyAuth"> 提交申请</view>
      <view class="btn-cont back-btn" @click="backIndex"> 返回 </view>
    </block>
  </view>
</template>

<script>
import { purchaserApi, User } from "@/config/api";
import zmmWatermark from "@/components/zmm-watermark/zmm-watermark.vue";
let app = getApp();
export default {
  data() {
    return {
      detailDataForm: "",
      detailId: 0,
      readTiem: 0,
      setIntervalTiem: null,
      watermarkText:''
    };
  },
  components:{zmmWatermark},
  methods: {
    previewImageMediahandler(key) {
      uni.previewImage({
        urls: [key], //查看图片的数组
      });
    },
    // 获取专栏详情
    async getDetaliData() {
      const res = await purchaserApi.yanxuanSpecialDetail({
        Id: this.detailId,
      });
      if (res.Ret === 200) {
        this.detailDataForm = res.Data;
        this.detailDataForm.HasPermission == 1 && this.setDisableCapture();
        this.watermarkText = `${this.$db.get("mobile")}`;
        let str = this.detailDataForm.Type == 1 ? "笔记" : "观点";
        wx.setNavigationBarTitle({
          title: `${str}详情`,
        });
      }
    },
    // 数据处理
    dataProcessing(item) {
      return item ? item.split(",") : [];
    },
    // 收藏
    async collectHandler(type) {
      await this.$store.dispatch("checkHandle");
      if (!this.$store.state.isAuth && !this.$store.state.isBind) {
        const res = await purchaserApi.yanxuanSpecialCollect({
          Id: this.detailDataForm.Id,
          Status: type,
        });
        if (res.Ret === 200) {
          this.$util.toast(res.Msg);
          this.getDetaliData();
        }
      }
    },
    // 去往预览文件
    goFilePages(item) {
      wx.downloadFile({
        // 示例 url，并非真实存在
        url: item.DocUrl,
        success: function (res) {
          const filePath = res.tempFilePath;
          wx.openDocument({
            filePath: filePath,
          });
        },
      });
    },
    goDetailPages() {
      uni.navigateTo({ url: "/pages-purchaser/columnDetail/columnDetail?id=" + this.detailDataForm.UserId });
    },
    /* 无权限申请开通权限 */
    applyAuth() {
      /* 区分是否是潜在用户 */
      this.detailDataForm.HasPermission === 2
        ? User.applyTry({
            ApplyMethod: 3,
            TryType: "YanxuanSpecial",
            DetailId: this.detailId,
          }).then((res) => {
            if (res.Ret === 200) {
              uni.navigateTo({
                url: "/pageMy/applyResult/applyResult",
              });
            }
          })
        : this.detailDataForm.HasPermission === 4
        ? uni.navigateTo({
            url: "/pageMy/applyTrial/applyTrial?tryType=YanxuanSpecial&detailId=" + this.detailId,
          })
        : uni.showModal({
            title: "",
            content: "您已经提交过申请了，请耐心等待",
            showCancel: false,
            confirmColor: "#365595",
            success: function (res) {},
          });
    },

    // 销售的立即申请
    sellerApplyAuth() {
      User.applyTry({
        TryType: "YanxuanSpecial",
        DetailId: this.detailId,
      }).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",
          });
        },
      });
    },
    // 关注作者
    async followAuthorHandler() {
      const res = await purchaserApi.yanxuanSpecialFollow({
        FollowUserId: this.detailDataForm.UserId,
        Status: this.detailDataForm.IsFollowAuthor ? 2 : 1,
      });
      if (res.Ret === 200) {
        this.detailDataForm.IsFollowAuthor = !this.detailDataForm.IsFollowAuthor;
        this.detailDataForm.IsFollowAuthor
          ? uni.showModal({
              title: "已关注专栏",
              content: "请关注【查研观向小助手】 公众号，及时获取专栏下内容更新提醒",
              confirmText: "知道了",
              showCancel: false,
              confirmColor: "#376cbb",
            })
          : uni.showToast({
              title: "已取消关注",
              icon: "none",
              duration: 2000,
            });
      }
    },

    setDisableCapture() {
      wx.setVisualEffectOnCapture({
        visualEffect: "hidden",
        complete: (res) => {
          console.log(res);
        },
      });
    },
    closeDisableCapture() {
      wx.setVisualEffectOnCapture({
        visualEffect: "none",
      });
    },
  },
  onLoad(options) {
    this.detailId = Number(options.id) || 0;
  },
  onShow() {
    this.detailId > 0 && this.getDetaliData();
    this.readTiem = 0;
    this.setIntervalTiem = setInterval(() => {
      this.readTiem++;
    }, 1000);
  },
  onHide() {
    //页面退出
    if (this.detailId && this.detailId > 0 && this.detailDataForm.HasPermission == 1) {
      clearInterval(this.setIntervalTiem);
      purchaserApi
        .YanxuanSpecialRecord({
          SpecialId: this.detailId,
          StopTime: this.readTiem,
        })
        .then((res) => {});
    }
    this.closeDisableCapture()
  },
  onUnload() {
    if (this.detailId && this.detailId > 0 && this.detailDataForm.HasPermission == 1) {
      //页面返回
      clearInterval(this.setIntervalTiem);
      purchaserApi
        .YanxuanSpecialRecord({
          SpecialId: this.detailId,
          StopTime: this.readTiem,
        })
        .then((res) => {});
    }
    this.closeDisableCapture()
  },
  /** 用户点击分享 */
  onShareAppMessage: function (res) {
    return {
      title: this.detailDataForm.Title,
      path: "/pages-purchaser/noteAndViewpoint/noteAndViewpoint?id=" + this.detailId,
    };
  },
};
</script>

<style lang="scss" scope>
.note-and-viewpoint {
  position: relative;
  padding: 30rpx;
  background: $uni-bg-color;
  .content-item {
    padding: 40rpx;
    background-color: #fff;
    .type-time {
      display: flex;
      align-items: center;
      justify-content: space-between;
      color: #666666;
      .type {
        width: 110rpx;
        height: 42rpx;
        line-height: 42rpx;
        text-align: center;
        border-radius: 38rpx;
        color: #928563;
        background-color: #fff6de;
        font-weight: 500;
      }
    }
    .title-item {
      margin: 10rpx 0 20rpx;
      line-height: 46rpx;
    }
    .file-item {
      width: 100%;
      height: 42rpx;
      margin: 20rpx 0;
      display: flex;
      align-items: center;
      background-color: #f8f8fa;
      color: #376cbb;
      image {
        margin-right: 15rpx;
        width: 27rpx;
        height: 27rpx;
      }
    }
    .text-conten {
      font-size: 32rpx;
      line-height: 44rpx;
    }
    .image-conten {
      display: flex;
      flex-wrap: wrap;
      image {
        width: 144rpx;
        height: 144rpx;
        margin-right: 20rpx;
      }
    }
    .lable-conten {
      display: flex;
      flex-wrap: wrap;
      margin: 20rpx 0;
      font-size: 24rpx;
      .item {
        display: flex;
        align-items: center;
        height: 34rpx;
        border-radius: 44rpx;
        padding: 0 35rpx;
        background-color: #f0f1f3;
        margin: 0 20rpx 20rpx 0;
      }
    }

    .author-name {
      display: flex;
      align-items: center;
      justify-content: space-between;
      margin-bottom: 50rpx;
      .author-box {
        display: flex;
        align-items: center;
      }
      .img-box {
        width: 48rpx;
        height: 48rpx;
        overflow: hidden;
        font-size: 28rpx;
        color: #333;
        image {
          width: 100%;
          height: 100%;
        }
      }
      .follow-content {
        display: flex;
        align-items: center;
        padding: 0 25rpx;
        height: 48rpx;
        border-radius: 150rpx;
        background-color: #376cbb;
        color: #fff;
        font-size: 24rpx;
      }
    }
  }
  .collect-conten {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    padding: 20rpx 0 10rpx;
    background-color: #fff;
    padding-bottom: constant(safe-area-inset-bottom);
    padding-bottom: env(safe-area-inset-bottom);
    display: flex;
    flex-direction: column;
    align-items: center;
    color: #666666;
    font-size: 24rpx;
    line-height: 28rpx;
    image {
      display: inline-block;
      flex-shrink: 0;
      width: 44rpx;
      height: 44rpx;
    }
  }
  .statement-content {
    margin-top: 20rpx;
    padding-bottom: 200rpx;
    font-size: 28rpx;
    line-height: 48rpx;
    color: #666;
    text {
      font-weight: 500;
      font-size: 34rpx;
      line-height: 48rpx;
    }
  }
  .please-login {
    margin: 50rpx auto 150rpx;
    width: 556rpx;
    height: 64rpx;
    color: #fff;
    font-size: 24rpx;
    font-weight: 600;
    background-color: #376cbb;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 9rpx;
  }
}
.noauth-cont {
  padding-top: 170rpx;
  padding-bottom: 100rpx;
  text-align: center;
  font-size: 28rpx;
  .noauth-ico {
    width: 365rpx;
    height: 229rpx;
    margin-bottom: 70rpx;
  }
  .tip {
    width: 532rpx;
    margin: 0 auto 100rpx;
    .contract {
      padding: 40rpx 90rpx 0;
      line-height: 44rpx;
      text {
        display: inline-block;
      }
    }
  }
  .btn-cont {
    width: 500rpx;
    height: 52rpx;
    background: $uni-color-new;
    color: #fff;
    font-size: 24rpx;
    font-weight: 600;
    border-radius: 8rpx;
    margin: 0 auto;
    text-align: center;
    line-height: 52rpx;
    &.back-btn {
      background: #e5efff !important;
      color: $uni-color-new;
      margin-top: 30rpx;
    }
    .btn_bg {
      width: 100%;
      height: 80rpx;
      position: absolute;
      left: 0;
      top: 0;
    }
    .btn-txt {
      width: 100%;
      position: absolute;
      z-index: 1;
    }
  }
  .research-noauth {
    color: #333;
    font-size: 36rpx;
    width: calc(100% + 80rpx);
    margin-left: -40rpx;
    .strong-text {
      display: inline-block;
      padding: 0 10rpx;
      font-weight: 500;
    }
    text:last-child {
      font-size: 32rpx;
      font-size: 34rpx;
      color: #999999;
      margin-top: 10rpx;
    }
  }
}
</style>
