// pages/cart/cart.js
const wxService = require('../../utils/wxService')

wxService.page({

  /**
   * 页面的初始数据
   */
  data: {
    product:{
      quantity:1
    },
    isAllSelect:false,
    currentHeight:52,
    startX:'',
    delBtnWidth: 66,//删除按钮宽度单位（rpx）
    cartList:[
      {
        imgUrl:'',
        name:'1111',
        sku:'1',
        price:'￥456',
        shows:'',
      },
      {
        imgUrl: '',
        name: '222',
        sku: '2',
        price: '￥222',
        shows:''
      }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wxService.getSystemInfo().then(res => {
      const { statusBarHeight } = res
      const { currentHeight, currentMarginTop, currentPaddingTop } = this.data
      this.setData({
        currentHeight: currentHeight + statusBarHeight,
      })
    })
    console.log(this.data.currentHeight)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 2
      })
    }
  },

  //选择商品
  selectPro(e){
    var index = e.currentTarget.dataset.index;
    this.data.cartList[index].isSelect = !this.data.cartList[index].isSelect;
    this.setData({
      cartList: this.data.cartList
    })
  },

  //全选
  allSelect(){
    this.setData({
      isAllSelect: !this.data.isAllSelect
    })
    if (this.data.isAllSelect){
      this.setData({
        cartList: this.data.cartList.map((item) =>{
          item.isSelect = true
          return item
        })
      })
    }else{
      this.setData({
        cartList: this.data.cartList.map((item) => {
          item.isSelect = false
          return item
        })
      })
    }
  },

  //删除商品
  deletePro(e){
    wx.showModal({
      title: '提示',
      content: '确认删除该商品',
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  // 开始滑动事件
  touchS: function (e) {
    console.log('e',e)
    if (e.touches.length == 1) {
      this.setData({
        //设置触摸起始点水平方向位置 
        startX: e.touches[0].clientX
      });
    }
  },
  touchM: function (e) {
    console.log('333',e)
    if (e.touches.length == 1) {
      //手指移动时水平方向位置 
      var moveX = e.touches[0].clientX;
      //手指起始点位置与移动期间的差值 
      var disX = this.data.startX - moveX;
      var delBtnWidth = this.data.delBtnWidth;
      var txtStyle = "";
      if (disX == 0 || disX < 0) { //如果移动距离小于等于0，文本层位置不变 
        txtStyle = "margin-left:0px";
      } else if (disX > 0) { //移动距离大于0，文本层left值等于手指移动距离 
        txtStyle = "margin-left:-" + disX + "px";
        if (disX >= delBtnWidth) {
          //控制手指移动距离最大值为删除按钮的宽度 
          txtStyle = "margin-left:-" + delBtnWidth + "px";
        }
      }
      var index = e.currentTarget.dataset.index;
      var cartList = this.data.cartList;
      cartList[index].shows = txtStyle;
      console.log("1", cartList[index].shows);
      //更新列表的状态 
      this.setData({
        cartList: cartList
      });
      
    }
  },
  // 滑动中事件
  touchE: function (e) {
    if (e.changedTouches.length == 1) {
      //手指移动结束后水平位置 
      var endX = e.changedTouches[0].clientX;
      //触摸开始与结束，手指移动的距离 
      var disX = this.data.startX - endX;
      var delBtnWidth = this.data.delBtnWidth;
      //如果距离小于删除按钮的1/2，不显示删除按钮 
      var txtStyle = "";
      txtStyle = disX > delBtnWidth / 2 ? "margin-left:-" + delBtnWidth + "px" : "margin-left:0px";
      //获取手指触摸的是哪一项 
      var index = e.currentTarget.dataset.index;
      var cartList = this.data.cartList;
      cartList[index].shows = txtStyle;
      console.log("1", cartList[index].shows);
      //更新列表的状态 
      this.setData({
        cartList: cartList
      });
    } else {
    }
  },
})