<template>
  <div>
    <div class="container">
      <div class="handle-box">
        <el-input type="text" size="small" v-model="query.deviceNum" class="Width200" placeholder="请输入子设备号"></el-input>
        <el-button type="primary" icon="el-icon-search" size="small" @click="handleSearch">搜索</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>
      <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 RequestSet from "../api/allFun.js"
  export default {
    name: "warningLog",
    data() {
      return {
        query: {
          operType: "",
          operateName: "",
          operModul: "",
          deviceNum:""
        },
        query1: {
          operType: "",
          operateName: "",
          operModul: "",
        },
        activeName: "first",
        tableData: [],
        tableData1: [],
        multipleSelection: [],
        delList: [],
        editVisible: false,
        pageTotal: 0,
        pageTotal1: 0,
        form: {},
        idx: -1,
        id: -1,
        AboutDevice: []
      };
    },
    props: ['deviceNum'],
    created() {

    },
    mounted() {
      this.getOperates();
    },
    methods: {
      //   获取操作日志
      getOperates() {
        console.log(this.deviceNum)
        var data = {
          deviceNum: this.deviceNum
        };
        RequestSet.getGatewaySubequipment(data,(callback)=>{
          if(callback.success){
            this.tableData = callback.data.list;
          }else{
            this.$message.error(callback.message)
          }
        })
      },
      searchParaProcess(obj) {
        var myObj;
        if (obj.alertType == "" || obj.alertType == "0") {
          if (obj.deviceNum == "") {
            myObj = {};
          } else {
            myObj = {
              deviceNum: obj.deviceNum
            };
          }
        } else {
          if (obj.deviceNum == "") {
            myObj = {
              alertType: obj.alertType
            }
          } else {
            myObj = {
              alertType: obj.alertType,
              deviceNum: obj.deviceNum
            }
          }
        }
        return JSON.stringify(myObj);
      },
      time(time = +new Date()) {
        var date = new Date(time + 8 * 3600 * 1000); // 增加8小时
        return date.toJSON().substr(0, 19).replace("T", " ");
      },
      // 触发搜索按钮
      handleSearch() {
        var q = {
          alertType: this.query.alertType,
          deviceNum: this.deviceNum.deviceNum
        };

        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 = 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.roomId = this.deviceNum.id
            li.roomName = this.deviceNum.roomName
          });
          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 = {
          alertType: this.query.alertType,
          deviceNum: this.deviceNum.deviceNum
        };

        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 = 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.roomId = this.deviceNum.id
            li.roomName = this.deviceNum.roomName
          });
          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>
