//index.js
import fetch from '../../../utils/http'
//获取应用实例
const app = getApp()
const $api = require('../../../config/api.js').$api

Component({
  behaviors: [],
  properties: {},
  data: {
    UserInfo: app.globalData.UserInfo,
    NavList: [{
      value: '项目动态',
      id: ''
    }, {
      value: '精彩活动',
      id: ''
    }],
    NavActiveIndex: 0,
    ListA: [],
    ListB: [],
    PostDataA: {
      PageNum: 1,
      PageSize: 15
    },
    PostDataB: {
      PageNum: 1,
      PageSize: 15
    }
  },
  lifetimes: {},
  ready: function() {
    this.GetProjectList()
    this.GetActivityList()
  },
  pageLifetimes: {
    show: function() {},
  },
  methods: {
    GetActivityList() { // 获取活动列表
      fetch({
        url: $api.activity.list.url + '?pageNum=' + this.data.PostDataB.PageNum + '&pageSize=' + this.data.PostDataB.PageSize,
        method: $api.activity.list.method
      }).then((res) => {
        if (res.code === 200) {
          let arr = res.data.records || []
          let pArr = this.data.ListB
          arr.map((item) => {
            pArr.push({
              Title: item.title,
              CreateDate: item.activityDate,
              Org: item.buildingName,
              Img: item.imgUrl,
              Id: item.activityId
            })
          })
          this.setData({
            ListB: pArr,
            PostDataB: {
              PageNum: res.data.total === this.data.ListB.length ? this.data.PostDataB.PageNum : this.data.PostDataB.PageNum + 1,
              PageSize: this.data.PostDataB.PageSize
            }
          })
        }
      })
    },
    GetProjectList() { // 获取项目动态列表
      fetch({
        url: $api.dynamic.list.url + '?pageNum=' + this.data.PostDataA.PageNum + '&pageSize=' + this.data.PostDataA.PageSize,
        method: $api.dynamic.list.method
      }).then((res) => {
        if (res.code === 200) {
          let arr = res.data.records || []
          let pArr = this.data.ListA
          arr.map((item) => {
            pArr.push({
              Title: item.title,
              CreateDate: item.createDate,
              Org: item.buildingName,
              Img: item.imgUrl,
              Id: item.dynamicId
            })
          })
          this.setData({
            ListA: pArr,
            PostDataA: {
              PageNum: res.data.total === this.data.ListA.length ? this.data.PostDataA.PageNum : this.data.PostDataA.PageNum + 1,
              PageSize: this.data.PostDataA.PageSize
            }
          })
        }
      })
    },
    CutNav(e) { // 切换nav
      if (e.target.dataset.index !== undefined) {
        this.setData({
          NavActiveIndex: e.target.dataset.index
        })
      }
    },
    ItemTap(e) {
      // console.log(e.target.dataset)
      wx.navigateTo({
        url: '/pages/ActivityDetail/index?type=' + e.target.dataset.type + '&id=' + e.target.dataset.id
      })
    }
  }
})