// pages/searchRoom/searchRoom.js
const util = require('../../utils/util.js');
const api = require('../../config/api.js');
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {

    searchKey: '',
    roomList: [],
    storeList: [],
    isSearch: false,
    loading: false,
    fetcherData: {
      type: null,
      address: app.globalData.city,
      page: 1,
      size: 20,
      name: ''
    },
    typeName: {
      0: '新房',
      1: '二手房',
      2: '公寓',
      3: '民宿',
      4: '装修'
    },

  },
  gotoWV: function (event) {
    let {
      id
    } = event.detail
    wx.navigateTo({
      url: `/pages/webview/index?id=${id}&type=${this.data.fetcherData.type}`,
    })
  },
  inputChange(e) {
    this.setData({
      'fetcherData.name': e.detail.value,
    });
  },
  search() {
    // if (this.data.fetcherData.name != '') {
    wx.showLoading({
      title: '加载中...',
    });
    // console.log(this.data.searchKey)
    if (this.data.fetcherData.type == '21') { //商家
      this.setData({
        storeList: []
      })
    } else {
      this.setData({
        roomList: []
      })
    }
    this.setData({
      'fetcherData.page': 1,
    })
    this.getRoomList()
    // } else {
    //   wx.showToast({
    //     title: '请输入VR场景名称',
    //     icon: 'none'
    //   })

    // }

  },

  getRoomList() {
    if (this.data.fetcherData.type == '21') { //商家
      this.setData({
        'fetcherData.address': '',
      })
    } else {
      if (app.globalData.city == '全国') {
        this.setData({
          'fetcherData.address': '',
        })
      } else {
        this.setData({
          'fetcherData.address': app.globalData.city,
        })
      }
    }
   
    if (!this.data.loading) {
      this.setData({
        loading: true
      })
      wx.showLoading({
        title: '加载中...',
      });
    
      util.request(api.BrandList, this.data.fetcherData).then((res) => {
        this.setData({
          loading: false
        })
        wx.hideLoading()
        if (res.errno === 0) {

          if (this.data.fetcherData.type == '21') {

            this.setData({
              storeList: this.data.storeList.concat(res.data.data),
              // storeList: res.data.data,
              'fetcherData.page': this.data.fetcherData.page + 1,
              isSearch: true
            })
          } else {

            this.setData({
              roomList: this.data.roomList.concat(res.data.data),
              // roomList: res.data.data,
              'fetcherData.page': this.data.fetcherData.page + 1,
              isSearch: true
            })

          }


        }
      }).catch(err => {
        this.setData({
          loading: false
        })
      });
    }

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    getApp().checkNetStatu();
    this.setData({
      'fetcherData.type': options.type || 21,
    })
    if (this.data.typeName[options.type]) {
      this.setData({
        isSearch: true
      })
      this.getRoomList()
      wx.setNavigationBarTitle({
        title: this.data.typeName[options.type]
      })
    }


  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

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

  }
})