<template>
  <div>
    <div class="container">
      <div class="handle-box">
        <el-input v-model="query.deviceNum" size="small" placeholder="设备号" :clearable="true" class="handle-input w150 mr10">
        </el-input>
        <el-select v-model="query.command" size="small" placeholder="操作类型" class="handle-select mr10">
          <el-option :label="item.desc" :value="item.code" :key="item.code" v-for="(item,index) in DeviceFeedbackLog"></el-option>
        </el-select>
        <el-select v-model="query.status" size="small" placeholder="状态" class="handle-select mr10">
          <el-option label="全部" value="-1" key="-1"></el-option>
          <el-option label="下发中" value="0" key="0"></el-option>
          <el-option label="下发成功" value="1" key="1"></el-option>
          <el-option label="未下发" value="2" key="2"></el-option>
          <el-option label="下发失败" value="3" key="3"></el-option>
          <el-option label="下发取消" value="4" key="4"></el-option>
          <el-option label="丢失" value="5" key="5"></el-option>
          <el-option label="等待发送" value="700" key="700"></el-option>
          <el-option label="发送失败" value="702" key="702"></el-option>
          <el-option label="已作废" value="703" key="703"></el-option>
          <el-option label="系统已锁定" value="101224" key="101224"></el-option>
          <el-option label="存储读写错误" value="101225" key="101225"></el-option>
          <el-option label="管理员手机号比对失败" value="101226" key="101226"></el-option>
          <el-option label="存储空间已满" value="101227" key="101227"></el-option>
        </el-select>
        <el-button type="primary" size="small" icon="el-icon-search" @click="handleSearch">搜索</el-button>
        <el-button type="primary" size="small" @click="resting">重置</el-button>
      </div>
      <el-table :data="tableData" border class="table" ref="multipleTable" header-cell-class-name="table-header">
        <el-table-column prop="deviceNum" label="设备号"></el-table-column>
        <el-table-column prop="roomName" label="房间名称"></el-table-column>
        <el-table-column prop="operationType" label="操作类型"></el-table-column>
        <el-table-column prop="statusText" label="命令状态"></el-table-column>
        <el-table-column prop="cardName" label="锁用户"></el-table-column>
        <el-table-column prop="operateName" label="操作员"></el-table-column>
        <el-table-column prop="serial" label="流水号"></el-table-column>
        <el-table-column prop="createTime" label="生效时间"></el-table-column>
      <!--  <el-table-column prop="createTime" label="操作" v-if="userInfo.type==0">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" @click="cancelDistribution(scope.$index,scope.row)"
              v-if="scope.row.result==700">取消下发</el-button>
          </template>
        </el-table-column> -->
      </el-table>
      <div class="pagination">
        <el-pagination background layout="total, prev, pager, next" :current-page="$store.state.currentPage"
          :page-size="query.pageSize" :total="pageTotal" @current-change="handlePageChange"></el-pagination>
      </div>
    </div>
  </div>
</template>

