// subpages/myMattress/myMattress.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    mList:[]
  },

  getList() {
    const that = this;
    wx.request({
      url: 'https://aipush.aidsleep.cn/customerlist',
      method: 'POST',
      data: {
        token: "89835e65993fee4a6a6cbbe4c271da51e5521822934e13769e61cadabaed72c3" || wx.getStorageSync('token'),
        udi: wx.getStorageSync('unionid'),
      },
      success: (res) => {
        var loadData = res.data.data;
        if (loadData) {
          const successId = wx.getStorageSync("successId");
          if (successId) {
            for (let i = 0; i < loadData.length; i++) {
              var element = loadData[i];
              if (element.pid == successId) {
                element.online = 'yes';
                break;
              }
            }
            wx.removeStorageSync("successId");
          }
          that.setData({
            mList: loadData
          })

        } else {
        }
      },
      fail: () => {
      }
    });
  },
  showMenu(e) {
    const item = e.currentTarget.dataset.item;
    console.log(item);
    const that = this;
    wx.showActionSheet({
      itemList: ['编辑', '删除'],
      success: function (e) {
        console.log(e.tapIndex) //没有item项下的key或index
        if (e.tapIndex == 0) {
          // 编辑
          wx.setStorageSync('item', item);
          wx.navigateTo({
            url:"/subpages/addMattresses/addMattresses?type=edit"
          })
        } else {
          // 删除
          that.deleteItem(item);
        }
      }
    })
  },
  deleteItem(item) {
    const that = this;
    wx.request({
      url: 'https://aipush.aidsleep.cn/customerdel',
      method: 'POST',
      data: {
        token: "89835e65993fee4a6a6cbbe4c271da51e5521822934e13769e61cadabaed72c3" || wx.getStorageSync('token'),
        pid: item.pid,
      },
      success: (res) => {
        var code = res.data.code;
        if (code) {
          wx.showToast({
            icon: 'none',
            title: "删除成功"
          });
          that.getList();
        } else {
          wx.showToast({
            icon: 'none',
            title: "删除失败"
          });
        }
      },
      fail: () => {
        wx.showToast({
          icon: 'none',
          title: "删除失败"
        });
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})