<!--设备类型管理-->
<template>
  <app-container>
    <!--筛选条件-->
    <search-area size="small" @search="search">
      <search-item>
        <el-input v-model="listQuery.deviceType" size="small" clearable placeholder="请输入设备类型" />
      </search-item>
      <search-item>
        <el-select v-model="listQuery.isValid" size="small" placeholder="是否启用" clearable>
          <el-option v-for="item in isValidList" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </search-item>
    </search-area>
    <!--查询结果Table显示-->
    <normal-table ref="table" :data="list" :total="total" :query="listQuery" :columns="columns" :list-loading="listLoading" :pagination="true" :options="tableOptions" size="small" @change="changePage">
      <template slot="btns">
        <el-button v-if="hasPerm('/deviceType/add')" size="small" @click="add">
          新增
        </el-button>
      </template>
      <template slot="columns">
        <el-table-column label="报警规则类型" align="center">
          <template v-slot="scope">
            <span>{{ alarmRuleTypeDict[scope.row.alarmRuleType] }}</span>
          </template>
        </el-table-column>
        <el-table-column label="是否需要报警上限" align="center">
          <template v-slot="scope">
            <span>{{ scope.row.alarmRuleHighvalue==='1'?'是':scope.row.alarmRuleHighvalue==='0'?'否':'' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="是否需要报警下限" align="center">
          <template v-slot="scope">
            <span>{{ scope.row.alarmRuleLowvalue==='1'?'是':scope.row.alarmRuleLowvalue==='0'?'否':'' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="是否启用" align="center" width="70px">
          <template v-slot="scope">
            <span>{{ scope.row.valid==='1'?'是':scope.row.valid==='0'?'否':'' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="160">
          <template v-slot="scope">
            <el-button v-if="hasPerm('/deviceType/update')" type="text" size="mini" @click="edit(scope.row)">
              编辑
            </el-button>
            <el-button v-if="hasPerm('/deviceType/delete')" type="text" size="mini" @click="del(scope.row)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </template>
    </normal-table>
    <edit-device ref="editDevice" @watchChild="fetchData" />
  </app-container>
</template>

<script>
import editDevice from '@/views/deviceManage/deviceType/components/editDeviceType'
import { getDeviceTypeList, delDeviceType } from '@/api/device/deviceType'

export default {
  name: 'DeviceType',
  components: { editDevice },
  data() {
    return {
      listQuery: {
        deviceType: '',
        isValid: '',
        offset: 1,
        limit: 20,
        sort: 'id',
        order: 'asc'
      }, // 查询条件
      columns: [
        {
          text: '设备类型编号',
          value: 'id',
          align: 'center',
          width: 100
        },
        {
          text: '设备类型',
          value: 'typeName',
          align: 'center'
        },
        {
          text: '描述',
          value: 'typeDescription',
          align: 'center'
        },
        {
          text: '监控内容',
          value: 'watchType',
          align: 'center'
        }
      ], // 显示列
      alarmRuleTypeDict: {
        '1': '需要单独设置报警规则',
        '2': '统一的报警规则',
        '3': '无报警规则'
      }, // 报警规则类型字典
      tableOptions: {
        needIndex: false, // 是否需要序号列
        border: true // 是否需要上方边框
      }, // 配置项
      total: 0, // 数据总数
      multipleSelection: [], // 多选选中项
      list: [], // 列表数据
      listLoading: true, // 加载动画
      dialogShow: false // 是否显示对话框
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    // 查询数据
    search() {
      this.fetchData(false)
    },
    // 数据列表
    fetchData(isNowPage = true) {
      this.listLoading = true
      if (!isNowPage) { // 是否显示当前页，否则跳转第一页
        this.listQuery.offset = 1
      }
      getDeviceTypeList(this.listQuery).then(response => {
        this.list = response.data.rows
        this.total = parseInt(response.data.total)
        this.listLoading = false
      })
    },
    // 删除设备
    del(row) {
      this.$confirm(
        '确定要删除所选设备类型吗？该操作可能会引起其他功能异常',
        '确认操作',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        delDeviceType(row.id).then(response => {
          if (response.code === 200) {
            this.$message.success('删除成功')
            this.fetchData()
          }
        })
      })
    },
    // 详情
    detail(row) {
      this.$refs.editDevice.initDialog('detail', row)
    },
    // 新增设备
    add() {
      this.$refs.editDevice.initDialog('create')
    },
    // 编辑设备信息
    edit(row) {
      this.$refs.editDevice.initDialog('update', row)
    },
    // 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
    changePage(val) {
      if (val && val.size) {
        this.listQuery.limit = val.size
      }
      if (val && val.page) {
        this.listQuery.offset = val.page
      }
      this.fetchData()
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>

</style>
