<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-input
          v-model="listQuery.devcode"
          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>
        <el-button type="text">
          <el-upload
            v-if="hasPerm('/device/batchImport')"
            :limit="1"
            :show-file-list="false"
            :http-request="uploadFile"
            :file-list="fileList"
            action="string"
            accept=".xls,.xlsx"
            class="edit_btn"
          >
            <el-button slot="trigger" size="small">
              批量导入
            </el-button>
          </el-upload>
        </el-button>
      </template>
      <template slot="columns">
        <el-table-column label="操作" width="120" 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>
            <el-button type="text" size="small" @click="detail(scope.row)">
              详情
            </el-button>
          </template>
        </el-table-column>
      </template>
    </normal-table>
    <dialog-list-warning ref="dialogAlarmContent" @watchChild="fetchData" />
  </app-container>
</template>

<script>
import dialogListWarning from '@/views/warningManage/components/dialogListWarning'
import { warningRuleList, delWarningRule, batchImportWarn } from '@/api/warning/warningRule'

export default {
  name: 'WarningRule',
  components: { dialogListWarning },
  data() {
    return {
      // 查询条件
      listQuery: {
        devcode: '',
        offset: 1,
        sort: 'id',
        order: 'asc'
      },
      columns: [
        // {
        //   text: '设备编号',
        //   value: 'devCode',
        //   align: 'center'
        // },
        {
          text: '报警上限',
          value: 'highValue',
          align: 'center'
        },
        {
          text: '报警下限',
          value: 'lowValue',
          align: 'center'
        },
        {
          text: '超时时间',
          value: 'overTime',
          align: 'center'
        },
        {
          text: '突然阈值',
          value: 'saltaTion',
          align: 'center'
        },
        {
          text: '传感器编号',
          value: 'sensorCode',
          align: 'center'
        },
        {
          text: '报警等级',
          value: 'grade',
          align: 'center'
        },
        {
          text: '时间戳',
          value: 'ts',
          align: 'center'
        },
        {
          text: '操作人姓名',
          value: 'openAtor',
          align: 'center'
        }
      ], // 显示列
      fileList: [],
      listLoading: false,
      deviceModelList: [], // 设备类型列表
      list: [], // 表格数据列表
      // 是否需要序号列
      options: {
        needIndex: false
      }
    }
  },
  created() {
    this.fetchData()
  },
  mounted() {
    this.$refs.normalTable.initColumnsState(false)
  },
  methods: {
    // 获取数据列表
    fetchData() {
      this.listLoading = true
      warningRuleList(this.listQuery).then(response => {
        this.list = response.data
        this.listLoading = false
      })
    },
    del(row) {
      this.$confirm(
        '确定要删除所选设备吗？',
        '确认操作',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        delWarningRule(row.id).then(response => {
          if (response.code === 200) {
            this.$message.success('删除成功')
            this.fetchData()
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 新增
    add() {
      this.$refs.dialogAlarmContent.initDialog('create')
    },
    // 修改
    edit(row) {
      this.$refs.dialogAlarmContent.initDialog('update', row)
    },
    // 详情
    detail(row) {
      this.$refs.dialogAlarmContent.initDialog('detail', row)
    },
    // 批量导入报警规则
    uploadFile(param) {
      // 判断文件大小是否符合要求
      const _file = param.file
      const isLt5M = _file.size / 1024 / 1024 < 5
      if (!isLt5M) {
        this.$message.error('请上传5M以下的excel文件')
        return false
      }
      // 全屏加载动画
      const loading = this.$loading({
        lock: true,
        text: '导入中,请稍后...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      // 发起导入请求
      batchImportWarn(_file).then(res => {
        loading.close() // 关闭加载动画
        if (res.code === 200) {
          this.$message.success('导入成功')
          this.fetchData()
        } else {
          this.$message.error(res.message)
        }
      }).catch(() => {
        loading.close() // 关闭加载动画
      })
      this.fileList = []
    }
  }
}
</script>

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