/*
 * Create:      柿子
 * CreateDate:  2024/02/28
 * Describe:    护士工作站 - 分床
 * */
import React, { Component } from 'react';
import { Modal, message } from 'antd';
import PatientInfo from './PatientInfo'; // 患者信息展示
import DynamicRenderingForm from 'pages/common/DynamicRenderingForm';

class DivideBed extends Component {
  constructor(props) {
    super(props);
    this.state = {
      userData: React.$getSessionData(),
      visible: false,
      rowData: {},
      formData: []
    };
  }

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

  modifyVisible = (visible, rowData = {}) => {
    const { formData } = this.state;
    if (visible) {
      !(formData && formData.length > 0) && this.getFormData();
    }
    this.setState({ visible, rowData });
  };

  // 获取列表表头数据
  getFormData = async () => {
    try {
      let data = {
        params: [{
          type: 'C',
          compontName: 'DivideBed',
        }]
      }
      let res = await React.$asyncPost(this, '01040073', data);
      let formData = res.result?.formData || [];
      this.setState({
        formData
      });
    } catch (error) {
      console.log(error)
    }
  };

  // 确认分床
  handleOk = async() => {
    try {
      let { rowData } = this.state;
      let values = await this.formRef.handleSave();
      if (!values?.error) {
        let data = {
          params: [{
            ...rowData,
            ...values,
          }]
        }
        console.log('data', data)
        // let res = await React.$asyncPost(this, '01040271', data);
        message.success(res.errorMessage);
        this.modifyVisible(false);
      }
    } catch (error) {
      console.log(error)
    }
  };

  render() {
    const { visible, rowData, formData } = this.state;

    return (
      <Modal
        title="分床"
        width="43vw"
        bodyStyle={{ padding: '12px', paddingTop: '6px' }}
        visible={visible}
        onOk={this.handleOk}
        onCancel={() => this.modifyVisible(false)}
      >
        <PatientInfo patInfo={rowData} />
        <div style={{ paddingTop: '8px' }}>
          <DynamicRenderingForm
            rowData={rowData}
            selectData={{}}
            formData={formData}
            onRef={ref => this.formRef = ref}
          />
        </div>
      </Modal>
    );
  }
};

export default DivideBed;