<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="所属服务区" prop="areaCode" label-width="100ox">
        <el-select v-model="queryParams.areaCode"  >
          <el-option v-for="item in areaOptions" :label="item.label" :value="item.id" :key="item.id" />
        </el-select>
      </el-form-item>

      <el-form-item label="告警类型" prop="alarmType">
        <el-select v-model="queryParams.alarmType" placeholder="请选择告警类型" clearable>
          <el-option
              v-for="dict in dict.type.alarm_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="子系统" prop="systemCode">
        <el-select v-model="queryParams.systemCode" placeholder="请选择子系统">
          <el-option
              v-for="subsystem in subsystemList"
              :key="subsystem.systemCode"
              :label="subsystem.systemName"
              :value="subsystem.systemCode"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="告警状态" prop="alarmState">
        <el-select v-model="queryParams.alarmState" placeholder="请选择告警状态">
          <el-option
              v-for="dict in dict.type.alarm_state"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </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">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['ems:alarm-info: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="['ems:alarm-info: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="['ems:alarm-info: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="['ems:alarm-info:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="alarmInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="园区代码" align="left" prop="areaCode" />
      <el-table-column label="子系统" align="center" prop="subSystemName" />
      <el-table-column label="对象类型" align="center" prop="objType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.obj_type" :value="scope.row.objType" />
        </template>
      </el-table-column>
      <el-table-column label="对象代码" align="center" prop="objCode" />
      <el-table-column label="告警时间" align="center" prop="alarmDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.alarmDate, '{y}-{m}-{d}') +" "+ parseTime(scope.row.alarmTime, '{hh}:{mm}:{s}')}}</span>
        </template>
      </el-table-column>
      <el-table-column label="告警代码" align="center" prop="alarmCode" />
      <el-table-column label="告警描述" align="center" prop="alarmMsg" />
      <el-table-column label="告警类型" align="center" prop="alarmType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.alarm_type" :value="scope.row.alarmType" />
        </template>
      </el-table-column>
      <el-table-column label="告警状态" align="center" prop="alarmState">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.alarm_state" :value="scope.row.alarmState" />
        </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="['ems:alarm-info:edit']"
          >修改
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" class="deleteBtn" @click="handleDelete(scope.row)" v-hasPermi="['basecfg:alarm-info: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"
    />

    <!-- 添加或修改能源设施告警对话框 -->
    <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="areaCode">
          <el-input v-model="form.areaCode" placeholder="请输入园区代码" />
        </el-form-item>
        <el-form-item label="对象类型" prop="objType">
          <el-select v-model="form.objType" placeholder="请选择对象类型">
            <el-option
                v-for="dict in dict.type.obj_type"
                :key="dict.value"
                :label="dict.label"
                :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="对象代码" prop="objCode">
          <el-input v-model="form.objCode" placeholder="请输入对象代码" />
        </el-form-item>
        <el-form-item label="告警时间" prop="alarmTime">
          <el-date-picker
              v-model="form.alarmTime"
              type="datetime"
              format="yyyy-MM-dd HH:mm"
              value-format="yyyy-MM-dd HH:mm:00"
              :style="{width: '100%'}"
              placeholder="请选择告警时间"
              @change="dateChange" clearable
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="子系统" prop="systemCode">
          <el-select v-model="form.systemCode" placeholder="请选择子系统">
            <el-option
                v-for="subsystem in subsystemList"
                :key="subsystem.systemCode"
                :label="subsystem.systemName"
                :value="subsystem.systemCode"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="告警代码" prop="alarmCode">
          <el-input v-model="form.alarmCode" placeholder="请输入告警代码" />
        </el-form-item>
        <el-form-item label="告警描述" prop="alarmMsg">
          <el-input v-model="form.alarmMsg" placeholder="请输入告警描述" />
        </el-form-item>
        <el-form-item label="告警类型" prop="alarmType">
          <el-select v-model="form.alarmType" placeholder="请选择告警类型">
            <el-option
                v-for="dict in dict.type.alarm_type"
                :key="dict.value"
                :label="dict.label"
                :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="告警状态" prop="alarmState">
          <el-select v-model="form.alarmState" placeholder="请选择告警状态">
            <el-option
                v-for="dict in dict.type.alarm_state"
                :key="dict.value"
                :label="dict.label"
                :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </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 { listSubsystem } from '@/api/adapter/subsystem';
import { addAlarmInfo, delAlarmInfo, getAlarmInfo, listAlarmInfo, updateAlarmInfo } from '@/api/alarm/alarm-info';
import dayjs from 'dayjs';
import { areaTreeSelect } from '@/api/basecfg/area'

export default {
  name: 'Alarm-info',
  dicts: ['obj_type', 'alarm_type', 'alarm_state'],
  data() {
    return {
      subsystemList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 能源设施告警表格数据
      alarmInfoList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      areaOptions: undefined,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        areaCode: null,
        objType: null,
        objCode: null,
        alarmCode: null,
        alarmMsg: null,
        alarmType: null,
        alarmState: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        areaCode: [
          {
            required: true,
            message: '园区代码不能为空',
            trigger: 'blur',
          },
        ],
        systemCode: [
          {
            required: true,
            message: '子系统不能为空',
            trigger: 'blur',
          },
        ],
        objType: [
          {
            required: true,
            message: '对象类型不能为空',
            trigger: 'change',
          },
        ],
        objCode: [
          {
            required: true,
            message: '对象代码不能为空',
            trigger: 'blur',
          },
        ],
        alarmTime: [
          {
            required: true,
            message: '告警时间不能为空',
            trigger: 'blur',
          },
        ],
        alarmType: [
          {
            required: true,
            message: '告警类型不能为空',
            trigger: 'change',
          },
        ],
        alarmState: [
          {
            required: true,
            message: '告警状态不能为空',
            trigger: 'blur',
          },
        ],
      },
    };
  },
  created() {
    this.getAreaTree("Area");
    this.getList();
    this.getSubList();
  },
  methods: {
    /** 查询能源设施告警列表 */
    getList() {
      this.loading = true;
      listAlarmInfo(this.queryParams).then(response => {
        this.alarmInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        areaCode: null,
        objType: null,
        objCode: null,
        alarmTime: null,
        alarmCode: null,
        alarmMsg: null,
        alarmType: null,
        alarmState: null,
      };
      this.resetForm('form');
    },
    async getSubList() {
      const { rows } = await listSubsystem({
        pageNum: 1,
        pageSize: 999,
      });
      this.subsystemList = rows;
    },
    dateChange(val) {
      this.$refs.form.alarmTime = val;
      this.$refs.form.alarmDate = dayjs(val).format('YYYY-MM-DD');
      this.form.alarmDate = dayjs(val).format('YYYY-MM-DD');
    },
    /** 搜索按钮操作 */
    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;
      getAlarmInfo(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) {
            updateAlarmInfo(this.form).then(response => {
              this.$modal.msgSuccess('修改成功');
              this.open = false;
              this.getList();
            });
          } else {
            addAlarmInfo(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 delAlarmInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess('删除成功');
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ems/alarm-info/export', {
        ...this.queryParams,
      }, `alarm-info_${new Date().getTime()}.xlsx`);
    },
    getAreaTree(tier) {
      areaTreeSelect(tier).then(response => {
        this.areaOptions = response.data;
      });
    }
  },
};
</script>
