// pages/train/train.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    newsUrl: app.newsUrl,
    typeList: [],
    currentNewsDictionariesId: '',
    currentPage: 1,
    rows: 10,
    directoriesParentId: 'd9d8c7ea-32e0-43ed-ad17-7c99a3c389b9',
    trainList: []
  },
  getType: function () {
    var self = this
    app.restAjax.get(app.restAjax.path('{newsUrl}/app/newsdirectories/listnewsdirectoriesrelease', [app.newsUrl]), {
      directoriesParentId: self.data.directoriesParentId
    }, null, function (code, data) {
      self.setData({
        typeList: data
      })
      self.getList(1)
    })
  },
  changeType: function (e) {
    this.setData({
      currentNewsDictionariesId: e.currentTarget.dataset.newsDirectoriesId
    })
    this.getList(1)
  },
  doClearCurrentNewsDictionariesId: function() {
    var self = this;
    self.setData({
      currentNewsDictionariesId: ''
    });
    this.getList(1)
  },
  getList: function (page) {
    var self = this
    app.dialog.loading('正在加载');
    var info = {
      newsDirectoriesParentId: self.data.directoriesParentId,
      newsDirectoriesId: self.data.currentNewsDictionariesId ? self.data.currentNewsDictionariesId : '',
      page: page,
      rows: self.data.rows
    }
    app.restAjax.get(app.restAjax.path('{newsUrl}/app/newscontent/listpagenewscontentrelease', [app.newsUrl]), info, null, function (code, data) {
      var newsArray;
      for (var i = 0; i < data.rows.length; i++) {
        data.rows[i].newsContentCoverList = data.rows[i].newsContentCoverList[0].contentCoverId
      }
      if(page <= 1) {
        newsArray = data.rows;
      } else {
        newsArray = self.data.news.newsList;
        newsArray = newsArray.concat(data.rows);
      }
      self.setData({
        currentPage: page,
        trainList: newsArray
      })
      if(data.rows.length == 0) {
        app.dialog.msg('暂无数据');
        return;
      }
    }, function(code, data) {
      app.dialog.msg(data.msg);
    }, function() {
      wx.stopPullDownRefresh();
      wx.hideLoading();
    })
  },
  goNewsDetail: function (e) {
    var url = e.currentTarget.dataset.templateRecordUrl
    var link = e.currentTarget.dataset.contentLink
    if (link) {
      wx.navigateTo({
        url: '../template/template?link=' + encodeURIComponent(link),
      })
    } else {
      wx.navigateTo({
        url: '../template/template?url=' + encodeURIComponent(url),
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getType()
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})