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:热门房源:热门装修;13:热门企业；
    // 21:商家列表；
    // 31：VR场景；32：VR直播间
    middleTabList: [{
        type: 11,
        title: '热门房源'
      },
      {
        type: 12,
        title: '热门装修'
      },
      {
        type: 13,
        title: '热门商家'
      },
    ],
    middleList: {},
    middleEmptyLength: 0,
    middleTypeList: [],
    roomTabList: [{
        type: 0,
        title: '新房'
      },
      {
        type: 1,
        title: '二手房'
      },
      {
        type: 2,
        title: '公寓'
      },
      {
        type: 4,
        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}&type=${this.data.middleType}`,
    })
    util.request(api.increaseViewCount, {
      brandId: id,
      type:this.data.middleType
    }, 'GET').then((res) => {
      
    });
  },
  gotoWV: function (event) {
    let {id} = event.detail
    wx.navigateTo({
      url: `/pages/webview/index?id=${id}&type=${this.data.roomType}`,
    })
    util.request(api.increaseViewCount, {
      brandId: id,
      type:this.data.roomType
    }, 'GET').then((res) => {
        // var num =  'roomList['+index+'].pvTotalNum'
        // this.setData({
        //   [num]:res.data
        // })
    });

  },
  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
        })
        wx.showLoading({
          title: '加载中...',
        })
        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) {
        let mt = (type - 0) + 1

        if (res.data.data.length == 0) {
          this.setData({
            middleEmptyLength: this.data.middleEmptyLength + 1,
            middleType: mt
          })

        } else {
          let t = this.data.middleTypeList
          t.push(type)
          this.setData({
            middleTypeList: t,
          }, () => {
            this.data.middleTypeList.sort()
            this.setData({
              middleType: this.data.middleTypeList[0]
            })
          })

        }
        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()
    this.setData({
      middleTypeList:[]
    })
    for (let i = 0; i < this.data.middleTabList.length; i++) {
      this.getMiddleList(this.data.middleTabList[i].type)
    }
  },





  onReady: function () {

  },

  onShow: function (options) {
    // 页面显示
    let city = app.globalData.city
    if (city != this.data.city) {
      this.setData({
        'city': app.globalData.city,
        'fetcherData.page': 1,
        middleEmptyLength: 0,
        hasData: true,
        loading: false,
        roomList: [],
        middleTypeList:[]
      })
      this.getRoomList()
      for (let i = 0; i < this.data.middleTabList.length; i++) {
        this.getMiddleList(this.data.middleTabList[i].type)
      }
    }


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