index.jsx 6.1 KB

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