<template>
  <div class="camerasMg">
    <el-row class="topBar">
      <el-col>
        <el-form :inline="true" :model="searchData">
          <el-form-item label>
            <el-input v-model="searchData.eventName" placeholder="请输事件名称"></el-input>
          </el-form-item>
          <el-form-item label>
            <el-select v-model="searchData.eventType" placeholder="请选择事件类型" @change="seletChangeEventSortArr" filterable>
              <el-option
                      v-for="item in eventSortArr"
                      :key="item.dictId"
                      :label="item.name"
                      :value="item.dictId.toString()"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="searchFun()">
              <i class="el-icon-search"></i> 搜索
            </el-button>
            <el-button type="primary" @click="resetFun()">
              <i class="el-icon-refresh"></i> 重置
            </el-button>
          </el-form-item>
          <el-form-item class="fr">
            <el-button plain type="primary" @click="addFun()">
              <i class="el-icon-plus"></i> 填报事件
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
    <el-table :data="tableData.pageData" stripe border style="width:100%;" :loading="false">
      <el-table-column width="50" type="index" label="序号"></el-table-column>
      <el-table-column prop="eventName" label="事件名称"></el-table-column>
      <el-table-column prop="eventLevel" label="事件等级">
        <template slot-scope="scope">
          <font>{{scope.row.eventLevel + '级'}}</font>
        </template>
      </el-table-column>
      <el-table-column prop="eventType" label="事件类型"></el-table-column>
      <el-table-column prop="eventAddress" label="事发地点"></el-table-column>
      <el-table-column prop="alarmTime" label="报案时间"></el-table-column>
      <el-table-column prop="alarmPeople" label="报案人"></el-table-column>
      <el-table-column prop="eventContent" label="事件描述" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column label="操作" width="400" align="center">
        <template slot-scope="scope">
          <el-button @click="editFun(scope.row)" size="small">
            <i class="el-icon-edit-outline"></i> 编辑
          </el-button>
          <el-button type="danger" @click="delFun(scope.row)" size="small">
            <i class="el-icon-delete"></i> 删除
          </el-button>
          <el-button type="success" @click="sendFun(scope.row)" size="small">
            <i class="el-icon-upload"></i> 信息报送
          </el-button>
          <el-button type="success" @click="pushFun(scope.row)" size="small">
            <i class="el-icon-upload"></i> 推送大屏
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="total,sizes, prev, pager, next, jumper"
      :total="tableData.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="PAGE.page"
      :page-size="PAGE.size"
    ></el-pagination>
    <EventsMgEdit
      v-if="dialogVisible"
      :dialogVisible="dialogVisible"
      :editForm="formData"
      :title="dialogTit"
      @dialogFun="closeDialog"
      @refreshTableData="getTableData"
      :key="zjKey"
    ></EventsMgEdit>
    <EventsMgSend
            v-if="dialogVisible2"
            :dialogVisible="dialogVisible2"
            :editForm="formData"
            :title="dialogTit"
            @dialogFun="closeDialog"
            @refreshTableData="getTableData"
            :key="zjsendKey"
    ></EventsMgSend>
  </div>
</template>
<script lang="ts">
import { Component, Vue, Provide } from "vue-property-decorator";
import EventsMgEdit from "./EventsMgEdit.vue";
import EventsMgSend from "./EventsMgSend.vue";
import METHOD from "@/utils/methods";
@Component({
  components: { EventsMgEdit,EventsMgSend }
})
export default class CamerasMg extends Vue {
  @Provide() signId: String = "";
  @Provide() tableData: Object = { pageData: [], total: 0 };
  @Provide() PAGE: any = { page: 1, size: 10 };
  @Provide() searchData: any = { deviceName: "" };
  @Provide() loading: Boolean = false;
  @Provide() eventSortArr: any = [];
  //编辑组件
  @Provide() dialogVisible: Boolean = false;
  @Provide() dialogVisible2: Boolean = false;
  @Provide() dialogTit: String = "新增";
  @Provide() formData: Object = {};
  @Provide() zjKey: any = 0;
  @Provide() zjsendKey: any = 0;
  @Provide() reVisible: Boolean = false;
  @Provide() reId: String = "";
  getTableData() {
    let that = this,
      params = Object.assign(that.PAGE, that.searchData);
    (that as any).$axios
      .post("/eventInfo/getEventInfoList", params)
      .then((res: any) => {
        if( res ) that.tableData = res;
      });
  }
  WebSocketConnect(json: any) {
    let that = this;
    if ("WebSocket" in window) {
      // 打开一个 web socket
      var ws = new WebSocket(`ws://${METHOD.GlobalUrl}/auth/websocket`);
      ws.onopen = function() {
        // Web Socket 已连接上，使用 send() 方法发送数据
        ws.send(JSON.stringify(json));
        (that as any).$message.success("推送成功!");
      };
      ws.onclose = function() {
        console.log("关闭WebSocket连接!");
      };
    } else {
      that.$message.error("您的浏览器不支持 WebSocket!");
    }
  }
  pushFun(row: any) {
    let that = this;
    (that as any)
      .$confirm("确定推送?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
      .then(() => {
        (that as any).$axios
          .get(`/eventInfo/pushScreenDataById/${row.id}`)
          .then((res: any) => {
            if (res.code == 0) {
              (that as any).WebSocketConnect(row)
            }
          });
      });
  }
  searchFun() {
    this.PAGE = { page: 1, size: 10 };
    this.getTableData();
  }
  resetFun() {
    this.PAGE = { page: 1, size: 10 };
    this.searchData = {};
    this.getTableData();
  }
  addFun() {
    this.zjKey++;
    this.dialogVisible = true;
  }
  editFun(row: any) {
    this.dialogTit = "编辑";
    this.formData = Object.assign({}, row);
    this.zjKey++;
    this.dialogVisible = true;
  }
  sendFun(row: any) {
        this.dialogTit = "信息报送";
        this.formData = Object.assign({}, row);
        this.zjsendKey++;
        this.dialogVisible2 = true;
    }
  delFun(row: any) {
    let that = this as any;
    METHOD.deleteFun(that, `/eventInfo/delEventInfoById/${row.id}`, function() {
      that.getTableData();
    });
  }
  handleSizeChange(val: any) {
    this.PAGE.size = val;
    this.getTableData();
  }
  handleCurrentChange(val: any) {
    this.PAGE.page = val;
    this.getTableData();
  }
  closeDialog() {
    //关闭窗口
    this.dialogVisible = false;
    this.dialogVisible2 = false;
    this.formData = {};
  }
  recloseDialog() {
    this.reVisible = false;
  }
  resoveFun(row: any){
    this.reVisible = true;
    this.formData = Object.assign({}, row);
  }
  seletChangeEventSortArr(dictId: any) {
    let that = this,
            arr;
    arr = that.eventSortArr.filter((ele: any) => dictId == ele.dictId);
    that.searchData.eventType = arr[0].name;
  }
  //事件分类
  getEventSort() {
    let that = this;
    (that as any).$axios
      .get(
              `/dict/getSysDictByDictTypeCode/event_sort`
      )
      .then((res: any) => {
        that.eventSortArr = res;
      });
  }
  created() {
    this.signId = this.$route.matched[1].meta.listId;
    this.getTableData();
    this.getEventSort();
  }
}
</script>
