const { request } = require('../../utils/request')

Page({
  data: {
    form: {
      username: '',
      password: '',
      nickname: '',
      company: '',
      title: '',
      phone: ''
    },
    loading: false
  },

  onInput(e) {
    const field = e.currentTarget.dataset.field
    this.setData({ [`form.${field}`]: (e.detail.value || '').trim() })
  },

  async submit() {
    if (this.data.loading) return
    const { username, password } = this.data.form
    if (!username || !password) {
      wx.showToast({ title: '用户名和密码必填', icon: 'none' })
      return
    }

    this.setData({ loading: true })
    try {
      await request({ url: '/auth/register', method: 'POST', data: this.data.form })
      wx.showToast({ title: '注册成功', icon: 'success' })
      setTimeout(() => wx.navigateBack(), 350)
    } finally {
      this.setData({ loading: false })
    }
  }
})
