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

  /**
   * 页面的初始数据
   */
  data: {
    baseImgUrl,
    isLoaded: 0,
    hiddenModel: false,
    deleteModel: false,
    MembersVipId:'',
    vip: {
      imgUrl: '',
      name: '',
      sex: '',
      jobs:''
    },
    gradeArray: ['一年级', '二年级', '三年级', '四年级', '五年级', '六年级', '七年级', '八年级', '九年级'],
    gradeIndex: 0,
    sexArray: ['男', '女'],
    sexIndex: 0,
    positionArray: ['馆长助理', '馆长'],
    positionIndex:0,
    footerArray: [
      {
        type: 1,
        text: '取消'
      },
      {
        type: 2,
        text: '保存'
      }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      MembersVipId: options.vipId
    });
    wx.setNavigationBarTitle({
      title: `个人资料`,
    })
    options.vipId = getGlobalVal('vipId', `vipId_${getGlobalVal('userId')}`) || ''
    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 () {

  },

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

  },

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

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

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

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    if (isFn(sharePage)) return sharePage()
  },
  /**
   * 提示框隐藏
   */
  allShowModel: function () {
    this.setData({ hiddenModel: false, deleteModel: false })
  },
  /**
   * 提示框打开
   */
  showModel: function () {
    this.setData({ hiddenModel: true })
  },
  /**
   * 删除打开
   */
  sureDelete: function (e) {
    const { index } = e.currentTarget.dataset
    this.setData({ deleteModel: true, deleteIndex: index })
  },
  /**
   * 获取个人资料
   */
  getData: function (cb) {
    const continuousFn = { fn: this.getData, param: { ...arguments } }
    const { libId, month, MembersVipId } = this.data
    ManagerEdit({ data: { id:MembersVipId }, continuousFn }).then(res => {
      this.setData({ vip: { ...res.data } })
      cb && cb(res)
    }).catch(res => {
      cb && cb(res)
    })
  },

  /**
   * 上传头像
   */
  uploadImgCtl: function () {
    const continuousFn = { fn: this.uploadImgCtl, param: { ...arguments } }
    wx.chooseImage({
      count: 1,
      success: res => {
        UploadFile({ file: res.tempFilePaths, continuousFn }).then(res => {
          this.setData({ 'vip.imgUrl': res[0] })
        })
      }
    })
  },

  /**
   * 保存输入值
   */
  inputCtl: function (e) {
    const { value } = e.detail
    const { type } = e.currentTarget.dataset
    const { vip } = this.data
    vip[type] = value
    this.setData({ vip })
  },

  /**
   * 改变年级
   */
  pickerGradeCtl: function (e) {
    const { value } = e.detail
    const { gradeArray, gradeIndex } = this.data
    if (value == gradeIndex) {
      return
    }
    this.setData({ 'vip.grade': gradeArray[value], gradeIndex: value })
  },

  /**
   * 改变性别
   */
  pickerSexCtl: function (e) {
    const { value } = e.detail
    const { sexArray, sexIndex } = this.data
    this.setData({ 'vip.sex': ~~value + 1, sexIndex: value })
  },
  /**
   * 改变馆内职务
   */
  pickerpositionCtl: function (e) {
    const { value } = e.detail
    const { positionArray, positionIndex } = this.data
    this.setData({ 'vip.jobs': ~~value + 1, positionIndex: value })
  },

  /**
   * 提交按钮
   */
  submitCtl: function (e) {
    const continuousFn = { fn: this.submitCtl, param: { ...arguments } }
    const { type } = e.currentTarget.dataset
    const { vip, libId, MembersVipId} = this.data
    if (type == 1) {
      wx.navigateBack({
        delta: 1
      })
    } else if (type == 2) {
      if (!vip['name']) {
        wx.showToast({
          title: '请填写完姓名',
          icon: 'none'
        })
        return
      }
      ManagerSave({ data: { ...vip, libId, id: MembersVipId}, continuousFn }).then(res => {
        wx.showToast({
          title: '保存成功',
        })
        setTimeout(() => {
          navigateBack()
        }, 2000)
      })
    }
  },
    /**
   * 删除
   */
  delMembersVipIdCtl: function (e) {
    const continuousFn = { fn: this.submitCtl, param: { ...arguments } }
    const {  MembersVipId } = this.data
      ManagerDel({ data: { id: MembersVipId }, continuousFn }).then(res => {
        wx.showToast({
          title: '保存成功',
        })
        setTimeout(() => {
          navigateBack()
        }, 2000)
      })
  }
})