// parents/pages/search/search.js
import { routers, viewImage, sharePage, isFn, getGlobalVal } from '../../../utils/util.js'
import { AgencyVipList} from '../../../utils/api.js'
const { navigateTo, redirectTo, navigateBack } = routers()
const { globalData, hasLibraryService, hasVipService } = getApp()
const { baseImgUrl, thumbnail, qrcodeInvalidToastText, borrowToastText, reservationsToastText, wishToastText } = globalData
Page({

  /**
   * 页面的初始数据
   */
  data: {
    thumbnail,
    baseImgUrl,
    borrowToastText,
    reservationsToastText,
    wishToastText,
    isLoaded: 1,
    isStaffData: 0,
    pageSize: 10,
    pageNo: 1,
    isResult: false,
    isAll: 0,
    isDown: false,
    isShow: 0,
    list:[],
    itemIndex: 0,
    items: ['按周阅读量排序', '按借阅热度排序', '按借阅量TOP10排序'],
    content: '',
    isReturn: 0,
    nodataArray: {
      text: "搜索无结果"
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    options.vipId = getGlobalVal('vipId', `vipId_${getGlobalVal('userId')}`)
    options.libId = getGlobalVal('list').length > 0 ? getGlobalVal('list')[0].libId : ''
    this.setData(options)
  },

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

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // this.getData(res => {
    //   this.setData({ isLoaded: 1 })
    // })
  },

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

  },

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

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

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    const { isAll, options } = this.data
    const { type = 1 } = options
    if (type == 1 || isAll) return
    this.getData()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    if (isFn(sharePage)) return sharePage()
  },
  /**
  * 选择跳转进入页
  */
  NavigateToInfo: function (e) {
    const { index } = e.currentTarget.dataset
    navigateTo({
      url: 'president/pages/Personnel_edit/Personnel_edit',
    })
  },
  /**
   * 获取搜索数据
   */
  /**
 * 获取书籍数据
 */
  getData: function (cb) {
    const continuousFn = { fn: this.getData, param: { ...arguments } }
    const { pageNo, pageSize, list, libId, childName, content } = this.data
    AgencyVipList({ data: { libId, pageNo, pageSize, childName: content }, continuousFn }).then(res => {
      const { list } = res.data
      let listTemp = []
      if (pageNo == 1) {
        listTemp = [].concat(list)
      } else {
        listTemp = [].concat(this.data.list, list)
      }
      const correctListTemp = (res => {
        console.log(res)
        var d = []
        for (let i in res) {
          res[i].number = 0
        }
        return res
      })(listTemp)
      this.setData({ list: correctListTemp, pageNo: list.length == pageSize ? pageNo + 1 : pageNo, isAll: list.length < pageSize })
      cb && cb(res)
    }).catch(res => {
      cb && cb(res)
    })
  },
  /**
   * 输入搜索值
   */
  inputSearchCtl: function (e) {
    const { value } = e.detail
    const { content } = this.data
    if (content == value) {
      return
    }
    this.setData({ content: value, pageNo: 1})
    this.setData({ isResult: true })
    this.getData()
  },

  /**
   * 清除搜索值
   */
  clearSearchCtl: function () {
    this.setData({ content: '' })
  },

  /**
   * 提交搜索值
   */
  submitSearchCtl: function () {
    this.setData({ isReturn: 1, pageNo: 1 })
    this.getData()
  },
 




})