<template>
  <div>
    <div class="app-container">
      <div class="level-title">
        <h1>课程信息</h1>
      </div>
      <div class="mb-20">
        <table class="table-ui">
          <tr>
            <td class="tbg">课程名称</td>
            <td colspan="3">{{ pd.CURRICULUMNAME }}</td>
          </tr>
          <tr>
            <td class="tbg">是否通用</td>
            <td colspan="3">{{ pd.CURRENCY === '1' ? '是' : '否' }}</td>
          </tr>
          <tr>
            <td class="tbg">培训行业类型</td>
            <td colspan="3">{{ pd.train_type_name }}</td>
          </tr>
          <tr>
            <td class="tbg">岗位培训类型</td>
            <td colspan="3">{{ pd.post_type_name }}</td>
          </tr>
          <tr>
            <td class="tbg">课程图片</td>
            <div v-viewer>
              <img :src="config.fileUrl + pd.CAPTURE" width="100" height="100">
            </div>
          </tr>
          <tr>
            <td class="tbg">课程介绍</td>
            <td colspan="3">{{ pd.CURRICULUMINTRODUCE }}</td>
          </tr>
        </table>
      </div>
      <div v-for="(data,index) in chapterList" :key="index">
        <div class="level-title">
          <h1>{{ data.NAME }}</h1>
        </div>
        <div class="level-title">
          <h1>视频课件</h1>
        </div>
        <div class="mb-20">
          <table class="table-ui">
            <tr>
              <th width="55" style="text-align:center">NO</th>
              <th width="200">视频课件名称</th>
              <th width="200">岗位培训类型</th>
              <th width="200">培训行业类型</th>
              <th width="100">学时</th>
              <th>课件描述</th>
              <th width="120">讲师</th>
            </tr>
            <tr v-for="(video,vIndex) in data.xzVideoList" :key="vIndex">
              <td align="center">{{ vIndex+1 }}</td>
              <td @click="goViewVideo(video)"> <a class="a_link" >{{ video.COURSEWARENAME }}</a></td>
              <td>{{ video.post_type_name }}</td>
              <td>{{ video.train_type_name }}</td>
              <td>{{ video.CLASSHOUR }}</td>
              <td>{{ video.COURSEWAREINTRODUCE }}</td>
              <td>{{ video.SPEAKERNAME }}</td>
            </tr>
          </table>
        </div>
        <div class="level-title">
          <h1>资料课件</h1>
        </div>
        <div class="mb-20">
          <table class="table-ui">
            <tr>
              <th width="55" style="text-align:center">NO</th>
              <th width="200">资料课件名称</th>
              <th width="200">岗位培训类型</th>
              <th width="200">培训行业类型</th>
              <th width="100">学时</th>
              <th>课件描述</th>
              <th width="120">讲师</th>
            </tr>
            <tr v-for="(dataCou,dIndex) in data.xzDataList" :key="dIndex">
              <td align="center">{{ dIndex+1 }}</td>
              <td @click="goViewPdf(dataCou.COURSEWAREFILES)" ><a class="a_link" >{{ dataCou.COURSEWARENAME }}</a></td>
              <td>{{ dataCou.post_type_name }}</td>
              <td>{{ dataCou.train_type_name }}</td>
              <td>{{ dataCou.CLASSHOUR }}</td>
              <td>{{ dataCou.COURSEWAREINTRODUCE }}</td>
              <td>{{ dataCou.SPEAKERNAME }}</td>
            </tr>
          </table>
        </div>
      </div>

    </div>
    <div class="ui-height" />
    <div class="ui-foot">
      <el-button plain type="info" @click="goBack">返 回</el-button>
    </div>
    <el-dialog v-show="dialogViewVideo" :visible.sync="dialogViewVideo" title="视频" width="800px" @close="closePlayer">
      <!-- <div>
        <video-player ref="videoPlayer" :playsinline="true" :options="playerOptions" class="video-player vjs-custom-skin" />
      </div> -->

      <div id="aLiVideoPlayer" class="prism-player"/>
    </el-dialog>
    <el-dialog v-if="dialogViewPdf" :visible.sync="dialogViewPdf" title="文档" width="90%" >
      <div>
        <pdf
          :page="pageNum"
          :src="pdfUrl"
          @progress="loadedRatio = $event"
          @num-pages="pageTotalNum=$event"
        />
        <div style="marginTop: 10px; color: #409EFF">{{ pageNum }} / {{ pageTotalNum }}</div>
        <el-button-group>
          <el-button type="primary" icon="el-icon-arrow-left" size="mini" @click="prePage">上一页</el-button>
          <el-button type="primary" size="mini" @click="nextPage">下一页<i class="el-icon-arrow-right el-icon--right"/></el-button>
        </el-button-group>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { requestFN } from '@/utils/request'
