// pages/newsList/newsList.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    news: {
      newsUrl: app.newsUrl,
      newsDictionaries: [],
      newsList: [],
    },
    directoriesParentId: 'f497904b-7727-4832-891c-604c36ae4167',
    currentNewsDictionariesId: '',
    currentPage: 1,
    rows: 10,
    areaId: ''
  },
  doGetNewsDictionariesList: function() {
    var self = this;
    app.dialog.loading('正在加载');
    app.restAjax.get(app.restAjax.path('{newsUrl}/app/newsdirectories/listnewsdirectoriesrelease', [self.data.news.newsUrl]), {
      directoriesParentId: self.data.directoriesParentId,
    }, null, function(code, data) {
      self.setData({
        'news.newsDictionaries': data
      })
      self.doGetNewsList(1);
    }, function(code, data) {
      app.dialog.msg(data.msg);
    }, function() {});
  },
  doClickDictionaries: function(event) {
    var self = this;
    console.log(event.currentTarget.dataset.newsDirectoriesId)
    self.setData({
      currentNewsDictionariesId: event.currentTarget.dataset.newsDirectoriesId
    });
    self.doGetNewsList(1);
  },
  doClearCurrentNewsDictionariesId: function() {
    var self = this;
    self.setData({
      currentNewsDictionariesId: ''
    });
    self.doGetNewsList(1);
  },
  doGetNewsList: function(page, lv) {
    var self = this;
    app.dialog.loading('正在加载');
    console.log(self.data.currentNewsDictionariesId)
    var info = {
      newsDirectoriesParentId: self.data.directoriesParentId,
      newsDirectoriesId: self.data.currentNewsDictionariesId ? self.data.currentNewsDictionariesId : '',
      page: page,
      rows: self.data.rows
    }
    if (lv) {
      lv = 'area' + lv
      info[lv] = self.data.areaId
    }
    app.restAjax.get(app.restAjax.path('{newsUrl}/app/newscontent/listpagenewscontentrelease', [self.data.news.newsUrl]), info, null, function(code, data) {
      var newsArray;
      if(page <= 1) {
        newsArray = data.rows;
      } else {
        newsArray = self.data.news.newsList;
        newsArray = newsArray.concat(data.rows);
      }
      self.setData({
        currentPage: page,
        'news.newsList': newsArray
      })
      console.log(self.data.news.newsList)
      if(data.rows.length == 0) {
        app.dialog.msg('暂无数据');
        return;
      }
    }, function(code, data) {
      app.dialog.msg(data.msg);
    }, function() {
      wx.stopPullDownRefresh();
      wx.hideLoading();
    });
  },
  goNewsDetail: function (event) {
    wx.navigateTo({
      url: '../newsDetail/newsDetail?templateRecordUrl='+ event.currentTarget.dataset.templateRecordUrl,
    })
  },
  getAreaList: function () {
    var self = this;
    app.restAjax.get(app.restAjax.path(app.usercenterUrl + '/app/area/listallbyparentidrelease/640675', []),
      self.data.pages, null, function (code, data) {
        if (code == '200') {
          self.setData({
            areaList: data
          })
        }
      }
    )
  },
  changeArea: function (e) {
    app.dialog.loading('正在加载');
    var lv = parseInt(e.currentTarget.dataset.level) + 1
    this.setData({
      areaId: e.currentTarget.dataset.area
    })
    this.doGetNewsList(1, lv)
  },
  loadAll: function () {
    this.setData({
      areaId: ''
    })
    this.doGetNewsList(1)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var self = this;
    if(options.type == 1) {
      self.setData({
        directoriesParentId: 'f497904b-7727-4832-891c-604c36ae4167'
      });
    } else {
      self.setData({
        directoriesParentId: 'f1d5d313-f728-4dda-9843-1116d97e17b0'
      });
    }
    self.doGetNewsDictionariesList();
    self.getAreaList();
  },

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

  },

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

  },

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

  },

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

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.doGetNewsList(1);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.doGetNewsList(this.data.currentPage + 1);
  },

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

  }
})