<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px" @submit.native.prevent>
      <el-form-item label="预警标题" prop="forewarningName">
        <el-input
          v-model="queryParams.forewarningName"
          placeholder="请输入预警标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备" prop="equipmentId">
        <template>
          <el-select v-model="queryParams.equipmentId" filterable placeholder="请选择">
            <el-option
              v-for="item in equipmentList"
              :key="item.equipmentId"
              :label="item.equipmentName"
              :value="item.equipmentId">
            </el-option>
          </el-select>
        </template>
      </el-form-item>
      <el-form-item label="园区" prop="parkId">
        <el-select v-model="queryParams.parkId" placeholder="请选择" clearable size="small">
          <el-option
            v-for="item in parkList"
            :key="item.parkId"
            :label="item.parkName"
            :value="item.parkId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否解决" prop="isResolve">
        <el-select v-model="queryParams.isResolve" placeholder="请选择是否解决" clearable size="small">
          <el-option
            v-for="dict in dict.type.sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="解决时间" prop="resolveTime">
        <el-date-picker clearable size="small"
                        v-model="queryParams.resolveTime"
                        type="datetime"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        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="['info:forewarningInfo: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="['info:forewarningInfo: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="['info:forewarningInfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['info:forewarningInfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="forewarningInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="预警id" align="center" prop="forewarningId" />
      <el-table-column label="预警标题" align="center" prop="forewarningName" />
      <el-table-column label="设备" align="center" prop="equipmentId" />
      <el-table-column label="设备名" align="center" prop="equipmentName" />
      <el-table-column label="记录时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否解决" align="center" prop="isResolve">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.isResolve"/>
        </template>
      </el-table-column>
      <el-table-column label="解决时间" align="center" prop="resolveTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.resolveTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="140">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['info:forewarningInfo:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['info:forewarningInfo: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="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="预警标题" prop="forewarningName">
          <el-input v-model="form.forewarningName" placeholder="请输入预警标题" />
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <el-input v-model="form.content" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="设备Id" prop="equipmentId">
          <el-select v-model="form.equipmentId" filterable placeholder="请选择" @change="onEquipmentChange">
            <el-option
              v-for="item in equipmentList"
              :key="item.equipmentId"
              :label="item.equipmentId"
              :value="item.equipmentId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="园区" prop="parkId">
          <el-select v-model="form.parkId" placeholder="请选择" clearable size="small">
            <el-option
              v-for="item in parkList"
              :key="item.parkId"
              :label="item.parkName"
              :value="item.parkId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="设备名" prop="equipmentName">
          <el-input v-model="form.equipmentName" placeholder="请输入设备名" />
        </el-form-item>
        <el-form-item label="是否解决" prop="isResolve">
          <el-select v-model="form.isResolve" placeholder="请选择是否解决">
            <el-option
              v-for="dict in dict.type.sys_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="解决时间" prop="resolveTime">
          <el-date-picker clearable size="small"
                          v-model="form.resolveTime"
                          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 { listForewarningInfo, getForewarningInfo, delForewarningInfo, addForewarningInfo, updateForewarningInfo, exportForewarningInfo } from "@/api/info/forewarningInfo";
import { listEquipmentInfo } from "@/api/info/equipmentInfo";
import { listParkInfo, getParkInfo, delParkInfo, addParkInfo, updateParkInfo, exportParkInfo } from "@/api/info/parkInfo";

export default {
  name: "ForewarningInfo",
  dicts: ['sys_yes_no'],
  data() {
    return {
      parkList: [],
      equipmentList: [],
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 预警信息表格数据
      forewarningInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        forewarningName: null,
        equipmentId: null,
        isResolve: null,
        resolveTime: null,
        parkId: null
      },
      queryEquipmentParams: {
        pageNum: 1,
        pageSize: 999999,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        forewarningId: [
          { required: true, message: "预警id不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getEquipmentList();
    this.getParkList();
  },
  methods: {

    getParkList() {
      listParkInfo(this.queryEquipmentParams).then(response => {
        this.parkList = response.data.list;
      });
    },

    onEquipmentChange(equipmentId) {
      const selectedEquipment = this.equipmentList.find(equipment => equipment.equipmentId === equipmentId);
      if (selectedEquipment) {
        this.form.equipmentName = selectedEquipment.equipmentName;
      } else {
        this.form.equipmentName = '';
      }
    },


    getEquipmentList() {
      listEquipmentInfo(this.queryEquipmentParams).then(response => {
        this.equipmentList = response.data.list;
      });
    },

    /** 查询预警信息列表 */
    getList() {
      this.loading = true;
      listForewarningInfo(this.queryParams).then(response => {
        this.forewarningInfoList = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        forewarningId: null,
        forewarningName: null,
        content: null,
        equipmentId: null,
        equipmentName: null,
        createTime: null,
        isResolve: null,
        resolveTime: null,
        parkId: 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.forewarningId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加预警信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const forewarningId = row.forewarningId || this.ids
      getForewarningInfo(forewarningId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改预警信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.forewarningId != null) {
            updateForewarningInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addForewarningInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const forewarningIds = row.forewarningId || this.ids;
      this.$modal.confirm('是否确认删除预警信息编号为"' + forewarningIds + '"的数据项？').then(function() {
        return delForewarningInfo(forewarningIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有预警信息数据项？').then(() => {
        this.exportLoading = true;
        return exportForewarningInfo(queryParams);
      }).then(response => {
        this.$download.name(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script>
