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

  /**
   * 页面的初始数据
   */
  data: {
    baseImgUrl,
    isLoaded: 0,
    vip: {
      imgUrl: '',
      childName: '',
      sex: '',
      grade: '',
      school: '',
      parentName:'',
      phone:'',
      parentRole:''
    },
    user_vipId:"",
    gradeArray: ['一年级', '二年级', '三年级', '四年级', '五年级', '六年级', '七年级', '八年级', '九年级'],
    gradeIndex: 0,
    parentRoleArray: ['爸爸', '妈妈','爷爷','奶奶','外公','外婆','其他'],
    parentRoleIndex:0,
    sexArray: ['男', '女'],
    sexIndex: 0,
    footerArray: [
      {
        type: 1,
        text: '取消'
      },
      {
        type: 2,
        text: '保存'
      }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const { vip } = this.data
    if (options.zindex == 3) {
      vip.school = options.name || ''
      vip.sex = options.sex || ''
      vip.phone = options.phone || ''
      vip.parentName = options.parentName || ''
      vip.parentRole = options.parentRole || ''
      vip.grade = options.grade || ''
      vip.childName = options.childName || ''
      vip.imgUrl = options.imgUrl || ''
      this.setData({ isLoaded: 1 })
    }
    const user_vipId = wx.getStorageSync("vipId")
    this.setData({
      user_vipId: user_vipId
    });
    options.vipId = getGlobalVal('vipId', `vipId_${getGlobalVal('userId')}`) || ''
    wx.setNavigationBarTitle({
      title: `个人资料`,
    })
    this.setData({ options, vip })
    console.log(vip)
    if (options.zindex != 3) {
      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()
  },

  /**
   * 获取个人资料
   */
  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({ 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 })
  },
   /**
   * 改变角色
   */
  pickerRoleCtl: function (e) {
    console.log(e)
    const { value } = e.detail
    const { parentRoleArray, parentRoleIndex } = this.data
    this.setData({ 'vip.parentRole': parentRoleArray[~~value], parentRoleIndex: value })
    console.log(parentRoleArray[~~value])
  },
  /**
   * 提交按钮
   */
  submitCtl: function (e) {
    const continuousFn = { fn: this.submitCtl, param: { ...arguments } }
    const { type } = e.currentTarget.dataset
    const { user_vipId, options} = this.data
    const { vip } = this.data
    vip.school = options.name || ''
    this.setData({ options, vip })
    console.log(vip)
    if (type == 1) {
      wx.navigateBack({
        delta: 1
      })
    } else if (type == 2) {
      if (!vip['childName']) {
        wx.showToast({
          title: '请填写完姓名',
          icon: 'none'
        })
        return
      }
      ReadingAaveVipInfo({ data: { ...vip, vipId: user_vipId  }, continuousFn }).then(res => {
        wx.showToast({
          title: '保存成功',
        })
        setTimeout(() => {
          navigateBack()
        }, 2000)
      })
    }
  }
})