king
2020-02-21 063b983daaf51a7f1e8677bde1e9c0e618866c91
src/templates/tableshare/verifycard/billcodeform/index.jsx
@@ -1,27 +1,184 @@
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { Form, Row, Col, Input, Select, Button, InputNumber } from 'antd'
import { is, fromJS } from 'immutable'
import { Form, Row, Col, Select, Button, InputNumber } from 'antd'
import './index.scss'
class UniqueForm extends Component {
  static propTpyes = {
    dict: PropTypes.object,  // 字典项
    billCode: PropTypes.object,  // 表单
    billCodeChange: PropTypes.func  // 表单
    dict: PropTypes.object,         // 字典项
    fields: PropTypes.array,        // 表单
    billcodes: PropTypes.array,     // 表单
    columns: PropTypes.array,       // 表单
    modular: PropTypes.array,       // 表单
    modularDetail: PropTypes.array, // 表单
    orderChange: PropTypes.func     // 表单
  }
  state = {
    editItem: null,
    modularDetail: [],
    funFields: [],
    billFields: [],
    type: '1'
  }
  UNSAFE_componentWillMount() {
    let _modularDetail = []
    let _billFields = []
    if (this.props.modular.length > 0) {
      _modularDetail = this.props.modularDetail.filter(item => item.BID === this.props.modular[0].ID)
    }
    let fieldMap = new Map()
    this.props.fields.forEach(_field => {
      if (_field.type === 'text' && !fieldMap.has(_field.field)) {
        _billFields.push(_field)
        fieldMap.set(_field.field, true)
      }
    })
    this.props.columns.forEach(_field => {
      if (_field.type === 'text' && !fieldMap.has(_field.field)) {
        _billFields.push(_field)
        fieldMap.set(_field.field, true)
      }
    })
    let _usedfields = this.props.billcodes.map(item => item.field)
    this.setState({
      modularDetail: _modularDetail,
      funFields: this.props.fields.filter(field => field.type === 'funcvar' && !_usedfields.includes(field.field)),
      billFields: _billFields
    })
  }
  UNSAFE_componentWillReceiveProps (nextProps) {
    if (!is(fromJS(this.props.modular), fromJS(nextProps.modular))) {
      let _detail = nextProps.modularDetail.filter(item => item.BID === nextProps.modular[0].ID)
      this.setState({
        modularDetail: _detail
      })
    }
  }
  edit = (record) => {
    let _usedfields = this.props.billcodes.map(item => item.field)
    let _funFields = this.props.fields.filter(field => field.type === 'funcvar' && (!_usedfields.includes(field.field) || field.field === record.field))
    if (record.TypeCharOne === 'Y' || record.TypeCharOne === 'n') {
      let _detail = this.props.modularDetail.filter(item => item.BID === record.ModularCode)
      let _modularDetailCode = ''
      if (_detail.filter(item => item.ModularDetailCode === record.ModularDetailCode).length > 0) {
        _modularDetailCode = record.ModularDetailCode
      }
      this.setState({
        type: '2',
        funFields: _funFields,
        editItem: record,
        modularDetail: _detail
      }, () => {
        this.props.form.setFieldsValue({
          field: record.field,
          TypeCharOne: record.TypeCharOne,
          ModularCode: record.ModularCode,
          ModularDetailCode: _modularDetailCode,
        })
      })
    } else {
      this.setState({
        type: '1',
        funFields: _funFields,
        editItem: record,
      }, () => {
        this.props.form.setFieldsValue({
          field: record.field,
          TypeCharOne: record.TypeCharOne,
          Type: record.Type,
          linkField: record.linkField,
        })
      })
    }
  }
  voucherChange = (value) => {
    let _detail = this.props.modularDetail.filter(item => item.BID === value)
    this.setState({
      modularDetail: _detail
    })
    this.props.form.setFieldsValue({
      ModularDetailCode: _detail[0] ? _detail[0].ModularDetailCode : ''
    })
  }
  TypeCharOneChange = (value) => {
    if (value === 'Y' || value === 'n') {
      this.setState({
        type: '2'
      })
    } else {
      this.setState({
        type: '1'
      })
    }
  }
  handleConfirm = () => {
    const { funFields, billFields } = this.state
    // 表单提交时检查输入值是否正确
    this.props.form.validateFieldsAndScroll((err, values) => {
      if (!err) {
        values.uuid = this.state.editItem ? this.state.editItem.uuid : ''
        values.field = values.field.join(',')
        this.props.uniqueChange(values)
        let _funField = funFields.filter(item => item.field === values.field)[0]
        values.fieldName = _funField ? _funField.label : ''
        // 一级菜单值为20190203125926873D6029A9C511041719420时TypeCharTwo=TableCode,其他用BillCode
        if (values.ModularCode === '20190203125926873D6029A9C511041719420') {
          values.TypeCharTwo = 'TableCode'
        } else {
          values.TypeCharTwo = 'BillCode'
        }
        // 设置类型名称,用于列表显示
        let billType = {
          Y: '单号',
          n: '编码',
          Lp: '行号',
          BN: '批次',
        }
        values.billType = billType[values.TypeCharOne]
        if (values.TypeCharOne === 'Y' || values.TypeCharOne === 'n') {
          // 设置凭证类型中文名
          let ModularCode = this.props.modular.filter(item => item.ID === values.ModularCode)[0]
          values.ModularCodeName = ModularCode.NameNO
          // 设置流水号位数
          let _detail = this.state.modularDetail.filter(item => item.ModularDetailCode === values.ModularDetailCode)[0]
          values.Type = _detail.Type
        } else {
          let _billField = billFields.filter(item => item.field === values.linkField)[0]
          values.linkFieldName = _billField ? _billField.label : ''
        }
        this.props.orderChange(values)
        this.setState({
          editItem: null
        }, () => {
          let _usedfields = this.props.billcodes.map(item => item.field)
          this.setState({
            funFields: this.props.fields.filter(field => field.type === 'funcvar' && !_usedfields.includes(field.field)),
          })
        })
        this.props.form.setFieldsValue({
          field: '',
        })
      }
    })
@@ -29,45 +186,54 @@
  render() {
    const { getFieldDecorator } = this.props.form
    const { billCode } = this.props
    const { type } = this.state
    const formItemLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 8 }
        sm: { span: 10 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
        sm: { span: 14 }
      }
    }
    return (
      <Form {...formItemLayout} className="custom-verify-form">
      <Form {...formItemLayout} className="verify-form">
        <Row gutter={24}>
          <Col span={6}>
            <Form.Item label={'字段'}>
          <Col span={10}>
            <Form.Item label={this.props.dict['header.form.funcvar']}>
              {getFieldDecorator('field', {
                initialValue: billCode.ModularDetailCode || '',
                initialValue: '',
                rules: [
                  {
                    required: true,
                    message: this.props.dict['form.required.input'] + '凭证标识!'
                  }
                ]
              })(<Input placeholder="" autoComplete="off" />)}
            </Form.Item>
          </Col>
          <Col span={6}>
            <Form.Item label={'单号类型'}>
              {getFieldDecorator('TypeCharOne', {
                initialValue: billCode.TypeCharOne || 'Y',
                rules: [
                  {
                    required: true,
                    message: this.props.dict['form.required.select'] + '单号类型!'
                    message: this.props.dict['form.required.input'] + this.props.dict['header.form.funcvar'] + '!'
                  }
                ]
              })(
                <Select>
                  {this.state.funFields.map(option =>
                    <Select.Option title={option.label} id={option.uuid} key={option.uuid} value={option.field}>
                      {option.label}
                    </Select.Option>
                  )}
                </Select>
              )}
            </Form.Item>
          </Col>
          <Col span={10}>
            <Form.Item label={'类型'}>
              {getFieldDecorator('TypeCharOne', {
                initialValue: 'Lp',
                rules: [
                  {
                    required: true,
                    message: this.props.dict['form.required.select'] + '类型!'
                  }
                ]
              })(
                <Select onChange={(value) => {this.TypeCharOneChange(value)}}>
                  <Select.Option value="Y"> 单号 </Select.Option>
                  <Select.Option value="n"> 编码 </Select.Option>
                  <Select.Option value="Lp"> 行号 </Select.Option>
@@ -76,90 +242,94 @@
              )}
            </Form.Item>
          </Col>
          <Col span={6}>
          <Col span={4} className="add">
            <Button onClick={this.handleConfirm} type="primary">
              确定
            </Button>
          </Col>
          {type === '1' ? <Col span={10}>
            <Form.Item label={'关联字段'}>
              {getFieldDecorator('linkField', {
                initialValue: '',
                rules: [
                  {
                    required: true,
                    message: this.props.dict['form.required.input'] + '关联字段!'
                  }
                ]
              })(
                <Select>
                  {this.state.billFields.map(option =>
                    <Select.Option title={option.label} id={option.uuid} key={option.uuid} value={option.field}>
                      {option.label}
                    </Select.Option>
                  )}
                </Select>
              )}
            </Form.Item>
          </Col> : null}
          {type === '1' ? <Col span={10}>
            <Form.Item label={'位数'}>
              {getFieldDecorator('Type', {
                initialValue: 4,
                rules: [
                  {
                    required: true,
                    message: this.props.dict['form.required.input'] + '位数!'
                  }
                ]
              })(<InputNumber min={1} max={10} precision={0} />)}
            </Form.Item>
          </Col> : null}
          {type === '2' ? <Col span={10}>
            <Form.Item label={'凭证类型'}>
              {getFieldDecorator('ModularCode', {
                initialValue: this.props.modular[0] ? this.props.modular[0].ID : '',
                rules: [
                  {
                    required: true,
                    message: this.props.dict['form.required.input'] + '凭证类型!'
                  }
                ]
              })(
                <Select
                  showSearch
                  filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
                  onChange={(value) => {this.voucherChange(value)}}
                >
                  {this.props.modular.map(option =>
                    <Select.Option title={option.NameNO} id={option.ID} key={option.ID} value={option.ID}>
                      {option.NameNO}
                    </Select.Option>
                  )}
                </Select>
              )}
            </Form.Item>
          </Col> : null}
          {type === '2' ? <Col span={10}>
            <Form.Item label={'凭证标识'}>
              {getFieldDecorator('ModularDetailCode', {
                initialValue: billCode.ModularDetailCode || '',
                initialValue: this.state.modularDetail[0] ? this.state.modularDetail[0].ModularDetailCode : '',
                rules: [
                  {
                    required: true,
                    message: this.props.dict['form.required.input'] + '凭证标识!'
                  }
                ]
              })(<Input placeholder="" autoComplete="off" />)}
            </Form.Item>
          </Col>
          <Col span={6}>
            <Form.Item label={'流水码位数'}>
              {getFieldDecorator('Type', {
                initialValue: billCode.Type || 4,
                rules: [
                  {
                    required: true,
                    message: this.props.dict['form.required.input'] + '流水码位数!'
                  }
                ]
              })(<InputNumber defaultValue={4} min={1} max={10} precision={0} />)}
            </Form.Item>
          </Col>
          <Col span={6}>
            <Form.Item label={'类型'}>
              {getFieldDecorator('TypeCharTwo', {
                initialValue: billCode.TypeCharTwo || 'BillCode',
                rules: [
                  {
                    required: true,
                    message: this.props.dict['form.required.select'] + '类型!'
                  }
                ]
              })(
                <Select>
                  <Select.Option value="BillCode"> 业务 </Select.Option>
                  <Select.Option value="TableCode"> 主数据 </Select.Option>
                <Select
                  showSearch
                  filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
                >
                  {this.state.modularDetail.map(option =>
                    <Select.Option title={option.CodeName} id={option.ModularDetailCode} key={option.ModularDetailCode} value={option.ModularDetailCode}>
                      {option.CodeName}
                    </Select.Option>
                  )}
                </Select>
              )}
            </Form.Item>
          </Col>
          <Col span={6}>
            <Form.Item label={'报错信息'}>
              {getFieldDecorator('errmsg', {
                initialValue: '',
                rules: [
                  {
                    required: true,
                    message: this.props.dict['form.required.input'] + '报错信息!'
                  }
                ]
              })(<Input placeholder="" autoComplete="off" />)}
            </Form.Item>
          </Col>
          <Col span={6}>
            <Form.Item label={'报错编码'}>
              {getFieldDecorator('errorCode', {
                initialValue: 'E',
                rules: [
                  {
                    required: true,
                    message: this.props.dict['form.required.select'] + '报错编码!'
                  }
                ]
              })(
                <Select>
                  <Select.Option value="E"> E </Select.Option>
                  <Select.Option value="N"> N </Select.Option>
                  <Select.Option value="F"> F </Select.Option>
                  <Select.Option value="NM"> NM </Select.Option>
                  <Select.Option value="S"> S </Select.Option>
                  <Select.Option value="-1"> -1 </Select.Option>
                </Select>
              )}
            </Form.Item>
          </Col>
          <Col span={2}>
            <Button onClick={this.handleConfirm} type="primary" className="add-row">
              确定
            </Button>
          </Col>
          </Col> : null}
        </Row>
      </Form>
    )