<template>
  <div>
    <div class="container">
      <div class="handle-box">
        <!-- <el-input v-model="query.deviceName" placeholder="设备号" class="handle-input mr10" :clearable="true"></el-input> -->
        <el-select v-model="query.alertType" placeholder="报警类型" class="handle-select mr10" size="small" clearable>
          <el-option key="0" label="全部" value="0"></el-option>
          <el-option key="1" label="门锁被撬" value="1"></el-option>
          <el-option key="2" label="强行开门" value="2"></el-option>
          <el-option key="3" label="指纹尝试开锁被冻结" value="3"></el-option>
          <el-option key="4" label="密码尝试开锁被冻结" value="4"></el-option>
          <el-option key="5" label="卡尝试开锁被冻结" value="5"></el-option>
          <el-option key="6" label="钥匙尝试开锁被冻结" value="6"></el-option>
          <el-option key="7" label="低电量报警" value="7"></el-option>
          <el-option key="8" label="保留" value="8"></el-option>
          <el-option key="9" label="胁迫开锁报警" value="9"></el-option>
          <el-option key="10" label="忘拔钥匙" value="10"></el-option>
          <el-option key="11" label="敲门提醒" value="11"></el-option>
          <el-option key="12" label="SOS求救提醒" value="12"></el-option>
          <el-option key="13" label="门没关好" value="13"></el-option>
          <el-option key="14" label="门已反锁" value="14"></el-option>
          <el-option key="15" label="门已解锁" value="15"></el-option>
          <el-option key="16" label="常开已开启" value="16"></el-option>
          <el-option key="17" label="其它报警" value="100"></el-option>
        </el-select>
        <el-button type="primary" icon="el-icon-search" @click="handleSearch" size="small">搜索</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 v-if="scope.row.alertType == 1">门锁被撬</span>
            <span v-if="scope.row.alertType == 2">强行开门</span>
            <span v-if="scope.row.alertType == 3">指纹尝试开锁被冻结</span>
            <span v-if="scope.row.alertType == 4">密码尝试开锁被冻结</span>
            <span v-if="scope.row.alertType == 5">卡尝试开锁被冻结</span>
            <span v-if="scope.row.alertType == 6">钥匙尝试开锁被冻结</span>
            <span v-if="scope.row.alertType == 7">低电量报警</span>
            <span v-if="scope.row.alertType == 8">保留</span>
            <span v-if="scope.row.alertType == 9">胁迫开锁报警</span>
            <span v-if="scope.row.alertType == 10">忘拔钥匙</span>
            <span v-if="scope.row.alertType == 11">敲门提醒</span>
            <span v-if="scope.row.alertType == 12">SOS求救提醒</span>
            <span v-if="scope.row.alertType == 13">门没关好</span>
            <span v-if="scope.row.alertType == 14">门已反锁</span>
            <span v-if="scope.row.alertType == 15">门已解锁</span>
            <span v-if="scope.row.alertType == 16">常开已开启</span>
            <span v-if="scope.row.alertType == 17">电量低压提醒</span>
            <span v-if="scope.row.alertType == 100">其他</span>
          </template>
        </el-table-column>
        <!-- <el-table-column prop="content" label="报错内容" :show-overflow-tooltip="true"></el-table-column>
            <el-table-column prop="battery" label="电量"></el-table-column>
            <el-table-column prop="rssi" label="信号"></el-table-column> -->
        <!-- <el-table-column prop="createAt" 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";
  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: []
      };
    },
    props: ['deviceNum'],
    created() {
      // this.getAboutDevice()
    },
    mounted() {
      this.getOperates();
    },
    methods: {
      // 查询和房间相关的设备
      getAboutDevice() {
        var data = {
          pageNum: 0,
          pageSize: 0,
          q: JSON.stringify({
            lockType: 1
          })
        };
        httpPost(data, this.$api.getDeviceItems).then((res) => {
          this.AboutDevice = res.data.list
        })
      },
      time(time = +new Date()) {
        var date = new Date(time + 8 * 3600 * 1000); // 增加8小时
        return date.toJSON().substr(0, 19).replace("T", " ");
      },
      //   获取操作日志
      getOperates() {
        var q = {
          deviceNum: this.deviceNum.deviceNum
        };
        var data = {
          q: JSON.stringify(q),
          pageNum: 1,
          pageSize: 10,
          orderBy: "alert_time desc",
          // 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 = 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;
        });
      },
      //门锁报警同步刷新
      getlockAlert() {
        httpGet("", this.$api.getLockAlert1).then((res) => {});
      },

      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);
      },

      // 触发搜索按钮
      handleSearch() {
        // this.getlockAlert()
        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>
