// pages/nfc/encryptionNfc/encryptionNfc.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type:null,
    path:null,
    showDialog: false,// 详情弹窗
    activeText:'--',
    scanType:'7', //1查看当前激活状态 2激活  3锁闭  4读温度  7读取UID  8读自定义信息 9写入自定义信息  10读采温周期  X106:X106端读卡
    seleteType:[{transportStatus:'00',transportStatusName:'入库'},{transportStatus:'10',transportStatusName:'出库'},{transportStatus:'20',transportStatusName:'结束'}],
    modalHidden:true,
    region:[],
    modalName: null,
    chainId:null,
  },
  getNfcRes(e){
    const {sta,msg} = e.detail;
    var type1 = ['X101','X102','X103','X104','X106','X107'];
    var type = this.data.type;
    var path = this.data.path;
    switch (path) {
      case 'find':
        this.goDetailInfo(msg.uid,type)
        break;
      case 'factorycheck':
        this.getInfo(msg.uid,type)
        break;
      case 'iodep':
        this.iodep(msg.uid,type)
        break;
      case 'addLabel':
        this.chainBind(msg.uid,type,this.data.chainId);
        break;
      default:
        break;
    }
    // this.setData({
    //   activeText:msg
    // })

  },
  // 绑定标签
  chainBind(deviceCode,type,chainId){
    var that = this;
    const {latitude,longitude} = that.data;
    app.requestP({
      url:'/chainBound/checkBind',
      method:'post',
      data:{
        deviceCode,
      }
    }).then(res=>{
      if(res.data.data == 0){
        app.requestP({
          url:'/chainBound/bind',
          method:'post',
          contentType:' application/json',
          data:{
            chainId,
            deviceCode,
            type:type,
            lat:latitude,
            lng:longitude
          }
        }).then(response=>{
          app.showToptip(that,'success',response.data.msg)
        }).catch(err=>{
          app.showToptip(that,'error',err.data.msg)
        })
      }else{
        app.showToptip(that,'error',res.data.msg);
      }
    }).catch(err=>{
      app.showToptip(that,'error',err.data.msg)
    })
  },
  // 查一查
  goDetailInfo(message,type){
    var that = this;
    app.requestP({
      url:'/chainBound/checkBind',
      method:'post',
      // contentType:' application/json',
      data:{
        deviceCode:message,
      }
    }).then(res=>{
      if(res.data.data !=1){
        app.showToptip(that,'error',res.data.msg);
        return;
      }
      app.requestP({
        url:'/chain/scan',
        method:'post',
        contentType:' application/json',
        data:{
          inoculatorId:'',
          payload:message,
          type
        }
      }).then(res=>{
        var id = res.data.msg;
        that.setData({
          showDialog: !this.data.showDialog,
          detailId:id,
          deviceCode:message
        });
      }).catch(err=>{
        app.showToptip(that,'error',err.data.msg)
      })
    }).catch(err=>{
      app.showToptip(that,'error',err.data.msg);
    })
  },
  goDetail(){
    var detailId = this.data.detailId;
    var deviceCode = this.data.deviceCode || '';
    wx.navigateTo({
      url: '/pages/query/details?id='+detailId+'&deviceCode='+deviceCode,
    })
    this.setData({
      showDialog:!this.data.showDialog,
    })
  },
  // 出厂检测
  getInfo(message){
    var that = this;
    app.requestP({
      url: "/query/scanChip1",
      method:"post",
      needToken:false,
      contentType:' application/json',
      data:{
        payload:message
      }
    }).then(res=>{
      wx.navigateTo({
        url: '/pages/nfc/outNfcTempInfo/outNfcTempInfo?data='+JSON.stringify(res.data.data),
      })
    }).catch(err=>{
      app.showToptip(that,'error',err.data.msg)
    })
  },
  // 出入库
  iodep(code,type){
    var that =this;
    app.requestP({
      url:'/chainBound/checkBind',
      method:'post',
      // contentType:' application/json',
      data:{
        deviceCode:code,
      }
    }).then(res=>{
      if(res.data.data!=1){
        app.showToptip(that,'error',res.data.msg);
        return;
      }
      app.requestP({
        url:'/chain/scan',
        method:'post',
        contentType:' application/json',
        data:{
          inoculatorId:'',
          payload:code,
          type,
        }
      }).then(res=>{
        const {transportStatus,id,code} = res.data.chain;
        that.setData({
          transportStatus,
          iodepId:id,
          modalHidden:!that.data.modalHidden,
          seleteType:that.setSeleteType(transportStatus),
        });
      }).catch(err=>{
        app.showToptip(that,'error',err.data.msg)
      })
    }).catch(err=>{
      app.showToptip(that,'error',err.data.msg);
    })
  },
  setTransportStatus(transportStatus){
    var seleteType = this.data.seleteType;
    var deptType;
    for(var i=0;i<seleteType.length;i++){
      if(seleteType[i].transportStatus == transportStatus){
        deptType = i;
        break;
      }
    }
    return deptType;
  },
  setSeleteType(transportStatus){
    var seleteType = this.data.seleteType;
    var type;
    for(var i=0;i<seleteType.length;i++){
      if(seleteType[i].transportStatus == transportStatus){
        seleteType.splice(i,1);
        break;
      }
    }
    this.setTransportStatus(transportStatus);
    return seleteType;
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    const {type,path,chainId} = options;
    this.setData({
      type,path,chainId
    })
    //获取定位信息
    wx.getLocation({
      altitude: 'altitude',
      success(res) {
        that.setData({
          latitude: res.latitude,
          longitude: res.longitude,
        })
      },
      error() {
        app.showToptip(that, "worning", "获取位置信息失败")
      }
    })
    //判断是否是iOS
    wx.getSystemInfo({
      success:function(res){
        if(res.platform=="ios"){
          that.isIOS = true; 
        }
      }
    })
  },
  //取消按钮点击事件
  modalBindcancel:function(){
    this.setData({
      modalHidden:!this.data.modalHidden,
      region:[],
      modalVal:'',
      iodepId:'',
    })
  },
  regionChange: function(e) {
    this.setData({
      region: e.detail.value
    })
  },
  modalBindaconfirm(){  //一型保存地址
    var that = this;
    var {region,modalVal,transportStatus,iodepId,seleteType,deptType} = that.data;
    var addr = region.join('')+modalVal;
    if(region.join('') && modalVal && seleteType[deptType].transportStatus){
      app.requestP({
        url:'/chain/editTransport',
        // contentType:' application/json',
        method:'post',
        data:{
          addr,
          chainId:iodepId,  // 00 在库 //10 运输 // 20 已结束
          // transportStatus:transportStatus == '00' ? '10':transportStatus == '10' ?'00':'',
          transportStatus:seleteType[deptType].transportStatus,
        }
      }).then(res=>{
        that.setData({
          modalHidden:!that.data.modalHidden,
          region:[],
          modalVal:'',
          iodepId:'',
        })
        app.showToptip(that,'success',res.data.msg);
      }).catch(err=>{
        app.showToptip(that,'error',err.data.msg);
      })
    }else{
      app.showToptip(that,'error','地址不能为空');
    }
  },
  setType(e){
    var value = e.detail.value;
    var that = this;
    that.setData({
      deptType:value
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if(that.data.isIOS){
      that.goApp()
      return;
    }
  },
  goApp(e) {
    this.setData({
      modalName: "goApp"
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

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

  },

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

  },

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

  },

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

  }
})