index.jsx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. import React, { useState, useEffect } from 'react'
  2. import { Form, Row, Col, Button, Modal } from 'antd'
  3. import LazyCascader from '@/components/LazyCascader'
  4. import { ProFormSelect, ProFormText, ProFormTextArea } from '@ant-design/pro-form'
  5. import { useDispatch, connect } from 'umi'
  6. import { ChangeCompMap } from '@/pages/Customer/Company/components/ChangeCompany'
  7. import { isNullOrUnDef } from '@/utils/is'
  8. import TelephoneFormItem from './TelephoneFormItem'
  9. import { validateEnum } from '@/pages/Customer/Company/components/AddCompany/CompanyFormItem'
  10. const AddClient = props => {
  11. const {
  12. visible,
  13. onCancel,
  14. reload,
  15. onConfirm,
  16. dataId,
  17. dataType,
  18. defaultValues,
  19. sourceList,
  20. ...resetModalProps
  21. } = props
  22. const dispatch = useDispatch()
  23. const [formRef] = Form.useForm()
  24. const layout = {
  25. layout: 'horizontal',
  26. labelCol: { flex: '100px' }
  27. }
  28. const [state, setState] = useState({
  29. validStatusFtl: '',
  30. confirmLoading: false,
  31. extraConfirmLoading: false
  32. })
  33. useEffect(() => {
  34. if (visible && !sourceList?.length) {
  35. dispatch({
  36. type: 'client/fetchSourceList'
  37. })
  38. }
  39. }, [visible])
  40. const handleChange = changedValues => {
  41. if (!changedValues) return
  42. if ('clientName' in changedValues) {
  43. if (!changedValues.clientName || changedValues.clientName?.length === 1) {
  44. formRef?.setFieldsValue({ niceName: '' })
  45. return
  46. }
  47. const [lastName, ...firstName] = changedValues.clientName.split('')
  48. const newClientName = `${lastName}(${firstName.join('')})工:`
  49. formRef?.setFieldsValue({ niceName: newClientName })
  50. }
  51. if ('qq' in changedValues) {
  52. if (!changedValues.qq) {
  53. formRef?.setFieldsValue({ email: '' })
  54. return
  55. }
  56. formRef?.setFieldsValue({ email: `${changedValues.qq}@qq.com` })
  57. }
  58. }
  59. const handleOnOk = () => {
  60. formRef
  61. ?.validateFields()
  62. .then(async values => {
  63. setState({ ...state, [dataId ? 'extraConfirmLoading' : 'comfirmLoading']: true })
  64. try {
  65. const success = await onConfirm(values)
  66. if (success) {
  67. !isNullOrUnDef(reload) && reload()
  68. formRef?.resetFields()
  69. setTimeout(() => {
  70. setState({ ...state, validStatusFtl: '' })
  71. onCancel()
  72. }, 80)
  73. }
  74. } finally {
  75. setState({ ...state, [dataId ? 'extraConfirmLoading' : 'comfirmLoading']: false })
  76. }
  77. })
  78. .catch(({ values }) => {
  79. if ('telephone' in values && !values.telephone) {
  80. setState({ ...state, validStatusFtl: validateEnum.Error })
  81. }
  82. })
  83. }
  84. return (
  85. <Modal
  86. {...resetModalProps}
  87. visible={visible}
  88. onCancel={onCancel}
  89. width="43vw"
  90. title="添加客户"
  91. getContainer={() => document.getElementById('root')}
  92. footer={
  93. <div>
  94. <Button type="default" className="mr-2" onClick={onCancel}>
  95. 取消
  96. </Button>
  97. <Button type="primary" onClick={handleOnOk} loading={state.confirmLoading}>
  98. 确认
  99. </Button>
  100. {dataId ? (
  101. <Button
  102. type="primary"
  103. className="ml-2"
  104. loading={state.extraConfirmLoading}
  105. onClick={() => {
  106. const values = {}
  107. if (dataType === ChangeCompMap.BUSINESS.key) {
  108. values.businessId = dataId
  109. }
  110. formRef?.setFieldsValue(values)
  111. handleOnOk()
  112. }}
  113. >
  114. 添加并关联
  115. </Button>
  116. ) : null}
  117. </div>
  118. }
  119. >
  120. <Form {...layout} form={formRef} onValuesChange={handleChange} initialValues={defaultValues}>
  121. {dataId ? <ProFormText name={`${dataType}Id`} hidden /> : null}
  122. <ProFormText
  123. name="clientName"
  124. label="姓名"
  125. placeholder="请输入客户名称"
  126. rules={[{ required: true, message: '请输入姓名' }]}
  127. />
  128. <Form.Item label="客户地区" name="districtIds" rules={[{ required: true, message: '请选择地区' }]}>
  129. <LazyCascader showFooter={true} />
  130. </Form.Item>
  131. <Row>
  132. <Col span={12}>
  133. <ProFormSelect
  134. name="gender"
  135. label="性别"
  136. placeholder=""
  137. rules={[{ message: '请选择性别' }]}
  138. options={[
  139. { label: '男', value: '男' },
  140. { label: '女', value: '女' }
  141. ]}
  142. />
  143. <ProFormText label="部门" name="department" placeholder="" />
  144. <Form.Item
  145. label="手机"
  146. name="telephone"
  147. validateStatus={state.validStatusFtl}
  148. hasFeedback
  149. rules={[{ required: true, message: '请输入手机号码' }]}
  150. >
  151. <TelephoneFormItem validateFn={status => setState({ ...state, validStatusFtl: status })} />
  152. </Form.Item>
  153. <ProFormText
  154. label="电话"
  155. name="phone"
  156. rules={[{ message: '请输入电话/座机号' }]}
  157. placeholder=""
  158. />
  159. </Col>
  160. <Col span={12}>
  161. <ProFormText label="昵称" name="niceName" placeholder="" />
  162. <ProFormText label="职位" name="position" placeholder="" />
  163. <ProFormText label="QQ" name="qq" placeholder="" />
  164. <ProFormText label="邮箱" name="email" placeholder="" />
  165. </Col>
  166. </Row>
  167. <Row>
  168. <Col span={12}>
  169. <ProFormText label="办公室" name="office" placeholder="" />
  170. </Col>
  171. <Col span={12}>
  172. <ProFormSelect
  173. name="sourceId"
  174. label="来源"
  175. placeholder=""
  176. rules={[{ message: '来源' }]}
  177. options={sourceList}
  178. />
  179. </Col>
  180. </Row>
  181. <Row>
  182. <Col span={12}>
  183. <ProFormText label="客户地址" name="address" placeholder="" />
  184. <ProFormText label="客户地标" name="landmarks" placeholder="" />
  185. </Col>
  186. <Col span={12}>
  187. <ProFormText label="客户乘车" name="ride" placeholder="" />
  188. <ProFormText label="客户住宿" name="stay" placeholder="" />
  189. </Col>
  190. </Row>
  191. <ProFormTextArea label="备注" name="mark" placeholder="" />
  192. </Form>
  193. </Modal>
  194. )
  195. }
  196. export default connect(({ client }) => ({
  197. sourceList: client.sourceList
  198. }))(AddClient)