king
2020-02-02 35b44bed4aa7b3f645c20fca025813cf4f3786ea
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
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { Form, Row, Col, Input, Button, Select, DatePicker, notification } from 'antd'
import moment from 'moment'
import './index.scss'
 
// const {MonthPicker, WeekPicker} = DatePicker
// const dateFormat = 'YYYY-MM-DD'
// const weekFormat = 'YYYYMMDD'
// const monthFormat = 'YYYY-MM'
 
class MainSearch extends Component {
  static propTpyes = {
    searchlist: PropTypes.array, // 搜索条件列表
    dict: PropTypes.object // 字典项
  }
 
  state = {
    formats: null, // 事件校验规则
    match: null // 搜索条件匹配规则
  }
 
  UNSAFE_componentWillMount () {
    let formats = {}
    let match = {}
    this.props.searchlist.forEach(item => {
      match[item.FieldName] = item.Op
    })
    this.setState({
      formats: formats,
      match: match
    })
  }
 
  getFields() {
    const { getFieldDecorator } = this.props.form
    const fields = []
    this.props.searchlist.forEach((item, index) => {
      if (item.Type === 'text' || item.Type === 'string') { // 文本搜索
        fields.push(
          <Col span={6} key={index}>
            <Form.Item label={item.label}>
              {getFieldDecorator(item.FieldName, {initialValue: item.InitVal })(<Input placeholder="" autoComplete="off" />)}
            </Form.Item>
          </Col>
        )
      } else if (item.Type === 'select') { // 下拉搜索
        let initval = item.FromField.filter(field => field.Selected === 'Selected')[0]
        if (!initval) {
          initval = item.FromField[0]
        }
        fields.push(
          <Col span={6} key={index}>
            <Form.Item label={item.label}>
              {getFieldDecorator(item.FieldName, {initialValue: initval.IdField })(
                <Select
                  showSearch
                  filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
                >
                  {item.FromField.map(option =>
                    <Select.Option id={option.IdField} title={option.TextField} key={option.IdField} value={option.IdField}>{option.TextField}</Select.Option>
                  )}
                </Select>
              )}
            </Form.Item>
          </Col>
        )
      } else if (item.Type === 'date') { // 时间搜索
        let _initval = null
        if (item.InitVal) {
          _initval = moment().subtract(parseInt(item.InitVal), 'days')
        }
        fields.push(
          <Col span={6} key={index}>
            <Form.Item label={item.label}>
              {getFieldDecorator(item.FieldName, {initialValue: _initval })(
                <DatePicker />
              )}
            </Form.Item>
          </Col>
        )
        // if (item.ID === 'WHE14002009024') {
        //   fields.push(
        //     <Col span={6} key={index}>
        //       <Form.Item label={item.label}>
        //         {getFieldDecorator(item.FieldName, {initialValue: moment('2019-09-14', dateFormat) })(
        //           <DatePicker format={dateFormat} />
        //         )}
        //       </Form.Item>
        //     </Col>
        //   )
        // } else if (item.ID === 'WHE1400200905') {
        //   fields.push(
        //     <Col span={6} key={index}>
        //       <Form.Item label={item.label}>
        //         {getFieldDecorator(item.FieldName, {initialValue: moment('2019-09', monthFormat) })(
        //           <MonthPicker format={monthFormat} />
        //         )}
        //       </Form.Item>
        //     </Col>
        //   )
        // } else if (item.ID === 'WHE1400200902') {
        //   fields.push(
        //     <Col span={6} key={index}>
        //       <Form.Item label={item.label}>
        //         {getFieldDecorator(item.FieldName, {initialValue: moment('20190906', weekFormat) })(
        //           <WeekPicker />
        //         )}
        //       </Form.Item>
        //     </Col>
        //   )
        // }
      }
    })
 
    if (this.props.searchlist.length >= 4) { // 添加搜索、重置按钮
      fields.push(
        <Col span={this.props.searchlist.length % 4 ? 6 : 24} style={{ textAlign: 'right' }} key="actions">
          <Button type="primary" htmlType="submit">
            {this.props.dict['main.search']}
          </Button>
          <Button style={{ marginLeft: 8 }} onClick={this.handleReset}>
            {this.props.dict['main.reset']}
          </Button>
        </Col>
      )
    } else {
      fields.push(
        <Col span={6} style={{ paddingTop: '4px' }} key="actions">
          <Button type="primary" htmlType="submit">
            {this.props.dict['main.search']}
          </Button>
          <Button style={{ marginLeft: 8 }} onClick={this.handleReset}>
            {this.props.dict['main.reset']}
          </Button>
        </Col>
      )
    }
    
    return fields
  }
 
  handleSearch = (e) => {
    // 回车或点击搜索
    e.preventDefault()
    this.props.form.validateFields((err, values) => {
      this.getFieldsValues(values)
    })
  }
 
  handleReset = () => {
    // 重置搜索条件,通知页面刷新
    this.props.form.resetFields()
    this.props.refreshdata('', 'reset')
  }
 
  getFieldsValues = (searches) => {
    // 获取搜索条件值
    let search = {}
    Object.keys(searches).forEach(key => {
      let val = searches[key] || ''
      if (searches[key] && typeof(searches[key]) === 'object') {
        val = moment(searches[key]).format('YYYY-MM-DD')
      }
      search[key] = val
    })
 
    let valid = true // 校验必填项
    this.props.searchlist.forEach(item => {
      let required = false
      if (item.Validate && item.Validate !== '{}') {
        try {
          required = !!JSON.parse(item.Validate).required
        } catch (e) {
          console.error('Validate Field parse error')
        }
      }
      if (required && !search[item.FieldName]) {
        valid = false
        notification.warning({
          top: 92,
          message: this.props.dict['form.required.input'] + item.label
        })
      }
    })
    if (valid) {
      this.props.refreshdata(search)
    }
  }
 
  render() {
    return (
      <Form className="ant-advanced-search-form datamanage-search" onSubmit={this.handleSearch}>
        <Row gutter={24}>{this.getFields()}</Row>
      </Form>
    )
  }
}
 
export default Form.create()(MainSearch)