// pages/mine/setting/editName/index.js
import HTTP from "../../../../../requestFn/Api"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nick_name: "",
    userInfo: {}
  },

  onLoad(options) {
    const {
      nick_name
    } = options;
    this.setData({
      nick_name
    })
    const that = this;
    wx.getStorage({
      key: "userInfo",
      success(e) {
        that.setData({
          userInfo: e.data
        })
      },
      fail() {
        wx.showToast({
          title: '请先登录',
          icon:"none",
          success: () => {
            setTimeout(() => {
              wx.switchTab({
                url: '/page/tabBar/mine/index',
              })
            }, 1000)
          }
        })
      }
    })
  },
  getValue(e) {
    const that = this;
    that.setData({
      nick_name: e.detail.value
    })
  },
  editName() {
    const that = this;
    const {
      nick_name,
      userInfo
    } = that.data;
    HTTP.UsernickName({
      nick_name
    }).then(res => {
      if (res.code == 0) {
        wx.showToast({
          title: '修改成功',
          icon: 'success',
          duration: 1500,
          success() {
            wx.setStorage({
              data: {
                ...userInfo,
                nick_name
              },
              key: "userInfo"
            })
            setTimeout(() => {
              wx.navigateBack({
                delta: 1,
              })
            }, 1000)
          }
        })
      }
    })
  }
})