<template>
  <div>
    <div class="container">
      <div class="handle-box">
        <selectoption></selectoption>
        <el-date-picker v-model="query.startTime" class="handle-input mr10" size="small" type="datetime" clearable
          placeholder="选择开始日期时间" default-time="00:00:00">
        </el-date-picker>
        <el-date-picker v-model="query.endTime" class="handle-input mr10" size="small" type="datetime" clearable
          placeholder="选择结束日期时间" default-time="00:00:00">
        </el-date-picker>
        <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" style="background: #FA8E00; border: none;">搜索</el-button>
        <el-button type="primary" size="small" icon="el-icon-search" @click="restFun" style="background: #FA8E00; border: none;">重置</el-button>
      </div>
      <el-table :data="tableData" border class="table" ref="multipleTable" header-cell-class-name="table-header" height="650">
        <el-table-column label="序号" type="index" width="80">
          <template slot-scope="scope">
            {{pageTotal - ((query.pageNum-1)*query.pageSize) - scope.$index}}
          </template>
        </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, sizes, prev, pager, next, jumper" :current-page="query.pageNum"
          :page-size="query.pageSize" :total="pageTotal" @current-change="handlePageChange" @size-change="handleSizeChange"></el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
  import {
    httpGet,
    httpPost
  } from "../api";
  import getLocalTime from "../utils/index";
  import moment from "moment"
  export default {
    // inject: ['reload'],
    name: "warningLog",
    data() {
      return {
        // selectoption:[],
        // selectoptionValue:'',
        query: {
          operType: "",
          operateName: "",
          operModul: "",
          pageSize:10,
          pageNum:1,
          startTime:'',
          endTime:''
        },
        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:[]
      };
    },
    beforeCreate() {
    },
    created() {
      this.alertTypeList=this.$MANUFACTURER.alertType.lockLog
      this.userInfo = JSON.parse(localStorage.getItem('info'))
      if(this.$route.params.alertType){
        this.query.alertType=this.$route.params.alertType
      }
      this.getAboutDevice()
    },
    mounted() {
    },
    methods: {
      selectTenantId(e) {
        localStorage.setItem('tenantId', e)
        this.reload()
      },
      restFun() {
        this.query = {
          operType: "",
          operateName: "",
          operModul: "",
          pageSize:10,
          pageNum:1,
          startTime:'',
          endTime:''
        }
        var q = {
          alertType: this.query.alertType,
          deviceNum: this.query.deviceName,
          startTime: moment(this.query.startTime).format("YYYY-MM-DD HH:mm:ss"),
          endTime: moment(this.query.endTime).format("YYYY-MM-DD HH:mm:ss"),
          deviceType:1
        };
        var data = {
          pageNum: 1,
          pageSize: this.query.pageSize,
          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 = this.time(+new Date(li.alertTime * 1000));
            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: 1
          })
        };
        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 data = {
          pageNum: 1,
          pageSize: this.query.pageSize,
          orderBy: "alert_time desc",
          q: JSON.stringify({
            deviceType:1,
            alertType:this.query.alertType,
          }),
          // orderType:"desc"
        };
        console.log(data)
        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 = this.time(+new Date(li.alertTime * 1000));
            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 = obj;
        for(var i in myObj){
          if(myObj[i]==''||myObj[i]==null||myObj[i]==undefined || myObj[i]=='Invalid date'){
            delete myObj[i]
          }
        }
        return JSON.stringify(myObj);
      },

      // 触发搜索按钮
      handleSearch() {
        // this.getlockAlert()
        this.query.pageNum=1
        var q = {
          alertType: this.query.alertType,
          deviceNum: this.query.deviceName,
          startTime: moment(this.query.startTime).format("YYYY-MM-DD HH:mm:ss"),
          endTime: moment(this.query.endTime).format("YYYY-MM-DD HH:mm:ss"),
          deviceType:1
        };
        console.log(q)
        var data = {
          pageNum: this.query.pageNum,
          pageSize: this.query.pageSize,
          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 = this.time(+new Date(li.alertTime * 1000));
            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);
      },
      getSearchList(){
        var q = {
          alertType: this.query.alertType,
          deviceNum: this.query.deviceName,
          startTime: moment(this.query.startTime).format("YYYY-MM-DD HH:mm:ss"),
          endTime: moment(this.query.endTime).format("YYYY-MM-DD HH:mm:ss"),
            deviceType:1
        };
        var data = {
          pageNum: this.query.pageNum,
          pageSize: this.query.pageSize,
          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 = this.time(+new Date(li.alertTime * 1000));
            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;
        });
      },
      // 分页导航
      handlePageChange(val) {
        this.query.pageNum=val
        this.getSearchList()
      },
       handleSizeChange(val) {
        this.query.pageNum=1
        this.query.pageSize=val
        this.getSearchList()
      },
    },
  };
</script>

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

  .handle-select {
    width: 120px;
  }

  .handle-input {
    width: 200px;
    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>
