const api = require('../../config/api')
const util = require('../../utils/util')
const check = require('../../utils/check')

Page({
	data: {
		id: 0,
		nickname: ''
	},

	onLoad() {
		const { id, nickname } = wx.getStorageSync('userInfo')
		this.setData({ id, nickname: nickname ? nickname : '' })
	},

	nameInput({ detail }) {
    this.setData({ nickname: detail })
	},

	// 修改昵称
	submit() {
		const { id, nickname } = this.data
		if (!check.validNickName(nickname)) return util.showToast('请输入4-20个字符')

		util.showLoad('加载中')
		util.request(api.UserUpdate, { id, nickname }, 'POST').then(res => {
			util.hideLoad()
			if (res.errno === 0) wx.navigateBack()
			else util.showErrorToast(res.errmsg)
		}).catch(() => {
			util.hideLoad()
			util.showErrorToast('网络连接失败')
		})
	}
})