<template>
  <div id="video" class="app" ref="elementAsconten">
    <van-sticky :offset-top="0">
      <video-player
        class="video-player-box"
        ref="videoPlayer"
        :options="playerOptions"
        :playsinline="true"
        customEventName="customstatechangedeventname"
        @play="onPlayerPlay($event)"
        @pause="onPlayerPause($event)"
        @ended="onPlayerEnded($event)"
        @waiting="onPlayerWaiting($event)"
        @playing="onPlayerPlaying($event)"
        @loadeddata="onPlayerLoadeddata($event)"
        @timeupdate="onPlayerTimeupdate($event)"
        @canplay="onPlayerCanplay($event)"
        @canplaythrough="onPlayerCanplaythrough($event)"
        @statechanged="playerStateChanged($event)"
        @ready="playerReadied"
      >
      </video-player>
    </van-sticky>
    <div v-if="item.id">
      <van-cell-group>
        <van-cell :title="item.title" :value="item.publishTime" />
        <div
          @click="showIntrodu = !showIntrodu"
          :class="{ introduction: true, 'van-ellipsis': !showIntrodu }"
          v-text="item.normalizedContent"
          v-if="item.normalizedContent"
        ></div>
      </van-cell-group>
    </div>
    <van-empty v-else description="内容没找到" />
  </div>
</template>

<script>
// import { progress } from "@/api/article/article.js";
import {
  Cell as vanCell,
  CellGroup as vanCellGroup,
  Sticky as vanSticky,
  Empty as vanEmpty
} from "vant";
import "vant/lib/cell-group/style";
import "vant/lib/sticky/style";
import "vant/lib/empty/style";
import "vant/lib/toast/style";
import "vant/lib/cell/style";
import "video.js/dist/video-js.css";
import { videoPlayer } from "vue-video-player";

import Config from "@/config/index.js";

export default {
  name: "app",
  maxPro: 0,
  props: {
    item: {
      type: Object,
      default: () => {
        return {};
      }
    },
    id: {
      type: String,
      default: ""
    }
  },
  data: function() {
    return {
      showIntrodu: true,
      video: {},
      playerOptions: {},
      more: false
    };
  },
  timeout: null,
  methods: {
    onPlayerPlay() {
      console.log("开始播放");
    },
    onPlayerPause() {
      console.log("暂停播放");
    },
    onPlayerEnded() {
      console.log("结束播放");
    },
    onPlayerWaiting() {
      console.log("等待播放");
    },
    onPlayerPlaying() {
      console.log("播放中");
    },
    onPlayerLoadeddata() {
      console.log("加载结束");
    },
    onPlayerTimeupdate(e) {
      let timeout = new Date() - 0;
      let pro = (e.cache_.currentTime / e.cache_.duration).toFixed(2);
      if (timeout - this.timeout < Config.proTimeout || pro <= this.maxPro)
        return;
      this.timeout = timeout;
      this.maxPro = pro;
      // progress({
      //   articleId: this.id,
      //   progress: pro
      // });
      window.Progress.progress = pro;
    },
    onPlayerCanplay() {
      console.log("允许播放");
    },
    onPlayerCanplaythrough() {
      console.log("允许播放通过");
    },
    playerStateChanged() {
      console.log("播放状态改变");
    },
    playerReadied() {
      console.log("准备好");
      window.webkit &&
        window.webkit.messageHandlers.setWebviewHeight.postMessage({
          height: this.$refs.elementAsconten.offsetHeight
        });
      window.H5Listener &&
        window.H5Listener.setWebviewHeight(
          this.$refs.elementAsconten.offsetHeight
        );
    }
  },
  mounted() {
    this.playerOptions = {
      // videojs options
      muted: true,
      width: "100%",
      aspectRatio: "16:9",
      language: "zh-CN",
      playbackRates: [0.5, 1.0, 1.5, 2.0],
      sources: [
        {
          type: "video/mp4",
          src: this.item.videoUrl
        }
      ],
      poster: "/static/images/author.jpg"
    };
    this.timeout = new Date() - 0;
  },
  beforeDestroy: function() {},
  components: {
    videoPlayer,
    vanCell,
    vanCellGroup,
    vanSticky,
    vanEmpty
  }
};
</script>
<style lang="scss" scoped>
#video {
  .video-js {
    .vjs-big-play-button {
      top: 50%;
      left: 50%;
    }
  }
  .introduction {
    padding: 0.5em;
    font-size: 0.14rem;
    color: #969799;
    text-indent: 2em;
  }
}
</style>
