<template>
  <div class="app-container">
    <el-row :gutter="28">
      <el-col :span="5" :xs="24">
        <div class="head-container">
          <el-input
            v-model="filterBoxName"
            placeholder="请输入电柜名称"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container">
          <div class="tree-container">
            <el-tree
              :data="InfoOptions"
              :props="props"
              :expand-on-click-node="false"
              :filter-node-method="filterNode"
              ref="tree"
              node-key="id"
              default-expand-all
              highlight-current
              @node-click="handleNodeClick"

            />

          </div>
        </div>
      </el-col>
      <el-col :span="19" :xs="24">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
<!--      <el-form-item label="租户号" prop="objId">
        <el-input
          v-model="queryParams.objId"
          placeholder="请输入租户号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="母排编号" prop="busbarCode">
        <el-input
          v-model="queryParams.busbarCode"
          placeholder="请输入母排编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电柜编号" prop="cabinetCode">
        <el-input
          v-model="queryParams.cabinetCode"
          placeholder="请输入电柜编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
<!--      <el-form-item label="最高温度" prop="tempMax">
        <el-input
          v-model="queryParams.tempMax"
          placeholder="请输入最高温度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
<!--      <el-form-item label="最低温度" prop="tempMin">
        <el-input
          v-model="queryParams.tempMin"
          placeholder="请输入最低温度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
<!--      <el-form-item label="平均温度" prop="tempAvg">
        <el-input
          v-model="queryParams.tempAvg"
          placeholder="请输入平均温度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
<!--      <el-form-item label="温差" prop="tempDiff">
        <el-input
          v-model="queryParams.tempDiff"
          placeholder="请输入温差"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
<!--      <el-form-item label="图像路径" prop="filePath">
        <el-input
          v-model="queryParams.filePath"
          placeholder="请输入图像路径"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
