text: const WXAPI = require('apifm-wxapi')
  const TOOLS = require('../../utils/tools.js')
  const AUTH = require('../../utils/auth')
  
  const app = getApp()
  
  Page({
    data: {
      wxlogin: true,
  
      saveHidden: true,
      allSelect: true,
      noSelect: false,
  
      delBtnWidth: 120, //删除按钮宽度单位（rpx）
  	title:"猜你喜欢",
  	orderBy:"ordersDown",
  	count:4
    },
  
    //获取元素自适应后的实际宽度
    getEleWidth: function(w) {
      var real = 0;
      try {
        var res = wx.getSystemInfoSync().windowWidth
        var scale = (750 / 2) / (w / 2)
        // console.log(scale);
        real = Math.floor(res / scale);
        return real;
      } catch (e) {
        return false;
        // Do something when catch error
      }
    },
    initEleWidth: function() {
      var delBtnWidth = this.getEleWidth(this.data.delBtnWidth);
      this.setData({
        delBtnWidth: delBtnWidth
      });
    },
    onLoad: function() {
      this.initEleWidth();
      this.onShow();
    },
    onShow: function() {
      AUTH.checkHasLogined().then(isLogined => {
        this.setData({
          wxlogin: isLogined
        })
        if (isLogined) {
          this.shippingCarInfo()
        }
      })    
    },
    async shippingCarInfo(){
      const token = wx.getStorageSync('token')
      if (!token) {
        return
      }
      const res = await WXAPI.shippingCarInfo(token)
      if (res.code == 0) {
        this.setData({
          shippingCarInfo: res.data
        })
      } else {
        this.setData({
          shippingCarInfo: null
        })
      }
    },
    toIndexPage: function() {
      wx.switchTab({
        url: "/pages/index/index"
      });
    },
  
    touchS: function(e) {
      if (e.touches.length == 1) {
        this.setData({
          startX: e.touches[0].clientX
        });
      }
    },
    touchM: function(e) {
      const index = e.currentTarget.dataset.index;
      if (e.touches.length == 1) {
        var moveX = e.touches[0].clientX;
        var disX = this.data.startX - moveX;
        var delBtnWidth = this.data.delBtnWidth;
        var left = "";
        if (disX == 0 || disX < 0) { //如果移动距离小于等于0，container位置不变
          left = "margin-left:0px";
        } else if (disX > 0) { //移动距离大于0，container left值等于手指移动距离
          left = "margin-left:-" + disX + "px";
          if (disX >= delBtnWidth) {
            left = "left:-" + delBtnWidth + "px";
          }
        }
        this.data.shippingCarInfo.items[index].left = left
        this.setData({
          shippingCarInfo: this.data.shippingCarInfo
        })
      }
    },
  
    touchE: function(e) {
      var index = e.currentTarget.dataset.index;
      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 left = disX > delBtnWidth / 2 ? "margin-left:-" + delBtnWidth + "px" : "margin-left:0px";
        this.data.shippingCarInfo.items[index].left = left
        this.setData({
          shippingCarInfo: this.data.shippingCarInfo
        })
      }
    },
    async delItem(e) {
      const key = e.currentTarget.dataset.key
      const token = wx.getStorageSync('token')
      const res = await WXAPI.shippingCarInfoRemoveItem(token, key)
  	this.shippingCarInfo()
    },
    async jiaBtnTap(e) {
      const index = e.currentTarget.dataset.index;
      const item = this.data.shippingCarInfo.items[index]
      const number = item.number + 1
      const token = wx.getStorageSync('token')
      const res = await WXAPI.shippingCarInfoModifyNumber(token, item.key, number)
      this.shippingCarInfo()
    },
    async jianBtnTap(e) {
      const index = e.currentTarget.dataset.index;
      const item = this.data.shippingCarInfo.items[index]
      const number = item.number-1
      if (number <= 0) {
        return
      }
      const token = wx.getStorageSync('token')
      const res = await WXAPI.shippingCarInfoModifyNumber(token, item.key, number)
      this.shippingCarInfo()
    },
    cancelLogin() {
      this.setData({
        wxlogin: true
      })
    },
    processLogin(e) {
      if (!e.detail.userInfo) {
        wx.showToast({
          title: '已取消',
          icon: 'none',
        })
        return;
      }
      AUTH.register(this);
    },
  
  
  
  })