<template>
  <div class="app-warp">
    <div class="app-structure">
      <div class="app-search">
        <el-form class="app-search-form">
          <div class="form-item">
            <label> 机构名称：</label>
            <el-input v-model="KEYWORDS" placeholder="搜索机构名称" clearable @clear="getQuery"/>
          </div>
          <div class="form-item text-right">
            <el-button v-waves type="primary" icon="el-icon-search" @click="getQuery">
              搜索
            </el-button>
            <el-button v-waves icon="el-icon-refresh" @click="goKeyReset">
              重置
            </el-button>
          </div>
        </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="NAME" label="机构名称" />
          <el-table-column prop="NUMBER" label="统一社会信用代码" />
          <el-table-column label="操作" align="center" width="200">
            <template slot-scope="{row}">
              <el-button type="text" @click="handleInfo(row.CORPINFO_ID)">查看</el-button>
              <span class="app-divider"/>
              <el-button type="text" @click="handleEdit(row.CORPINFO_ID)">编辑</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="page-btn-group">
        <div/>
        <pagination :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
      </div>
    </div>
    <div v-if="isShowProgress" class="popContainer">
      <div class="progress_title">视频上传中，请耐心等待...</div>
    </div>
    <el-dialog :visible.sync="dialogFormEdit" :title="dialogType==='edit'?'修改':'查看'" width="600px">
      <el-form ref="form" :model="form" label-width="110px" style="width: 500px;">
        <el-form-item label="机构名称" prop="NAME">
          <el-input :disabled="dialogFormEdit" v-model="form.NAME" placeholder="请输入机构名称"/>
        </el-form-item>
        <el-form-item label="视频文件">
          <div v-if="dialogType==='edit'">
            <input v-show="false" id="fileUpload" ref="fileUpload" type="file" @change="fileChange($event)">
            <el-button type="primary" @click="uploadFile">选择文件</el-button>
            <span v-if="file" class="text-tip">{{ file.name }}</span>
            <span v-if="!file.name" class="text-tip">文件格式.mp4，最大上传500M</span>
          </div>
          <div v-if="dialogType==='view'">
            <span v-if="file" class="a_link" @click="goViewVideo()">{{ file.name }} [视频预览]</span>
          </div>
        </el-form-item>
      </el-form>
      <div v-if="dialogType ==='edit'" slot="footer" class="dialog-footer">
        <el-button @click="dialogFormEdit = false">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog :visible.sync="dialogFormVideo" :title="'视频'" width="650px">
      <div class="video">
        <video
          :src="videoPath"
          controls="controls"
          width="600px"
          height="350px"
          autoplay="autoplay"/>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import Pagination from '@/components/Pagination' // 通过 el-pagination二次打包
import { requestFN } from '@/utils/request'
import { upload } from '@/utils/upload'
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
      },
      form: {
        NAME: ''
      },
      total: 0,
      KEYWORDS: '',
      dialogFormEdit: false,
      dialogFormVideo: false,
      dialogType: 'view',
      varList: [],
      CORPINFO_ID: '',
      file: {},
      videoPath: '',
      isShowProgress: false,
      checkList: ['机构名称', '统一社会信用代码'],
      checkboxList: ['机构名称', '统一社会信用代码']
    }
  },
  created() {
    this.getList(this.ROLE_ID)
  },
  methods: {
    getRowKey(row) {
      return row.TEACHER_ID
    },
    // 搜索
    getQuery() {
      this.$refs.multipleTable.clearSelection()
      this.getList()
    },
    goKeyReset() {
      this.KEYWORDS = ''
      this.getQuery()
    },
    // 获取列表
    getList() {
      this.listLoading = true
      requestFN(
        '/training/list?showCount=' + this.listQuery.limit + '&currentPage=' + this.listQuery.page,
        {
          KEYWORDS: this.KEYWORDS
        }
      ).then((data) => {
        this.listLoading = false
        this.varList = data.list
        this.total = data.page.totalResult
      }).catch((e) => {
        this.listLoading = false
      })
    },
    confirm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.loading = true
          if (!this.file || !this.file.name) {
            this.$message.warning('视频文件不能为空，请上传')
            this.loading = false
            return false
          }
          this.isShowProgress = true
          const formData = new FormData()
          formData.append('CORPINFO_ID', this.CORPINFO_ID)
          formData.append('VIDEOFILE', this.file)
          formData.append('VIDEO_NAME', this.file.name)
          upload('/training/editVideo', formData).then((res) => {
            if (res.errorMessage) {
              this.loading = false
              this.$message({
                message: res.errorMessage,
                type: 'error'
              })
            } else {
              this.$message({
                message: '操作成功',
                type: 'success'
              })
              this.isShowProgress = false
              this.dialogFormEdit = false
              this.loading = false
              location.reload()
            }
          })
        } else {
          this.$message.error('请检查输入项')
        }
      })
    },
    // 修改
    handleEdit(ID) {
      this.dialogType = 'edit'
      this.getVideoInfo(ID)
      this.dialogFormEdit = true
      this.CORPINFO_ID = ID
    },
    handleInfo(ID) {
      this.dialogType = 'view'
      this.getVideoInfo(ID)
      this.dialogFormEdit = true
    },
    getVideoInfo(ID) {
      requestFN(
        '/training/getInfo',
        {
          CORPINFO_ID: ID
        }
      ).then((data) => {
        this.form = Object.assign({}, data.info) // copy obj
        this.file.name = data.info.VIDEO_NAME
        console.log('addd:' + data.info.VIDEO_PATH)
        this.videoPath = config.fileUrl + data.info.VIDEO_PATH
      }).catch((e) => {
        this.listLoading = false
      })
    },
    uploadFile() {
      this.$refs.fileUpload.dispatchEvent(new MouseEvent('click'))
    },
    fileChange(e) {
      console.log(e.target.files[0])
      if (!e.target.files[0]) {
        this.$message.warning('请先选择需要上传的文件!')
        return
      } else if (e.target.files[0].type != 'video/mp4') {
        this.$message.warning('请选择MP4文件!')
        return
      } else if (e.target.files[0].size > 524288000) {
        this.$message.warning('文件最大500M!')
        return
      }

      this.file = e.target.files[0]
    },
    goViewVideo() {
      this.dialogFormVideo = true
    }
  }
}

</script>
<style lang="scss">
  html{
    touch-action:none;
  }
  .hide .el-upload--picture-card {
    display: none;
  }

  .popContainer {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 999999;
    background: rgba(0, 0, 0, 0.6);
    .el-progress__text{
      color: #fff;
    }
    .progress_title{
      position: relative;
      top: 35%;
      color: #e8e8e8;
      left:44.7%;
      text-shadow:#000 1px 1px 2px
    }
  }
</style>
