king
2024-05-08 db731b1d8b3ed1fcce588307b8bd5eccc4f805b2
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
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { is, fromJS } from 'immutable'
import { Table, Input, Button } from 'antd'
 
import Api from '@/api'
import UtilsDM from '@/utils/utils-datamanage.js'
// import './index.scss'
 
class SearchWrap extends Component {
  static propTpyes = {
    search: PropTypes.array,
    onChange: PropTypes.func
  }
 
  state = {
    search: []
  }
 
  UNSAFE_componentWillMount () {
    const { search } = this.props
 
    this.setState({
      search: fromJS(search).toJS()
    })
  }
 
  /**
   * @description 组件销毁,清除state更新
   */
  componentWillUnmount () {
    this.setState = () => {
      return
    }
  }
 
  searchChange = (val, key) => {
    const { search } = this.state
 
    this.setState({search: search.map(item => {
      if (item.field === key) {
        item.value = val
      }
      return item
    })})
  }
 
  trigger = () => {
    const { search } = this.state
 
    this.props.onChange(search)
  }
 
  render() {
    const { search } = this.state
 
    return (
      <div className="tb-search-wrap">
        {search.map(item => (
          <div className="search-item" key={item.field}>
            {item.label}:
            <Input autoComplete="off" onChange={(e) => this.searchChange(e.target.value, item.field)}/>
          </div>
        ))}
        <Button onClick={this.trigger}>搜索</Button>
      </div>
    )
  }
}
 
class SubTable extends Component {
  static propTpyes = {
    config: PropTypes.object
  }
 
  state = {
    pageIndex: 1,         // 初始页面索引
    pageSize: 10,         // 每页数据条数
    columns: null,        // 显示列
    pageOptions: [],
    search: []
  }
 
  UNSAFE_componentWillMount () {
    const { config } = this.props
 
    let _columns = []
 
    config.columns.forEach(item => {
      if (item.Hide === 'true') return
      _columns.push({
        align: 'left',
        dataIndex: item.field,
        title: item.label,
        sorter: false,
        width: item.Width || 120
      })
    })
 
    let size = (config.setting.pageSize || 10) + ''
    let pageOptions = ['10', '25', '50', '100', '500', '1000']
 
    if (!pageOptions.includes(size)) {
      pageOptions.push(size)
      pageOptions = pageOptions.sort((a, b) => a - b)
    }
 
    this.setState({
      pageSize: config.pageSize || 10,
      pageOptions,
      search: fromJS(config.search).toJS(),
      columns: _columns
    })
  }
 
  componentDidMount() {
    const { config } = this.props
 
    if (config.setting.onload === 'true') {
      this.loadData()
    }
  }
 
  shouldComponentUpdate (nextProps, nextState) {
    return !is(fromJS(this.props), fromJS(nextProps)) || !is(fromJS(this.state), fromJS(nextState))
  }
 
  /**
   * @description 组件销毁,清除state更新
   */
  componentWillUnmount () {
    this.setState = () => {
      return
    }
  }
 
  /**
   * @description 数据加载
   */
  async loadData () {
    const { config, tax_type } = this.props
    const { search, pageIndex, pageSize } = this.state
 
    this.setState({
      loading: true
    })
 
    let searches = search.map(item => ({
      key: item.field,
      match: item.match,
      type: item.type,
      value: item.value || ''
    }))
 
    let param = UtilsDM.getQueryDataParams(config.setting, searches, config.setting.order, pageIndex, pageSize, '')
 
    this.requestId = config.uuid + new Date().getTime()
 
    let result = await Api.genericInterface(param, config.setting.js_script, '', this.requestId)
 
    if (result.status) {
      if (result.$requestId && this.requestId !== result.$requestId) return
 
      let start = pageSize * (pageIndex - 1) + 1
 
      let data = result.data.map((item, index) => {
        item.key = index
        item.$Index = start + index + ''
 
        if (tax_type) {
          item.tax_rate = tax_type === 'special_invoice' ? item.general_tax_rate : item.small_tax_rate
          item.tax_rate = isNaN(item.tax_rate) ? 0 : +item.tax_rate
        }
 
        return item
      })
 
      let total = result.total || 0
      if (config.setting.custompage && data.length) {
        total = data[data.length - 1].mk_total || 0
      }
 
      this.setState({
        data: data,
        total: total,
        loading: false
      })
 
      UtilsDM.querySuccess(result)
    } else {
      this.setState({
        loading: false
      })
      
      UtilsDM.queryFail(result)
    }
  }
 
  changeTable = (pagination) => {
    this.setState({
      pageIndex: pagination.current,
      pageSize: pagination.pageSize,
    }, () => {
      this.loadData()
    })
  }
 
  doubleClickLine = (record) => {
    this.props.onChange(record)
  }
 
  searchChange = (search) => {
    this.setState({search: search}, () => {
      this.loadData()
    })
  }
 
  render() {
    const { pageIndex, pageSize, pageOptions, columns, total, loading, data, search } = this.state
 
    return (
      <>
        <SearchWrap search={search} onChange={this.searchChange}/>
        <Table
          className="inv-table"
          columns={columns}
          dataSource={data}
          bordered={true}
          loading={loading}
          onRow={(record) => {
            return {
              onDoubleClick: () => {this.doubleClickLine(record)}
            }
          }}
          onChange={this.changeTable}
          pagination={{
            current: pageIndex,
            pageSize: pageSize,
            pageSizeOptions: pageOptions,
            showSizeChanger: true,
            total: total || 0,
            showTotal: (total, range) => `${range[0]}-${range[1]} 共 ${total} 条`
          }}
        />
      </>
    )
  }
}
 
export default SubTable