// miniprogram/pages/interList/interList.js
const app = getApp();
Page({
  active: 0,
  oriList: {},
  pageSize: 20,
  /**
   * 页面的初始数据
   */
  data: {
    newsListAll: {},
    tabList: [],
    mainHeight: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const sys = wx.getSystemInfoSync();
    let list = app.globalData.origin.ArticlesOverview || [];
    this.getJSON(list[0].id);
    this.active = list[0].id;
    this.setData({
      tabList: app.globalData.origin.ArticlesOverview || [],
      mainHeight: sys.windowHeight - 60
    })
  },
  onChange(e) {
    let { id } = this.data.tabList[e.detail.index];
    this.active = id;
    !this.oriList[id] && this.getJSON(id);
  },
  atBottom() {
    this.oriList[this.active]
    let newsList = this.data.newsListAll || {};
    if (newsList[this.active]) {
      newsList[this.active].push(...this.oriList[this.active].splice(0, this.pageSize))
    } else
      newsList[this.active] = this.oriList[this.active].splice(0, this.pageSize);
    this.setData({
      newsListAll: newsList
    })
  },
  getJSON(id) {
    wx.showLoading();
    const that = this;
    const fs = wx.getFileSystemManager();
    wx.downloadFile({
      url: app.base.url + '/qf-platform/' + id + '.json',
      success(r) {
        fs.readFile({
          filePath: r.tempFilePath,
          encoding: 'utf8',
          position: 0,
          success(res) {
            let data = res.data ? JSON.parse(res.data) : {};
            that.oriList[id] = data || [];
            let newsList = that.data.newsListAll || {};
            newsList[id] = that.oriList[id].splice(0, that.pageSize);
            that.setData({
              newsListAll: newsList
            })
          },
          complete() {
            wx.hideLoading()
          }
        })
      },
      complete() {
        wx.hideLoading()
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  toDetail: function (e) {
    let title = e.currentTarget.dataset.title, newsUrl = e.currentTarget.dataset.newsurl;
    wx.navigateTo({
      url: '/pages/webView/index?title=' + title + "&newsUrl=" + newsUrl,
    })
  }
})