let Parse = getApp().Parse
const company = getApp().globalData.company
const compute = require('../../../utils/compute.js')
let navigationBarHeight = getApp().globalData.statusBarHeight + 44;
Component({
  /**
   * 组件的属性列表
   */
  properties: {},

  /**
   * 组件的初始数据
   */
  data: {
    imgUrls: [
      'https://s1.ax1x.com/2023/04/10/ppqifvF.png',
      'https://s1.ax1x.com/2023/04/10/ppqiWgU.png',
      'https://s1.ax1x.com/2023/04/10/ppqiR3T.png'
    ],
    list: [],
    hotels: [],
    rooms: [],
    active: 0,
    type: 'recommend',
    loadIndex: 1,
    searchVal: '',
    showTab: false,
    stickytop: navigationBarHeight,
    tabs: [{
        title: '推荐',
        icon: 'https://s1.ax1x.com/2023/04/10/ppqEqdH.png',
        type: 'recommend'
      },
      {
        title: '价格',
        icon: 'https://s1.ax1x.com/2023/04/10/ppqkh79.png',
        type: 'price'
      },
    ],
    tabIndex: 0,
    banners:[]
  },

  ready: function () {
    // 在组件布局完成后执行，确保options参数中有data信息
    // this.selectComponent('#tabs').resize()
    // this.getRooms()
  },


  lifetimes: {
    created() {
      this.getBanner()
    },
    attached() {
    }
  },


  /**
   * 组件的方法列表
   */
  methods: {
    details(e) {
      let id = e.detail.id
      console.log(id)
      wx.navigateTo({
        url: `/nova-tourism/pages/homestay/room-detail/index?id=${id}`
      })
    },
    async getHotels(type) {
      let Hotel = new Parse.Query('ShopStore')
      Hotel.notEqualTo('isDeleted', "true")
      Hotel.equalTo('company', company)
      Hotel.equalTo('type', 'stay')
      Hotel.select(
        'cover',
        'storeName',
        'perCapita',
        'address',
        'type',
        'isShow'
      )
      Hotel.equalTo('isShow', true)
      Hotel.skip((this.data.loadIndex - 1) * 10)
      if (type == 'price') {
        Hotel.ascending('perCapita')
      }
      Hotel.limit(10)
      let hotels = await Hotel.find()
      hotels = hotels.map((hotel) => hotel.toJSON())
      hotels = this.data.loadIndex == 1 ? hotels : this.data.hotels.concat(hotels)
      this.setData({
        hotels
      })
    },
    toSearch() {
      wx.navigateTo({
        url: '/nova-tourism/pages/search/search'
      })
    },
    search(event) {
      console.log(event);
      this.getRooms()
    },
    searchClear() {
      this.data.searchVal = '';
      this.getRooms()
    },
    showSearchBtn() {
      this.setData({
        searchBtn: true
      })
    },
    hiddenSearchBtn() {
      this.setData({
        searchBtn: false
      })
    },
    onTabClick(e) {
      let {
        id,
        type
      } = e.currentTarget.dataset;
      this.data.active = id
      this.data.loadIndex = 1
      this.data.type = type
      this.setData({
        tabIndex: id,
      })
      this.getRooms()

    },
    tabChange(event) {
      this.data.active = event.detail.name
      this.data.loadIndex = 1
      this.data.type = event.detail.name
      this.getRooms()
    },
    loadMore() {
      this.data.loadIndex++
      this.getRooms()
    },
    async getRooms() {
      let type = this.data.type;
      console.log('getRooms');
      let Room = new Parse.Query('ShopRoom')
      Room.equalTo('company', company)
      Room.equalTo('isEnabled', true)
      if (this.data.searchVal != '') {
        Room.contains('name', this.data.searchVal)
      }
      if (type == 'price') {
        Room.ascending('price')
      }
      if (type == 'recommend') {
        Room.equalTo('isRecom', true)
      }
      Room.skip((this.data.loadIndex - 1) * 10)
      Room.select(
        'name',
        'images',
        'price',
        'total',
        'remaining',
        'merber',
        'type',
        'area',
        'tags',
        'address'
      )
      Room.limit(10)
      let rooms = await Room.find()
      rooms = rooms.map((room) => room.toJSON())
      // hotels = hotels.concat(hotels).concat(hotels).concat(hotels).concat(hotels)
      rooms = this.data.loadIndex == 1 ? rooms : this.data.rooms.concat(rooms)
      this.setData({
        rooms
      })
    },
    async getBanner() {
      let Banner = new Parse.Query('Banner')
      Banner.notEqualTo('isDeleted', "true")
      Banner.equalTo('company', company)
      Banner.equalTo('isEnabled', "true")
      Banner.equalTo('type', 'homestay')
      let banner = await Banner.find()
      if (banner && banner.length > 0) {
        let listJSON = []
        banner.forEach(c => {
          listJSON.push(c.toJSON())
        })
        this.setData({
          banners: listJSON
        })
      }
    },
  }
})