<template>
  <div class="app-warp">
    <div class="app-structure">
      <div class="app-search">
        <el-form ref="searchFrom" :model="searchFrom" label-width="100px">
          <el-row>
            <el-col :span="6">
              <el-form-item label="课件名称" prop="KEYWORDS">
                <el-input v-model="searchFrom.KEYWORDS" placeholder="请输入课件名称"/>
              </el-form-item>
            </el-col>
            <el-col :span="18">
              <el-button v-waves type="primary" icon="el-icon-search" class="ml-10" @click="getQuery">
                搜索
              </el-button>
              <el-button icon="el-icon-refresh" @click="goKeyReset">
                重置
              </el-button>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div class="app-tools">
        <div class="app-function"/>
        <div class="app-minbtn">
          <span class="screening">
            <el-dropdown trigger="click">
              <el-button type="text" icon="el-icon-finished"/>
              <el-dropdown-menu slot="dropdown" style="margin-top:3px;">
                <div class="app-checkbox-group">
                  <el-checkbox-group v-model="checkList">
                    <el-checkbox v-for="item in checkboxList" :key="item" :label="item" style="display: block"/>
                  </el-checkbox-group>
                </div>
              </el-dropdown-menu>
            </el-dropdown>
          </span>
          <el-button type="text" icon="el-icon-refresh-left"/>
          <el-button type="text" icon="el-icon-full-screen"/>
        </div>
      </div>
      <div class="app-container">
        <el-table
          v-loading="listLoading"
          ref="multipleTable"
          :data="varList"
          :row-key="getRowKey"
          :header-cell-style="{
            background:'#f5f7fa',
            'font-weight': 'bold',
            'color': '#000'
          }"
          tooltip-effect="dark"
          border
          fit
          highlight-current-row>
          <el-table-column :reserve-selection="true" type="selection" width="55" align="center" />
          <el-table-column type="index" label="序号" width="50" align="center" />
          <el-table-column prop="COURSEWARENAME" label="课件名称" />
          <el-table-column v-if="checkList.includes('上传时间')" :show-overflow-tooltip="true" prop="CREATTIME" label="上传时间" />
          <el-table-column v-if="checkList.includes('课件时长')" label="课件时长" width="140" align="center">
            <template slot-scope="{row}">
              {{ secondsCount(row.VIDEOTIME) }}
            </template>
          </el-table-column>
          <el-table-column v-if="checkList.includes('有效时间')" :show-overflow-tooltip="true" prop="LIMIT_TIME" label="有效时间" />
          <el-table-column v-if="checkList.includes('教师名称')" :show-overflow-tooltip="true" prop="SPEAKERNAME" label="教师名称" />
          <el-table-column v-if="checkList.includes('状态')" prop="isAddName" label="状态" />
          <el-table-column label="操作" align="center" width="150">
            <template slot-scope="{row}">
              <el-button type="text" @click="handleShow(row.VIDEOCOURSEWARE_ID)">查看</el-button>
              <span class="app-divider"/>
              <el-button type="text" @click="syncCurriculum(row.VIDEOCOURSEWARE_ID, row.LIMIT_TIME)">加入本地</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="page-btn-group">
          <div/>
          <pagination :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import Pagination from '@/components/Pagination' // 通过 el-pagination二次打包
import { requestFN } from '@/utils/request'
import waves from '@/directive/waves' // waves directive
export default {
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      listLoading: true,
      add: false,
      del: false,
      edit: false,
      listQuery: {
        page: 1,
        limit: 10
      },
      searchFrom: {
        KEYWORDS: ''
      },
      searchFromCollapse: true,
      total: 0,
      config: config,
      KEYWORDS: '',
      varList: [],
      pd: [],
      defaultProps: {
        value: 'id',
        children: 'nodes',
        label: 'name'
      },
      form: {
      },
      dialogType: 'add',
      activeName: 'first', // 弹出成的 tabName
      checkList: ['上传时间', '课件时长', '有效时间', '教师名称', '状态'],
      checkboxList: ['上传时间', '课件时长', '有效时间', '教师名称', '状态']
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getRowKey(row) {
      return row.CUSTOMER_BOUNS_ID
    },
    // 搜索
    getQuery() {
      this.$refs.multipleTable.clearSelection()
      this.getList()
    },
    goKeyReset() {
      this.$refs.searchFrom.resetFields()
      this.getList()
    },
    handleShow(VIDEOCOURSEWARE_ID) {
      this.$parent.activeName = 'Info'
      this.$parent.VIDEOCOURSEWARE_ID = VIDEOCOURSEWARE_ID
    },
    syncCurriculum(VIDEOCOURSEWARE_ID, LIMIT_TIME) {
      this.$confirm('确定要加入吗?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        requestFN(
          '/videoRes/syncVideo',
          {
            VIDEOCOURSEWARE_ID: VIDEOCOURSEWARE_ID,
            LIMIT_TIME: LIMIT_TIME
          }
        ).then(() => {
          this.$message({
            message: '加入成功',
            type: 'success'
          })
          this.listLoading = false
          this.varList = []
          this.listQuery.page = 1
          this.getList()
        }).catch((e) => {
          this.listLoading = false
        })
      }).catch(() => {
      })
    },
    // 获取列表
    getList() {
      this.listLoading = true
      requestFN(
        '/videoRes/upgradeVideoList?showCount=' + this.listQuery.limit + '&currentPage=' + this.listQuery.page,
        {
          ...this.searchFrom
        }
      ).then((data) => {
        this.listLoading = false
        this.varList = data.varList
        this.total = data.page.totalResult
        this.hasButton()
      }).catch((e) => {
        this.listLoading = false
      })
    },
    secondsCount(second) {
      if (!second) return 0
      const h = parseInt(second / 60 / 60 % 24, 10)
      const m = parseInt(second / 60 % 60, 10)
      const s = parseInt(second % 60, 10)
      if (h) {
        return h + '小时' + m + '分钟' + s + '秒'
      } else {
        if (m) {
          return m + '分钟' + s + '秒'
        } else {
          return s + '秒'
        }
      }
    }
  }
}

</script>
<style>
.tree_dialog .selectTree{
  width: 300px;
}
  .hide .el-upload--picture-card {
    display: none;
  }
</style>
