const app = getApp();
import HTTP from "../../../../requestFn/Api"
Page({
  data: {
    house_list: [],
  },
  onShow(options) {
    const that = this;
    wx.getStorage({
      key: "house_list",
      success(e) {
        let house_id
        e.data.map(item => {
          if (item.selected) {
            house_id = item.house_id
          }
        });
        that.setData({
          house_id,
        })
      }
    });
    that.getMyHouse();
  },
  myhouse(e) {
    const {
      id
    } = e.currentTarget;
    const {
      house_list
    } = this.data;
    let newArr = house_list.map(item => {
      if (item.id == id) {
        wx.setStorage({
          data: item,
          key: "houseInfo"
        })
        return {
          ...item,
          selected: true
        }
      } else {
        return {
          ...item,
          selected: false
        }
      }
    })
    wx.setStorage({
      data: newArr,
      key: "house_list"
    })
    this.setData({
      house_list: newArr
    })
  },
  async getMyHouse() {
    const {
      list
    } = await HTTP.GetuserHouse();
    const that = this;
    const {
      house_id
    } = that.data;
   const house_list =  list.map(item => {
      if (house_id == item.house_id) {
        return {...item,selected:true,}
      }else{
        return {...item,selected:false}
      }
    })
    that.setData({
      house_list
    })
    wx.setStorage({
      data: house_list,
      key: "house_list"
    })
  }
})