const app = getApp()
Page({
  data: {
    deviceInfo:{
      imei:null,
      address:null,
      power:null,
      signalLevel:null,
      acqCycle:null,
      packageNum:null,
      cycle1:60,
      ifClean:false,
      pagenum:0,
    },
    isSuccess:false
  },
  onLoad: function (options) {
    var imei = options.imei;
    var setImei = 'deviceInfo.imei'
    this.setData({
      [setImei]:imei,
    })
    if(imei){
      this.getUnfo()
    }else{
      var that = this;
      this.setHdVersion();
    }
  },
  setHdVersion(){
    var hdVersion = 'deviceInfo.hdVersion';
    var that = this;
    wx.showActionSheet({
      itemList: ['X201','X202'],
      alertText:'请选择设备类型',
      success(res){
        if(res.tapIndex == 0){
          that.setData({
            [hdVersion]:1
          })
        }else{
          that.setData({
            [hdVersion]:2
          })
        }
      },fail(){
        wx.navigateBack({
          delta: 1,
        })
      }
    })
  },
  setCycle(e){
    var cycle1 = e.detail.value.replace(/[^\d]/g, "")-0
    if(cycle1 == 0)cycle1=""
    var setCycle1 = 'deviceInfo.cycle1'
    this.setData({
      [setCycle1]:cycle1
    })
  },
  setPackageNum(e){
    var packageNum = 'deviceInfo.pagenum'
    this.setData({
      [packageNum]:e.detail.value
    })
  },
  goSubmit(){
    var that = this
    var deviceCode = that.data.deviceInfo.imei;
    var second = that.data.deviceInfo.cycle1;
    var packageNum = that.data.deviceInfo.pagenum;
    if(!second){
      app.showToptip(that,"worning", "采温周期不能为空！")
      return
    }
    if(second<61){
      app.showToptip(that,"worning", "采温周期不能小于65秒")
      return;
    }
    // if(second > 43100){
    //   app.showToptip(that,"worning", "采温周期不能大于43100秒")
    //   return;
    // }
    if(that.data.deviceInfo.hdVersion == 2){
      var ifClean = that.data.deviceInfo.ifClean ? 'Y':'N'
      if(packageNum < 1){
        app.showToptip(that,"worning", "包内温度数量最小为1个！")
        return 
      }
      if(!packageNum){
        app.showToptip(that,"worning", "包内温度数量不能为空！")
        return
      }
      if(packageNum > 50){
        app.showToptip(that,"worning", "包内温度数量最大为50个！")
        return 
      }
      if(second % 65 != 0){
        app.showToptip(that,'error','采温周期必须为65的倍数')
        return
      }
      if((second * packageNum) < 300){
        app.showToptip(that,'error','采温周期和包内温度数量设置错误，请重新设置！')
        return
      }
    }
    app.requestP({
      url: "/deviceConsole/editCommand",
      contentType:"application/json",
      data:JSON.stringify({
        deviceCode,
        second,
        type:deviceCode?"0":"1",
        packageNum:that.data.deviceInfo.hdVersion == 2 ? packageNum :'',
        ifClean:that.data.deviceInfo.hdVersion == 2 ? ifClean : ''
      })
    }).then(function(res){
      that.setData({
        isSuccess:true
      })
      // app.showToptip(that,"success", res.data.msg)
    }).catch(function(res){
      app.showToptip(that,"worning", res.data.msg)
    })
  },
  setChecked(e){
    var value = e.detail.value;
    var ifClean = 'deviceInfo.ifClean'
    this.setData({
      [ifClean]:value
    })
    if(value){
      app.showToptip(this,'worning','已开启清除设备缓存！')
    }else{
      app.showToptip(this,'worning','已关闭清除设备缓存！')
    }
  },
  butMsg(){
    this.setData({
      isSuccess:false
    })
    wx.navigateBack({
      delta: 1,
    })
  },
  getUnfo(){
    const that = this
    app.requestP({
      url: "/deviceConsole/deviceInfo",
      method:"post",
      data:{
        deviceCode:that.data.deviceInfo.imei
      }
    }).then(function(res){
      var data = res.data.data
      that.setData({
        deviceInfo:data
      })
    }).catch(err=>{
      app.showToptip(that,'error',err.data.msg)
    })
    // app.requestP({
    //   url: "/vvm/bussiness/"+that.data.id,
    //   method:"get",
    // }).then(function(res){
    //   var data = res.data.data
    //   that.setData({
    //     address:data.address
    //   })
    // })
  }
})