<template>
  <div class="app-container">
    <el-row>
      <el-col v-show="tenantList.length>1" :span="5">
        <div style="width:95%;">
          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <span>项目</span>
            </div>
            <el-tree ref="tenantlist" :data="tenantList" :props="defaultProps" :highlight-current="true" :current-node-key="selectedTenant.id" :default-checked-keys="defaultCheckedKeys" node-key="id" @node-click="handleNodeClick" />
          </el-card>
        </div>
      </el-col>
      <el-col :span="tenantList.length>1?19:24">
        <!--查询结果Table显示-->
        <div>
          <el-row class="table-title">
            <el-col :span="6">
              <div class="title-header">
                <i class="el-icon-menu" />{{ selectedTenant.tenantName }}
              </div>
            </el-col>
            <el-col :span="12" :offset="6" class="edit_btns">
              <!-- v-if="hasPerm('/device/delete')" -->
              <el-button
                class="edit_btn"
                size="small"
                @click="add"
              >
                新增
              </el-button>
            </el-col>
          </el-row>
          <el-table v-loading="listLoading" :data="list" class="table" border>
            <!--<el-table-column align="center" type="selection" width="55"/>-->
            <el-table-column align="center" type="index" />
            <el-table-column v-for="column in columns" :key="column.value" :label="column.text" :width="column.width" :align="column.align" show-overflow-tooltip>
              <template slot-scope="scope">
                <span v-if="column.formatValue" :class="column.class">{{ formatterState(scope.row[column.value]) }}</span>
                <span v-else :class="column.class">{{ scope.row[column.value] }}</span>
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center" width="180">
              <template slot-scope="scope">
                <el-button type="text" @click="show(scope.row)">
                  查看
                </el-button>
                <el-button type="text" size="small" @click="del(scope.row)">
                  删除
                </el-button>
                <el-button type="text" @click="edit(scope.row)">
                  编辑
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <component :is="cmpName" ref="editdialog" @watchChild="fetchTenantList" />
      </el-col>
    </el-row>
  </div>
</template>
<script>
import { getTenantList } from '@/api/base/tenant'
import { getAlarmLevelList, delAlarmLevel } from '@/api/systemConfig/alarmLevel'
import infoWell from '@/views/wellManage/infoWell'
import editLiquidLevel from './components/editLiquidLevel'
import editDigLevel from './components/editDigLevel'
import editGasLevel from './components/editGasLevel'
import editTempLevel from './components/editTempLevel'
import EditCOLevel from './components/editCOLevel'
import EditCh4Level from './components/editCh4Level'
import EditH2sLevel from './components/editH2sLevel'
import EditHumiLevel from './components/editHumiLevel'
import EditO2Level from './components/editO2Level'
import editAlarmLevel from './components/editAlarmLevel'

