ApprovalModal.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. import React, { useState, useEffect } from 'react';
  2. import { Form, Select, Modal, Input, TreeSelect } from 'antd';
  3. import moment from 'moment';
  4. import provinces from './provinces';
  5. import { queryApproval } from '@/services/approval';
  6. const { Option } = Select;
  7. const { TreeNode } = TreeSelect;
  8. // 新建
  9. function AddModal(props) {
  10. const {
  11. total,
  12. dispatch,
  13. visible,
  14. onClose,
  15. onOk,
  16. data,
  17. currentUser,
  18. depUserTree,
  19. flowList = [],
  20. industryList = [],
  21. typeList = [],
  22. disabled,
  23. loading,
  24. } = props;
  25. const [form] = Form.useForm();
  26. const [codes, setCodes] = useState({
  27. type: '',
  28. industry: '',
  29. location: '',
  30. name: '',
  31. version: '',
  32. });
  33. const [type, setType] = useState({});
  34. const handleOk = () => {
  35. form.validateFields().then(fieldsValue => {
  36. let values = { ...fieldsValue, id: data.id };
  37. values.project_name = fieldsValue.project_name;
  38. values.flow_id = Number(fieldsValue.flow_id);
  39. values.type_id = Number(fieldsValue.type_id);
  40. // 获得flow下第一个node的id
  41. values.node_id = flowList.find(item => item.id == values.flow_id).Nodes[0].id;
  42. //项目分类为不为研发时
  43. if (fieldsValue.type_id != 7) {
  44. values.industry_id = Number(fieldsValue.industry_id);
  45. let [location, location_code] = fieldsValue.location.split('##');
  46. values.location = location;
  47. values.location_code = location_code;
  48. values.project_full_code = `${codes.type}${codes.industry}${codes.location}${codes.name}${codes.version}`;
  49. }
  50. if (fieldsValue.author) {
  51. values.author = Number(fieldsValue.author.split('||')[0]);
  52. values.author_dep_id = Number(fieldsValue.author.split('||')[1]);
  53. } else {
  54. values.author = null;
  55. }
  56. onOk(values);
  57. });
  58. };
  59. const renderTreeNodes = data => {
  60. return data.map(item => {
  61. let title = item.name;
  62. let code = item.code || '';
  63. if (code.length == 4) {
  64. code = code.substr(1);
  65. }
  66. if (code) {
  67. title += `(${code})`;
  68. }
  69. let key = `${item.name}##${code}`;
  70. return (
  71. <TreeNode title={title} key={key} value={key} dataRef={item} selectable={Boolean(code)}>
  72. {item.children && renderTreeNodes(item.children || [])}
  73. </TreeNode>
  74. );
  75. });
  76. };
  77. const changeType = id => {
  78. const item = typeList.find(item => item.id == id);
  79. setCodes({
  80. ...codes,
  81. type: item.code,
  82. });
  83. setType(item);
  84. form.setFieldsValue({ flow_id: id == 7 ? '4' : '1' });
  85. };
  86. const changeIndustry = id => {
  87. const item = industryList.find(item => item.id == id);
  88. setCodes({
  89. ...codes,
  90. industry: item.code,
  91. });
  92. };
  93. const changeVersion = index => {
  94. setCodes({
  95. ...codes,
  96. version: index,
  97. });
  98. };
  99. const changeLocation = value => {
  100. const [location, code] = value.split('##');
  101. setCodes({
  102. ...codes,
  103. location: code,
  104. });
  105. };
  106. const onBlurName = e => {
  107. let value = e.target.value.toUpperCase();
  108. while (value.length < 3) {
  109. value = value + 'V';
  110. }
  111. form.setFieldsValue({
  112. name: value,
  113. });
  114. setCodes({
  115. ...codes,
  116. name: value,
  117. });
  118. };
  119. const renderDetail = () => {
  120. return (
  121. <>
  122. <Form.Item
  123. label="行业名称"
  124. name="industry_id"
  125. initialValue={String(data.industry_id || '')}
  126. rules={[{ required: true, message: '请选择行业名称' }]}
  127. >
  128. <Select style={{ width: '100%' }} onChange={changeIndustry}>
  129. {industryList.map(item => (
  130. <Option key={item.id}>
  131. {item.name}({item.code})
  132. </Option>
  133. ))}
  134. </Select>
  135. </Form.Item>
  136. <Form.Item
  137. label="项目地区"
  138. name="location"
  139. initialValue={data.location}
  140. rules={[{ required: true, message: '请选择项目地区' }]}
  141. >
  142. <TreeSelect
  143. dropdownStyle={{ maxHeight: 300, overflow: 'auto' }}
  144. onChange={changeLocation}
  145. >
  146. {renderTreeNodes(provinces)}
  147. </TreeSelect>
  148. </Form.Item>
  149. <Form.Item
  150. label="项目简称"
  151. name="name"
  152. initialValue={data.name}
  153. rules={[
  154. { required: true, message: '请输入项目简称' },
  155. {
  156. validator: (rule, value, callback) => {
  157. if (value.match(/[^A-Za-z]/g)) callback('项目简称只能是英文字符');
  158. else callback();
  159. },
  160. },
  161. ]}
  162. >
  163. <Input maxLength={3} onBlur={onBlurName} />
  164. </Form.Item>
  165. <Form.Item
  166. label="项目期数"
  167. name="version"
  168. initialValue={data.version}
  169. rules={[{ required: true, message: '请选择项目期数' }]}
  170. >
  171. <Select style={{ width: '100%' }} onChange={changeVersion}>
  172. {['一期', '二期', '三期', '四期', '五期'].map((item, index) => (
  173. <Option key={index + 1}>{item}</Option>
  174. ))}
  175. </Select>
  176. </Form.Item>
  177. <Form.Item label="项目编号">
  178. {codes.type || '***'}-{codes.industry || '***'}-{codes.location || '***'}-
  179. {codes.name || '***'}-{codes.version || '*'}
  180. </Form.Item>
  181. </>
  182. );
  183. };
  184. // const renderCode = () => {
  185. // let project_full_code = '';
  186. // if (total < 10) {
  187. // project_full_code = '00' + total;
  188. // } else if (total < 100) project_full_code = '0' + total;
  189. // else project_full_code = total;
  190. // project_full_code = `RD${moment().format('YYYYMM')}${project_full_code}`;
  191. // return <Form.Item label="项目编号">{project_full_code}</Form.Item>;
  192. // };
  193. useEffect(() => {
  194. if (data.id) {
  195. const type = typeList.find(item => item.id == data.type_id);
  196. const industry = industryList.find(item => item.id == data.industry_id);
  197. setCodes({
  198. type: type?.code,
  199. industry: industry?.code,
  200. location: data.location_code,
  201. name: data.name,
  202. version: data.version,
  203. });
  204. setType(type);
  205. } else {
  206. setCodes({
  207. type: '',
  208. industry: '',
  209. location: '',
  210. name: '',
  211. version: '',
  212. });
  213. setType({});
  214. }
  215. }, [data, visible]);
  216. return (
  217. <Modal
  218. title="项目立项"
  219. confirmLoading={loading}
  220. maskClosable={false}
  221. destroyOnClose
  222. visible={visible}
  223. onCancel={onClose}
  224. onOk={handleOk}
  225. >
  226. <Form labelCol={{ span: 5 }} wrapperCol={{ span: 15 }} form={form}>
  227. <Form.Item
  228. label="项目名称"
  229. name="project_name"
  230. initialValue={String(data.project_name || '')}
  231. rules={[{ required: true, message: '请输入项目名称' }]}
  232. >
  233. <Input style={{ width: '100%' }} />
  234. </Form.Item>
  235. <Form.Item
  236. label="项目类别"
  237. name="type_id"
  238. initialValue={String(data.type_id || '')}
  239. rules={[{ required: true, message: '请选择项目类别' }]}
  240. >
  241. <Select style={{ width: '100%' }} onChange={changeType}>
  242. {typeList.map(item => (
  243. <Option key={item.id}>
  244. {item.name}({item.code})
  245. </Option>
  246. ))}
  247. </Select>
  248. </Form.Item>
  249. <Form.Item
  250. label="流程"
  251. name="flow_id"
  252. initialValue={String(data.flow_id || '')}
  253. rules={[{ required: true, message: '请选择流程' }]}
  254. >
  255. <Select style={{ width: '100%' }} disabled>
  256. {flowList
  257. .filter(item => item && item.id != 2 && item.id != 3)
  258. .map(item => (
  259. <Option key={item.id}>{item.name}</Option>
  260. ))}
  261. </Select>
  262. </Form.Item>
  263. {currentUser.IsSuper && (
  264. <Form.Item
  265. label="售前经理"
  266. name="author"
  267. initialValue={String(
  268. data.author && data.author_dep_id ? `${data.author}||${data.author_dep_id}` : ''
  269. )}
  270. >
  271. <TreeSelect
  272. showSearch
  273. allowClear
  274. style={{ width: '100%' }}
  275. multiple={false}
  276. filterTreeNode={(input, option) => {
  277. return option.props.title === input;
  278. }}
  279. treeData={depUserTree}
  280. />
  281. </Form.Item>
  282. )}
  283. {type?.id != 7 && renderDetail()}
  284. </Form>
  285. </Modal>
  286. );
  287. }
  288. export default AddModal;