<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div>
        <el-input v-model="searchParam.videoNameLike" clearable size="small" placeholder="视频名称" style="width: 200px;"
          class="filter-item" />
        <el-button class="filter-item" size="mini" type="success" icon="el-icon-search"
          @click="searchSubmit">搜索</el-button>
        <el-button class="filter-item" size="mini" type="warning" icon="el-icon-refresh-left"
          @click="searchParamReset">重置</el-button>
      </div>
    </div>
    <el-button class="filter-item" size="mini" type="danger" icon="el-icon-delete" @click="doDel()">删除</el-button>
    <!--表格-->
    <el-table ref="table" v-loading="searching" :data="tableData" row-key="id" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column :show-overflow-tooltip="true" prop="id" label="序号" width="100px" align="center" />
      <el-table-column :show-overflow-tooltip="true" prop="videoName" label="视频点名称" align="center" />
      <el-table-column :show-overflow-tooltip="true" prop="videoId" label="视频点编码" align="center" />
      <el-table-column :show-overflow-tooltip="true" prop="warnDatetime" label="报警时间" align="center" />
      <el-table-column :show-overflow-tooltip="true" prop="warnTypeName" label="报警类型" align="center" />
      <el-table-column :show-overflow-tooltip="true" prop="warnImg" label="图片" align="center">
        <template slot-scope="scope">
          <div :style="{ 'width': '100px', 'height': '100px', 'margin': 'auto' }">
            <el-image v-show="scope.row.warnImg && getFirstFileUrl(scope.row.warnImg)"
              :style="{ 'width': '100px', 'height': '100px' }"
              :src="getFirstFileUrl(scope.row.warnImg)"
              :preview-src-list="[getFirstFileUrl(scope.row.warnImg)]" fit="cover" />
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="300px" align="center" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" round @click="gotoVideo(scope.row)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination :page-sizes="[10, 20, 50, 100]" :current-page.sync="searchParam.page"
      :page-size.sync="searchParam.count" :total="searchResult.total" layout="total, sizes, prev, pager, next, jumper"
      @size-change="searchSubmit" @current-change="searchSubmit" />
  </div>
</template>

<script>
import videoApi from '@/api/kd/video'
import { getToken } from '@/utils/auth'
import { mapGetters } from 'vuex'

const searchParam = {// 默认搜索条件
  page: 1,
  count: 10,
  searchCount: true,
  videoNameLike: null
}
export default {
  name: 'EVideoAi', // 本页面名
  components: {},
  data() {
    return {
      selectedMine: {},
      httpHeaders: {
        'Authorization': getToken()
      },
      searching: false,
      exporting: false,
      searchParam: {},
      searchResult: {
        total: 0,
        results: [] // 搜索结果
      },
      editState: 0, // （刚打开窗口时）记录了上次窗口作用；（保存编辑时）记录了当前窗口作用； 0无 1新增 2修改 3查看
      multipleSelection: []
    }
  },
  computed: {
    ...mapGetters([
      'user',
      'baseApi'
    ]),
    tableData() {
      // 此处对返回值进行算法重组
      return this.searchResult.results
    }
  },
  mounted() {
    const selectedMineJson = window.localStorage.getItem('kd.selected.mine')
    let noMine = false
    if (!selectedMineJson) {
      noMine = true
    } else {
      const selectedMine = JSON.parse(selectedMineJson)
      if (!selectedMine || selectedMine.enterpriseId !== this.user.enterpriseId) {
        noMine = true
      } else {
        this.selectedMine = selectedMine
      }
    }
       // if (noMine) {
    //   this.$message.error('当前未选择矿山')
    //   this.$router.replace('/dashboard')
    // }
    this.searchParamReset()
    this.searchSubmit()
  },
  methods: {
    searchParamReset() {
      this.searchParam = JSON.parse(JSON.stringify(searchParam))
    },
    searchSubmit() {
      this.searching = true
      this.searchParam.mineType = this.selectedMine.mineType
      this.searchParam.mineId = this.selectedMine.id
      videoApi.getWarnPage(this.searchParam).then(response => {
        this.searching = false
        this.searchResult = response
      }).catch(() => {
        this.searching = false
      })
    },
    getFirstFileUrl(filePath) {
      if (filePath) {
        return this.baseApi + '/file' + filePath
      }
      return ''
    },
    gotoVideo(row) {
      this.$router.push({
        path: '/e/video/live',
        query: {
          deviceId: row.deviceId,
          videoId: row.videoId
        }
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    doDel() {
      console.log(this.multipleSelection)
      if (this.multipleSelection && this.multipleSelection.length > 0) {
        videoApi.delBatch({
          ids: this.multipleSelection.map(item => item.id)
        }).then(response => {
          this.$message.success('删除成功')
          this.searchSubmit()
        })
      } else {
        this.$message.error('请先选择要删除的信息')
      }
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
::v-deep .el-input-number .el-input__inner {
  text-align: left;
}

::v-deep .vue-treeselect__control,
::v-deep .vue-treeselect__placeholder,
::v-deep .vue-treeselect__single-value {
  height: 30px;
  line-height: 30px;
}
</style>
