// pages/openLockLog/openLockLog.js
const app = getApp()
const WXAPI = require('../../utils/request')
var util = require('../../utils/util');
import moment from "../../utils/moment"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    locklogList: [],
    locklogListOne:[],
    locklogListTwo:[],
    pageNum: 1,
    pageNumOne:1,
    pageNumTwo:1,
    pageSize: 10,
    roomId: '',
    cardId:'',
    stoppull: false,
    active:0,
    openType:0,
    bottomroolTwo:true,
    bottomroolOne:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.roomId && !options.cardId) {
      this.data.roomId = options.roomId
      this.getopenlocklog(options.roomId)
    }else{
      this.data.roomId = options.roomId
      this.data.cardId = options.cardId
      this.minGetopenlocklog()
    }
  },

  onChange(event){
    this.setData({
      active:event.detail.name
    })
    if(this.data.cardId){
      this.minGetopenlocklog()
    }else{
      if(event.detail.name==0){
        this.getopenlocklog(this.data.roomId)      
      }else if(event.detail.name==1){
        this.getopenlocklogOne(this.data.roomId)   
      }else if(event.detail.name==2){
        this.getopenlocklogTwo(this.data.roomId)   
      }
    }
  },
  timestampToTime(cjsj) {
    if (cjsj.toString().length == 10) {
      cjsj = cjsj * 1000
    }
    var date = new Date(cjsj); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
    var Y = date.getFullYear() + "-";
    var M =
      (date.getMonth() + 1 < 10 ?
        "0" + (date.getMonth() + 1) :
        date.getMonth() + 1) + "-";
    var D = (date.getDate() < 10 ? "0" + date.getDate() : date.getDate()) + " ";
    var h = (date.getHours() < 10 ? "0" + date.getHours() : date.getHours()) + ":";
    var m = (date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes()) + ":";
    var s = (date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds());
    return Y + M + D + h + m + s;
  },
  getopenlocklogTwo(roomId){
    let operUrl="",q={}
    var carname=wx.getStorageSync('username')
    var deviceinfo=wx.getStorageSync('deviceinfo')
    operUrl="/v1/device/isp/openLock"
    q= util.searchParaProcess({
      operUrl:operUrl,
      roomIds:[this.data.roomId],
      request:[carname],
    })
    var dataq = {
      pageNum: this.data.pageNumTwo,
      pageSize: 30,
      orderType: "desc",
      orderBy:"createAt",
      q: q
    };
    WXAPI.getOperateList(dataq).then(res=>{
      if(res.success){        
        var data = [];
        if(res.data.total<30){
          this.setData({
            bottomroolTwo:false
          })
        }
        res.data.list.map((li, index) => {
          var operRequ=JSON.parse(li.operRequ).param[0]
          let path="",compli=""
          if(operRequ.hasOwnProperty("data")){
            path=deviceinfo.roomName
            li.roomName = path?path:operRequ.data.roomName?operRequ.data.roomName:'';
            li.roomId = operRequ.data.roomId;
            li.name=operRequ.data.cardName
            li.deviceNum=operRequ.data.lockerId?operRequ.data.lockerId:operRequ.data.deviceNum;
          }else{
            li.roomName = operRequ.roomName
            li.name=operRequ.cardName
            li.deviceNum=operRequ.deviceNum || operRequ.imei
          }
          li.openType="远程开锁"
          li.openTime = moment(li.createAt).format("YYYY-MM-DD HH:mm:ss");
        });
        var locklogListTwo=this.data.locklogListTwo.concat(res.data.list)
        this.setData({
          locklogListTwo:locklogListTwo
        })
      }
    })
  },
  getopenlocklogOne(roomId){
    let operUrl="",q={}
    var carname=wx.getStorageSync('username')
    operUrl="/v1/device/send/bleCommand"
    q= util.searchParaProcess({
      operRequ: "bleUnlockRecord",
      // operUrl:operUrl,
      roomIds:[this.data.roomId],
      request:[carname],
    })
    var dataq = {
      pageNum: this.data.pageNumOne,
      pageSize: 30,
      orderType: "desc",
      orderBy:"createAt",
      q: q
    };
    WXAPI.getOperateList(dataq).then(res=>{
      if(res.success){        
        if(res.data.total<30){
          this.setData({
            bottomroolOne:false
          })
        }
        var data = [];
        res.data.list.map((li, index) => {
          var operRequ=JSON.parse(li.operRequ).param[0]
          let path="",compli=""
          if(operRequ.hasOwnProperty("data")){
            path=wx.getStorageSync('deviceinfo').roomName
            li.roomName = path?path:operRequ.data.roomName?operRequ.data.roomName:'';
            li.roomId = operRequ.data.roomId;
            li.name=operRequ.data.cardName
            li.deviceNum=operRequ.data.lockerId?operRequ.data.lockerId:operRequ.data.deviceNum;
          }else{
            li.roomName = operRequ.roomName
            li.name=operRequ.cardName
            li.deviceNum=operRequ.deviceNum || operRequ.imei
          }
          li.openType="蓝牙开锁"
          li.openTime = moment(li.createAt).format("YYYY-MM-DD HH:mm:ss");
        });
        var locklogListOne=this.data.locklogListOne.concat(res.data.list)
        this.setData({
          locklogListOne:locklogListOne
        })
      }
    })
  },
  // 租客获取开锁记录
  minGetopenlocklog(){
    var q = {
      // roomId: this.data.roomId,
      cardId:this.data.cardId
    }
    var data1 = {
      param:{
      pageNum: this.data.pageNum,
      pageSize: this.data.pageSize,
      orderBy:"open_time desc",
      q: JSON.stringify(q)
    },  
    signature:wx.getStorageSync('signature')
  }
  let deviceinfo = wx.getStorageSync('deviceinfo')
    let data2={
      param:{
        pageNum: 1,
        pageSize:10,
        orderType: "desc",
        orderBy:"createAt",
        q: JSON.stringify({
          operateName:deviceinfo.name,
          roomId:deviceinfo.roomId
        })
      },
      signature:wx.getStorageSync('signature')
    }
    WXAPI.getBleUnlockRecord(data2).then(res2=>{
      if(res2.success){
        this.setData({
          locklogListOne:res2.data.list
        })
      }
    })
    WXAPI.minGetOpenLocks(data1).then(res=>{
      if (res.success) {
        if (res.data.list.length<10) {
          this.data.stoppull = true
          this.data.pageNum = 1
          wx.showToast({
            title: '暂无更多数据！',
            duration:2000,
            icon:'none'
          })
        }
        res.data.list.map((li, index) => {
          var str = ""
          switch (li.openType) {
            case 1:
              str = "指纹开锁";
              break;
            case 2:
              str = "数字键盘开锁";
              break;
            case 3:
              str = "NFC开锁";
              break;
            case 4:
              str = "机械钥匙开锁";
              break;
            case 5:
              str = "蓝牙开锁";
              break;
            case 6:
              str = "NB开锁";
              break;
            case 7:
              str = "身份证";
              break;
            case 8:
              str = "单机密码开锁";
              break;
            case 9:
              str = "人脸开锁";
              break;
            default:
              break;
          }
          li.openTime = this.timestampToTime(li.openTime)
          li.openType = str
        })
        var locklogList=this.data.locklogList.concat(res.data.list)
        console.log(locklogList)
        this.setData({
          locklogList:locklogList
        })
      }
    })
  },
  getopenlocklog(roomId) {
    var q = {
      roomId: roomId
    }
    var data1 = {
      pageNum: this.data.pageNum,
      pageSize: this.data.pageSize,
      orderBy:"open_time desc",
      q: JSON.stringify(q)
    }
    WXAPI.getOpenRecord(data1).then(res=>{
      console.log(res);
      if (res.success) {
        if (res.data.length<10) {
          this.data.stoppull = true
        }
        res.data.list.map((li, index) => {
          var str = ""
          switch (li.openType) {
            case 1:
              str = "指纹开锁";
              break;
            case 2:
              str = "数字键盘开锁";
              break;
            case 3:
              str = "NFC开锁";
              break;
            case 4:
              str = "机械钥匙开锁";
              break;
            case 5:
              str = "蓝牙开锁";
              break;
            case 6:
              str = "NB开锁";
              break;
            case 7:
              str = "身份证";
              break;
            case 8:
              str = "单机密码开锁";
              break;
            case 9:
              str = "人脸开锁";
              break;
            default:
              break;
          }
          li.openTime = this.timestampToTime(li.openTime)
          li.openType = str
        })
        var locklogList=this.data.locklogList.concat(res.data.list)
        console.log(locklogList)
        this.setData({
          locklogList:locklogList
        })
      }
    })
  },
  getopenlocklog1() {
    var data1 = {
      pageNum: this.data.pageNum,
      pageSize: this.data.pageSize,
      orderBy: "open_time desc",
      q: JSON.stringify({
        roomId: this.data.roomId
      }),
    };
    WXAPI.getOpenRecord(data1).then(res=>{
      if (res.success) {
        if (res.data.list.length<10) {
          this.data.stoppull = true
        }
        res.data.list.map((li, index) => {
          var str = ""
          switch (li.openType) {
            case 1:
              str = "指纹开锁";
              break;
            case 2:
              str = "数字键盘开锁";
              break;
            case 3:
              str = "NFC开锁";
              break;
            case 4:
              str = "机械钥匙开锁";
              break;
            case 5:
              str = "蓝牙开锁";
              break;
            case 6:
              str = "NB开锁";
              break;
            case 7:
              str = "身份证";
              break;
            case 8:
              str = "单机密码开锁";
              break;
            case 9:
              str = "人脸开锁";
              break;
            default:
              break;
          }
          li.openTime = this.timestampToTime(li.openTime)
          li.openType = str
        })
        var array = []
        var locklogList = array.concat(this.data.locklogList, res.data.list)
        this.setData({
          locklogList
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.cardId){
      // this.getopenlocklogOne()
      let pageNum = this.data.pageNum+1
      this.setData({
        pageNum
      })
      this.minGetopenlocklog()
    }else{
      if(this.data.active==0){
        if (!this.data.stoppull) {
          this.data.pageNum = this.data.pageNum + 1
          this.getopenlocklog1()
          setTimeout(() => {
            wx.stopPullDownRefresh()
          })
        } else {
          wx.showToast({
            title: "暂无更多数据！",
            icon: "none",
            duration: 2000
          })
        }
      }else if(this.data.active==1){      
        if (!this.data.bottomroolOne) {
          this.data.pageNumOne = this.data.pageNumOne + 1
          this.getopenlocklogOne(this.data.roomId)
          setTimeout(() => {
            wx.stopPullDownRefresh()
          })
        } else {
          wx.showToast({
            title: "暂无更多数据！",
            icon: "none",
            duration: 2000
          })
        }
      }else if(this.data.active==2){      
        if (!this.data.bottomroolTwo) {
          this.data.pageNumTwo = this.data.pageNumTwo + 1
          this.getopenlocklogTwo(this.data.roomId)
          setTimeout(() => {
            wx.stopPullDownRefresh()
          })
        } else {
          wx.showToast({
            title: "暂无更多数据！",
            icon: "none",
            duration: 2000
          })
        }
      }
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})