// dispatcher/pages/withdraw_detail/withdraw_detail.js
import { routers, viewImage, sharePage, isFn, getGlobalVal } from '../../../utils/util.js'
import { GetWithdrawLibraryDetail, ComfirmWithdrawLibrary } from '../../../utils/api.js'
const { navigateTo, redirectTo, navigateBack } = routers()
const { globalData } = getApp()
const { baseImgUrl } = globalData
Page({

  /**
   * 页面的初始数据
   */
  data: {
    baseImgUrl,
    isLoaded: false,
    league_name: '',
    league_address: '',
    count_total: '',
    in_book: '',
    out_book: '',
    itemIndex: 0,
    items: [
      {
        icon: '../../../assets/dispatch_icon_1_gray.png',
        actIcon: '../../../assets/dispatch_icon_1_blue.png',
        text: '开始撤馆'
      },
      {
        icon: '../../../assets/dispatch_icon_2_gray.png',
        actIcon: '../../../assets/dispatch_icon_2_blue.png',
        text: '书籍回收'
      },
      {
        icon: '../../../assets/dispatch_icon_3_gray.png',
        actIcon: '../../../assets/dispatch_icon_3_blue.png',
        text: '盘点归库'
      },
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({ options })
    this.getData()
  }, 

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  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 () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    if (isFn(sharePage)) return sharePage()
  },

  /**
   * 获取图书馆详情
   */
  getData: function (cb) {
    const continuousFn = { fn: this.getData, param: { ...arguments } }
    const { libId } = this.data.options
    GetWithdrawLibraryDetail({ data: { libId }, continuousFn }).then(res => {
      this.setData({ ...res.data, isLoaded: true })
      if (isFn(cb)) cb()
    }).catch(res => {
      this.setData({ isLoaded: true })
      if (isFn(cb)) cb()
    })
  },

  /**
   * 确定撤馆
   */
  submitCtl: function () {
    const continuousFn = { fn: this.getData, param: { ...arguments } }
    const { libId, type } = this.data.options
    ComfirmWithdrawLibrary({ data: { libId }, continuousFn }).then(res => {
      wx.showModal({
        title: '确定撤馆?',
        content: '一旦确定撤馆将无法取消,同时该馆无完成撤销,无法选择其他馆。请谨慎选择!',
        success: res => {
          if (res.confirm) {
            redirectTo({ url: 'dispatcher/pages/withdraw/withdraw', paras: { libId, type }, zindex: 3 })
          }
        }
      })
    })
  },
})