// pages/my/my.js
const app = getApp()
const wxService = require('../../utils/wxService')
const utils = require('../../utils/util')
const envInfo = require('../../config/index').envInfo

wxService.page({

  /**
   * 页面的初始数据
   */
  data: {
    unfold:false,
    startY:'',
    refreshClass:'',
    delBtnWidth:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady () {
    this.animation = wx.createAnimation({
      duration: 1000,
      timingFunction: 'linear'
    })
  },
  translate () {
    // 平移变换
    // this.animation.translateY(- 80).step()
    this.animation.translateY(- 180).step()
    this.setData({animation: this.animation.export()})
  },
  reset () {
    this.animation.translateY(0)
                  .step({duration: 0})
    this.setData({animation: this.animation.export()})
  },
  //点击展开
  onTapUnfold(){
    this.setData({
      unfold: !this.data.unfold
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 3 
      })
    }

    const query = wx.createSelectorQuery()
    query.select('#plusImg').boundingClientRect()
    query.selectViewport().scrollOffset()
    var that = this;
    query.exec(function (res) {
      that.setData({
        delBtnWidth: res[0].height
      })
    })
  },

  // 开始滑动事件
  touchS: function (e) {
    console.log('e', e)
    if (e.touches.length == 1) {
      this.setData({
        //设置触摸起始点水平方向位置 
        startY: e.touches[0].clientY
      });
    }
  },
  touchM: function (e) {
    console.log('333', e)
    if (e.touches.length == 1) {
      //手指移动时y方向位置 
      var moveY = e.touches[0].clientY;
      //手指起始点位置与移动期间的差值 
      var disY = moveY - this.data.startY;
      var delBtnWidth = this.data.delBtnWidth;
      var moveDistance = delBtnWidth - disY;
      var txtStyle = "";
      console.log('------', disY)
      if (disY == 0 || disY < 0) { //如果移动距离小于等于0，文本层位置不变 
        txtStyle = "margin-top:-" + delBtnWidth;
      } else if (disY > 0) { //移动距离大于0，文本层left值等于手指移动距离 
        txtStyle = "margin-top:-" + moveDistance + "px";
        console.log('+++', txtStyle)
        if (disY >= delBtnWidth) {
          //控制手指移动距离最大值为删除按钮的宽度 
          txtStyle = "margin-top:0px" ;
        }
      }
      this.setData({
        refreshClass: txtStyle
      })
      console.log(txtStyle)
     
    }
  },
  // 滑动结束事件
  touchE: function (e) {
    if (e.changedTouches.length == 1) {
      //手指移动结束后水平位置 
      var endY = e.changedTouches[0].clientY;
      //触摸开始与结束，手指移动的距离 
      var disY = endY-this.data.startY;
      var delBtnWidth = this.data.delBtnWidth;
      //如果距离小于删除按钮的1/2，不显示删除按钮 
      var txtStyle = "";
      console.log('.....', disY)
      if (disY > 0){
        txtStyle = disY > delBtnWidth ? "margin-top:-" + delBtnWidth : "margin-top:" + disY + "px";
        this.setData({
          refreshClass: txtStyle
        })
      }
     
      console.log('////',txtStyle)
    } else {
    }
  },
})