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

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

Page({

  data: {
    departmentId:0,
    keyword:'',
    api:'',
    from:0,
    size:10,
    isLogin: false,
    userInfo:{},
    apis:{
      'getViewArticalList':'浏览记录',
      'getLikeArticalList':'收藏记录',
      'getDownloadArticalList':'下载记录',
      'waitConfirmArticalList':'审核审批',
      'waitDownloadArticalList':'下载审批',
      'waitPublishArticalList':'发布审批'
    },
    department:{departmentId:0},
    departments:[],
    articalList:[]
  },
  onLoad: function (options) {
    let api = options.api||'getViewArticalList'
    let title = this.data.apis[api]||'浏览记录'
    console.log( title, api, this.data.apis)
    wx.setNavigationBarTitle({
      title: title,
    })
    this.setData({api})
  },
  onShow(){
    if( this.data.isLogin ) return
    app.checkLogin( userInfo=>{
      this.setData({userInfo})
      this.loadData()
    } )
  },
  loadData(){
    this.loadArtical(   )
  },
  getDetail(e){
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/artical/info/index?articalId=${id}`,
    })
  },
  loadArtical(  cb ){
    let {from, size, api,keyword, articalList} = this.data
    let param = { from, size, keyword }
    app.formPost( `weixin/${api}`, 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})
    this.loadArtical(   )
  },
  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 () {

  }
})