<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="KEYWORDS" placeholder="搜索" class="filter-item" style="width: 200px;" />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="getQuery">
        搜索
      </el-button>
      <el-button v-waves class="filter-item" type="success" icon="el-icon-refresh" @click="goKeyReset">
        重置
      </el-button>
    </div>
    <el-table v-loading="listLoading" ref="multipleTable" :data="varList" :row-key="getRowKey" :header-cell-style="{'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="CREATOR" label="添加人" />
      <el-table-column prop="CREATTIME" label="添加时间" />
      <el-table-column prop="NAME" label="是否置顶">
        <template slot-scope="{row}">
          <span v-if="row.TYPE=='0'">否</span>
          <span v-if="row.TYPE=='1'">是</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template slot-scope="{row}">
          <el-button v-show="row.TYPE==0 && add" type="success" size="mini" style="margin-right:10px" @click="zhiding(row.VIDEO_ID)">置顶</el-button>
          <el-button v-show="del" type="danger" icon="el-icon-delete" size="mini" @click="handleDelete(row.VIDEO_ID)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="page-btn-group">
      <div>
        <el-button v-show="add" type="primary" icon="el-icon-plus" @click="handleAdd">新增</el-button>
      </div>
      <pagination :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    </div>

    <el-dialog :visible.sync="dialogFormEdit" :title="dialogType==='edit'?'修改':'新增'" width="600px">
      <el-form ref="form" :rules="rules" :model="form" label-width="110px" style="width: 500px;">
        <el-form-item label="视频名称" prop="NAME">
          <el-input id="NAME" ref="NAME" v-model="form.NAME" maxlength="255" placeholder="这里输入视频名称..." title="视频名称" />
        </el-form-item>
        <el-form-item label="内容" prop="DESCR">
          <el-input id="DESCR" ref="DESCR" :rows="3" v-model="form.DESCR" type="textarea" maxlength="255" placeholder="这里输入内容..." title="内容" />
        </el-form-item>
        <el-form-item label="附件路径">
          <div class="uo-flex">
            <el-input v-model="form.fileName" :disabled="true" title="请上传附件" />
            <el-upload :before-upload="beforeFileUpload" class="avatar-uploader" action="">
              <el-button type="primary" icon="el-icon-upload" style="margin-left:10px; " plain>上传附件</el-button>
            </el-upload>
          </div>
          <div>
            最大支持100M视频
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormEdit = false">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import Pagination from '@/components/Pagination' // 通过 el-pagination二次打包
import { requestFN } from '@/utils/request'
import waves from '@/directive/waves' // waves directive
import { upload } from '@/utils/upload'
import { Message } from 'element-ui'
export default {
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      listLoading: true,
      add: false,
      del: false,
      edit: false,
      serverurl: 'https://qask.qhdsafety.com/file',
      listQuery: {
        page: 1,
        limit: 10
      },
      total: 0,
      KEYWORDS: '',
      varList: [],
      pd: [],
      studyTypeList: [],
      form: {
        NAME: '', // 名称
        CONTENT: '', // 内容
        file: '',
        fileName: ''
      },
      multipleSelectionAll: [], // 所有选中的数据包含跨页数据
      multipleSelection: [], // 当前页选中的数据
      dialogFormEdit: false,
      dialogType: 'add',
      rules: {
        NAME: [{ required: true, message: '类型不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getRowKey(row) {
      return row.VIDEO_ID
    },
    // 搜索
    getQuery() {
      this.$refs.multipleTable.clearSelection()
      this.getList()
    },
    goKeyReset() {
      this.KEYWORDS = ''
      this.getQuery()
    },
    // 获取列表
    getList() {
      this.listLoading = true
      requestFN(
        '/video/list?showCount=' + this.listQuery.limit + '&currentPage=' + this.listQuery.page,
        {
          KEYWORDS: this.KEYWORDS,
          CORPINFO_TYPE: '2'
        }
      ).then((data) => {
        this.listLoading = false
        this.varList = data.varList
        this.total = data.page.totalResult
        this.hasButton()
        this.pd = data.pd
      }).catch((e) => {
        this.listLoading = false
      })
    },
    // 添加
    handleAdd() {
      this.$refs.form && this.$refs.form.clearValidate()
      this.dialogType = 'add'
      this.resetForm()
      this.getDict()
      this.dialogFormEdit = true
    },
    // 修改
    handleEdit(ID) {
      this.getDict()
      this.dialogType = 'edit'
      requestFN(
        '/video/goEdit',
        {
          VIDEO_ID: ID
        }
      ).then((data) => {
        this.form = Object.assign({}, data.pd) // copy obj
      }).catch((e) => {
        this.listLoading = false
      })
      this.dialogFormEdit = true
    },
    // 保存
    confirm() {
      this.$refs.form.validate(valid => {
        if (!this.form.file) {
          Message({
            message: '请上传附件',
            type: 'error',
            duration: 5 * 1000
          })
          return
        }
        const loading = this.$loading({
          lock: true,
          text: '上传中...',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        })
        const formData = new FormData()
        Object.keys(this.form).map(key => {
          formData.append(key, this.form[key])
        })
        formData.append('CORPINFO_TYPE', '2')
        upload(
          '/video/add',
          formData
        ).then((data) => {
          this.dialogFormEdit = false
          this.getList()
          loading.close()
        }).catch((e) => {
          loading.close()
        })
      })
    },
    beforeFileUpload(file) {
      const isLt100M = file.size / 1024 / 1024 < 100
      if (['video/mp4', 'video/ogg', 'video/flv', 'video/avi', 'video/wmv', 'video/rmvb'].indexOf(file.type) == -1) {
        this.$message.error('请上传正确的视频格式')
        return false
      }
      if (!isLt100M) {
        this.$message.error('上传视频大小不能超过100MB!')
        return false
      }
      this.form.file = file
      this.form.fileName = file.name
      this.$forceUpdate()
      return false
    },
    zhiding(id) {
      this.$confirm('确定要发布吗?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        requestFN(
          '/video/editZhiding',
          {
            VIDEO_ID: id
          }
        ).then(() => {
          this.$message({
            message: '发布成功',
            type: 'success'
          })
          this.listLoading = false
          this.varList = []
          this.listQuery.page = 1
          this.getList()
        }).catch((e) => {
          this.listLoading = false
        })
      }).catch(() => {
      })
    },
    handleDelete(id) {
      this.$confirm('确定要删除吗?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        requestFN(
          '/video/delete',
          {
            VIDEO_ID: id
          }
        ).then(() => {
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.listLoading = false
          this.varList = []
          this.listQuery.page = 1
          this.getList()
        }).catch((e) => {
          this.listLoading = false
        })
      }).catch(() => {
      })
    },

    batchDel() {
      const _selectData = this.$refs.multipleTable.selection
      if (_selectData == null || _selectData.length == 0) {
        this.$message({
          message: '请选中要删除的项...',
          type: 'error'
        })
        return false
      }
      const ids = _selectData.map((item, index) => {
        return item.VIDEO_ID
      }).join(',')

      this.$confirm('确定要删除选中的数据吗?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        requestFN(
          '/video/deleteAll',
          {
            DATA_IDS: ids
          }
        ).then(() => {
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.listLoading = false
          this.varList = []
          this.listQuery.page = 1
          this.$refs.multipleTable.clearSelection()
          this.getList()
        }).catch((e) => {
          this.listLoading = false
        })
      }).catch(() => {
      })
    },
    // 判断按钮权限，用于是否显示按钮
    hasButton() {
      var keys = 'video:add,video:del,video:edit,toExcel'
      requestFN(
        '/head/hasButton',
        {
          keys: keys
        }
      ).then((data) => {
        this.add = data.videofhadminadd		// 新增权限
        this.del = data.videofhadmindel		// 删除权限
        this.edit = data.videofhadminedit	// 修改权限
      }).catch((e) => {
        this.listLoading = false
      })
    },
    // 获取数据字典数据
    getDict() {
    },
    resetForm() {
      this.form = {
        NAME: '', // 名称
        CONTENT: '', // 内容
        file: '',
        fileName: ''
      }
    }
  }
}

</script>
<style lang="sass" scoped>
  .el-button+.el-button
    margin-left: 0
  .uo-flex
    display: flex
</style>
