const util = require('../../utils/util.js');
const api = require('../../config/api.js');
const user = require('../../services/user.js');
import header from './data'
import {
  Router
} from '../../utils/router.js'
const app = getApp()
//获取应用实例
Router({
  data: {
    // 头部导航栏的高度
    statusBarHeight: app.globalData.statusBarHeight,
    hasData: true,
    classifyList: [{
        type: 0,
        title: '新房',
        icon: "../../static/images/icon/house@2x.png",
      },
      {
        type: 1,
        title: '二手房',
        icon: "../../static/images/icon/second_hand@2x.png",
      },

      {
        type: 2,
        title: '公寓',
        icon: "../../static/images/icon/apartments@2x.png",
      },
      {
        type: 3,
        title: '民宿',
        icon: "../../static/images/icon/b&b@2x.png",
      },
      {
        type: 4,
        title: '社区',
        icon: "../../static/images/icon/community@2x.png",
      },
    ],
    type: '',
    middleType: 11,
    // type :-1:所有；0：新房；1:二手房；2:公寓；3:民宿；4：社区
    // 11:新上房源；12:热门房源;13:热门企业；
    // 21:商家列表；
    // 31：VR场景；32：VR直播间
    middleTabList: [{
        type: 11,
        title: '新上房源'
      },
      {
        type: 12,
        title: '热门房源'
      },
      {
        type: 13,
        title: '热门企业'
      },
    ],
    middleList: {},
    middleEmptyLength: 0,
    // middleList: [{
    //   url: 'https://m4.publicimg.browser.qq.com/publicimg/pcqb/ewtab_yunying/477_266_datu_20210802.jpg',
    //   title: '华发水岸1'
    // }, {
    //   url: 'https://m4.publicimg.browser.qq.com/publicimg/pcqb/ewtab_yunying/477_266_datu_20210802.jpg',
    //   title: '华发水岸1'
    // }, {
    //   url: 'https://m4.publicimg.browser.qq.com/publicimg/pcqb/ewtab_yunying/477_266_datu_20210802.jpg',
    //   title: '华发水岸1'
    // }, ],
    roomTabList: [{
        type: 0,
        title: '新房'
      },
      {
        type: 1,
        title: '二手房'
      },
      {
        type: 2,
        title: '公寓'
      },
    ],
    roomType: 0,
    roomList: [],
    city: app.globalData.city,
    fetcherData: {
      type: '',
      address: '',
      page: 1,
      size: 20,
      name: ''
    },
    loading: false,

  },

  onShareAppMessage: function () {
    return {
      path: '/pages/index/index'
    }
  },
  middle_gotoWV: function (event) {
    let {
      id
    } = event.currentTarget.dataset
    console.log(id)
    wx.navigateTo({
      url: `/pages/webview/index?id=${id}`,
    })
  },
  gotoWV: function (event) {
    let id = event.detail
    wx.navigateTo({
      url: `/pages/webview/index?id=${id}`,
    })
  },
  onPullDownRefresh() {
    this.setData({
      roomList: [],
      'fetcherData.page': 1,
      hasData: true
    });
    this.getRoomList()
    app.onPullDownRefresh()
  },
  onReachBottom: function () {
    this.getRoomList()
    console.log('onReachBottom')
  },
  inputFocus: function () {},
  changeMiddleTab(e) {
    let type = e.currentTarget.dataset.type
    this.setData({
      middleType: type
    })

  },
  changeRoomType(e) {
    let type = e.currentTarget.dataset.type
    this.setData({
      roomType: type,
      roomList: [],
      'fetcherData.page': 1,
      hasData: true
    })
    this.getRoomList()
  },

  getRoomList() {
    if (this.data.hasData) {
      this.setData({
        'fetcherData.type': this.data.roomType,
      })
      if (app.globalData.city == '全国') {
        this.setData({
          'fetcherData.address': '',
        })

      } else {
        this.setData({
          'fetcherData.address': app.globalData.city,
        })
      }
      if (!this.data.loading) {
        this.setData({
          loading: true
        })
        util.request(api.BrandList, this.data.fetcherData).then((res) => {
          this.setData({
            loading: false
          })
          if (res.errno === 0) {
            if (res.data.data.length != 0) {
              this.setData({
                roomList: this.data.roomList.concat(res.data.data),
                'fetcherData.page': this.data.fetcherData.page + 1
              })

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

    } else {
      console.log('没有更多数据')
    }

  },
  getMiddleList(type) {
    let address
    if (app.globalData.city == '全国') {
      address = ''
    } else {
      address = app.globalData.city
    }
    util.request(api.BrandList, {
      type: type,
      address: address,
      page: 1,
      size: 3,
      name: '',
    }).then((res) => {
      console.log(res)
      if (res.errno === 0) {
        if (res.data.data.length == 0) {
          this.setData({
            middleEmptyLength: this.data.middleEmptyLength + 1
          })
        }
        let value = `middleList.${type}`
        this.setData({
          [value]: res.data.data
        })
      }
    });
  },

  toCity() {
    wx.navigateTo({
      url: `/pages/city/city`
    })
  },
  toSearch(e) {
    var type = e.currentTarget.dataset.type
    console.log(type)
    wx.navigateTo({
      url: `/pages/searchRoom/searchRoom?type=` + type
    })
  },

  onLoad: async function (options) {

    app.checkNetStatu();
    // 页面渲染完成
    this.getRoomList()
    for (let i = 0; i < this.data.middleTabList.length; i++) {
      this.getMiddleList(this.data.middleTabList[i].type)
    }
  },





  onReady: function () {
    console.log(2)

  },

  onShow: function (options) {
    console.log(3)

    // 页面显示
    let city = app.globalData.city
    if (city != this.data.city) {
      this.setData({
        'city': app.globalData.city,
        middleEmptyLength: 0
      })
      this.getRoomList()
      for (let i = 0; i < this.data.middleTabList.length; i++) {
        this.getMiddleList(this.data.middleTabList[i].type)
      }
    }


  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  }
})