<template>
  <app-container>
    <search-area
      class="searchBottom"
      :need-clear="false"
      :need-search-more="false"
      type="seperate"
      size="small"
      search-more-type="default"
      @search="fetchData"
    >
      <!--一般查询条件-->
      <search-item>
        <el-select
          v-model="listQuery.modelName"
          placeholder="设备类型"
          clearable
        >
          <el-option
            v-for="item in deviceModelList"
            :key="item.value"
            :label="item.name"
            :value="item.value"
          />
        </el-select>
      </search-item>
      <search-item>
        <el-select
          v-model="listQuery.alarmType"
          placeholder="告警类型"
          clearable
        >
          <el-option
            v-for="item in alarmTypeList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </search-item>
      <search-item>
        <el-input
          v-model="listQuery.alarmContent"
          placeholder="告警内容"
          clearable
        />
      </search-item>
    </search-area>
    <normal-table
      ref="normalTable"
      :data="list"
      :query="listQuery"
      :columns="columns"
      :list-loading="listLoading"
      :options="options"
      size="small"
    >
      <template slot="btns">
        <el-button size="small" @click="add">
          新增
        </el-button>
      </template>
      <template slot="columns">
        <el-table-column label="操作" width="160" align="center">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="edit(scope.row)">
              修改
            </el-button>
            <el-button type="text" size="small" @click="del(scope.row)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </template>
    </normal-table>
    <edit-alarm-content ref="editAlarmContent" @watchChild="fetchData" />
  </app-container>
</template>

<script>
import editAlarmContent from '@/views/systemConfig/alarmContent/components/editAlarmContent'
import { getDevice, alarmContentList, delAlarmContent } from '@/api/systemConfig/alarmContent'

export default {
  name: 'ListContent',
  components: { editAlarmContent },
  data() {
    return {
      // 查询条件
      listQuery: {
        modelName: '',
        alarmType: '',
        alarmContent: '',
        offset: 1,
        sort: 'id',
        order: 'asc'
      },
      // 告警类型列表
      alarmTypeList: [
        {
          label: '告警类型一',
          value: '0'
        },
        {
          label: '告警类型二',
          value: '1'
        },
        {
          label: '告警类型三',
          value: '2'
        }
      ],
      columns: [
        {
          text: '告警类型',
          value: 'alarmType',
          align: 'center'
        },
        {
          text: '设备类型',
          value: 'deviceType',
          align: 'center'
        },
        {
          text: '告警内容',
          value: 'alarmContent',
          align: 'center'
        }
      ], // 显示列
      listLoading: false,
      deviceModelList: [], // 设备类型列表
      list: [], // 表格数据列表
      // 是否需要序号列
      options: {
        needIndex: false
      }
    }
  },
  created() {
    this.fetchDeviceType()
    this.fetchData()
  },
  mounted() {
    this.$refs.normalTable.initColumnsState(false)
  },
  methods: {
    // 获取设备类型
    fetchDeviceType() {
      getDevice(this.listQuery).then(response => {
        this.deviceModelList = response.data
      })
    },
    // 获取数据列表
    fetchData() {
      this.listLoading = true
      alarmContentList(this.listQuery).then(response => {
        this.list = response.data
        this.listLoading = false
      })
    },
    del(row) {
      this.$confirm(
        '确定要删除所选设备吗？',
        '确认操作',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        delAlarmContent(row.id).then(response => {
          if (response.code === 200) {
            this.$message.success('删除成功')
            this.fetchData()
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 新增
    add() {
      this.$refs.editAlarmContent.initDialog('create')
    },
    // 修改
    edit(row) {
      this.$refs.editAlarmContent.initDialog('update', row)
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .searchBottom{
    border-bottom: 12px solid #ebebeb;
  }
</style>
