index.jsx 6.1 KB

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