<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="deviceName">
        <el-select v-model="queryParams.deviceName" placeholder="风机名称" clearable>
          <el-option
            v-for="dict in deviceName"
            :key="dict.value"
            :label="dict.name"
            :value="dict.name"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="">
        <el-date-picker v-model="dateRange" style="width: 240px" value-format="yyyy-MM-dd" type="daterange" 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-table v-loading="loading" :data="infoList">
      <el-table-column label="风机名称" align="center" prop="deviceName"/>
      <el-table-column label="风机编号" align="center" prop="deviceId"/>
      <el-table-column label="风机安装描述" align="center" prop="installationLocation" />
      <el-table-column label="报警项" align="center" prop="alarmProduct" />
      <el-table-column label="监测值" align="center" prop="alarmValue" />
      <el-table-column label="报警时间" align="center" prop="alarmTime" />
      <el-table-column label="状态" align="center" prop="status" >
        <template slot-scope="scope">
          <el-button
            type="primary"
            @click="handleUpdate(scope.row)"
            v-if="scope.row.status==1"
          >处置</el-button>
          <span v-else>已处置</span>
       
        </template>
      </el-table-column>
      <el-table-column label="解除报警时间" align="center" prop="delAlarmTime" />
      <el-table-column label="解除原因" align="center" prop="delAlarmResion" />
      
     
    </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="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-form-item label="解除原因" prop="delAlarmResion">
          <el-input type="textarea" v-model="form.delAlarmResion" placeholder="请输入解除原因" />
        </el-form-item>
        <el-form-item label="解除报警时长(h)" prop="time">
          <el-input v-model="form.time" placeholder="请输入解除报警时长(h)" />
        </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 { listalarmFan, alarmFanEdit } from "@/api/tyler/fan";
  import { fanDeviceName } from "@/api/tyler/common";

  export default {
  name: "Info",
  data() {
    return {
      // 根路径
      baseURL: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 风机基本信息表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deviceName: null,
        deviceId: null,
    
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          { required: true, message: "id不能为空", trigger: "blur" }
        ],
        delAlarmResion: [
          { required: true, message: "解除原因不能为空", trigger: "blur" }
        ],
        time: [
          { required: true, message: "解除时长不能为空", trigger: "blur" }
        ],
      },
      deviceName:[],
      dateRange: [],
    };
  },
  created() {
    fanDeviceName().then(res=>{
      this.deviceName = res.data;
    })
    this.getList();
  },
  methods: {
    /** 查询风机基本信息列表 */
    getList() {
      this.loading = true;
      listalarmFan(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        delAlarmResion:null,
        time:null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
     
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.form.id = row.id;
      this.open = true;
      // getHistory(id).then(response => {
      //   this.form = response.data;
      //   this.open = true;
      //   this.title = "修改设备历史信息";
      // });
    },
    /** 提交按钮 */
    submitForm() {
      console.log(this.form)
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            alarmFanEdit(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addHistory(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
  
  }
};
</script>
