// parents/pages/information/information.js
import { routers, viewImage, sharePage, isFn, getGlobalVal } from '../../../utils/util.js'
import { CheckBookCard, AddBook, UploadFile,RemoveBookLib,BackBookLib} from '../../../utils/api.js'
const { navigateTo, redirectTo, navigateBack } = routers()
const { globalData, hasLibraryService, hasVipService } = getApp()
const { baseImgUrl, qrcodeInvalidToastText } = globalData
Page({

  /**
   * 页面的初始数据
   */
  data: {
    baseImgUrl,
    isLoaded: 1,
    hiddenModel: false,
    deleteModel: false,
    bookInfo: {
      author: "",
      themeName:'',
      isbn13: "",
      localImage: "",
      libName:'',
      borrowName:'',
      price: "",
      grade: "",
      summary: "",
      title: ""
    },
    haveReturn:false,
    value:[2,1],
    isDetail:false,
    user_vipId:"",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    options.userId = getGlobalVal('userId')
    options.libId = (options.libId ? options.libId : getGlobalVal(`dispatchLibId_${options.userId}`) ? getGlobalVal(`dispatchLibId_${options.userId}`) : '')
    this.setData({ options })
    const bookInfo = wx.getStorageSync("bookInfo")
    const detailsId = wx.getStorageSync("detailsId")
    this.setData({
      bookInfo: bookInfo,
      detailsId: detailsId
    });
    // options.vipId = getGlobalVal('vipId', `vipId_${getGlobalVal('userId')}`) || ''
    // 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 () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  /**
   * 提示框隐藏
   */
  allShowModel: function () {
    this.setData({ hiddenModel: false, deleteModel: false })
  },
  /**
   * 确认归还提示框打开
   */
  showModel: function () {
      const { haveReturn } = this.data
      if(haveReturn){
          wx.showToast({
          title: '本书已经归还',
        })
      }
  else{
      this.setData({ hiddenModel: true })
  }
  },
  /**
   * 解绑框打开
   */
  sureDelete: function () {
    this.setData({ deleteModel: true })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    if (isFn(sharePage)) return sharePage()
  },

  /**
   * 获取个人资料
   */
  getData: function (cb) {
    const continuousFn = { fn: this.getData, param: { ...arguments } }
    const { user_vipId } = this.data
    VipInfo({ data: { vipId: user_vipId }, continuousFn }).then(res => {
      this.setData({ signList: { ...res.data } })
      cb && cb(res)
    }).catch(res => {
      cb && cb(res)
    })
  },

  /**
   * 提交按钮
   */         
  operationCtl: function (e) {
    const continuousFn = { fn: this.submitCtl, param: { ...arguments } }
    const { type } = e.currentTarget.dataset
    const { detailsId } = this.data
    if (type == 1) {
      RemoveBookLib({ data: {detailId:detailsId}, continuousFn }).then(res => {
        wx.showToast({
          title: '解绑成功',
        })
          this.allShowModel()
        setTimeout(() => {
          navigateBack()
        }, 2000)

      })
    } else if (type == 2) {
      BackBookLib({ data: {detailId:detailsId}, continuousFn }).then(res => {
        wx.showToast({
          title: '归还成功',
        })
      this.setData({
        haveReturn: true,
      });
        
      this.allShowModel()
      })
    }
  },

})