const util = require("../../../utils/util")

//获取应用实例
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    departmentId:0,
    keyword:'',
    from:0,
    size:10,
    department:{departmentId:0},
    articalList:[]
  },
  onLoad: function (options) {
    let departmentId = +options.departmentId
    if( departmentId ){
      wx.setStorageSync('@departmentId', departmentId)
      this.setData({departmentId})
    }
    app.checkLogin( this.loadData )
  },
  loadData(){
    let departmentId = this.data.departmentId;
    app.getDepartments( departments=>{
      let department = {}
      for( let i in departments){
        if( departments[i].departmentId == departmentId){
          department = departments[i]
          break
        }
      }
      this.setData({department, from:0})
      // 
      wx.setNavigationBarTitle({
        title: department.department
      })
    })
    this.loadArtical(   )
  },
  loadArtical(  cb ){
    let {from, size, departmentId,keyword, articalList} = this.data
    let param = { from, size, departmentId, keyword }
    app.formPost( "weixin/getArticalList", param).then( res=>{
      if( res.code == 200 ){
        if( from == 0) articalList = []
        from= res.data.from;
        articalList = articalList.concat( res.data.list );
        this.setData( {from, articalList})
      }
      cb &&cb()
    })
  },
  onInputEvent(e){
    let {value} = e.detail;
    if( this.data.keyword == value) return;
    this.setData({keyword: value, from:0})
  },
  onReady: function () {

  },
  onShow: function () {
  },
  onHide: function () {

  },

  onUnload: function () {

  },

  onPullDownRefresh: function () {
    wx.showNavigationBarLoading();
    this.setData( {from:0})
    this.loadArtical( ()=>{
      wx.hideNavigationBarLoading(); 
      wx.stopPullDownRefresh();
    })
  },
  onReachBottom: function () {
    var that = this
    let newFrom = that.data.from
    if (newFrom == -1) {
      wx.stopPullDownRefresh();
      return;
    }
    this.loadArtical( );
  },

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

  }
})