<template>
  <div class="app-container">
    <!--筛选条件-->
    <div class="search-div">
      <el-form ref="selectForm" :inline="true" :model="listQuery" class="form-container">
        <el-row>
          <el-form-item class="selectForm-container-item" prop="keywords">
            <el-input v-model.trim="listQuery.keywords" placeholder="井编号/设备编号" clearable />
          </el-form-item>
          <el-form-item v-if="showDeviceType" class="selectForm-container-item" prop="wellType">
            <el-select v-model="listQuery.deviceType" placeholder="设备类型" clearable>
              <el-option
                v-for="item in deviceTypeList"
                :key="item.value"
                :label="item.name"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-button class="filter-item" type="primary" icon="el-icon-search" @click="search">
            搜索
          </el-button>
        </el-row>
      </el-form>
    </div>
    <!--查询结果Table显示-->
    <div>
      <el-row class="table-title">
        <el-col :span="6">
          <div class="title-header">
            <i class="el-icon-menu" />数据列表
          </div>
        </el-col>
        <el-col :span="12" :offset="6" class="edit_btns">
          <el-button v-if="hasPerm('/alarmRule/add')" class="edit_btn" size="small" @click="add">
            新增
          </el-button>
        </el-col>
      </el-row>
      <el-table v-loading="listLoading" :data="list" class="table" border @selection-change="handleSelectionChange">
        <!--<el-table-column align="center" type="selection" width="55"/>-->
        <el-table-column :index="indexMethod" 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.type!='Button'" :class="column.class">{{ scope.row[column.value] }}</span>
            <el-button v-if="column.type=='Button'" type="text" @click="showWellDetail(scope.row)">
              {{ scope.row.wellCode }}
            </el-button>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="90">
          <template slot-scope="scope">
            <el-button v-if="hasPerm('/alarmRule/update')" type="text" @click="edit(scope.row)">
              编辑
            </el-button>
            <el-button v-if="hasPerm('/alarmRule/delete')" type="text" @click="del(scope.row)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!--分页-->
    <div class="pagination-container">
      <el-pagination
        v-show="total>listQuery.limit"
        :current-page="listQuery.offset"
        :page-sizes="[20,30,50]"
        :page-size="listQuery.limit"
        :total="total"
        align="center"
        layout="total, sizes, prev, pager, next"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>

    <edit-rule v-show="editShow" ref="editrule" @watchChild="fetchData" />
    <info-well v-show="wellShow" ref="wellInfo" />
  </div>
</template>

<script>
import infoWell from '@/views/wellManage/infoWell'
import editRule from '@/views/alarmRule/components/editRule'
import { getDeviceType } from '@/api/device/device'
import { getAlarmRuleList, delAlarmRule } from '@/api/alarm/alarmRule'

export default {
  name: 'ListRule',
  components: { editRule, infoWell },
  data() {
    return {
      listQuery: {
        keywords: '',
        status: '',
        offset: 1,
        limit: 20,
        sort: '',
        order: ''
      }, // 筛选条件
      columns: [
        {
          text: '井编号',
          value: 'wellCode',
          align: 'center',
          type: 'Button'
        },
        {
          text: '井名称',
          value: 'wellName',
          align: 'center'
        },
        {
          text: '井深',
          value: 'deep',
          align: 'center'
        },
        {
          text: '设备类型',
          value: 'deviceTypeName',
          align: 'center'
        },
        {
          text: '设备编号',
          value: 'devcode',
          align: 'center'
        },
        {
          text: '告警上限',
          value: 'highValue',
          align: 'center'
        },
        {
          text: '操作人',
          value: 'user',
          width: 100,
          align: 'center'
        }
      ], // 显示列
      multipleSelection: [], // 多选选中项
      list: [], // 列表数据
      total: 0, // 数据总数
      deviceTypeList: [], // 设备类型列表
      showDeviceType: true,
      listLoading: true, // 加载动画
      editShow: false, // 是否显示编辑框
      wellShow: false // 是否显示井详情
    }
  },
  created() {
    this.fetchDeviceType()// 获取部门树
    this.fetchData()// 获取数据
  },
  methods: {
    // 显示井详情
    showWellDetail(row) {
      this.wellShow = true
      this.$refs.wellInfo.initDialog(row.wellId)
    },
    checkSelection() {
      if (this.multipleSelection.length === 0) {
        return false
      } else {
        return true
      }
    },
    // 新增告警规则
    add() {
      this.editShow = true
      this.$refs.editrule.initDialog('create', true)
    },
    // 编辑告警规则信息
    edit(row) {
      this.editShow = true
      this.$refs.editrule.initDialog('update', true, row)
    },
    // 删除告警规则信息
    del(row) {
      this.$confirm(
        '确定要删除该告警规则吗？',
        '确认操作',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        delAlarmRule(row.id).then(response => {
          if (response.code === 200) {
            this.$message.success('删除成功')
            this.fetchData()
          }
        })
      })
    },
    // 查询数据
    search() {
      this.fetchData(false)
    },
    // 获取数据
    fetchData(isNowPage = true) {
      this.listLoading = true
      if (!isNowPage) { // 是否显示当前页，否则跳转第一页
        this.listQuery.offset = 1
      }
      getAlarmRuleList(this.listQuery).then(response => {
        this.list = response.data.rows
        this.total = parseInt(response.data.total)
        this.listLoading = false
      })
    },
    // 获取设备类型
    fetchDeviceType() {
      getDeviceType(this.listQuery).then(response => {
        this.deviceTypeList = []
        const supportDeviceTypes = this.$store.getters.deviceTypes
        for (const item of response.data) {
          if (supportDeviceTypes.indexOf(item.value) !== -1 && (item.value === '2' || item.value === '6')) {
            this.deviceTypeList.push(item)
          }
        }
        if (this.deviceTypeList.length <= 1) {
          this.showDeviceType = false
        }
      })
    },
    indexMethod(index) {
      return this.listQuery.limit * (this.listQuery.offset - 1) + index + 1
    },
    // 改变页容量
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.fetchData()
    },
    // 改变当前页
    handleCurrentChange(val) {
      this.listQuery.offset = val
      this.fetchData()
    },
    // 多选触发方法
    handleSelectionChange(val) {
      this.multipleSelection = val
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  $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>
