forked from nsjcy/frontEnd/nsjcy

liuwh
2020-02-24 febf1743bfa473d7a00eaec206e833c9bd4ee5e5
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import React from 'react';
import { Row, Col, Icon, Form, message, Breadcrumb, Modal } from 'antd';
import SearchTree from '../view/tree'
import HeadView from '../view/HeadView'
import Fetch from '../fetch';
import UnitDetailEdit from './UnitDetailEdit';
// import { unitDel } from "../fetch/api";
 
const confirm = Modal.confirm;
 
class UnitManage extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      loading: true,
      resourceList: [],
      treeKey: 'init',
      expandedKeys: [],
      tableData: [],
      totalElements: 1,
      pageSize: 10,
      treeData: [],
      edit: 'false',
      rightShow: false,
      dataSet: {},
      currentNode: [],
      id: '',
      selectedKeys: []
    };
  }
  componentDidMount() {
    this.loadData()
  }
 
  editFun = () => {
    this.setState({
      edit: 'true'
    })
  }
 
  saveFun = (obj) => {
    let _this = this;
    this.setState({
      edit: 'false',
      dataSet: obj,
      expandedKeys: []
    })
    _this.loadData();
    _this.setState({ treeKey: Date.now() - 1512523199245 })
  }
 
  cancelFun = () => {
    let _this = this;
    _this.setState({
      edit: 'false'
    });
    const { dataSet, currentNode } = _this.state;
    if (Object.keys(dataSet).length == 0) {
      Fetch.unitDetail({ id: currentNode[0].id }).then(res => {
        _this.setState({
          dataSet: res,
          id: currentNode[0].id,
          loading: false
        })
      })
    }
 
  }
 
  cancelData = () => {
    let _this = this;
    this.setState({
      rightShow: false
    });
 
  }
 
  loadData = () => {
    let _this = this;
    _this.setState({
      loading: true
    })
    Fetch.getAllUnits().then(res => {
      res = _this.handleTreeData(res);
      console.log(res);
      _this.setState({
        treeData: res,
        treeKey: () => Date.now() - 1512523199245,
        loading: false
      });
    });
  };
 
  handleTreeData = data => {
    for (let i = 0; i < data.length; i++) {
      const node = data[i];
      node.key = node.id;
      node.title = node.channelName;
      if (data[i].children) {
        this.handleTreeData(node.children);
      }
    }
    return data;
  };
 
  onSelect = (selectedKeys) => {
    let _this = this;
    _this.setState({
      selectedKeys
    })
  }
 
  treeSelect = (tableData, currentNode) => {
    let _this = this;
    // 点击任意节点都可以查看、编辑、添加
    _this.setState({
      rightShow: true,
      loading: true,
      leafNodes: tableData,
      currentNode,
      selectedKeys: [currentNode[0].id]
    }, () => {
      if (currentNode[0].canDelOrUp != 0) {
        Fetch.unitDetail({ id: currentNode[0].id }).then(res => {
          _this.setState({
            edit: 'false',
            dataSet: res,
            id: currentNode[0].id,
            loading: false
          })
        })
      } else {
        _this.setState({
          rightShow: false
        })
      }
    })
  };
 
  addBtn = () => {
    let _this = this;
    _this.setState({
      rightShow: true,
      loading: false,
      dataSet: {},
      edit: 'true',
      id: ''
    })
  }
 
  deleteBtn = (node) => {
    if (node == "") {
      message.error("没有选择资源,无法删除")
    } else {
      this.ConfirmTip(node);
    }
  }
 
  ConfirmTip = (node) => {
    let _this = this
    confirm({
      title: <span style={{ fontSize: 19 }}>确定要删除该资源吗?</span>,
      content: <span style={{ fontSize: 18 }}>删除该资源的同时,会删除与该资源的所有关系,也会删除所有的子资源。</span>,
      onOk() {
        Fetch.unitDel(node.id).then(res => {
          message.success('节点删除成功')
          _this.setState({
            rightShow: false
          })
        });
        _this.loadData();
        _this.setState({ treeKey: Date.now() - 1512523199245 })
      },
      onCancel() { },
    });
  }
 
  onExpand = (expandedKeys) => {
    this.setState({
      expandedKeys,
      treeKey: () => Date.now() - 1512523199245
    });
    this.setState({
      treeKey: () => Date.now() - 1512523199245
    });
  }
 
  clearKeys = () => {
    let _this = this;
    _this.setState({
      currentNode: [],
      rightShow: false
    })
  }
 
  render() {
    const { treeKey, rightShow, treeData, edit } = this.state;
 
    const params = {
      currentNode: this.state.currentNode,
      id: this.state.id
    }
 
    return (
      <div className="app-page" style={{ display: 'flex', flexDirection: 'column' }}>
        <HeadView history={this.props.history} />
        <Breadcrumb style={{ padding: '20px' }}>
          <Breadcrumb.Item><a href="">后台中心</a></Breadcrumb.Item>
          <Breadcrumb.Item>扫黑除恶</Breadcrumb.Item>
        </Breadcrumb>
        <div style={{ border: 20, margin: 20, padding: 32, flex: 1, background: '#fff' }} >
          <Row style={{ height: '95%' }} >
            <Col span={8} style={{ border: "1px solid #e8e8e8", padding: 10, height: '100%' }} className="h-100">
              <SearchTree key={treeKey}
                // data={resourceList}
                data={treeData}
                onExpand={this.onExpand}
                expandedKeys={this.state.expandedKeys}
                treeSelect={this.treeSelect}
                add={this.addBtn}
                delete={this.deleteBtn}
                clearKeys={this.clearKeys}
                onSelect={this.onSelect}
                selectedKeys={this.state.selectedKeys}
                delShow={ this.state.currentNode.length > 0 ? this.state.currentNode[0].canDelOrUp == 0 ? false : true : false}
                addShow={ this.state.currentNode.length > 0 ? this.state.currentNode[0].canDelOrUp == 0 ? true : false : false}
              />
            </Col>
            <Col span={1}></Col>
            <Col span={15} className="h-100">
              {rightShow &&
                <UnitDetailEdit edit={edit} editFun={this.editFun} saveFun={this.saveFun} cancelFun={this.cancelFun} dataSet={this.state.dataSet} params={params} cancelData={this.cancelData} loading={this.state.loading}></UnitDetailEdit>}
              {
                !rightShow &&
                <span className='position-middle'><Icon type="frown" theme="outlined" />暂无详情,请点击左侧树形结构</span>
              }
            </Col>
          </Row>
          <Row>
          </Row>
        </div>
      </div>
    )
  }
 
 
}
 
 
 
const UnitManageList = Form.create()(UnitManage);
export default UnitManageList;