const app = getApp()

Page({
  data: {
    user: {},
    formData: {
        name: '',
        phone: '',
        age: '',
        education: '',
        address: ''
    }
  },
  onLoad() {
      this.fetchUser();
  },
  fetchUser() {
    const { request } = require('../../utils/request')
    request({ url: '/user' })
      .then((data) => {
        this.setData({ user: data })
      })
      .catch(() => {
        this.setData({
          user: {
            id: '8839201',
            name: '学员用户',
            avatar:
              'https://images.unsplash.com/photo-1535713875002-d1d0cf377fde?w=200&auto=format&fit=crop&q=60',
            stats: {
              learning: 12,
              coupons: 3,
              hours: 28
            }
          }
        })
      })
  },
  handleInput(e) {
      const field = e.currentTarget.dataset.field;
      const value = e.detail.value;
      this.setData({
          [`formData.${field}`]: value
      });
  },
  handleSubmit() {
      console.log(this.data.formData);
      wx.showToast({
          title: '保存成功',
          icon: 'success'
      })
  const { request } = require('../../utils/request')
  request({ url: '/user', method: 'POST', data: this.data.formData })
    .then((data) => {
      console.log(data)
    })
  }
})
