forked from nsjcy/frontEnd/nsjcy

liuwh
2020-06-09 5a887acf1924950715086633d329db045223b5c4
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
import React from 'react';
 
import HeadView from '../view/HeadView';
import TableBtnView from '../view/TableBtnView';
import CommonSearchForm from '../view/CommonSearchForm';
import { Input, Button, DatePicker, Divider, Modal, message } from 'antd'
import moment from 'moment'
import Fetch from '../fetch';
import TableView from '../view/TableView';
const confirm = Modal.confirm;
export default class KeyWord extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      data: [],
      loading: false,
      visible: false,
      savedate: {},
      formData: {
        __key: Date.now(),
        keywords: '',
      },
      resetKey: Date.now(),
      closeKey: Date.now()
    };
  }
 
  componentDidMount() {
    document.title = '典型案例关键词';
    this.getData();
  }
 
  setFormData = data => {
    console.log('form', data);
    this.setState({
      formData: data,
    });
  }
 
  searchonClick = data => {
    console.log('form', data);
    this.setState({
      formData: data,
    });
    this.setState({ loading: true })
    Fetch.searchLaw({ ...data })
      .then(res => {
        for (var i = 0; i < res.length; i++) {
          res[i]['index'] = i + 1;
        }
        this.setState({
          data: res,
          loading: false
        });
      });
  }
  getData = () => {
    const { formdata } = this.state;
    this.setState({ loading: true })
    Fetch.searchLaw({ formdata })
      .then(res => {
        console.log(res)
        this.setState({
          data: res,
          loading: false
        });
      });
  }
  onInputChange = ({ target: { value, name } }) => {
    this.setState(({ formdata }) => ({
      formdata: {
        ...formdata,
        [name]: value
      }
    }))
  }
  saveInputChange = ({ target: { value, name } }) => {
    this.setState(({ savedate }) => ({
      savedate: {
        ...savedate,
        [name]: value
      }
    }))
  }
  Seaech = () => {
    const { formdata } = this.state;
    Fetch.searchLaw(formdata)
      .then(res => {
        for (var i = 0; i < res.length; i++) {
          res[i]['index'] = i + 1;
        }
        this.setState({
          data: res
        });
      });
  }
  Reset = () => {
    this.setState({
      resetKey: Date.now(),
      formdata: {}
    }, this.getData);
  }
 
  showModal = (id) => {
  console.log(id)
    this.setState({
      visible: true,
    });
    Fetch.searchLawFind(id)
      .then(res => {
        console.log(res)
        this.setState({ savedate: res })
      })
  }
 
  handleOk = () => {
    this.setState({ loading: true });
    const { savedate } = this.state;
    console.log(savedate)
    if (!savedate.keywords) {
      return message.warning("关键词不能为空");
    }
    Fetch.searchLawSave(savedate)
      .then(res => {
        console.log(res)
        if (res.code === 0) {
          this.setState({
            loading: false,
            visible: false,
            closeKey: Date.now()
          }, this.getData);
          message.success("提交成功!")
        } else {
          message.error('保存失败,请联系管理员', 2)
        }
      });
  }
 
  handleCancel = () => {
    this.setState({
      closeKey: Date.now(),
      visible: false
    });
  }
 
  deleteItems = (id) => {
    confirm({
      title: '确认要删除这条数据吗?',
      onOk: () => {
        Fetch.searchLawDelete(id)
          .then(data => {
              message.success("删除成功!")
              this.setState({
                resetKey: Date.now(),
                formdata: {}
              }, this.getData);
          })
      }
    });
  }
  render() {
    const columns = [{
      title: '编号',
      dataIndex: 'id',
      key: 'id'
    }, {
      title: '关键词',
      dataIndex: 'keywords',
      key: 'keywords'
    }, {
      title: '创建时间',
      dataIndex: 'createTime',
      key: 'createTime',
      render: text => <span>{moment(text).format("YYYY-MM-DD HH:mm")}</span>
    }, {
      title: '操作',
      key: 'action',
      render: (text, record) => (
        <span>
          <label onClick={() => this.showModal(record.id)} className='theme-color' style={{ cursor: 'pointer' }}>编辑</label>
          <Divider type="vertical" />
          <label onClick={() => this.deleteItems(record.id)} className='theme-color' style={{ cursor: 'pointer' }}>删除</label>
        </span>
      ),
    }];
    const { data, resetKey, visible, loading, savedate, closeKey, formData } = this.state;
    console.log(savedate)
    return (
 
      <div className="app-page">
        <HeadView history={this.props.history} />
        <div style={{ marginLeft: '90%', marginBottom: '20px', marginTop: '20px' }}>
          <Button type="primary" size='large' onClick={() => this.showModal('new')}>新增</Button>
        </div>
        <div style={{ background: '#fff', margin: 20 }}>
          <CommonSearchForm
            {...this.props}
            formData={formData}
            setFormData={this.setFormData}
            searchonClick={this.searchonClick}
            pathName={this.props.location.pathname}
            data={[
              {
                type: 'input', name: '关键词', label: '关键词', key: 'keywords'
              }
            ]} />
          <TableView columns={columns} data={data} pageSize='10' size='default' loading={loading} />
        </div>
        <Modal
          key={closeKey}
          confirmLoading={loading}
          visible={visible}
          title="新增关键词"
          onOk={this.handleOk}
          onCancel={this.handleCancel}
          bodyStyle={{ lineHeight: 4 }}
        >
          <Input addonBefore="关键词" name='keywords' onChange={this.saveInputChange} value={savedate.keywords || ''} />
        </Modal>
      </div>
    );
  }
 
}