// pages/changeLock/changeLock.js
import ICINLockerMethods from '../../utils/equipment'
import utils from '../../utils/index'
import moment from "../../utils/moment"
const WXAPI = require('../../utils/request')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    oldLock:'',
    newLock:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  // 确认换锁
  confirmChange(){
    var _this=this
    if(this.data.oldLock == "" || this.data.newLock == ""){
      wx.showToast({
        title: '请输入旧锁和新锁设备号',
        duration:2000,
        icon:'none'
      })
    }else{      
      wx.showModal({
        title: "提示",
        content: "请确认是否进行此操作！",
        showCancel: false,
        confirmText: "确定",
        success(res) {
          var data={
            q: JSON.stringify({
              deviceNum:_this.data.oldLock
            }),
            pageNum: 0,
            pageSize: 0,
          }
          WXAPI.getDeviceItems(data).then(res=>{
            if(res.success && res.data.list.length>0){
              var oldlock=res.data.list[0]
              var data1={
                q: JSON.stringify({
                  deviceNum:_this.data.newLock
                }),
                pageNum: 0,
                pageSize: 0,
              }  
              WXAPI.getDeviceItems(data1).then(res1=>{
                if(res1.success && res1.data.list.length>0){
                  var newlock=res1.data.list[0]
                  _this.orderDeviceData(oldlock,newlock)
                }
              })
              
            }
          })
        }
      });      
    }
  },
  orderDeviceData(row,newlock) {
    if (row.roomId) {
      var data1 = {
        pageNum: 0,
        pageSize: 0,
        q: {
          id: row.roomId
        }
      }
      WXAPI.getSiteTenant(data1).then(res => {
        if (res.success) {
          var person = res.data.list
          var data = {
            imei: row.imei
          }
          WXAPI.orderDeviceData(data).then(res1 => {
            if (res1.success) {
              var alllist = res1.data
              ICINLockerMethods.getPersonCallback(person, alllist, (callback) => {
                var personlist = callback
                ICINLockerMethods.resetInitialization(newlock, (callback) => {
                  console.log(personlist,'personlist')
                  if (newlock.manufactureId == 101) {
                    personlist.map(lt => {
                      var endTime = new Date(lt.person.endTime).getTime()
                      var startTime = new Date(lt.person.startTime).getTime()
                      var passwordEndTime = moment(endTime).format("YYYYMMDDHHmmss");
                      var passwordStartTime = moment(startTime).format("YYYYMMDDHHmmss");
                      var userTelephone = JSON.parse(lt.person.info).userTelephone;
                      var userPassword = JSON.parse(lt.person.info).userPassword;
                      var data = {
                        deviceNum: newlock.deviceNum,
                        imei: newlock.imei,
                        userTelephone: userTelephone,
                        userPassword: userPassword,
                        password: userPassword,
                        operateMode: 26,
                        order: 6226,
                        passwordStartTime: passwordStartTime,
                        passwordEndTime: passwordEndTime,
                        cardName: lt.person.name,
                        roomName: row.roomName
                      }
                      ICINLockerMethods.LDAddUser(data.deviceNum, data.imei, data.userTelephone, data
                        .userPassword,
                        data
                        .password, passwordStartTime, passwordEndTime, data.operateMode, data
                        .order,
                        data
                        .cardName,
                        data.roomName, (callback) => {
                          lt.data.map(ll => {
                            utils.putCarfidAdd(newlock, ll)
                          })
                        })
                    })
                  } else if (newlock.manufactureId == 201||newlock.manufactureId == 221) {
                    personlist.map(lt => {
                      var endTime = new Date(lt.person.endTime).getTime()
                      var startTime = new Date(lt.person.startTime).getTime()
                      var passwordEndTime = moment(endTime).format("YYYYMMDDHHmmss");
                      var passwordStartTime = moment(startTime).format("YYYYMMDDHHmmss");
                      var userTelephone = JSON.parse(lt.person.info).userTelephone;
                      var userPassword = JSON.parse(lt.person.info).userPassword;
                      var data = {
                        lockerId: newlock.deviceNum,
                        imei: newlock.imei,
                        lockerUserId: userPassword,
                        keyId: userPassword,
                        role: 0,
                        action: 1,
                        order: 6226,
                        cardName: lt.person.name,
                        roomName: row.roomName
                      };
                      ICINLockerMethods.addUserToLock(data.lockerId, data.imei, data
                        .lockerUserId, data.role, data.action, data.order, data.cardName, data
                        .roomName, (
                          callback) => {
                          if (callback.success) {
                            lt.data.map(ll => {
                              utils.putCarfidzgAdd(lt,newlock, ll)
                            })
                          }
                        })
                    })
                  }
                })
              })
            } else {
              this.$message.error(res1.msg)
            }
          })

        } else {
          this.$message.error(res.msg)
        }
      })
    } else {
      this.$message.error("暂无数据")
    }
  },
  onChange(event) {
    // event.detail 为当前输入的值
    console.log(event.detail);
    this.setData({
      oldLock:event.detail
    })
  },
  onChange1(event) {
    // event.detail 为当前输入的值
    console.log(event.detail);
    this.setData({
      newLock:event.detail
    })
  },
  // 扫描
  scanning() {
    var that = this
    wx.showLoading({
      title: '加载中',
      mask: true
    });
    wx.scanCode({
      success(res) {
        var deviceNum = ''
        if (res.result.indexOf('&') != -1) {
          deviceNum = res.result.split('&')[0]
        } else if (res.result.indexOf('_')) {
          deviceNum = "ICIN_" + res.result.split('_')[0]
        }
        that.setData({
          oldLock: deviceNum
        })
        wx.hideLoading()
      },
      fail(res) {
        wx.hideLoading()
      }
    })
  },
  // 扫描
  scanning1() {
    var that = this
    wx.showLoading({
      title: '加载中',
      mask: true
    });
    wx.scanCode({
      success(res) {
        var deviceNum = ''
        if (res.result.indexOf('&') != -1) {
          deviceNum = res.result.split('&')[0]
        } else if (res.result.indexOf('_')) {
          deviceNum = "ICIN_" + res.result.split('_')[0]
        }
        that.setData({
          newLock: deviceNum
        })
        wx.hideLoading()
      },
      fail(res) {
        wx.hideLoading()
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

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

  },

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

  },

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

  },

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

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

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

  }
})