<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="KEYWORDS" placeholder="请输入关键字" class="filter-item" style="width: 150px;"/>
      <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="VIDEONAME" label="视频名称" />
      <!--      <el-table-column prop="CODE" label="视频编码" />-->
      <el-table-column prop="VIDEOURL" label="播放地址" />
      <el-table-column prop="ISSHOW" label="是否展示视频" >
        <template slot-scope="{row}">
          <span v-if="row.ISSHOW===0">否</span>
          <span v-if="row.ISSHOW===1">是</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="left" width="500">
        <template slot-scope="{row}">
          <el-button type="success" icon="el-icon-caret-right" size="mini" @click="showVideo(row.VIDEOURL)">播放</el-button>
          <el-button v-show="edit" type="primary" icon="el-icon-edit" size="mini" @click="handleEdit(row.VIDEOMANAGER_ID)">编辑</el-button>
          <el-button v-show="del" type="danger" icon="el-icon-delete" plain @click="handleDelete(row.VIDEOMANAGER_ID)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="page-btn-group">
      <div>
        <el-button type="primary" icon="el-icon-plus" @click="handleAdd">新增</el-button>
        <el-button plain @click="goBack">返回</el-button>
      </div>
      <pagination :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    </div>

    <el-dialog :visible.sync="dialogForm" :title="msg==='add'?'新增':'编辑'" width="600px">
      <el-form ref="form" :model="form" :rules="rules" label-width="110px" style="width: 500px;">
        <el-form-item label="视频名称" prop="VIDEONAME">
          <el-input v-model="form.VIDEONAME" placeholder="这里输入视频名称..." />
        </el-form-item>
        <el-form-item label="播放地址" prop="VIDEOURL">
          <el-input v-model="form.VIDEOURL" placeholder="这里输入播放地址..." />
        </el-form-item>
        <el-form-item label="摄像头编号" prop="CODE">
          <el-input v-model="form.CODE" placeholder="这里输入摄像头编号..." />
        </el-form-item>
        <el-form-item label="视频设备编号" prop="GBS_NUM">
          <el-input v-model="form.GBS_NUM" placeholder="这里输入视频设备编号..." />
        </el-form-item>
        <el-form-item label="是否展示视频" prop="ISSHOW">
          <el-switch v-model="form.ISSHOW" :active-value="1" :inactive-value="0"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="back">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </div>

    </el-dialog>
    <el-dialog :visible.sync="dialogVideo" title="视频" width="600px">
      <video v-if="VIDEOURL.endsWith('.m3u8')" ref="video" class="full-height full-width" controls/>
      <iframe v-else :src="VIDEOURL" width="100%" height="380" allowfullscreen allow="autoplay; fullscreen;microphone" style="position: relative;border:none"/>
      <div slot="footer" class="dialog-footer">
        <el-button @click="back">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :visible.sync="dialogVideoAll" title="视频" width="800px">
      <iframe src="http://192.168.192.121:10800/?menu=no/#/screen" width="100%" height="500px" allowfullscreen allow="autoplay; fullscreen" style="position: relative;border:none"/>
      <div slot="footer" class="dialog-footer">
        <el-button @click="back">取 消</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