<!--组件逻辑-->
<script>
  import {
    httpGet,
    httpPost
  } from "../api";
  import utils from "../utils/util.js";
  export default {
    inject: ['reload'],
    name: "warningLog",
    data() {
      return {
        selectoption: [],
        selectoptionValue: '',
        query: {
          command: "",
          deviceNum: "",
          operModul: "",
          status: ''
        },
        pageIndex: 1,
        query1: {
          command: "",
          deviceNum: "",
          operModul: "",
        },
        activeName: "first",
        tableData: [],
        tableData1: [],
        multipleSelection: [],
        delList: [],
        editVisible: false,
        pageTotal: 0,
        pageTotal1: 0,
        form: {},
        idx: -1,
        id: -1,
        AboutDevice: [],
        userInfo: {
          type: ''
        },
		DeviceFeedbackLog:[]
      };
    },
    props: ['deviceNum'],
    beforeCreate() {
      // var tenantId = JSON.parse(localStorage.getItem('tenantId'))
      // if (tenantId == 400) {
      //   this.$store.commit('defaultsettenantid')
      // }
    },
    created() {
		this.DeviceFeedbackLog=this.$MANUFACTURER.DeviceFeedbackLog
		this.userInfo = JSON.parse(localStorage.getItem('info'))
		this.getAboutDevice()
		this.selectoption = JSON.parse(localStorage.getItem('selectoption'))
		this.selectoptionValue = localStorage.getItem('tenantId')
    },
    mounted() {
      this.getOperates();
    },
    methods: {
      selectTenantId(e) {
        localStorage.setItem('tenantId', e)
        this.reload()
      },
      // 取消下发
      cancelDistribution(index, row) {
        var data = [row.id]
        httpPost(data, this.$api.cancelDistribution).then(res => {
          if (res.success) {
            this.$message.success("取消成功")
          } else {
            this.$message.error("取消失败")
          }
        })
      },
      // 重置
      resting() {
        this.query = {
          command: "",
          deviceNum: "",
          operModul: "",
          status: ''
        }
        this.handleSearch()
      },
      // 查询和房间相关的设备
      getAboutDevice() {
        var data = {
          pageNum: 1,
          pageSize: 1,
          q:JSON.stringify({
              lockType: 1,
              deviceNum:this.deviceNum
          })
        };
        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", " ");
      },
      searchParaProcess(obj) {
        var myObj = obj;
        if (myObj.deviceNum == "" || myObj.command == "" || myObj.result == '') {
          if (myObj.deviceNum == "" && myObj.command == "" && myObj.result == '') {
            myObj = {};
          } else {
            if (myObj.deviceNum == '') {
              delete myObj.deviceNum
            }
            if (myObj.command == '') {
              delete myObj.command
            }
            if (myObj.result == '') {
              delete myObj.result
            }
          }
        }
        return JSON.stringify(myObj);
      },
      //   获取操作日志
      getOperates() {
        var data = {
          pageNum: 1,
          pageSize: 10,
          orderType: "desc",
          orderBy: "createTime",
          q:JSON.stringify({
            deviceNum:this.deviceNum
          })
        };
        httpPost(data, this.$api.getDeviceResult).then((res) => {
          var AboutDevice = this.AboutDevice
          let datalist=utils.zlib_unzip(res.data)
          datalist.map((li) => {
            li.createTime = this.time(+new Date(li.createTime));
            var resultArr = AboutDevice.filter(_item => _item.imei == li.imei)[0]
            li['statusText'] = utils.statusCode(li.result)
            if (resultArr) {
              li.roomId = resultArr.id
              li.roomName = resultArr.roomName
            } else {
              li.roomId = ''
              li.roomName = ''
            }
			li['operationType']=''
			var operationType=this.DeviceFeedbackLog.filter(item=>item.code==li.command)[0]
			if(operationType){
				li['operationType']=operationType.desc
			}
          });
          this.tableData = datalist;
          this.pageTotal = res.data.total;
        });
      },
      // 触发搜索按钮
      handleSearch() {
        var q = {}
        if (this.query.command == 0) {
          q = {
            deviceNum: this.deviceNum,
            result: this.query.status
          };
        } else {
          q = {
            command: this.query.command,
            deviceNum: this.deviceNum,
            result: this.query.status
          };
        }
        if (this.query.status == -1) {
          q.result = ''
        }
        var data = {
          pageNum: 1,
          pageSize: 10,
          q: this.searchParaProcess(q),
          orderType: "desc",
          orderBy: "createTime",
        };
        httpPost(data, this.$api.getDeviceResult).then((res) => {
          var AboutDevice = this.AboutDevice
          let datalist=utils.zlib_unzip(res.data)
          datalist.map((li) => {
            li['statusText'] = utils.statusCode(li.result)
            var resultArr = AboutDevice.filter(_item => _item.imei == li.imei)[0]
            li.createTime = this.time(+new Date(li.createTime));
            if (resultArr) {
              li.roomId = resultArr.id
              li.roomName = resultArr.roomName
            } else {
              li.roomId = ''
              li.roomName = ''
            }
			li['operationType']=''
			var operationType=this.DeviceFeedbackLog.filter(item=>item.code==li.command)[0]
			if(operationType){
				li['operationType']=operationType.desc
			}
          });
          this.tableData = datalist
          this.pageTotal = res.data.total;
          this.$store.commit('increment', 1)
        });
        //   this.$set(this.query, "pageIndex", 1);
      },
      // 分页导航
      handlePageChange(val) {
        this.$store.commit('increment', val)
        var q = {
          command: this.query.command,
          deviceNum: this.deviceNum,
          result: this.query.status
        };
        var data = {
          pageNum: val,
          pageSize: 10,
          q: this.searchParaProcess(q),
          orderType: "desc",
          orderBy: "createTime",
        };
        httpPost(data, this.$api.getDeviceResult).then((res) => {
          var AboutDevice = this.AboutDevice
          let datalist=utils.zlib_unzip(res.data)
          datalist.map((li) => {
            li['statusText'] = utils.statusCode(li.result)
            var resultArr = AboutDevice.filter(_item => _item.imei == li.imei)[0]
            li.createTime = this.time(+new Date(li.createTime));
            if (resultArr) {
              li.roomId = resultArr.id
              li.roomName = resultArr.roomName
            } else {
              li.roomId = ''
              li.roomName = ''
            }
			li['operationType']=''
			var operationType=this.DeviceFeedbackLog.filter(item=>item.code==li.command)[0]
			if(operationType){
				li['operationType']=operationType.desc
			}
          });
          this.tableData = datalist;
          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;
  }
  .w150{width: 150px;}
</style>
