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

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

	onLoad() {
		const { id, mobile } = wx.getStorageSync('userInfo')
		this.setData({ id, mobile: mobile && mobile.length == 11 ? mobile : '' })
	},

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

	submit() {
		const { id, mobile } = this.data
		if (!check.validPhone(mobile)) return util.showToast('请输入正确手机号')

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