// pages/query/vvmInfo.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    detailsList:[],
    keyword:'',
    scrollHeight: 0, // scroll-view高度
    startX: 0, // 开始X坐标
    startY: 0, // 开始Y坐标
    showDialog: false,// 选择弹窗
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const {details,devicetype} = JSON.parse(decodeURIComponent(options.details));
    var that = this;
    wx.getLocation({
      altitude: 'altitude',
      success(res) {
        that.setData({
          latitude: res.latitude,
          longitude: res.longitude,
        })
      },
      error() {
        app.showToptip(that, "worning", "获取位置信息失败")
      }
    })
    var name = ['X201','X202','X203','X101','X102','X103','X104','X105','X106','X107',];
    this.setData({
      detailsList:details,
      devicetype,
      devicetypeName: name.indexOf(devicetype) != -1 ? name[name.indexOf(devicetype)] : devicetype == 10 ?'追溯标签': devicetype == 11 ? '全程温控': '实时追溯',
      id:options.id
    })
  },
  toInfo(e){
    var that = this;
    const deviceCode = e.currentTarget.dataset.devicecode;
    const {devicetype,id} =that.data;
    var deviceId = e.currentTarget.dataset.id;
    wx.redirectTo({
      url: '/pages/query/details?deviceCode=' + deviceCode+'&deviceType='+devicetype+'&id='+id+'&chainBoundId='+deviceId,
    })
  },
  addLabel(){
    var that = this;
    var {id,devicetype} = that.data;
    if(devicetype == 'X101' || devicetype == 'X202' ||devicetype == 'X203'){
      // that.chainBind();
      this.setData({
        showDialog:!this.data.showDialog
      })
    }else {
      wx.navigateTo({
        url: '/pages/nfc/zhuisu2?id='+id+'&type='+devicetype+'&addLabel=20',
      })
    }
  },
  chainBind(){
    var that = this;
    wx.scanCode({
      success: function (res) {
        var {result} = res;
        if(result.indexOf('imei=') != -1){
          result = result.slice(result.indexOf('imei=')+5,result.length)
        }
        that.setchainBoundBind(result);
      },
      fail:function(err){
        app.showToptip(that, 'error', '二维码无效');
      }
    })
  },
  setchainBoundBind(result){
    var that = this;
    const {id,devicetype,latitude,longitude} = that.data;
    app.requestP({
      url:'/chainBound/checkBind',
      method:'post',
      data:{
        deviceCode:result,
      }
    }).then(res=>{
      if(res.data.data==0){
        app.requestP({
          url:'/chainBound/bind',
          method:'post',
          contentType:' application/json',
          data:{
            chainId:id,
            deviceCode:result,
            type:devicetype,
            lat:latitude,
            lng:longitude
          }
        }).then(response=>{
          app.showToptip(that,'success',response.data.msg);
          wx.navigateBack({
            delta: 1,
          })
        }).catch(error=>{
          app.showToptip(that,'error',error.data.msg)
        })            
      }else{
        app.showToptip(that,'error',res.data.msg)
      }
    }).catch(err=>{
      app.showToptip(that,'error',err.data.msg)
    })
  },
  inputBind(){
    var that = this
    wx.showModal({
      title: '请输入标签编号',
      editable:true,
      success (res) {
        if (res.confirm) {
          var numreg = /^[0-9]*$/;
          if(res.content){
            if(!numreg.test(res.content)){
              app.showToptip(that, "error", "标签编号只能为数字！");
              that.inputBind()
              return;
            }
            that.setchainBoundBind(res.content)
          }else{
            app.showToptip(that, "error",'标签编号不能为空！')
            that.inputBind()
          }
        }else{
          //pass
        }
      }
    })
  },
  toggleDialog() { //弹窗关闭
    this.setData({
      showDialog: !this.data.showDialog
    });
  },
  // 手指触摸动作开始
  touchStart: function (e) {
    let that = this;
    //开始触摸时 重置所有删除
    that.data.detailsList.forEach(function (v, i) {
      if (v.isTouchMove) v.isTouchMove = false; // 只操作为true的
    })
    // 记录手指触摸开始坐标
    that.setData({
      startX: e.changedTouches[0].clientX, // 开始X坐标
      startY: e.changedTouches[0].clientY, // 开始Y坐标
      detailsList: that.data.detailsList
    })
    },
    // 手指触摸后移动
    touchMove: function (e) {
      let that = this,
        index = e.currentTarget.dataset.index, // 当前下标
        startX = that.data.startX, // 开始X坐标
        startY = that.data.startY, // 开始Y坐标
        touchMoveX = e.changedTouches[0].clientX, // 滑动变化坐标
        touchMoveY = e.changedTouches[0].clientY, // 滑动变化坐标
        // 获取滑动角度
        angle = that.angle({
          X: startX,
          Y: startY
        }, {
          X: touchMoveX,
          Y: touchMoveY
        });
      // 判断滑动角度
      that.data.detailsList.forEach(function (v, i) {
        v.isTouchMove = false
        // 滑动超过30度角 return
        if (Math.abs(angle) > 30) return;
        if (i == index) {
          // 右滑
          if (touchMoveX > startX)
            v.isTouchMove = false
          // 左滑
          else
            v.isTouchMove = true
        }
      })
      // 更新数据
      that.setData({
        detailsList: that.data.detailsList
      })
    },
    // 计算滑动角度
    angle: function (start, end) {
      let that = this,
        _X = end.X - start.X,
        _Y = end.Y - start.Y;
      // 返回角度 /Math.atan()返回数字的反正切值
      return 360 * Math.atan(_Y / _X) / (2 * Math.PI);
    },
    delList(e){
      var that = this;
      wx.showModal({
        title: '提示',
        content: '请确认是否删除！',
        success (res) {
          if (res.confirm) {
            var id = e.currentTarget.dataset.id;
            app.requestP({
              url:'/chainBound/deleteChainBound',
              method:'post',
              data:{
                id
              }
            }).then(res=>{
              app.showToptip(that,'success',res.data.msg);
              wx.navigateBack({
                delta: 1
              });
            }).catch(err=>{
              app.showToptip(that,'error',err.data.msg)
            })
          } else if (res.cancel) {
            //点击取消
          }
        }
      })
    }
})