<template>
  <div class="bg" ref="scroll">
    <div id="news" ref="croll">
      <div class="content" ref="content">
        <div class="grid-cell">
          <div class="render-detail-article">
            <div
              class="render-detail-title"
              v-if="item.title"
              v-text="item.title"
            ></div>
            <!-- <span
              style="font-size: 14px; color: #abb2b8;float:right"
              v-if="item.origSource || item.publishTime"
            >
              阅读量： {{ item.readCount | read }}
            </span> -->
            <span
              style="font-size: 14px; color: #abb2b8"
              v-if="item.origSource || item.publishTime"
              v-text="item.origSource + ' ' + item.publishTime"
            ></span>
            <div class="contentDetail" v-html="item.content"></div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import Config from "@/config/index.js";
// import { progress } from "@/api/article/article.js";
export default {
  name: "app",
  timeout: null,
  props: {
    item: {
      type: Object,
      default: () => {
        return {};
      }
    },
    id: {
      type: String,
      default: ""
    }
  },
  watch: {},
  data: function() {
    return {};
  },
  filters: {
    read(v) {
      if (isNaN(v)) return 0;
      let o = v;
      if (v > 10000) {
        o = (v / 10000).toFixed(2) + "万";
      }
      return o;
    }
  },
  methods: {
    setProgress() {
      if (!this.$refs.croll) return;
      let timeout = new Date() - 0;
      let scrollTop = this.$refs.croll.scrollTop;
      let height =
        this.$refs.content.offsetHeight + 70 - this.$refs.croll.clientHeight;
      let pro = (scrollTop / height).toFixed(2);
      pro = pro > 1 ? "1" : pro;
      pro = height <= 0 ? "1" : pro;
      if (
        (height > 0 && timeout - this.timeout < Config.proTimeout) ||
        pro <= this.maxPro
      )
        return;
      this.maxPro = pro;
      this.timeout = timeout;
      // progress({
      //   articleId: this.id,
      //   progress: pro
      // });

      window.Progress.progress = pro;
    }
  },
  mounted() {
    if (!this.item.title) return;
    this.timeout = new Date() - 1001;
    window.webkit &&
      window.webkit.messageHandlers.setWebviewHeight.postMessage({
        height: this.$refs.scroll.offsetHeight
      });
    window.H5Listener &&
      window.H5Listener.setWebviewHeight(this.$refs.scroll.offsetHeight);
    this.$refs.scroll.addEventListener("scroll", this.setProgress, true);
    this.$nextTick(() => {
      this.setProgress();
    });
  },
  beforeDestroy: function() {
    this.setProgress();
    clearTimeout(this.timeout);
  },
  components: {}
};
</script>
<style lang="scss">
#news {
  padding: 0.1rem;
  color: #262626;
  overflow-y: scroll;
  font-size: 16px;
  font-weight: 450;
  height: 100%;
  .render-detail-article .render-detail-title {
    text-align: center;
    font-size: 0.2rem;
    letter-spacing: 0;
    line-height: 1.2em;
    font-weight: 500;
    margin: 0.05rem auto;
  }
  p {
    line-height: 1.5em;
    text-indent: 2em;
    margin: 0.2rem 0;
    strong {
      font-weight: 450;
    }
    .render-detail-footer {
      text-align: right;
      padding-right: 5px;
    }
  }

  img {
    width: 100%;
    display: block;
  }
}
</style>
