import UserApi from '../../apis/user'

let app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs: [
      { name: '新房', saleType: 1, house_list: []},
      { name: '二手房', saleType: 2, house_list: []},
      { name: '租房', saleType: 3, house_list: []},
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onShow: function (options) {
    this.fetchHistory(this.data.tabs[0].saleType)
  },
  fetchHistory (sale_type) {
    UserApi.fetchFocusHouses({
      user_id: app.globalData.userinfo.user_id,
      sale_type
    }).then(res => {
      let { tabs } = this.data
      tabs = tabs.map(item => {
        if (item.saleType === sale_type) {
          item.house_list = res.data.filter(item => item.sale_type == sale_type)
        }
        return item
      }) 
      console.log(tabs)
      this.setData({
        tabs: tabs
      })
    })
  },
  change (e) {
    const item = e.detail
    this.fetchHistory(item.saleType)
  }
})