export default {
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      msg: 'add',
      config: config,
      listLoading: true,
      dialogForm: false,
      dialogImageUrl: '',
      dialogVisible: false,
      add: true,
      del: true,
      edit: true,
      listQuery: {
        page: 1,
        limit: 10
      },
      total: 0,
      KEYWORDS: '',
      dates: [],
      videoList: [],
      varList: [],
      form: {
        VIDEOMANAGER_ID: '',
        VIDEONAME: '', //
        VIDEOURL: '',
        CODE: '',
        GBS_NUM: '',
        ISSHOW: 0
      },
      rules: {
        VIDEONAME: [{ required: true, message: '视频名称不能为空', trigger: 'blur' }],
        VIDEOURL: [{ required: true, message: '播放地址不能为空', trigger: 'blur' }],
        CODE: [{ required: true, message: '摄像头编号不能为空', trigger: 'blur' }],
        GBS_NUM: [{ required: true, message: '视频设备编号不能为空', trigger: 'blur' }]
      },
      dialogVideo: false,
      dialogVideoAll: false,
      VIDEOURL: ''
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getRowKey(row) {
      return row.VIDEOMANAGER_ID
    },
    // 搜索
    getQuery() {
      this.$refs.multipleTable.clearSelection()
      this.getList()
    },
    // 播放
    showVideo(path) {
      this.VIDEOURL = path
      this.dialogVideo = true
    },
    // 获取列表
    getList() {
      this.listLoading = true
      requestFN(
        '/videomanager/list?showCount=' + this.listQuery.limit + '&currentPage=' + this.listQuery.page,
        {
          KEYWORDS: this.KEYWORDS,
          CORPINFO_ID: this.$parent.CORPINFO_ID
        }
      ).then((data) => {
        this.listLoading = false
        this.varList = data.varList
        this.total = data.page.totalResult
        this.hasButton()
      }).catch((e) => {
        this.listLoading = false
      })
    },
    // 添加
    handleAdd() {
      this.msg = 'add'
      this.resetForm()
      this.dialogForm = true
    },
    // 修改
    handleEdit(id) {
      this.msg = 'edit'
      this.form.VIDEOMANAGER_ID = id
      this.getData()
      this.dialogForm = true
    },
    // 获取详情
    getData() {
      this.listLoading = true
      requestFN(
        '/videomanager/goEdit',
        {
          VIDEOMANAGER_ID: this.form.VIDEOMANAGER_ID
        }
      ).then((data) => {
        this.form = data.pd
        this.VIDEOURL = data.pd.VIDEOURL + '&aspect=600x380'
        this.listLoading = false
      }).catch((e) => {
        this.listLoading = false
      })
    },
    // 保存
    confirm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.listLoading = true
          requestFN(
            '/videomanager/' + this.msg,
            {
              VIDEOMANAGER_ID: this.form.VIDEOMANAGER_ID,
              VIDEONAME: this.form.VIDEONAME,
              VIDEOURL: this.form.VIDEOURL,
              CODE: this.form.CODE,
              GBS_NUM: this.form.GBS_NUM,
              ISSHOW: this.form.ISSHOW,
              CORPINFO_ID: this.$parent.CORPINFO_ID
            }
          ).then((data) => {
            this.listLoading = false
            this.dialogForm = false
            this.getList()
          }).catch((e) => {
            this.listLoading = false
          })
        } else {
          return false
        }
      })
    },

    handleDelete(id) {
      this.$confirm('确定要删除吗?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        requestFN(
          '/videomanager/delete',
          {
            VIDEOMANAGER_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.VIDEOMANAGER_ID
      }).join(',')

      this.$confirm('确定要删除选中的数据吗?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        requestFN(
          '/videomanager/deleteAll',
          {
            DATA_IDS: ids
          }
        ).then(() => {
          this.listLoading = false
          this.varList = []
          this.listQuery.page = 1
          this.$refs.multipleTable.clearSelection()
          this.getList()
        }).catch((e) => {
          this.listLoading = false
        })
      }).catch(() => {
      })
    },
    // 判断按钮权限，用于是否显示按钮
    hasButton: function() {
      var keys = 'videomanager:add,videomanager:del,videomanager:edit,toExcel'
      requestFN(
        '/head/hasButton',
        {
          keys: keys
        }
      ).then((data) => {
        this.add = data.videomanagerfhadminadd // 新增权限
        this.del = data.videomanagerfhadmindel // 删除权限
        this.edit = data.videomanagerfhadminedit // 修改权限
      }).catch((e) => {
        this.listLoading = false
      })
    },
    goKeyReset() {
      this.KEYWORDS = ''
      this.getList()
    },
    back() {
      this.dialogVideo = false
      this.dialogVideoAll = false
      this.dialogForm = false
      this.listLoading = false
    },
    resetForm() {
      this.form = {
        VIDEOMANAGER_ID: '',
        VIDEONAME: '', //
        VIDEOURL: '',
        CODE: '',
        GBS_NUM: '',
        ISSHOW: 0
      }
    },
    goBack() {
      this.$parent.activeName = 'CorpList'
      this.$parent.CORPINFO_ID = ''
    }
  }

}

</script>
<style>
.hide .el-upload--picture-card {
  display: none;
}
</style>
