// parents/pages/growth/growth.js
import { routers, viewImage, sharePage, isFn, getGlobalVal } from '../../../utils/util.js'
import { BookInLibInfo , SortSearch} from '../../../utils/api.js'
const { navigateTo, redirectTo, navigateBack } = routers()
const { globalData, hasLibraryService, hasVipService } = getApp()
const { baseImgUrl } = globalData
Page({

  /**
   * 页面的初始数据
   */
  data: {
    baseImgUrl,
    isLoaded: 1,
    pageNo: 1,
    pageSize: 10,
    isAll: 0,
    list: [],
    themeIdx:0,
    isClass:true,
    themeList: [
      // {
      //   "code": "child1",
      //   "name": "历史文化"
      // },
      // {
      //   "code": "child2",
      //   "name": "人文艺术"
      // },
      // {
      //   "code": "child3",
      //   "name": "学科素养"
      // },
      // {
      //   "code": "child4",
      //   "name": "想象幻想"
      // },
      // {
      //   "code": "child4",
      //   "name": "想象幻想"
      // },
      // {
      //   "code": "child4",
      //   "name": "想象幻想"
      // }
    ],
    nodataArray: {
      text: '本馆库存无此图书分类',
      image: '../../../assets/nodata_1.png'
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    options.libId = getGlobalVal('list').length > 0 ? getGlobalVal('list')[0].libId : ''
    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 () {
    this.getBookSort()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  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()
  },
  /**
  * 获取主题年龄段
  */
  getBookSort: function (cb) {
    const { libId } = this.data
    const continuousFn = { fn: this.getBookSort, param: { ...arguments } }
    wx.showLoading({
      title: '加载中',
    })
    BookInLibInfo({ data: { libId}, continuousFn }).then(res => {
      wx.hideLoading()
      const { themeList, booksSize, gradeList} = res.data
      themeList.unshift({ code: '', name:'全部'})
      gradeList.unshift({ gradeCode: '', grade: '全部' })
      this.setData({ themeList, gradeList, booksSize: booksSize})
      cb && cb()
    }).catch(res => {
      this.setData({ isLoaded: true })
      cb && cb()
    })
  },
  /**
  * 选择主题
  */
  selectTheme:function(e){
    const { code, theme, type } = e.currentTarget.dataset;
    this.setData({ code, isClass: false, isTheme: type, grade: type == 0 ? theme : "",theme,isLoaded:false})
    wx.setNavigationBarTitle({
      title: `${theme}`,
    })
    this.getData()
  },
  /**
  * 选择二级主题
  */
  selectOne: function (e) {
    const { code, index} = e.currentTarget.dataset;
    this.setData({ code, themeIdx: index, list: [], isLoaded: false, pageNo:1 })
    this.getData()
  },
  /**
 * 选择班级
 */
  selectTwo: function (e) {
    const { theme, index } = e.currentTarget.dataset;
    this.setData({ grade: theme == '全部' ? '' : theme, themeIdx: index, list: [], isLoaded: false, pageNo: 1 })
    this.getData()
  },
  /**
   * 获取数据
   */
  getData: function (cb) {
    const continuousFn = { fn: this.getData, param: { ...arguments } }
    const { pageNo, pageSize, options, list, code, grade, libId } = this.data

    SortSearch({ data: { pageNo, pageSize, libId, code, grade }, continuousFn }).then(res => {
      const newList = res.data.list
      const listTemp = (res => {
        for (let i in res) {
          if (res[i].summary.length > 30) {
            res[i].summary = res[i].summary.slice(0, 30) + '...'
          } else {
            res[i].summary = res[i].summary
          }
        }
        return res
      })(newList)
      let arr = []
      if (pageNo == 1) {
        arr = [...listTemp]
      } else {
        arr = [...list, ...listTemp]
      }
      this.setData({ list: arr, isAll: newList.length < pageSize, pageNo: newList.length == pageSize ? pageNo + 1 : pageNo, isLoaded: true })
      cb && cb()
    }).catch(res => {
      this.setData({ isLoaded: true })
      cb && cb()
    })
  }
})