<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div>
        <el-select v-model="searchParam._mineIndex" placeholder="所属矿山" clearable size="small" class="filter-item" style="width: 200px;">
          <el-option v-for="mine in mineList" :key="mine.index" :label="mine.name" :value="mine.index" />
        </el-select>
        <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="120px" 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 mineApi from '@/api/kd/mine'
import { getToken } from '@/utils/auth'
import { mapGetters } from 'vuex'

const searchParam = {// 默认搜索条件
  page: 1,
  count: 10,
  searchCount: true,
  videoNameLike: null
}
export default {
  name: 'CVideoAi', // 本页面名
  components: {},
  data() {
    return {
      mineList: [],
      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() {
    // 矿山选项卡
    mineApi.getEnableMine().then(response => {
      let i = 1
      response.results.forEach(x => { x.index = i++ })
      this.mineList = response.results
    })
    this.searchParamReset()
    this.searchSubmit()
  },
  methods: {
    searchParamReset() {
      this.searchParam = JSON.parse(JSON.stringify(searchParam))
    },
    searchSubmit() {
      this.searching = true
      if (this.searchParam._mineIndex) {
        const mine = this.mineList[this.searchParam._mineIndex - 1]
        this.searchParam.mineType = mine.mineType
        this.searchParam.mineId = mine.id
      } else {
        this.searchParam.mineType = null
        this.searchParam.mineId = null
      }
      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: '/c/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>
