forked from huge/frontEnd/hugeOA

Mr Ke
2020-05-27 77a2a090ff1b9d1b03795d696954a28ba0e0871f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/* eslint-disable */
import React from 'react';
import { Card, Row, Col, Form, Input, Button, Spin, message } from 'antd';
import fetch from '../../api/request';
 
const { TextArea } = Input;
const FormItem = Form.Item;
 
class GroupDetail extends React.Component {
  constructor(props) {
    super(props)
    this.state = {
      spinning: false,
      id: '',
      parentId: '',
      dataSet: {},
      btnLoading: false,//保存按钮loading
    }
  }
 
  componentWillMount() {
  }
  componentDidMount() {
  }
 
  handleSubmit = (e) => { // 提交表单数据
    e.preventDefault();
    let parentId = '';
    if (this.props.params.currentNode.length == 0) {
      //没有选中节点 
      parentId = '';
    } else {
      // 判断是新增还是编辑
      if (this.props.params.id == '') {
        parentId = this.props.params.currentNode[0].id  //此为新增
      } else {
        parentId = this.props.params.currentNode[0].parentId  //此为编辑
      }
    }
    this.props.form.validateFields((err, values) => {
      if (err) return;
      this.setState({ btnLoading: true });
      fetch({
        url: `api/unit/save`,
        method: 'POST',
        data: {
          ...values,
          id: this.props.params.id,
          parentId
        }
      }).then(res => {
        this.setState({ btnLoading: false })
        if (res) {
          message.success("保存成功");
          this.props.saveFun(values);//触发父级保存响应函数,刷新页面数据
        }
      })
    })
  }
 
  render() {
    const { getFieldDecorator } = this.props.form;
    const formItemLayout = {
      labelCol: { xs: { span: 24 }, sm: { span: 5 }, },
      wrapperCol: { xs: { span: 24 }, sm: { span: 19 }, },
    };
 
    const { params } = this.props;
    const dataSet = this.props.dataSet || {};
    const { btnLoading } = this.state;
 
    return (
      <div>
        {
          this.props.edit == 'true' ?
            <div>
              <Spin spinning={this.props.loading}>
                <div>
                  <Form onSubmit={this.handleSubmit}>
                    <Card title={this.props.params.id ? '单位信息(编辑)' : '单位信息(新增)'} bordered={false}>
                      {
                        this.props.params.currentNode.length > 0 && Object.keys(dataSet).length == 0 &&
                        <Row type="flex">
                          <Col span={16}>
                            <FormItem label={"上级单位"} {...formItemLayout}>
                              <span>{params.currentNode[0].name}</span>
                            </FormItem>
                          </Col>
                        </Row>
                      }
                      <Row type="flex">
                        <Col span={16}>
                          <FormItem label={"单位名称"} {...formItemLayout}>
                            {getFieldDecorator('name', {
                              rules: [{ required: true, message: '请输入单位名称' }],
                              initialValue: dataSet.name || '',
                            })(
                              <Input placeholder="请输入单位名称" />
                            )}</FormItem>
                        </Col>
                      </Row>
                      <Row type="flex">
                        <Col span={16}>
                          <FormItem label={"描述"} {...formItemLayout}>
                            {getFieldDecorator('description', {
                              rules: [{ max: 200, message: '描述的内容不能超过200字' }],
                              initialValue: dataSet.description || ''
                            })(
                              <TextArea placeholder="请输入描述" rows={5} />
                            )}</FormItem>
                        </Col>
                      </Row>
                      <Row type="flex" justify="center" gutter={20} style={{ width: '75%' }}>
                        <Col>
                          <Button onClick={this.props.cancelFun}>取消</Button>
                        </Col>
                        <Col>
                          <Button type="primary" htmlType="submit" loading={btnLoading}>保存</Button>
                        </Col>
                      </Row>
                    </Card>
                  </Form>
                </div>
              </Spin>
            </div>
            :
            <div>
              <Spin spinning={this.props.loading}>
                <div>
                  <Form onSubmit={this.props.editFun}>
 
                    <Card title={'单位信息'} bordered={false}>
                      <Row type="flex">
                        <Col span={16}>
                          <FormItem label={"单位名称"} {...formItemLayout} style={{ marginBottom: 10 }}>
                            <span>{dataSet.name || '暂无'}</span>
                          </FormItem>
                        </Col>
                      </Row>
                      <Row type="flex">
                        <Col span={16}>
                          <FormItem label={"描述"} {...formItemLayout} style={{ marginBottom: 10 }}>
                            {dataSet.description || '暂无'}
                          </FormItem>
                        </Col>
                      </Row>
                      <Row type='flex' gutter={20} justify="center" style={{ width: '75%' }}>
                        <Col>
                          <Button onClick={this.props.cancelData}>取消</Button>
                        </Col>
                        <Col>
                          <Button className="button-do" htmlType="submit" type="primary">编辑</Button>
                        </Col>
                      </Row>
                    </Card>
                  </Form>
                </div>
              </Spin>
            </div>
        }
      </div>
    )
  }
}
 
const GroupDetailForm = Form.create()(GroupDetail);
export default GroupDetailForm;