<template>
  <div>
    <div class="container">
      <div class="handle-box">
        <selectoption></selectoption>
        <el-input v-model="query.deviceName" size="small" placeholder="设备号" class="handle-input mr10" :clearable="true">
        </el-input>
        <el-select v-model="query.alertType" size="small" :clearable="true" placeholder="报警类型"
          class="handle-select mr10">
          <el-option :key="item.code" :label="item.desc" :value="item.code" v-for="item in alertTypeList"></el-option>
        </el-select>
        <el-button type="primary" size="small" icon="el-icon-search" @click="handleSearch">搜索</el-button>
        <el-button type="primary" size="small" icon="el-icon-search" @click="restFun">重置</el-button>
      </div>
      <el-table :data="tableData" border class="table" ref="multipleTable" header-cell-class-name="table-header">
        <el-table-column prop="id" label="ID"></el-table-column>
        <el-table-column prop="deviceNum" label="设备号"></el-table-column>
        <el-table-column prop="roomName" label="房间名称"></el-table-column>
        <el-table-column prop="alertTime" label="报警时间"></el-table-column>
        <el-table-column label="报警类型">
          <template slot-scope="scope">
            <span>{{scope.row.alertTypeDesc}}</span>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination background layout="total, prev, pager, next" :current-page="$store.state.currentPage"
          :page-size="10" :total="pageTotal" @current-change="handlePageChange"></el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
  import {
    httpGet,
    httpPost
  } from "../../api";
  import getLocalTime from "../../utils/index";
  import moment from "moment"
  export default {
    name: "warningLog",
    data() {
      return {
        query: {
          operType: "",
          operateName: "",
          operModul: "",
        },
        query1: {
          operType: "",
          operateName: "",
          operModul: "",
        },
        activeName: "first",
        tableData: [],
        tableData1: [],
        multipleSelection: [],
        delList: [],
        editVisible: false,
        pageTotal: 0,
        pageTotal1: 0,
        form: {},
        idx: -1,
        id: -1,
        AboutDevice: [],
        userInfo: {
          type: ''
        },
        alertTypeList: []
      };
    },
    props:['deviceNum'],
    beforeCreate() {},
    created() {
      this.alertTypeList = this.$MANUFACTURER.alertType.smokeSensationLog
      this.userInfo = JSON.parse(localStorage.getItem('info'))
      this.getAboutDevice()
    },
    mounted() {},
    methods: {
      selectTenantId(e) {
        localStorage.setItem('tenantId', e)
        this.reload()
      },
      restFun() {
        this.query = {
          operType: "",
          operateName: "",
          operModul: "",
        }
         var q = {}
        if(this.deviceNum!=''){
          q = {
            alertType: this.query.alertType,
            deviceNum: this.deviceNum,
            deviceType:4
          };
        }else{
          q = {
            alertType: this.query.alertType,
            deviceNum: this.query.deviceName,
            deviceType:4
          };
        }

        var data = {
          pageNum: 1,
          pageSize: 10,
          q: this.searchParaProcess(q),
          orderBy: "alert_time desc"
        };
        httpGet(data, this.$api.getLockAlert).then((res) => {
          var AboutDevice = this.AboutDevice
          res.data.list.map((li) => {
            li.createAt = this.time(+new Date(li.createAt));
            li.alertTime = moment(li.alertTime*1000).format("YYYY-MM-DD HH:mm:ss")
            var resultArr = AboutDevice.filter(_item => _item.deviceNum == li.deviceNum)[0]
            if (resultArr) {
              li.roomId = resultArr.id
              li.roomName = resultArr.roomName
            }
            li['alertTypeDesc'] = ''
            var alertTypeDesc = this.alertTypeList.filter(item => item.code == li.alertType)[0]
            if (alertTypeDesc) {
              li['alertTypeDesc'] = alertTypeDesc.desc
            }
          });
          this.$store.commit('increment', 1)
          this.tableData = res.data.list;
          this.pageTotal = res.data.total;
        });
      },
      // 查询和房间相关的设备
      getAboutDevice() {
        var data = {
          pageNum: 0,
          pageSize: 0,
          q: JSON.stringify({
            lockType: 4
          })
        };
        httpPost(data, this.$api.getDeviceItems).then((res) => {
          this.AboutDevice = res.data.list
          this.getOperates()
        })
      },
      time(time = +new Date()) {
        var date = new Date(time + 8 * 3600 * 1000); // 增加8小时
        return date.toJSON().substr(0, 19).replace("T", " ");
      },
      //   获取操作日志
      getOperates() {
         var q = {}
        if(this.deviceNum!=''){
          q = {
            alertType: this.query.alertType,
            deviceNum: this.deviceNum,
            deviceType:4
          };
        }else{
          q = {
            alertType: this.query.alertType,
            deviceNum: this.query.deviceName,
            deviceType:4
          };
        }

        var data = {
          pageNum: 1,
          pageSize: 10,
          orderBy: "alert_time desc",
          q: JSON.stringify(q)
          // orderType:"desc"
        };
        httpGet(data, this.$api.getLockAlert).then((res) => {
          var AboutDevice = this.AboutDevice
          res.data.list.map((li) => {
            li.createAt = this.time(+new Date(li.createAt));
            li.alertTime = moment(li.alertTime*1000).format("YYYY-MM-DD HH:mm:ss")
            var resultArr = AboutDevice.filter(_item => _item.deviceNum == li.deviceNum)[0]
            if (resultArr) {
              li.roomId = resultArr.id
              li.roomName = resultArr.roomName
            }
            li['alertTypeDesc'] = ''
            var alertTypeDesc = this.alertTypeList.filter(item => item.code == li.alertType)[0]
            if (alertTypeDesc) {
              li['alertTypeDesc'] = alertTypeDesc.desc
            }
          });
          this.tableData = res.data.list;
          this.pageTotal = res.data.total;
        });
      },
      //门锁报警同步刷新
      getlockAlert() {
        httpGet("", this.$api.getLockAlert1).then((res) => {});
      },

      searchParaProcess(obj) {
        var myObj;
        if (obj.alertType == "" || obj.alertType == "0") {
          if (obj.deviceNum == "") {
            myObj = {
              deviceType: obj.deviceType,
            };
          } else {
            myObj = {
              deviceNum: obj.deviceNum,
              deviceType: obj.deviceType
            };
          }
        } else {
          if (obj.deviceNum == "") {
            myObj = {
              alertType: obj.alertType,
              deviceType: obj.deviceType
            }
          } else {
            myObj = {
              alertType: obj.alertType,
              deviceNum: obj.deviceNum,
              deviceType: obj.deviceType
            }
          }
        }
        return JSON.stringify(myObj);
      },

      // 触发搜索按钮
      handleSearch() {
        this.getlockAlert()
         var q = {}
        if(this.deviceNum!=''){
          q = {
            alertType: this.query.alertType,
            deviceNum: this.deviceNum,
            deviceType:4
          };
        }else{
          q = {
            alertType: this.query.alertType,
            deviceNum: this.query.deviceName,
            deviceType:4
          };
        }

        var data = {
          pageNum: 1,
          pageSize: 10,
          q: this.searchParaProcess(q),
          orderBy: "alert_time desc"
        };
        httpGet(data, this.$api.getLockAlert).then((res) => {
          var AboutDevice = this.AboutDevice
          res.data.list.map((li) => {
            li.createAt = this.time(+new Date(li.createAt));
            li.alertTime = moment(li.alertTime*1000).format("YYYY-MM-DD HH:mm:ss")
            var resultArr = AboutDevice.filter(_item => _item.deviceNum == li.deviceNum)[0]
            if (resultArr) {
              li.roomId = resultArr.id
              li.roomName = resultArr.roomName
            }
            li['alertTypeDesc'] = ''
            var alertTypeDesc = this.alertTypeList.filter(item => item.code == li.alertType)[0]
            if (alertTypeDesc) {
              li['alertTypeDesc'] = alertTypeDesc.desc
            }
          });
          this.$store.commit('increment', 1)
          this.tableData = res.data.list;
          this.pageTotal = res.data.total;
        });
        //   this.$set(this.query, "pageIndex", 1);
      },
      // 分页导航
      handlePageChange(val) {
        this.$store.commit('increment', val)
         var q = {}
        if(this.deviceNum!=''){
          q = {
            alertType: this.query.alertType,
            deviceNum: this.deviceNum,
            deviceType:4
          };
        }else{
          q = {
            alertType: this.query.alertType,
            deviceNum: this.query.deviceName,
            deviceType:4
          };
        }

        var data = {
          pageNum: val,
          pageSize: 10,
          q: this.searchParaProcess(q),
          orderBy: "alert_time desc"
        };
        httpGet(data, this.$api.getLockAlert).then((res) => {
          var AboutDevice = this.AboutDevice
          res.data.list.map((li) => {
            li.createAt = this.time(+new Date(li.createAt));
            li.alertTime = moment(li.alertTime*1000).format("YYYY-MM-DD HH:mm:ss")
            var resultArr = AboutDevice.filter(_item => _item.deviceNum == li.deviceNum)[0]
            if (resultArr) {
              li.roomId = resultArr.id
              li.roomName = resultArr.roomName
            }
            li['alertTypeDesc'] = ''
            var alertTypeDesc = this.alertTypeList.filter(item => item.code == li.alertType)[0]
            if (alertTypeDesc) {
              li['alertTypeDesc'] = alertTypeDesc.desc
            }
          });
          this.tableData = res.data.list;
          this.pageTotal = res.data.total;
        });
      },
    },
  };
</script>

<style scoped>
  .handle-box {
    margin-bottom: 20px;
  }

  .handle-select {
    width: 120px;
  }

  .handle-input {
    width: 300px;
    display: inline-block;
  }

  .table {
    width: 100%;
    font-size: 14px;
  }

  .red {
    color: #ff0000;
  }

  .mr10 {
    margin-right: 10px;
  }

  .table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
  }
</style>
