<template>
  <div class="guidance">
    <div class="guide_title">{{ article.title }}</div>
    <div v-html="article.content" class="guide_contant" ref="scrollView" id="scroll-view"></div>
    <div class="guide_btn">
      <div class="guide_actBtn" @click="doCancel">取消</div>
      <div class="guide_actBtn blue" @click="doRead">已阅读</div>
    </div>
  </div>
</template>

<script>
import { queryArticleDetail } from "@/api/hospital";
export default {
  data() {
    return {
      isFlag: false,//是否阅读到底部
      article: {
        title: '',
        content: ''
      },//须知内容
    }
  },
  mounted() {
    if (this.$route.query.deptId) {
      this.queryAticle(this.$route.query.deptId);
    }
  },
  methods: {
    //须知详情
    queryAticle(id) {
      this.$toast.loading({
        message: '加载中',
        duration: 0,
      })
      try {
        queryArticleDetail(id).then(res => {
          this.$toast.clear();
          if (JSON.stringify(res.data) != '{}') {
            res.data.content = decodeURIComponent(res.data.content);
            this.article = res.data;
          }
        }, err => {
          this.$toast.clear();
        })
      } catch (error) {
        this.$toast.clear();
      }
    },
    //取消
    doCancel() {
      this.$router.go(-1);
    },
    //已阅读
    doRead() {
      let readBox = this.$refs.scrollView;
      console.log(readBox.scrollHeight, readBox.scrollTop, readBox.clientHeight)
      if (readBox.scrollHeight - readBox.scrollTop !== readBox.clientHeight) {
        console.log('未阅读完成');
        this.isFlag = false;
      } else {
        this.isFlag = true;
      }
      if (!this.isFlag) {
        //未阅读完整
        lightAppJssdk.notification.alert({
          message: "须知未完全阅读，请滑动屏幕完成阅读",
          title: "提示",//可传空
          buttonName: "继续阅读",
          success: function (data) {
            //onSuccess将在点击button之后回调
            /*回调*/
          },
          fail: function (data) { //错误返回
          }
        });
      }
      else {
        //阅读完整
        this.$router.replace({ name: 'examination', query: { id: this.$route.query.deptId } })
      }
    },
    //监听阅读进度
    scroll(e) {
      if (this.isFlag) {
        return;
      }
      const { scrollTop, clientHeight, scrollHeight } = e.target;
      console.log(scrollTop, clientHeight, scrollHeight)
      if ((scrollTop + clientHeight) >= (scrollHeight - 5) || scrollTop == 0) {
        this.isFlag = true;
        console.log('阅读完成')
      }
    },
  }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="scss">
.guidance {
  height: 100%;
  background-color: #fff;

  .guide_title {
    color: #333333;
    line-height: 0.5rem;
    font-size: 0.36rem;
    font-family: PingFang SC-Bold, PingFang SC;
    font-weight: bold;
    text-align: center;
    margin: 0.72rem 0 0;
  }

  .guide_contant {
    height: 6rem;
    color: #333333;
    font-size: 0.26rem;
    font-family: PingFang SC-Regular, PingFang SC;
    font-weight: 400;
    overflow-y: auto;
    margin: 0.93rem 0.4rem 0 0;
    padding: 0 0.4rem;

    img {
      max-width: 100% !important;
    }
  }

  .guide_btn {
    margin: 0.86rem 0.4rem 0;

    .guide_actBtn {
      float: left;
      display: inline-block;
      width: 3rem;
      color: #999999;
      font-size: 0.3rem;
      font-family: PingFang SC-Bold, PingFang SC;
      font-weight: bold;
      line-height: 0.98rem;
      text-align: center;
      background: #FFFFFF;
      border-radius: 0.08rem;
      border: 0.02rem solid #E1E1E1;

      &.blue {
        float: right;
        color: #fff;
        background: #1677FF;
        border-color: #1677FF;
      }
    }
  }
}
</style>