export default {
  name: 'ListLevel',
  components: { EditO2Level, EditHumiLevel, EditCOLevel, EditCh4Level, EditH2sLevel, editLiquidLevel, editDigLevel, editGasLevel, editTempLevel, infoWell, editAlarmLevel },
  data() {
    return {
      selectedTenant: {
        id: '1',
        tenantName: '亦庄项目'
      },
      tenantList: [],
      defaultProps: {
        children: 'children',
        label: 'tenantName'
      },
      defaultCheckedKeys: [],
      listQuery: {
        id: ''
      }, // 筛选条件
      columns: [
        {
          text: '监控指标',
          value: 'name',
          align: 'center'
        },
        {
          text: '设备类型',
          value: 'deviceTypeName',
          align: 'center'
        },
        {
          text: '开启报警',
          value: 'openAlarm',
          align: 'center',
          width: 100,
          formatValue: true
        },
        {
          text: '开启工单',
          value: 'openJob',
          align: 'center',
          width: 100,
          formatValue: true
        },
        // {
        //   text: '操作人',
        //   value: 'user',
        //   align: 'center'
        // },
        {
          text: '更新时间',
          value: 'ts',
          align: 'center'
        }
      ], // 显示列
      multipleSelection: [], // 多选选中项
      list: [], // 列表数据
      total: 0, // 数据总数
      deviceTypeList: [], // 设备类型列表
      showDeviceType: true,
      listLoading: false, // 加载动画
      editShow: false, // 是否显示编辑框
      wellShow: false, // 是否显示井详情
      cmpName: '' // 组件名称
    }
  },
  created() {
    this.fetchTenantList()
    this.fetchData()
  },
  methods: {
    // 点击切换项目
    handleNodeClick(data) {
      this.selectedTenant = {
        id: data.id,
        tenantName: data.tenantName
      }
      this.fetchData()
    },
    // 获取项目列表
    fetchTenantList() {
      // 调用项目列表，获取项目列表
      const params = {
        keywords: '',
        offset: 1,
        limit: 20,
        sort: '',
        order: ''
      }
      getTenantList(params).then(response => {
        this.tenantList = response.data.rows
        if (this.tenantList.length > 0) {
          this.selectedTenant = this.tenantList[0]
          this.fetchData()
        }
      })
    },
    // 格式化开关状态
    formatterState(value) {
      if (value === '1') {
        return '是'
      } else {
        return '否'
      }
    },
    // 新增
    add() {
      this.cmpName = 'edit-alarm-level'
      setTimeout(() => {
        this.$refs.editdialog.initDialog('create')
      }, 100)
    },
    // 删除
    del(row) {
      this.$confirm(
        '确定要删除所选设备吗？',
        '确认操作',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        const paramsData = {
          tenantId: row.tenantId,
          name: row.name,
          deviceType: row.deviceType
        }
        delAlarmLevel(paramsData).then(response => {
          if (response.code === 200) {
            this.$message.success('删除成功')
            this.fetchData()
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 查看报警规则
    show(row) {
      this.editShow = false
      if (row.name === '液位') {
        this.cmpName = 'edit-liquid-level'
      } else if (row.name === '燃气') {
        this.cmpName = 'edit-gas-level'
      } else if (row.name === '温度') {
        this.cmpName = 'edit-temp-level'
      } else if (row.name === '开挖') {
        this.cmpName = 'edit-dig-level'
      } else if (row.name === '一氧化碳') {
        this.cmpName = 'edit-c-o-level'
      } else if (row.name === '硫化氢') {
        this.cmpName = 'edit-h2s-level'
      } else if (row.name === '甲烷') {
        this.cmpName = 'edit-ch4-level'
      } else if (row.name === '氧气') {
        this.cmpName = 'edit-o2-level'
      } else if (row.name === '湿度') {
        this.cmpName = 'edit-humi-level'
      }
      setTimeout(() => {
        this.$refs.editdialog.initDialog('show', true, this.selectedTenant.id, row)
      }, 100)
    },
    // 编辑告警规则信息
    edit(row) {
      this.editShow = true
      if (row.name === '液位') {
        this.cmpName = 'edit-liquid-level'
      } else if (row.name === '燃气') {
        this.cmpName = 'edit-gas-level'
      } else if (row.name === '温度') {
        this.cmpName = 'edit-temp-level'
      } else if (row.name === '开挖') {
        this.cmpName = 'edit-dig-level'
      } else if (row.name === '一氧化碳') {
        this.cmpName = 'edit-c-o-level'
      } else if (row.name === '硫化氢') {
        this.cmpName = 'edit-h2s-level'
      } else if (row.name === '甲烷') {
        this.cmpName = 'edit-ch4-level'
      } else if (row.name === '氧气') {
        this.cmpName = 'edit-o2-level'
      } else if (row.name === '湿度') {
        this.cmpName = 'edit-humi-level'
      }
      setTimeout(() => {
        this.$refs.editdialog.initDialog('update', true, this.selectedTenant.id, row)
      }, 100)
    },
    // 查询数据
    search() {
      this.fetchData(false)
    },
    // 获取数据
    fetchData() {
      this.listQuery = {
        tenantId: this.selectedTenant.id
      }
      this.listLoading = true
      getAlarmLevelList(this.listQuery).then(response => {
        console.log(response)
        this.list = response.data
        this.listLoading = false
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .tenant-list-ul{
    .tenant-list-li{
      list-style: none;
      line-height: 2    }
  }
  $tableTitleHeight:46px;
  .table{
    margin-bottom: 20px;
  }
  .pagination-container{
    margin-bottom: 50px;
  }
  .table-title{
    background-color:rgba(243, 243, 243, 1);
    height: $tableTitleHeight;
    .title-header{
      line-height:$tableTitleHeight;
      color: #606266;
      font-size: 15px;
      i{
        margin-left: 5px;
        margin-right: 5px;
      }
    }
  }
  .edit_btns{
    .edit_btn{
      float:right;
      margin:7px 3px;//为了需要居中显示margin-top和bottom要用$tableTitleHeight减去按钮原高度除以2
    }
  }
</style>
