const baseUrl = require("../../api/index.js").config;
const masses = require("../../api/masses.js");
Page({
  data: {
    items: [],
    startX: 0, //开始坐标
    startY: 0,
    concernslist: [],
    scrollHeight: "",
    windowHeight: 0,
    c: [],
    concernsoutArea: [],
    concernsinArea: [],
  },
  onLoad: function () {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          scrollHeight: res.screenHeight - 190,
          windowHeight: res.windowHeight,
        });
      },
    });
  },
  onShow: function () {
    var that = this;
    var memberId = wx.getStorageSync("memberId");
    var lat = wx.getStorageSync("latitude");
    var lon = wx.getStorageSync("longitude");
    masses.getLikestore(
      baseUrl.waimaiUrl + "/like-store/members/" + memberId + "/likes",
      {
        lat: lat,
        lon: lon,
      },
      {},
      "加载中",
      function (res) {
        // var result = res.results;
        let {
          results: { inArea = [], outArea = [] },
        } = res || {};
        inArea = inArea || [];
        outArea = outArea || [];
        for (var i = 0; i < inArea.length; i++) {
          if (inArea[i].summary != null && inArea[i].summary != "") {
            inArea[i].summary = inArea[i].summary.split(",");
          }
        }
        for (var j = 0; j < outArea.length; j++) {
          if (outArea[j].summary != null && outArea[j].summary != "") {
            outArea[j].summary = outArea[j].summary.split(",");
          }
        }

        that.setData({
          concernsinArea: inArea,
          concernsoutArea: outArea,
        });
      },
      function (err) {}
    );
  },

  //手指触摸动作开始 记录起点X坐标
  touchstart: function (e) {
    //开始触摸时 重置所有删除
    let deliverysign = e.currentTarget.dataset.delivery;
    if (deliverysign == "agree") {
      // 可配送区域
      this.data.concernsinArea.forEach(function (v, i) {
        if (v.isTouchMove)
          //只操作为true的
          v.isTouchMove = false;
      });
      this.setData({
        startX: e.changedTouches[0].clientX,
        startY: e.changedTouches[0].clientY,
        concernsinArea: this.data.concernsinArea,
      });
    } else if (deliverysign == "disagree") {
      // 不可配送区域
      this.data.concernsoutArea.forEach(function (v, i) {
        if (v.isTouchMove)
          //只操作为true的
          v.isTouchMove = false;
      });
      this.setData({
        startX: e.changedTouches[0].clientX,
        startY: e.changedTouches[0].clientY,
        concernsoutArea: this.data.concernsoutArea,
      });
    }
  },
  //滑动事件处理
  touchmove: function (e) {
    var that = this;
    let deliverysign = e.currentTarget.dataset.delivery;
    var 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,
        }
      );
    if (deliverysign == "agree") {
      // 可配送区域
      that.data.concernsinArea.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({
        concernsinArea: that.data.concernsinArea,
      });
    } else if (deliverysign == "disagree") {
      // 不可配送
      that.data.concernsoutArea.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({
        // concernsinArea: that.data.concernsinArea,
        concernsoutArea: that.data.concernsoutArea,
      });
    }
  },

  /**
   * 计算滑动角度
   * @param {Object} start 起点坐标
   * @param {Object} end 终点坐标
   */
  angle: function (start, end) {
    var _X = end.X - start.X,
      _Y = end.Y - start.Y; //返回角度 /Math.atan()返回数字的反正切值
    return (360 * Math.atan(_Y / _X)) / (2 * Math.PI);
  },
  //删除事件
  del: function (e) {
    var that = this;
    let deliverysign = e.currentTarget.dataset.delivery;
    let storeid = e.currentTarget.dataset.id;
    if (deliverysign == "agree") {
      that.data.concernsinArea.splice(e.currentTarget.dataset.index, 1);
      that.setData({
        concernsinArea: that.data.concernsinArea,
      });
    } else if (deliverysign == "disagree") {
      that.data.concernsoutArea.splice(e.currentTarget.dataset.index, 1);
      that.setData({
        concernsoutArea: that.data.concernsoutArea,
      });
    }

    let memberId = wx.getStorageSync("memberId");
    masses.getDetelestore(
      baseUrl.waimaiUrl + "/like-store/dislike",
      {},
      {
        memberId: memberId,
        storeId: storeid,
      },
      "加载中",
      function (res) {
        var result = res.results;
        wx.showToast({
          title: "删除成功",
        });
      },
      function (err) {}
    );
  },
  // 跳转商家门店
  jumpSaleroom: function (e) {
    var that = this;
    var storeid = e.currentTarget.dataset.storeid;
    wx.setStorageSync("storeid", storeid);
    // 友盟埋点
    wx.uma.trackEvent("follow");
    wx.navigateTo({
      url: "../../store/pages/lunch/index",
    });
  },
});
