/*
 * Creater:     shizi
 * CreateDate:  2022/05/24
 * Describe：   添加收费项
 * */
import React, { Component } from 'react';
import { Modal } from 'antd';
import ChargeItemInformation from './ChargeItemInformation';

class ChargeItemModal extends Component {
  constructor(props) {
    super(props);
    this.state = {
      visible: false
    };
  }

  componentDidMount() {
    this.props.onRef && this.props.onRef(this);
  }

  // 关闭弹窗
  modifyVisible = (visible) => {
    this.setState({ visible })
  }

  // 获取数据
  getFieldsValue = () => {
    let fromValues = this.chargeItemInfoRef && this.chargeItemInfoRef.getFieldsValue();
    return {
      ...fromValues,
    }
  }

  // 关闭弹窗
  handleCancel = () => {
    let fieldsValue = this.getFieldsValue(); // 关闭弹窗的时候记录表单输入的值
    this.props.recordFormInput && this.props.recordFormInput(fieldsValue);
    this.modifyVisible(false);
  }

  // 保存
  handleSave = () => {
    this.chargeItemInfoRef && this.chargeItemInfoRef.handleSave();
    setTimeout(() => {
      const values = this.chargeItemInfoRef && this.chargeItemInfoRef.getStaticFieldsValue();
      if ((values && !values.error && !values.priceError)) {
        // statusFlag: A - 正在用  B - 已停用  C - 将启用
        this.props.handleAdd && this.props.handleAdd([{ ...values, statusFlag: 'A' }], 'clear');
        this.modifyVisible(false);
      }
    }, 300)
  }

  render() {
    let { rowData, selectData } = this.props;
    let { visible } = this.state;
    return (
      <Modal
        title="添加收费项"
        okText={'id' in rowData && rowData.id ? '确认修改' : '保存'}
        cancelText={'id' in rowData && rowData.id ? '取消修改' : '取消'}
        visible={visible}
        onCancel={this.handleCancel}
        destroyOnClose={true}
        width="80vw"
        onOk={this.handleSave}
      >
        <ChargeItemInformation
          rowData={rowData}
          selectData={selectData}
          onRef={ref => this.chargeItemInfoRef = ref}
        />
      </Modal>
    )
  }
};

export default ChargeItemModal;