let Parse = getApp().Parse
const company = getApp().globalData.company
const rechText = require('../../../../utils/rech-text')
import date from '../../../../utils/date';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    // list: {},
    // show: false,
    hotelId: null,
    SwiperInfo: {
      duration: 500,
      autoplay: false,
      interval: 2000
    },
    rooms: [],
    loadIndex: 1,
    time: '', // 入住时间
    condition: '', // 入住条件
    orders: [],
    commentCount: 0 // 评论总数
  },
  showPopup() {
    this.setData({
      show: true
    })
  },
  //关闭 弹窗
  onClose: function () {
    console.log(123)
    this.setData({
      show: false
    })
    console.log(this.data.show)
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      activeColor: getApp().globalData.activeColor || '#229293'
    })
    let hotelId = options.id
    if (!hotelId) {
      wx.navigateTo({
        url: `/nova-tourism/pages/index/index`
      })
    }
    this.setData({
      hotelId
    })
    this.getData()
  },

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

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

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

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

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

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

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '民宿详情',

    }
  },
  async getData() {
    await this.getHotel()
    this.getRooms()
    this.getRoomComments()

  },
  async getRoomComments() {
    let Order = new Parse.Query('RoomOrder')
    Order.equalTo("shopStore", this.data.hotelId);
    Order.include("user");
    Order.greaterThanOrEqualTo("status", 800);
    Order.limit(2)
    let orders = await Order.find()
    if (orders && orders.length) {
      orders = orders.map(order => {
        order['time'] = date.formatTime('YY-mm-dd HH:MM', order.updatedAt)
        order.toJSON()
      })
      console.log(orders)
      this.setData({
        orders
      })
    }
    let commentCount = await Order.count()
    console.log(commentCount);
    this.setData({
      commentCount
    })
    console.log(this.data.orders);
  },
  async getHotel() {
    let Hotel = new Parse.Query('ShopStore')
    let hotel = await Hotel.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('isEnabled', true)
    Room.equalTo("shop", this.data.hotelId)
    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
    })
  },
  navigateComments() {
    wx.navigateTo({
      url: `/nova-tourism/pages/homestay/comments/index?id=${this.data.roomId}`
    })
  },
  details(event) {
    let id = event.detail.id
    console.log(id)
    wx.navigateTo({
      url: `/nova-tourism/pages/homestay/room-detail/index?id=${id}`
    })
  },
  loadMore() {
    this.data.loadIndex++;
    console.log('ddddddddddddd', this.data.loadIndex)
    this.getRooms()
  },
  /* 选择入住条件 */
  selectCheck() {},
  /* 选择入住时间 */
  selectTime() {}
})
