123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. import { Form, Input, Card, Select, message, Button } from 'antd';
  2. import { history } from 'umi';
  3. import ProCard from '@ant-design/pro-card';
  4. import { useState, useEffect } from 'react';
  5. import POI from '@/components/POI/POI';
  6. import { getRegionList } from '@/services/region';
  7. import { addCooperative, getCooperativeDetail, updateCooperative } from '@/services/cooperative';
  8. const { TextArea } = Input;
  9. const { Option } = Select;
  10. const formItemLayout = { labelCol: { span: 6 }, wrapperCol: { span: 14 } };
  11. const goBack = () => {
  12. history.goBack();
  13. };
  14. const FormItem = Form.Item;
  15. export default (props) => {
  16. const { location } = props;
  17. const { id } = location.query;
  18. const [form] = Form.useForm();
  19. const [listForm, setListForm] = useState({});
  20. const [loading, setLoading] = useState(false);
  21. const [newLocName, setLocName] = useState('');
  22. const [newAddress, setAddress] = useState('');
  23. const [regionList, setRegionList] = useState([]);
  24. const Submit = (data) => {
  25. console.log(data);
  26. setLoading(true);
  27. if (id) {
  28. updateCooperative(id, {
  29. ...listForm,
  30. ...data,
  31. lng: data.location.split(',')[0],
  32. lat: data.location.split(',')[1],
  33. })
  34. .then(() => {
  35. setLoading(false);
  36. message.success('数据更新成功');
  37. goBack();
  38. })
  39. .catch((err) => {
  40. setLoading(false);
  41. message.error(err.message || err);
  42. });
  43. } else {
  44. addCooperative({
  45. ...data,
  46. lng: data.location.split(',')[0],
  47. lat: data.location.split(',')[1],
  48. })
  49. .then((res) => {
  50. setLoading(false);
  51. message.success('数据保存成功');
  52. history.goBack();
  53. })
  54. .catch((err) => {
  55. setLoading(false);
  56. message.error(err.message || err);
  57. });
  58. }
  59. };
  60. useEffect(() => {
  61. getRegionList()
  62. .then((res) => {
  63. setRegionList(res.records || []);
  64. })
  65. .catch((err) => {});
  66. }, []);
  67. useEffect(() => {
  68. if (id) {
  69. getCooperativeDetail(id)
  70. .then((res) => {
  71. setListForm(res || []);
  72. form.setFieldsValue({ ...res, location: res.lng + ',' + res.lat });
  73. })
  74. .catch((err) => {
  75. message.error(err.message || err);
  76. });
  77. }
  78. }, [id]);
  79. return (
  80. <Card>
  81. <ProCard tabs={{ type: 'card' }}>
  82. <ProCard.TabPane key={1} tab="合作社编辑">
  83. <Form {...formItemLayout} onFinish={Submit} form={form}>
  84. <FormItem
  85. label="机构名"
  86. name="name"
  87. rules={[{ required: true, message: '请输入机构名称16个字符以内' }]}
  88. >
  89. <Input
  90. placeholder="请输入机构名称16个字符以内"
  91. maxLength="16"
  92. style={{ width: '350px' }}
  93. />
  94. </FormItem>
  95. <FormItem
  96. label="信用代码"
  97. name="creditCode"
  98. rules={[{ required: true, message: '请输入信用代码' }]}
  99. >
  100. <Input placeholder="请输入信用代码" style={{ width: '350px' }} />
  101. </FormItem>
  102. <FormItem
  103. label="联系人"
  104. name="contactPerson"
  105. rules={[{ required: true, message: '请输入联系人' }]}
  106. >
  107. <Input placeholder="请输入联系人" rows={4} style={{ width: '350px' }} />
  108. </FormItem>
  109. <FormItem
  110. label="联系方式"
  111. name="phone"
  112. rules={[{ required: true, message: '请输入联系方式' }]}
  113. >
  114. <Input placeholder="请输入联系方式" style={{ width: '350px' }} />
  115. </FormItem>
  116. <FormItem
  117. label="定位经纬"
  118. name="location"
  119. rules={[{ required: true, message: '请定位' }]}
  120. >
  121. <POI
  122. placeholder="定位"
  123. setLocName={setLocName}
  124. setAddress={setAddress}
  125. roomBoole={true}
  126. />
  127. </FormItem>
  128. <FormItem
  129. label="地址"
  130. name="address"
  131. rules={[{ required: true, message: '请输入地址' }]}
  132. >
  133. <TextArea placeholder="请输入地址" style={{ width: '350px' }} />
  134. </FormItem>
  135. <FormItem
  136. label="区域"
  137. name="regionId"
  138. rules={[{ required: true, message: '请输入区域' }]}
  139. >
  140. <Select placeholder="请选择区域" style={{ width: '350px' }}>
  141. {regionList.map((item) => (
  142. <Option value={item.regionId} key={item.regionId}>
  143. {item.name}
  144. </Option>
  145. ))}
  146. </Select>
  147. </FormItem>
  148. <FormItem label="身份" name="role" rules={[{ required: true, message: '请选择身份' }]}>
  149. <Select placeholder="请选择身份" style={{ width: '350px' }}>
  150. <Option value="合作社" key="合作社" />
  151. <Option value="个体户" key="个体户" />
  152. </Select>
  153. </FormItem>
  154. <FormItem label="员工数">{listForm?.workerNum || 0}</FormItem>
  155. <FormItem label="农机数">{listForm?.machineryNum || 0}</FormItem>
  156. <FormItem label="订单完成数">{listForm?.orderNum || 0}</FormItem>
  157. <FormItem label="评分">{listForm?.score || 0}</FormItem>
  158. <FormItem label=" " colon={false}>
  159. <Button type="default" onClick={() => goBack()}>
  160. 返回
  161. </Button>
  162. <Button
  163. type="primary"
  164. loading={loading}
  165. htmlType="submit"
  166. style={{ marginLeft: '4em' }}
  167. >
  168. 保存
  169. </Button>
  170. </FormItem>
  171. </Form>
  172. </ProCard.TabPane>
  173. </ProCard>
  174. </Card>
  175. );
  176. };