<!--      <el-form-item label="报警阈值" prop="alarmValue">
        <el-input
          v-model="queryParams.alarmValue"
          placeholder="请输入报警阈值"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="是否标识" prop="isFlag">
        <el-select v-model="queryParams.isFlag" placeholder="请选择是否标识" clearable>
          <el-option
            v-for="dict in dict.type.record_busbar_alarm_is_flag"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="记录时间">
        <el-date-picker
          v-model="daterangeRecordTime"
          style="width: 240px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </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">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['record:recordBusbarAlarm: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="['record:recordBusbarAlarm: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="['record:recordBusbarAlarm: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="['record:recordBusbarAlarm:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="recordBusbarAlarmList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="租户号" align="center" prop="objId" />
      <el-table-column label="母排编号" align="center" prop="busbarCode" />
      <el-table-column label="电柜编号" align="center" prop="cabinetCode" />
      <el-table-column label="最高温度" align="center" prop="tempMax" />
      <el-table-column label="最低温度" align="center" prop="tempMin" />
      <el-table-column label="平均温度" align="center" prop="tempAvg" />
      <el-table-column label="温差" align="center" prop="tempDiff" />
      <el-table-column label="图像路径" align="center" prop="filePath" />
      <el-table-column label="报警类型" align="center" prop="alarmType" />
      <el-table-column label="报警阈值" align="center" prop="alarmValue" />
      <el-table-column label="是否标识" align="center" prop="isFlag">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.record_busbar_alarm_is_flag" :value="scope.row.isFlag"/>
        </template>
      </el-table-column>
      <el-table-column label="记录时间" align="center" prop="recordTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.recordTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['record:recordBusbarAlarm:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['record:recordBusbarAlarm:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>


      </el-col>
    </el-row>


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

    <!-- 添加或修改巡检报警记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="租户号" prop="objId">
          <el-input v-model="form.objId" placeholder="请输入租户号" />
        </el-form-item>
        <el-form-item label="母排编号" prop="busbarCode">
          <el-input v-model="form.busbarCode" placeholder="请输入母排编号" />
        </el-form-item>
        <el-form-item label="电柜编号" prop="cabinetCode">
          <el-input v-model="form.cabinetCode" placeholder="请输入电柜编号" />
        </el-form-item>
        <el-form-item label="最高温度" prop="tempMax">
          <el-input v-model="form.tempMax" placeholder="请输入最高温度" />
        </el-form-item>
        <el-form-item label="最低温度" prop="tempMin">
          <el-input v-model="form.tempMin" placeholder="请输入最低温度" />
        </el-form-item>
        <el-form-item label="平均温度" prop="tempAvg">
          <el-input v-model="form.tempAvg" placeholder="请输入平均温度" />
        </el-form-item>
        <el-form-item label="温差" prop="tempDiff">
          <el-input v-model="form.tempDiff" placeholder="请输入温差" />
        </el-form-item>
        <el-form-item label="图像路径" prop="filePath">
          <el-input v-model="form.filePath" placeholder="请输入图像路径" />
        </el-form-item>
        <el-form-item label="报警阈值" prop="alarmValue">
          <el-input v-model="form.alarmValue" placeholder="请输入报警阈值" />
        </el-form-item>
        <el-form-item label="是否标识" prop="isFlag">
          <el-radio-group v-model="form.isFlag">
            <el-radio
              v-for="dict in dict.type.record_busbar_alarm_is_flag"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="记录时间" prop="recordTime">
          <el-date-picker clearable
            v-model="form.recordTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择记录时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRecordBusbarAlarm, getRecordBusbarAlarm, delRecordBusbarAlarm, addRecordBusbarAlarm, updateRecordBusbarAlarm } from "@/api/record/recordBusbarAlarm";
import {CabinetAndBusbarTree} from "@/api/record/recordBusbarTemp";

export default {
  name: "RecordBusbarAlarm",
  dicts: ['record_busbar_alarm_is_flag'],
  data() {
    return {

      //电柜信息下拉树选项
      InfoOptions: [],
      //左侧树结构筛选框
      filterBoxName: undefined,
      props:{
        children: 'children',
        label: 'label'
        /*        label: (data) => `(编号${data.code}) ${data.label} `*/
      },

      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 巡检报警记录表格数据
      recordBusbarAlarmList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 记录时间时间范围
      daterangeRecordTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        objId: null,
        busbarCode: null,
        cabinetCode: null,
        tempMax: null,
        tempMin: null,
        tempAvg: null,
        tempDiff: null,
        filePath: null,
        alarmType: null,
        alarmValue: null,
        isFlag: null,
        recordTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getTreeselect();
  },
  methods: {
    /** 查询巡检报警记录列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeRecordTime && '' != this.daterangeRecordTime) {
        this.queryParams.params["beginRecordTime"] = this.daterangeRecordTime[0];
        this.queryParams.params["endRecordTime"] = this.daterangeRecordTime[1];
      }
      listRecordBusbarAlarm(this.queryParams).then(response => {
        this.recordBusbarAlarmList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        objId: null,
        busbarCode: null,
        cabinetCode: null,
        tempMax: null,
        tempMin: null,
        tempAvg: null,
        tempDiff: null,
        filePath: null,
        alarmType: null,
        alarmValue: null,
        isFlag: null,
        recordTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeRecordTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.objId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加巡检报警记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const objId = row.objId || this.ids
      getRecordBusbarAlarm(objId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改巡检报警记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.objId != null) {
            updateRecordBusbarAlarm(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRecordBusbarAlarm(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const objIds = row.objId || this.ids;
      this.$modal.confirm('是否确认删除巡检报警记录编号为"' + objIds + '"的数据项？').then(function() {
        return delRecordBusbarAlarm(objIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('record/recordBusbarAlarm/export', {
        ...this.queryParams
      }, `recordBusbarAlarm_${new Date().getTime()}.xlsx`)
    },
    /** 查询电柜下拉树结构 */
    getTreeselect() {
      CabinetAndBusbarTree({}).then(response => {
        this.InfoOptions = response.data
      })
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true
      return data.label.indexOf(value) !== -1
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.busbarCode = data.code
      this.handleQuery()
    },

  }
};
</script>
<style scoped>

.tree-container {
height: 666px; /* 设置固定高度 */
overflow-y: auto; /* 添加垂直滚动条 */
}
</style>
