<template>
  <div id="audioDetail" class="app">
    <van-sticky :offset-top="0">
      <van-nav-bar :title="audioDetail.title" left-arrow @click-left="goBack">
      </van-nav-bar>
    </van-sticky>
    <van-sticky :offset-top="50">
      <div class="head">
        <audio-player
          ref="audioEle"
          color="#e42417"
          @playing="playing"
          :show-prev-button="false"
          :show-next-button="false"
          :audio-list="[audioDetail.videoUrl]"
          :before-play="onBeforePlay"
        ></audio-player>
      </div>
    </van-sticky>
    <div v-if="audioDetail.id">
      <van-cell-group>
        <van-cell
          :title="'阅读量: ' + (audioDetail.readCount || 0)"
          :value="audioDetail.publishTime"
        />
        <div
          @click="showIntrodu = !showIntrodu"
          :class="{ introduction: true, 'van-ellipsis': !showIntrodu }"
          v-text="audioDetail.normalizedContent"
          v-if="audioDetail.normalizedContent"
        ></div>
      </van-cell-group>
    </div>
    <van-empty v-else description="内容没找到" />
    <comment-list
      :commentList="audioDetail.commentList"
      :isFablous="!!audioDetail.fabulous"
      :isCollect="!!audioDetail.collection"
      :diggCount="audioDetail.diggCount"
      :commentCount="audioDetail.commentCount"
      v-if="$pageParams"
      :id="$pageParams.id"
      :more="more"
      @changeFab="changeFab"
      @changeColl="collection"
      @upset="upset"
    ></comment-list>
  </div>
</template>

<script>
import {
  digg,
  collect,
  progress,
  diggCancel,
  articleDetail,
  collectCancel,
  getCommentList
} from "@/api/article/article.js";
import CommentList from "../../../components/Comment/Comment";
import { currentUrlToParams } from "@/utils/common.js";
import {
  Cell as vanCell,
  CellGroup as vanCellGroup,
  NavBar as vanNavBar,
  Sticky as vanSticky,
  Empty as vanEmpty,
  Toast
} from "vant";
import "video.js/dist/video-js.css";
import "vant/lib/cell-group/style";
import "vant/lib/nav-bar/style";
import "vant/lib/sticky/style";
import "vant/lib/empty/style";
import "vant/lib/cell/style";
import "vant/lib/toast/style";
import "@liripeng/vue-audio-player/lib/vue-audio-player.css";
import AudioPlayer from "@liripeng/vue-audio-player";
import Config from "@/config/index.js";
export default {
  name: "app",
  maxPro: 0,
  data: function() {
    return {
      chosenAddressId: "1",
      showIntrodu: true,
      audioDetail: {},
      value: "",
      more: false,
      audioList: []
    };
  },
  timeout: null,
  methods: {
    goBack() {
      history.go(-1);
    },
    // 播放前做的事
    onBeforePlay(next) {
      this.playing();
      next(); // 开始播放
    },
    playing() {
      const duration = (this.$refs.audioEle.duration * 1000).toFixed(0) - 0;
      const currentTime =
        (this.$refs.audioEle.currentTime * 1000).toFixed(0) - 0;
      let timeout = new Date() - 0;
      let pro = (currentTime / duration).toFixed(2);
      if (timeout - this.timeout < Config.proTimeout || pro <= this.maxPro)
        return;
      this.timeout = timeout;
      this.maxPro = pro;
      progress({
        articleId: this.$pageParams.id,
        progress: pro
      });
    },
    collection() {
      this.$set(this.audioDetail, "collection", !this.audioDetail.collection);
      this.audioDetail.collection
        ? collect(this.$pageParams.id).then(() => {
            Toast("收藏成功！");
          })
        : collectCancel(this.$pageParams.id).then(() => {
            Toast("取消收藏！");
          });
    },
    changeFab(val) {
      // 点赞
      this.$set(this.audioDetail, "fabulous", val);
      val
        ? digg(this.$pageParams.id).then(() => {
            Toast("点赞成功！");

            this.$set(
              this.audioDetail,
              "diggCount",
              this.audioDetail.diggCount + 1
            );
          })
        : diggCancel(this.$pageParams.id).then(() => {
            Toast("取消点赞！");
            this.$set(
              this.audioDetail,
              "diggCount",
              this.audioDetail.diggCount - 1
            );
          });
    },
    upset() {
      let list = this.audioDetail.commentList
        ? this.audioDetail.commentList
        : [];
      this.more = true;
      let p = {};
      list.length ? (p.lastId = list[list.length - 1].id) : "";
      getCommentList(p, this.$pageParams.id).then(res => {
        this.more = false;
        list.push(...(res || []));
        this.$set(this.audioDetail, "commentList", list);
        this.$set(
          this.audioDetail,
          "commentCount",
          this.audioDetail.commentCount + 1
        );
      });
    }
  },
  mounted() {
    this.$pageParams = currentUrlToParams();
    this.timeout = new Date();
    articleDetail(this.$pageParams.id).then(res => {
      this.audioDetail = res || {};
    });
  },
  beforeDestroy: function() {},
  components: {
    vanCellGroup,
    CommentList,
    AudioPlayer,
    vanNavBar,
    vanSticky,
    vanEmpty,
    vanCell
  }
};
</script>
<style lang="scss" scoped>
#audioDetail {
  .van-button {
    border: none;
  }
  .video-js {
    .vjs-big-play-button {
      top: 50%;
      left: 50%;
    }
  }
  .introduction {
    padding: 0.5em;
    font-size: 0.14rem;
    color: #969799;
    text-indent: 2em;
  }
  .head {
    padding: 10em 2em 2em 2em;
  }

  .selectText {
    color: #ee0a24;
  }
}
</style>
