const Parse = getApp().Parse;
const app = getApp();
const company = getApp().globalData.company;
let navigationBarHeight = getApp().globalData.statusBarHeight + 44;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    storeId: null,
    loadIndex: 1,
    rooms: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let merchant = wx.getStorageSync('merchant')
    let store = wx.getStorageSync('store')
    if (!merchant) {
      wx.navigateTo({
        url: `/nova-tourism/pages/index/index`
      })
    }


    this.setData({
      storeId: store.objectId,
      store: store,
      merchant: merchant
    })
    this.getRooms()

  },
  
  async getStore() {
    let Store = new Parse.Query('ShopStore')
    let hotel = await Store.get(this.data.hotelId)
    hotel = hotel.toJSON()
    if (hotel.content) {
      hotel.content = rechText.formatRichText(hotel.content)
    }
    console.log(hotel)
    this.setData({
      hotel
    })
  },
  async getRooms() {
    let Room = new Parse.Query('ShopRoom')
    Room.equalTo('company', company)
    Room.equalTo("shop", this.data.storeId)
    Room.select(
      'name',
      'images',
      'price',
      'total',
      'remaining',
      'merber',
      'type',
      'area',
      'tags'
    )
    Room.skip((this.data.loadIndex - 1) * 10)
    Room.limit(10)
    let rooms = await Room.find()
    rooms = rooms.map((room) => {
      room = room.toJSON()
      room.tags.slice(0, 5)
      return room
    })
    console.log(rooms)
    if (this.data.loadIndex != 1) {
      rooms = this.data.rooms.concat(rooms)
    }
    this.setData({
      rooms
    })
  },
  loadMore() {
    this.data.loadIndex++;
    this.getRooms()
  },
  edit(event) {
    console.log(event);
    let id = event.detail.id;
    wx.navigateTo({
      url: `/nova-tourism/pages/my/merchant/room-manage/room-edit/index?roomId=${id}`
    })
  },
  addRoom() {
    wx.navigateTo({
      url: `/nova-tourism/pages/my/merchant/room-manage/room-edit/index?storeId=${this.data.storeId}`
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

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

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

  },

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

  },

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

  },

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

  },

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

  }
})