// index.js
// 获取应用实例
const app = getApp()
import HTTP from "../../../requestFn/Api"
Page({
  data: {
    garden_name: "请先登录",
    houseList: [],
    tabbar: {},
    notice_list: [],
  },
  onShow() {
    wx.hideTabBar();
    const that = this;
    wx.getStorage({
      key: "houseInfo",
      success(e) {
        const item = e.data;
        app.globalData.garden_id = item.garden_id;
        app.globalData.can_pay = item.can_pay;
        app.globalData.house_id = item.house_id;
        app.globalData.garden_name = item.garden_name;
        app.globalData.house_name = item.house_name;
        app.globalData.authentication = true;
        that.setData({
          garden_name: item.garden_name,
          house_name: item.house_name
        })
        that.getNotice(item.garden_id);
        that.getCommunity(item.garden_id);
      },
      fail() {
        that.setData({
          garden_name: "请先登录",
          house_name: "",
          notice_list: []
        });
        that.getMyhouse();
      }
    });
    wx.getStorage({
      key: "house_list",
      success(e) {
        app.globalData.house_list = e.data;
      }
    });
  },
  getMyhouse() {
    const that = this;
    HTTP.GetuserHouse().then(res => {
      if (res.list.length > 0) {
        const result = res.list.map((item, index) => {
          if (index == 0) {
            that.setData({
              garden_name: item.garden_name,
              house_name: item.house_name
            })
            that.getNotice(item.garden_id);
            that.getCommunity(item.garden_id);
            app.globalData.garden_id = item.garden_id;
            app.globalData.can_pay = item.can_pay;
            app.globalData.house_id = item.house_id;
            app.globalData.garden_name = item.garden_name;
            app.globalData.house_name = item.house_name;
            wx.setStorage({
              data: item,
              key: "houseInfo"
            })
            return {
              ...item,
              selected: true
            }
          } else {
            return {
              ...item,
              selected: false
            }
          }
        })
        that.setData({
          house_list: result
        })
        wx.setStorage({
          data: result,
          key: "house_list"
        })
      } else {
        that.setData({
          garden_name: "",
          house_name: "",
          notice_list: []
        })
      }
    })
  },
  onLoad() {
    getApp().watch('unreadCountHome', this.watchBack);
    app.editTabbar();
  },
  watchBack(name, value) {
    let data = {};
    data[name] = value;
    this.setData(data);
  },
  onUnload() {
    console.log("卸载");
  },
  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })

      }
    })
  },
  //获取公告第一条
  getNotice(garden_id) {
    const that = this;
    HTTP.GetAnnouncement({
      garden_id
    }).then(res => {
      that.setData({
        notice_list: [res.list[0]]
      })
    })
  },
  //获取社区活动第一条
  getCommunity(garden_id) {
    const that = this;
    HTTP.GetEvent({
      garden_id
    }).then(res => {
      that.setData({
        event_list: res.list[0]
      })
    })
  },
  //物业缴费
  propertyFee() {
    wx.navigateTo({
      url: '../property/property',
    })
  },
  //小区公告
  goNotice() {
    wx.navigateTo({
      url: '/page/home/pages/notice/index',
    })
  },
  //全部服务
  goAllServe() {
    wx.navigateTo({
      url: '/page/home/pages/allService/index',
    })
  },
  //投票活动
  goVote() {
    wx.navigateTo({
      url: "/page/home/pages/vote/index"
    })
  },
  //服务电话
  goServicePhone() {
    wx.navigateTo({
      url: '/page/home/pages/phone/phone',
    })
  },
  //报事报修
  goRepair() {
    wx.navigateTo({
      url: '/page/home/pages/repair/repair',
    })
  },
  //投诉建议
  goSuggestion() {
    wx.navigateTo({
      url: '/page/home/pages/suggestion/suggestion',
    })
  },
  //房屋租售
  goHouse() {
    wx.navigateTo({
      url: '../../home/pages/house/house',
    })
  },
  goLivingPay() {
    wx.navigateTo({
      url: '../../mine/pages/livingPay/index',
    })
  },
  goDetail(e) {
    const that = this;
    const data = that.data.notice_list;
    wx.navigateTo({
      url: "/page/home/pages/notice/detail/index",
      success: function (res) {
        res.eventChannel.emit('acceptDataFromOpenerPage', {
          data: data
        })
      }
    })
  },
  //选择房屋
  goMyhouse() {
    wx.navigateTo({
      url: '/page/home/pages/myhouse/myhouse',
    })
  },
  goEvent() {
    const {
      event_list
    } = this.data;
    let data = [event_list]
    wx.navigateTo({
      url: '../../home/pages/event/detail/index',
      success: function (res) {
        res.eventChannel.emit('acceptDataFromOpenerPage', {
          data
        })
      }
    })
  }
})