<template>
  <div class="app-container pit-alarm-index">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="项目名称" prop="projectId">
        <el-select v-model="queryParams.projectId" filterable placeholder="请选择项目" clearable @change="doQuerySub">
          <el-option v-for="(item, index) in projectOptions" :key="index" :label="item.projectName" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="总包单位" prop="subDeptId">
        <el-select v-model="queryParams.subDeptId" filterable placeholder="请选择总包单位" clearable>
          <el-option v-for="(item, index) in depts" :key="index" :label="item.deptName" :value="item.deptId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="告警状态" prop="status">
        <el-radio-group v-model="queryParams.status" @change="getList">
          <el-radio label="false">未处理</el-radio>
          <el-radio label="true">已处理</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="告警原因" prop="type">
        <el-select v-model="queryParams.type" filterable placeholder="请选择总包单位" clearable>
          <el-option v-for="(item, index) in '数据异常,DTU异常,传感器异常,网关异常,节点异常'.split(',')" :key="index" :label="item"
            :value="item">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8" v-if="1==2">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['device:pitAlarm:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['device:pitAlarm:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['device:pitAlarm:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['device:pitAlarm:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="pitAlarmList" @selection-change="handleSelectionChange"> 
      <el-table-column label="编号" align="center" prop="id" width="80" />
      <el-table-column label="项目名称" align="center" prop="prjName" width="180" />
      <el-table-column label="总包单位" align="center" prop="deptName" width="180" />
      <el-table-column label="告警源" align="center" prop="alarmSource" width="180" >
        <template slot-scope="scope">
          {{ scope.row.alarmSource}}-{{ scope.row.variety}}
        </template>
      </el-table-column>
      <el-table-column label="等级" align="center" prop="level" width="180">
        <template slot-scope="scope">
          <span class="sp-data" :class="'sp-level-'+scope.row.level">{{ getLevel(scope.row.level)}}</span>
        </template>
      </el-table-column>
      <el-table-column label="告警信息" align="center" prop="alarmInfo" width="180"/>
      <el-table-column label="产生次数" align="center" prop="numbers" width="80"/>
      <el-table-column label="产生时间" align="center" prop="gmtCreate" width="150"/>
      <el-table-column label="更新时间" align="center" prop="gmtModified" width="150"/>     
      <el-table-column label="处理人" align="center" prop="staff" width="100" v-if="queryParams.status=='true'"/>     
      <el-table-column label="处理人电话" align="center" prop="staffPhone" width="180" v-if="queryParams.status=='true'"/> 
      <el-table-column label="处理人结果" align="center" prop="result" width="180" v-if="queryParams.status=='true'"/>  
      <el-table-column label="处理图片" align="center" prop="result" width="180" v-if="queryParams.status=='true'">
        <template slot-scope="scope">
          <span style="color:#ccc" v-if="!scope.row.resultUrl">暂无</span>
          <el-image :src="scope.row.resultUrl" v-else
          :preview-src-list="[scope.row.resultUrl]"/>
        </template>
      </el-table-column> 
      <el-table-column label="操作" align="center"  fixed="right" class-name="small-padding fixed-width">
        <template slot-scope="scope">     
          <el-button type="text" v-if="queryParams.status=='false'" icon="el-icon-tickets" @click="showDetail(scope.row)">详情</el-button>     
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['device:pitAlarm:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
      <alarmDetailDlg ref="detailDlg"/>
  </div>
</template>

<script>
import { listPitAlarm, getPitAlarm, delPitAlarm, addPitAlarm, updatePitAlarm } from "@/api/device/pitAlarm";
import alarmDetailDlg from './alarmDetailDlg.vue'
export default {
  components:{
    alarmDetailDlg
  },
  name: "PitAlarm",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 报警信息表格数据
      pitAlarmList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectId: null,
        subDeptId: null,
        status: 'false',
        type: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      projectOptions: [],
      depts: [],
    };
  },
  created() {
    this.getList();
    this.init();
  },
  methods: {
    showDetail(row){
      this.$refs.detailDlg.showDialog(row);
    },
    doQuerySub() {
      let prjId=this.queryParams.projectId;      
      let tmps = this.prjDept2 && this.prjDept2[prjId] ? this.prjDept2[prjId] || [] : [];
      if (tmps.length > 0 || !prjId) {
        this.depts = tmps;
        if (tmps.length == 1) {
          this.queryParams.subDeptId = tmps[0].deptId;
        } else {
          this.queryParams.subDeptId = '';
        }
        this.getList();
        return;
      }
      this.$api.publics.queryUnitList({
        projectId: prjId,
        unitTypes: "2".split(","),
      }).then((d) => {
        let objs = d.rows || [];
        if (!this.prjDept2) {
          this.prjDept2 = {};
        }
        this.prjDept2[prjId] = objs;
        this.depts = objs;
        if (objs.length == 1) {
          this.queryParams.subDeptId = objs[0].deptId;
        } else {
          this.queryParams.subDeptId = '';
        }
        this.getList();
      });
    },
    init() {
      if (this.projectOptions && this.projectOptions.length > 0) {
        return;
      }
      this.$api.publics.getMyProjectList({}).then((response) => {
        this.projectOptions = response.rows;
      });
    },
    /** 查询报警信息列表 */
    getList() {
      this.loading = true;
      listPitAlarm(this.queryParams).then(response => {
        this.pitAlarmList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getLevel(lvl){
      return ["正常","超预警值","超报警值","超控制值"][lvl];
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        cfgId: null,
        srvId: null,
        alarmInfo: null,
        alarmSource: null,
        gmtAlarm: null,
        gmtAlarmOver: null,
        gmtAlarmStart: null,
        gmtCreate: null,
        gmtModified: null,
        level: null,
        meId: null,
        meName: null,
        monitorItemId: null,
        numbers: null,
        result: null,
        resultUrl: null,
        spId: null,
        spName: null,
        staff: null,
        staffPhone: null,
        status: null,
        structureId: null,
        structureName: null,
        threshold: null,
        type: null,
        variety: null,
        state: null,
        remark: null,
        isDel: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加报警信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPitAlarm(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改报警信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePitAlarm(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPitAlarm(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除报警信息编号为"' + ids + '"的数据项？').then(function () {
        return delPitAlarm(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('device/pitAlarm/export', {
        ...this.queryParams
      }, `pitAlarm_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style lang="scss">
.pit-alarm-index{
  .sp-data{
        &.sp-level-0{
          color: #FFEBEE;
        }
        &.sp-level-1{
          color: #EF9A9A;
        }
        &.sp-level-2{
          color: #EF5350; 
        }
        &.sp-level-3{
          color: #D32F2F;
        }
      }
}
</style>

