<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
    
      <el-form-item label="报警地址" prop="emergencyEventAddress">
        <el-input
          v-model="queryParams.emergencyEventAddress"
          placeholder="请输入报警地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报警电话" prop="alarmPhoneNumber">
        <el-input
          v-model="queryParams.alarmPhoneNumber"
          placeholder="请输入报警电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="源id" prop="sourceId">
        <el-input
          v-model="queryParams.sourceId"
          placeholder="请输入源id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->

      <!-- <el-form-item label="预警大类" prop="aiClass">
        <el-input
          v-model="queryParams.aiClass"
          placeholder="请输入预警大类"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预警小类" prop="aiClass2">
        <el-input
          v-model="queryParams.aiClass2"
          placeholder="请输入预警小类"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="紧急程度" prop="aiLevel">
        <el-input
          v-model="queryParams.aiLevel"
          placeholder="请输入紧急程度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="紧急系数" prop="aiNum">
        <el-input
          v-model="queryParams.aiNum"
          placeholder="请输入紧急系数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="报警时间范围" prop="">
        <!-- <el-date-picker clearable
          v-model="queryParams.emergencyEventTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择报警时间">
        </el-date-picker> -->
        <el-date-picker
          v-model="timeScope"
          type="datetimerange"
          range-separator="至"
          value-format="yyyy-MM-dd HH:mm:ss"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="创建时间范围" prop="">
        <el-date-picker
          v-model="creatTimeScope"
          type="datetimerange"
          range-separator="至"
          value-format="yyyy-MM-dd HH:mm:ss"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="是否关联其他单位事件" prop="selectIsNull">
        <el-select 
          clearable
          v-model="queryParams.selectIsNull"
          placeholder="请选择是否关联其他单位事件"
          @keyup.enter.native="handleQuery"
        >
        <el-option :value="1" label="是"></el-option>
        <el-option :value="2" label="否"></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="false">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['page:alarm: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="['page:alarm: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="['page:alarm: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="['page:alarm:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="alarmList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <!-- <el-table-column label="主键id" align="center" prop="emergencyEventId" /> -->
      <el-table-column label="报警时间" align="center" prop="emergencyEventTime" width="180">
        <!-- <template slot-scope="scope">
          <span>{{ parseTime(scope.row.emergencyEventTime, '{y}-{m}-{d}') }}</span>
        </template> -->
      </el-table-column>
      <el-table-column label="报警内容" align="center" prop="emergencyEventDesc" />
      <el-table-column label="报警地址" align="center" prop="emergencyEventAddress" />
      <el-table-column label="报警电话" align="center" prop="alarmPhoneNumber" />
      <el-table-column label="ai_120_id" align="center" prop="ai120Id" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <!-- <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
        </template> -->
      </el-table-column>
      <!-- <el-table-column label="源id" align="center" prop="sourceId" /> -->
      <!-- <el-table-column label="预警大类" align="center" prop="aiClass" />
      <el-table-column label="预警小类" align="center" prop="aiClass2" />
      <el-table-column label="紧急程度" align="center" prop="aiLevel" />
      <el-table-column label="紧急系数" align="center" prop="aiNum" /> -->
      <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="['page:alarm:edit']"
          >修改</el-button> -->
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['page:alarm:remove']"
          >删除</el-button> -->
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
          >查看详情</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="100px" disabled>
        <el-row>
          <el-col :span="12">
            <el-form-item label="报警时间" prop="emergencyEventTime" class="item-ajms">
              <el-date-picker clearable
                v-model="form.emergencyEventTime"
                type="date"
                value-format="yyyy-MM-dd"
                >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="预警大类" prop="aiClass" class="item-ajms">
              <el-input v-model="form.aiClass" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="报警地址" prop="emergencyEventAddress" class="item-ajms">
              <el-input v-model="form.emergencyEventAddress" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否删除" prop="delFlag" class="item-ajms">
              <el-input v-model="form.delFlag" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="报警电话" prop="alarmPhoneNumber" class="item-ajms">
              <el-input v-model="form.alarmPhoneNumber" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="预警小类" prop="aiClass2" class="item-ajms">
              <el-input v-model="form.aiClass2" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="紧急程度" prop="aiLevel" class="item-ajms">
              <el-input v-model="form.aiLevel" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="紧急系数" prop="aiNum" class="item-ajms">
              <el-input v-model="form.aiNum" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="报警内容" prop="emergencyEventDesc" class="item-ajms">
              <el-input v-model="form.emergencyEventDesc" resize="none" :rows="3" type="textarea" />
            </el-form-item>
          </el-col>
        </el-row>
        
      </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 { listAlarm, getAlarm, delAlarm, addAlarm, updateAlarm } from "@/api/page/alarm";

export default {
  name: "Alarm",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 报警事件表格数据
      alarmList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        emergencyEventSource: null,
        emergencyEventStatus: null,
        warnResponseStatus: null,
        emergencyResponseStatus: null,
        emergencyEventTitle: null,
        emergencyEventType: null,
        emergencyEventLevel: null,
        emergencyEventTime: null,
        emergencyEventDesc: null,
        emergencyEventAddress: null,
        emergencyEventArea: null,
        emergencyEventLon: null,
        emergencyEventLat: null,
        submitUnit: null,
        submitUnitContact: null,
        submitUnitTelephone: null,
        reportPerson: null,
        reportPersonTelephone: null,
        fileUrls: null,
        sendOutTimes: null,
        groupId: null,
        isRehearsal: null,
        alarmPhoneNumber: null,
        sourceId: null,
        isJudged: null,
        eventDetail: null,
        aiClass: null,
        aiClass2: null,
        aiLevel: null,
        aiNum: null,
        selectIsNull: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      timeScope: [],
      creatTimeScope: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询报警事件列表 */
    getList() {
      this.loading = true;
      listAlarm(this.queryParams).then(response => {
        this.alarmList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        emergencyEventId: null,
        emergencyEventSource: null,
        emergencyEventStatus: null,
        warnResponseStatus: null,
        emergencyResponseStatus: null,
        emergencyEventTitle: null,
        emergencyEventType: null,
        emergencyEventLevel: null,
        emergencyEventTime: null,
        emergencyEventDesc: null,
        emergencyEventAddress: null,
        emergencyEventArea: null,
        emergencyEventLon: null,
        emergencyEventLat: null,
        submitUnit: null,
        submitUnitContact: null,
        submitUnitTelephone: null,
        reportPerson: null,
        reportPersonTelephone: null,
        remark: null,
        fileUrls: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        sendOutTimes: null,
        groupId: null,
        isRehearsal: null,
        alarmPhoneNumber: null,
        sourceId: null,
        isJudged: null,
        eventDetail: null,
        aiClass: null,
        aiClass2: null,
        aiLevel: null,
        aiNum: null,
        selectIsNull: null,
      };
      this.timeScope = [];
      this.creatTimeScope = [];
      this.queryParams.startTime = "";
      this.queryParams.endTime = "";
      this.queryParams.createStartTime = "";
      this.queryParams.createEndTime = "";
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.queryParams.startTime = this.timeScope[0];
      this.queryParams.endTime = this.timeScope[1];
      this.queryParams.createStartTime = this.creatTimeScope[0];
      this.queryParams.createEndTime = this.creatTimeScope[1];
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.timeScope = [];
      this.creatTimeScope = [];
      this.queryParams.startTime = "";
      this.queryParams.endTime = "";
      this.queryParams.createStartTime = "";
      this.queryParams.createEndTime = "";
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.emergencyEventId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加报警事件";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const emergencyEventId = row.emergencyEventId || this.ids
      getAlarm(emergencyEventId).then(response => {
        this.form = response.data;
        this.open = true;
        // this.title = "修改报警事件";
        this.title = "详情";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.emergencyEventId != null) {
            updateAlarm(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAlarm(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const emergencyEventIds = row.emergencyEventId || this.ids;
      this.$modal.confirm('是否确认删除报警事件编号为"' + emergencyEventIds + '"的数据项？').then(function() {
        return delAlarm(emergencyEventIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('page/alarm/export', {
        ...this.queryParams
      }, `alarm_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