import { videoPlayer } from 'vue-video-player'
import 'video.js/dist/video-js.css'
import pdf from 'vue-pdf'
export default {
  components: { videoPlayer, pdf },
  data() {
    return {
      playerOptions: {
        playbackRates: [0.5, 1.0, 1.5, 2.0], // 可选的播放速度
        autoplay: false, // 如果为true,浏览器准备好时开始回放。
        muted: false, // 默认情况下将会消除任何音频。
        loop: false, // 是否视频一结束就重新开始。
        preload: 'auto', // 建议浏览器在<video>加载元素后是否应该开始下载视频数据。auto浏览器选择最佳行为,立即开始加载视频（如果浏览器支持）
        language: 'zh-CN',
        aspectRatio: '16:9', // 将播放器置于流畅模式，并在计算播放器的动态大小时使用该值。值应该代表一个比例 - 用冒号分隔的两个数字（例如"16:9"或"4:3"）
        fluid: true, // 当true时，Video.js player将拥有流体大小。换句话说，它将按比例缩放以适应其容器。
        sources: [{
          type: 'video/mp4', // 类型
          src: 'https://whrx.qhdsafety.com/file/uploadFiles/file/13cf0f4ec77e4d98ae8cdd9c3386ae0c/20210625/691910cd81024986a392f6203c4045f8.mp4' // url地址
        }],
        poster: '', // 封面地址
        notSupportedMessage: '此视频暂无法播放，请稍后再试', // 允许覆盖Video.js无法播放媒体源时显示的默认信息。
        controlBar: {
          timeDivider: true, // 当前时间和持续时间的分隔符
          durationDisplay: true, // 显示持续时间
          remainingTimeDisplay: true, // 是否显示剩余时间功能
          fullscreenToggle: true // 是否显示全屏按钮
        }
      },
      dialogViewVideo: false,
      pdfUrl: '',
      pageNum: 1,
      pageTotalNum: 1, // 总页数
      loadedRatio: 0, // 当前页面的加载进度，范围是0-1 ，等于1的时候代表当前页已经完全加载完成了
      dialogViewPdf: false,
      config: config,
      pd: [],
      chapterList: [],
      player: {}
    }
  },
  created() {
    this.getData()
  },
  methods: {
    getData() {
      requestFN(
        '/curriculum/goEdit',
        { CURRICULUM_ID: this.$parent.COURSEPAPERS_ID }
      ).then((data) => {
        this.pd = Object.assign({}, data.pd)
        this.chapterList = data.chapterList
      }).catch((e) => {
        this.listLoading = false
      })
    },
    closePlayer() {
      this.player.dispose()
    },
    // goViewVideo(row) {
    //   const type = row.VIDEOFILES.slice(row.VIDEOFILES.lastIndexOf('.') + 1)
    //   this.playerOptions.sources[0].src = config.fileUrl + row.VIDEOFILES
    //   this.playerOptions.poster = config.fileUrl + row.VIDEOCAPTURE
    //   this.playerOptions.sources[0].type = 'video/' + type
    //   this.dialogViewVideo = true
    // },
    goViewVideo(row) {
      if (row.VIDEOCOURSEWARE_ID_REMOTE) {
        requestFN(
          '/videoRes/getVideoPlayAuth',
          {
            VIDEOCOURSEWARE_ID: row.VIDEOCOURSEWARE_ID_REMOTE,
            CURRICULUM_ID: row.CURRICULUM_ID_REMOTE
          }
        ).then((data) => {
          if (data.result == 'success') {
            this.dialogViewVideo = true
            const video = {}
            data.responseBody.videoList.map(item => {
              video[item.definition] = item.playURL
            })
            console.log(video)
            this.$nextTick(() => {
              // eslint-disable-next-line no-undef
              this.player = new Aliplayer({
                id: 'aLiVideoPlayer',
                source: JSON.stringify(video),
                width: '100%',
                height: '500px',
                autoplay: true,
                isLive: false,
                components: [{
                  name: 'QualityComponent',
                  type: window.AliPlayerComponent.QualityComponent,
                  args: [function(definition, desc) {
                    console.log(definition + '-----' + desc)
                  }]
                }]

              }, function(player) {
                console.log('The player is created')
                player.on('sourceloaded', function(params) {
                  var paramData = params.paramData
                  var desc = paramData.desc
                  var definition = paramData.definition
                  player.getComponent('QualityComponent').setCurrentQuality(desc, definition)
                })
              })
            })
          }
        })
      } else {
        requestFN(
          '/videocourseware/getVideoPlayAuth',
          {
            videoId: row.VIDEOFILES
          }
        ).then((data) => {
          if (data.type == 'success') {
            this.dialogViewVideo = true
            this.$nextTick(() => {
              // eslint-disable-next-line no-undef
              this.player = new Aliplayer({
                'id': 'aLiVideoPlayer',
                'vid': row.VIDEOFILES,
                'playauth': data.playAuth,
                'qualitySort': 'asc',
                'format': 'm3u8',
                'encryptType': 1,
                'mediaType': 'video',
                'width': '100%',
                'height': '500px',
                'autoplay': true,
                'isLive': false,
                'rePlay': false,
                'playsinline': true,
                'preload': true,
                'controlBarVisibility': 'hover',
                'useH5Prism': true
              }, function(player) {
                console.log('The player is created')
              }
              )
            })
          }
        }).catch((e) => {
        })
      }
      // const type = row.VIDEOFILES.slice(row.VIDEOFILES.lastIndexOf('.') + 1)
      // this.playerOptions.sources[0].src = this.serverurl + row.VIDEOFILES
      // this.playerOptions.poster = this.serverurl + row.VIDEOCAPTURE
      // this.playerOptions.sources[0].type = 'video/' + type
    },
    closePlay() {
      this.$refs.videoPlayer.player.pause()
    },
    clickevaluatePicture(URL) {
    },
    // pdf 预览
    goViewPdf(filePath) {
      this.pageNum = 1
      this.pdfUrl = config.fileUrl + filePath
      this.dialogViewPdf = true
    },
    // 上一页
    prePage() {
      let page = this.pageNum
      page = page > 1 ? page - 1 : this.pageTotalNum
      this.pageNum = page
    },

    // 下一页
    nextPage() {
      let page = this.pageNum
      page = page < this.pageTotalNum ? page + 1 : 1
      this.pageNum = page
    },
    // 返回
    goBack() {
      this.$parent.activeName = 'List'
    }
  }
}
</script>

<style lang="sass" scoped>
  .table-ui
    td
      line-height: 34px
    .tbg
      width: 200px
  .img-flex
    display: flex
    .img-ui
      width: 110px
      height: 110px
      margin-right: 20px
      & img
        width: 100%
        height: 100%
</style>
