//店铺会员管理页面
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    curShopId: '',
    currentPage: 1,
    memberList: [],
    refresherTriggered: false,
    isCellOpen: false,
  },
  onOpen() {
    this.setData({
      isCellOpen: true
    })
  },
  onClose(event) {
    let _self = this
    const {
      position,
      instance
    } = event.detail;
    switch (position) {
      case 'right':
      case 'cell':
        instance.close();
        _self.setData({
          isCellOpen: false
        })
        break;
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      curShopId: options.shopId
    })
    this.getMemberList(this.data.curShopId)
  },
  getMemberList(id) {
    console.log(app)
    let _self = this
    wx.showLoading({
      title: '加载中...',
    })
    app.http.get(app.urls.getMemberList.format({
        shopId: id
      }), {
        header: {
          token: app.globalData.token
        },
        data: {
          shopId: id
        }
      })
      .then(res => {
        wx.hideLoading({})
        wx.stopPullDownRefresh({})
        _self.setData({
          memberList: res.data
        })
      })
      .catch(err => {
        wx.hideLoading({})
        wx.stopPullDownRefresh({})
      })
  },
  showMemberDetail(e) {
    console.log(e)
  },
  //删除会员
  doDelMember(e) {
    let _self = this
    wx.showModal({
      title: '提示',
      content: '确定要删除该会员吗?',
      success(res) {
        if (res.confirm) {
          wx.showLoading({
            title: '删除中...',
          })
          app.http.delete(app.urls.doDelMember.format({
              ids: e.currentTarget.dataset.key.memberId
            }), {
              header: {
                token: app.globalData.token
              }
            })
            .then(res => {
              wx.hideLoading({})
              wx.showToast({
                title: '删除成功',
              })
              _self.getMemberList(_self.data.curShopId)
            })
            .catch(err => {
              wx.hideLoading({})
            })
        }
      }
    })
  },
})