// parents/pages/wish/wish.js
import { routers, viewImage, sharePage, isFn, getGlobalVal } from '../../../utils/util.js'
import { MyWishBook, OthersWishBook, WishHelp } from '../../../utils/api.js'
const { navigateTo, redirectTo, navigateBack } = routers()
const { globalData, hasLibraryService, hasVipService } = getApp()
const { baseImgUrl, qrcodeInvalidToastText } = globalData

Page({

  /**
   * 页面的初始数据
   */
  data: {
    baseImgUrl,
    isLoaded: 0,
    isShowWishModal: 0,
    navIndex: 0,
    navList: [
      {
        text: '我的心愿',
        num: 0,
        pageNo: 1,
        pageSize: 10,
        isAll: 0,
        list: [],
        nodataArray: {
          text: '暂没有心愿书单',
          image: '../../../assets/nodata_4.png',
          hasNodataBtn: 1,
          btnText: '去书库看看'
        }
      },
      {
        text: '他们的心愿',
        num: 0,
        pageNo: 1,
        pageSize: 10,
        isAll: 0,
        list: [],
        nodataArray: {
          text: '暂没有心愿书单',
          image: '../../../assets/nodata_5.png',
          hasNodataBtn: 1,
          btnText: '去书库看看'
        }
      }
    ],
    hotBooks: [
      // {
      //   imgUrl: '',
      //   bookName: '绝地反击是绝地反击是绝地反击是绝地反击是绝地反击是',
      //   hotNum: 756656565656568
      // },
      // {
      //   imgUrl: '',
      //   bookName: '绝地反击是',
      //   hotNum: 78
      // },
      // {
      //   imgUrl: '',
      //   bookName: '绝地反击是',
      //   hotNum: 78
      // },
      // {
      //   imgUrl: '',
      //   bookName: '绝地反击是',
      //   hotNum: 78
      // },
      // {
      //   imgUrl: '',
      //   bookName: '绝地反击是',
      //   hotNum: 78565656565656568
      // },
      // {
      //   imgUrl: '',
      //   bookName: '绝地反击是',
      //   hotNum: 78
      // },
      // {
      //   imgUrl: '',
      //   bookName: '绝地反击是',
      //   hotNum: 78
      // }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    options.vipId = getGlobalVal('vipId', `vipId_${getGlobalVal('userId')}`) || ''
    options.libId = getGlobalVal('library').id || ''
    this.setData({ options })
    this.getData(res => {
      this.setData({ isLoaded: 1 })
    })

    const isShowWishModal = getGlobalVal('wishModal')
    if (!isShowWishModal) {
      this.setData({ isShowWishModal: 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 () {
    const { navList, navIndex } = this.data
    navList[navIndex]['pageNo'] = 1
    this.getData(res => {
      wx.stopPullDownRefresh()
    })
  },

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

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    const { index } = e.target.dataset
    const { navList, navIndex, options } = this.data
    const { vipId, libId } = options
    const { isbn13 } = navList[navIndex]['list'][index]
    const { child_name = '' } = getGlobalVal('vip', `vip_${getGlobalVal('userId')}`)
    return {
      title: `${child_name}的心愿书单详情`,
      path: `parents/pages/wish_detail/wish_detail?type=10&vipId=${vipId}&isbn13=${isbn13}&libId=${libId}`,
      success: res => {
        wx.showToast({
          title: '转发成功',
          icon: 'success'
        })
      }
    }
  },

  /**
   * 获取数据
   */
  getData: function (cb) {
    const continuousFn = { fn: this.getData, param: { ...arguments } }
    const { navList, navIndex, options } = this.data
    const { pageNo, pageSize, list } = navList[navIndex]
    const { vipId, libId } = options
    const Fn = [MyWishBook, OthersWishBook]
    const data = [{ vipId, libId, pageNo, pageSize }, { libId, pageNo, pageSize }]
    if (navIndex == 0 && !hasVipService()) {
      cb && cb()
      return
    }
    if (!hasLibraryService()) {
      cb && cb()
      return
    }
    Fn[navIndex]({ data: data[navIndex], continuousFn }).then(res => {
      const newList = res.data.list
      let listTemp = []
      if (pageNo == 1) {
        listTemp = [].concat(newList)
      } else {
        listTemp = [].concat(list, newList)
      }
      navList[navIndex]['list'] = listTemp
      navList[navIndex]['pageNo'] = newList.length == pageSize ? pageNo + 1 : pageNo
      navList[navIndex]['isAll'] = newList.length < pageSize
      this.setData({ navList })
      cb && cb(res)
    }).catch(res => {
      cb && cb(res)
    })
  },

  /**
   * 选择导航栏
   */
  selectNavCtl: function (e) {
    const { index } = e.currentTarget.dataset
    const { navIndex, navList } = this.data
    if (navIndex == index) return
    this.setData({ navIndex: index })
    const { list } = navList[index]
    if (list.length == 0) {
      this.getData()
    }
  },

  /**
   * 切换心愿弹框
   */
  toggleWishModal: function () {
    this.setData({ isShowWishModal: 0 })
    wx.setStorageSync('wishModal', 1)
  },

  /**
   * 助力
   */
  helpWishCtl: function (e) {
    const continuousFn = { fn: this.helpWishCtl, param: { ...arguments } }
    if (!hasVipService() || !hasLibraryService()) return
    const { index } = e.currentTarget.dataset
    const { navIndex, navList, options } = this.data
    const { vipId, libId } = options
    const { list } = navList[navIndex]
    const { isbn13 } = list[index]
    WishHelp({ data: { vipId, isbn13 }, continuousFn }).then(res => {
      wx.showToast({
        title: '助力成功',
        icon: 'none'
      })
    })
  },

  /**
   * 没有数据模板按钮触发事件
   */
  nodataBtnCtl: function () {
    redirectTo({ url: 'parents/pages/home/home', zindex: 3 })
  }
})