// parents//pages/ranking/ranking.js
import { routers, viewImage, sharePage, isFn, getGlobalVal,formatNumber } from '../../../utils/util.js'
import { UserRankingLib, ReadinglibRankingHis, RankingLib } from '../../../utils/api.js'
const { navigateTo, redirectTo, navigateBack } = routers()
const { globalData, hasLibraryService, hasVipService } = getApp()
const { baseImgUrl, qrcodeInvalidToastText } = globalData

Page({

  /**
   * 页面的初始数据
   */
  data: {
    baseImgUrl,
    isMonth:true,
    isLoaded: 1,
    content: '',
    hiddenModel:false,
    navIndex: 0,
    date:'2018-10',
    pageNo: 1,
    pageSize: 10,
    isAll: false,
    type:1,
    nodataArray: {
          text: '该月暂无排行数据',
          image: '../../../assets/book.png',
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    options.libId = getGlobalVal('list').length > 0 ? getGlobalVal('list')[0].libId : ''
    wx.setNavigationBarTitle({
      title: `排行榜`,
    })
    this.setData(options)
    this.getData(res => {
      this.setData({ isLoaded: 1 })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    const obj = { navigateTo, redirectTo, navigateBack, viewImage }
    for (const i in obj) {
      this[i] = obj[i]
    }
  },

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

  },

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

  },

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

  },
  
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({ pageNo: 1 })
    this.getData(res => {
      wx.stopPullDownRefresh()
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    const { isAll } = this.data
    if (isAll) {
      return
    }
    this.getData()
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    if (isFn(sharePage)) return sharePage()
  },
  /**
     * 提示框隐藏
     */
  allShowModel: function () {
    this.setData({ hiddenModel: false })
  },
  /**
   * 提示框打开
   */
  showModel: function () {
    this.setData({ hiddenModel: true })
  },
  /**
     * 选择月度年度排行榜
     */
  selectMonth: function (e) {
    const {num}=e.currentTarget.dataset;
    this.setData({ type:num })
    this.getData()
  },
 
  /**
   * 获取馆内数据
   */
  getData: function (cb) {
    const continuousFn = { fn: this.getDataTwo, param: { ...arguments } }
    const { pageNo, pageSize, libId, list, options,type } = this.data
    RankingLib({ data: { libId, pageNo, pageSize,type }, continuousFn }).then(res => {
      const { rankingList, pages,borrowNumTotal,borrowTimesTotal } = res.data;
      let arr = []
      if (pageNo == 1) {
        arr = [...rankingList]
      } else {
        arr = [...list, ...rankingList]
      }
      console.log(arr, rankingList)
      this.setData({
        borrowNumTotal,
        borrowTimesTotal,
        list: arr,
        pages: pages,
        pageNo: rankingList.length == pageSize ? pageNo + 1 : pageNo,
        isAll: rankingList.length < pageSize,
        isLoaded: true
      })
      cb && cb()
    }).catch(res => {
      cb && cb()
    })
  },
  /**
   * 获取历史数据
   */
  getDataHistory: function (cb) {
    const continuousFn = { fn: this.getDataHistory, param: { ...arguments } }
    const { pageNo, pageSize, libId, options,date} = this.data
    ReadinglibRankingHis({ data: {  libId, pageNo, pageSize, searchDate:date }, continuousFn }).then(res => {
      const listTemp = res.data.rankingList;
      const pages = res.data.pages
      let arr = []
      if (pageNo == 1) {
        arr = [...listTemp]
      } else {
        arr = [...list, ...listTemp]
      }
      this.setData({
        pages,
        list: arr,
        pageNo: listTemp.length == pageSize ? pageNo + 1 : pageNo,
        isAll: listTemp.length < pageSize,
        isLoaded: true
      })
      cb && cb()
    }).catch(res => {
      cb && cb()
    })
  },

  /**
   * 查看历史记录
   */
  selectHistory: function () {
    const { isMonth } = this.data
    const year = new Date().getFullYear()
    const month = new Date().getMonth()
    this.setData({ isMonth: !isMonth, pageNo: 1, list: [], date: year + '-' + formatNumber(month) })
    this.getDataHistory()
  },
  /**
   * 选择日期
   */
  changeDate:function(e){
    const {value}=e.detail;
    this.setData({ date:value})
    this.getDataHistory()
  }
})