import {
  ihome
} from '../../../api/api.js'
const app = getApp()
Page({
  data: {
    pagination: {
      page: 1,
      pagesize: 3
    },
    imgUrl: app.globalData.imgUrl,
  },
  clickArticle(e) {
    app.data.nav.newsDetails = this.data.news[e.currentTarget.dataset.i]
    wx.navigateTo({
      url: '../newsDetails/newsDetails?id=' + e.currentTarget.dataset.id
    })
  },
  loadHotnews() {
    let news = this.data.news,
      p = this.data.pagination
    if (p.page == 1) {
      news = []
    }
    ihome.loadHotnews(this.data.pagination, r => {
      this.setData({
        news: news.concat(r.list),
        ['pagination.page']: r.list.length > 0 ? p.page : (p.page - 1)
      })
    })
  },
  onLoad() {
    this.loadHotnews()
  },
  onPullDownRefresh() {
    this.data.pagination.page = 1
    this.loadHotnews()
  },
  onReachBottom: function() {
    this.data.pagination.page += 1
    this.loadHotnews()
  },
})