// nova-tourism/pages/my/merchant/merchant-home/my-users/index.js
let Parse = getApp().Parse;
const company = getApp().globalData.company
const uid = Parse.User.current()?.id

Page({

  /**
   * 页面的初始数据
   */
  data: {
    users: [], //用户列表
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getToStoreUser()
  },
  /** 获取本店用户*/
  async getToStoreUser() {
    let {
      users
    } = this.data
    let toStoreUid = wx.getStorageSync('merchant')?.objectId
    let store = wx.getStorageSync('store')?.objectId
    if (!toStoreUid) return
    if (!store) return
    let query = new Parse.Query('_User')
    query.equalTo('company', company);
    query.equalTo('store', store);
    query.notEqualTo('isDeleted', true);
    query.limit(30)
    query.skip(users?.length || 0)
    let r = await query.find()
    let list = r?.map(item => item?.toJSON())
    this.setData({
      users: [...users, ...(list || [])]
    })
  },


  async agentParent() {
    if (!wx.getStorageSync('merchant')?.objectId) return
    let that = this
    let {
      users
    } = this.data
    wx.request({
      url: 'https://server.fmode.cn/api/user/agent/children',
      data: {
        uid: wx.getStorageSync('merchant')?.objectId,
        limit: 50,
        skip: users?.length || 0
      },
      header: {
        'content-type': 'application/json'
      },
      method: 'POST',
      dataType: 'json',
      responseType: 'text',
      success: (result) => {
        console.log(result);
        that.setData({
          users: [...users, ...(result?.data?.data || [])]
        })
      },
      fail: () => {},
      complete: () => {}
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

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

  },

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

  },

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

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.getToStoreUser()
  },

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

  }
})