const app = getApp(),
  {
    globalData: {
      util: {
        regeneratorRuntime,
        api: {
          server,
          getUser,
          updateUser
        },
        showTip,
        request: {
          post_auth,
          get
        },
        getUserInfo,
        goSuccess,
        pickerChange,
        verify: {
          isPhone,
          isEmail,
          form_verify
        }
      }
    }
  } = app,
  list = [
    [{
      title: '用户姓名',
      field: 'userName',
      maxLength: 20
    },
    {
      title: '证件类型',
      field: 'pDocumentType',
      picker: true
    },
    {
      title: '证件号码',
      field: 'pDocumentNum',
      // maxLength: 18 //因为证件类型不确定
    },
    {
      title: '手机号码',
      field: 'pPhone',
      maxLength: 11,
      fn: x => x ? !isPhone(x) && '您输入的电话格式不正确' : '请输入手机号码'
    },
    {
      title: '电子邮箱',
      field: 'pEmail',
      ignore: true,
      fn: x => x && !isEmail(x) && '您输入的电子邮箱格式不正确'
    },
    {
      title: '用户地址',
      field: 'pAddress',
      ignore: true,
      textarea: true,
      fn: () => false
    },

    ],
    [{
      title: '公司名称',
      field: 'lInstitutionName',
      maxLength: 30
    }, {
      title: '公司证件类型',
      field: 'lInstitutionType',
      picker: true
    }, {
      title: '公司证件号码',
      field: 'lInstitutionNum',
      maxLength: 20
    }, {
      title: '法人姓名',
      field: 'lUsername',
      maxLength: 20
    }, {
      title: '法人证件类型',
      field: 'lDocumentType',
      picker: true
    }, {
      title: '法人证件号码',
      field: 'lDocumentNum',
      // maxLength: 18
    }, {
      title: '联系方式',
      field: 'lPhone',
      maxLength: 11,
      fn: x => x ? !isPhone(x) && '您输入的电话格式不正确' : '请输入手机号码'
    }, {
      title: '电子邮箱',
      field: 'lEmail',
      ignore: true,
      fn: x => x && !isEmail(x) && '您输入的电子邮箱格式不正确'
    }, {
      title: '联系地址',
      field: 'lAddress',
      ignore: true,
      textarea: true,
      fn: () => false
    }]
  ]
Page({
  async onLoad() {
    wx.showLoading({ mask: true, title: '信息获取中...' })
    let userInfo = await getUserInfo(),
      codeList = await get(server, {
        code: 'CARDTYPE'
      }),
      items = list[userInfo.identity | 0]

    if (codeList._err) codeList = []
    codeList = codeList.map(({
      dictValueCode: id,
      dictValueName: name
    }) => ({
      id,
      name
    }))
    items.filter(x => {
      x.value = userInfo[x.field]
      return x.picker
    }).forEach(x => {
      x.picker_index = Math.max(codeList.map(({ id }) => id).indexOf(userInfo[x.field]), 0)
      x.picker_name = codeList[x.picker_index].name
      x.value = codeList[x.picker_index].id
    })
    this.setData({
      codeList,
      items,
      userId: app.globalData.userId,
      verify: Object.fromEntries(items.map(({ title, field, fn }) => [field, fn || `请输入${title}`]))
    })
    wx.hideLoading()
  },
  _pickerChange(index, value) {
    let { data: { items, codeList } } = this,
      item = items[index],
      { id, name } = codeList[value]

    item.value = id
    item.picker_index = value
    item.picker_name = name
    this.setData({
      [`items[${index}]`]: item
    })
  },
  async formSubmit({
    detail: {
      value: userInfo
    }
  }) {
    const msg = form_verify(userInfo, this.data.verify)
    if (msg) return showTip(msg)
    const { _err } = await post_auth(updateUser, { userInfo }, { loading: true, loadingMsg: '信息提交中。。。' });
    if (_err) return _err.status && showTip('更新信息失败');
    app.globalData.userInfo = null
    goSuccess('userinfo')
  },
  pickerChange
})