<template>
  <div class="bbsl-common-page">
    <div class="bbsl-table-search">
      <el-form v-model="queryParam" :inline="true">
        <el-form-item label="设备名称：" prop="">
          <el-input
            v-model="querys[0].value"
            clearable
            placeholder="请输入设备名称"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" type="primary" @click="handleSearch"
            >搜索
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <div ref="contentPanel" class="bbsl-table-content">
      <div ref="btnPanel" class="bbsl-btn-content">
        <el-button icon="el-icon-plus" type="success" @click="handleAdd"
          >新增
        </el-button>
        <el-popconfirm title="确定删除选中数据吗？" @confirm="handleDelete">
          <el-button
            slot="reference"
            :disabled="!multipleSelection.length"
            icon="el-icon-delete"
            style="margin-left: 10px"
            type="danger"
            >删除
          </el-button>
        </el-popconfirm>
        <el-button
          icon="el-icon-sort"
          plain
          style="margin-left: 10px"
          type="primary"
          @click="savePropertyf"
        >
          保存排序号
        </el-button>
      </div>
      <el-table
        ref="multipleSelection"
        v-loading="loading"
        :data="tableData"
        :max-height="tableMaxHeight"
        :style="{width: '100%'}"
        size="mini"
        @row-dblclick="handleDbClick"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column
          label="设备编码"
          prop="code"
          show-overflow-tooltip
          sortable
        >
        </el-table-column>
        <el-table-column label="设备名称" prop="name" show-overflow-tooltip>
        </el-table-column>
        <el-table-column
          label="设备类型"
          prop="typeName"
          show-overflow-tooltip
          sortable
        >
        </el-table-column>
        <el-table-column
          label="所属考场"
          prop="roomName"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column label="IP地址" prop="ip" show-overflow-tooltip>
        </el-table-column>
        <el-table-column label="通道号" prop="aisle" show-overflow-tooltip>
        </el-table-column>
        <el-table-column
          label="访问地址"
          min-width="120"
          prop="url"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          align="center"
          label="排序"
          min-width="70"
          prop="sn"
          show-overflow-tooltip
          sortable
        >
          <template slot-scope="scope">
            <el-input
              v-model="scope.row.sn"
              min="1"
              size="small"
              style="width:70px"
              type="number"
              @input="insort(scope.row)"
            >
            </el-input>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        ref="paginationPanel"
        :current-page="queryParam.pageBean.page"
        :page-size="queryParam.pageBean.pageSize"
        :page-sizes="[20, 30, 40, 50]"
        :total="total"
        align="right"
        background
        class="bbsl-pagination"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      ></el-pagination>
    </div>
    <deviceMenuDialog ref="formModal"></deviceMenuDialog>
  </div>
</template>

<script>
import {TableMixin} from '@/mixins/tableMixin'
import deviceMenuDialog from './components/deviceMenuDialog.vue'
import deviceMenu from '@/api/examination/deviceMenu.js'
import dictUtils from '@/components/dict/DictSelectUtils.js'

export default {
  mixins: [TableMixin],
  name: 'deviceMenu',
  components: {
    deviceMenuDialog
  },
  data() {
    return {
      url: {
        delete: '/exam/camera/info/'
      },
      tableData: [],
      multipleSelection: [],
      total: 0,
      sortList: {},
      querys: [
        {
          group: 'main',
          operation: 'EQUAL',
          parentGroup: '',
          property: 'name',
          relation: 'AND',
          value: ''
        }
      ],
      typeList: [],
      roomList: []
    }
  },
  created() {
    this.getRoomQuery()
    this.typeList = dictUtils.getDictItemsFromCache('sblx')
  },
  methods: {
    loadData(page) {
      if (page) {
        this.queryParam.pageBean.page = page
      }
      this.loading = true
      this.tableData = []
      this.queryParam.querys = []
      if (this.querys[0].value) {
        this.queryParam.querys.push(this.querys[0])
      }

      deviceMenu.pagingQuery(this.queryParam, res => {
        this.total = res.total
        this.tableData = res.rows
        this.tableData.forEach(e => {
          this.typeList.forEach(s => {
            if (e.type == s.value) {
              e.typeName = s.name
            }
          })
          this.roomList.forEach(s => {
            if (e.roomId == s.id) {
              e.roomName = s.name
            }
          })
        })
        this.$set(this.tableData)
        this.loading = false
      })
    },
    insort(val) {
      let id = val.id
      let value = val.sn
      this.sortList[id] = value
    },
    //获取考场
    getRoomQuery() {
      deviceMenu.roomQuery(this.queryParam, res => {
        if (res.rows) {
          this.roomList = res.rows
          this.loadData(1)
        }
      })
    },
    //保存排序号
    savePropertyf() {
      deviceMenu.saveSort(this.sortList, res => {
        if (res.state) {
          this.$notify({
            type: 'success',
            message: res.message,
            duration: 2000
          })
          this.loadData()
        } else {
          this.$message.warning(res.message)
        }
      })
    },
    tableRowClassName({row, rowIndex}) {
      console.log(rowIndex % 2 == 0)
      if (rowIndex % 2 == 0) {
        return 'warning-row'
      } else {
        return ''
      }
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .warning-row {
  // background-color: #C1C2C5 !important;
}
</style>
