/*
 * Create:      柿子
 * CreateDate:  2023/05/26
 * Describe：   患者黑名单
 * */
import React, { Component } from 'react';
import { Modal, message } from 'antd';
import { dayFormat } from 'tools';
import moment from 'moment';
import DynamicRenderingForm from 'pages/common/DynamicRenderingForm';

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

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

  componentWillUnmount() {
    // 组件销毁前将静止让setState修改state的状态
    this.setState = (state, callback) => { return; }
  }

  getBlackListType = async () => {
    try {
      const { userData } = this.state;
      let data = {
        params: [{
          language: userData?.language || '',
        }],
      }
      let res = await React.$asyncPost(this, '03050187', data);
      this.state.selectData.blackListType = res.result?.Data || [];
      this.setState({});
    } catch (error) {
      console.log('error', error);
    }
  }

  // 修改弹窗状态
  modifyVisible = (visible, rowData) => {
    const { columns } = this.state;
    if (visible && !(columns && columns.length > 0)) {
      this.getFormData();
    }
    if (visible) {
      this.state.rowData = {
        blackListTypeID: '1',
        ...rowData,
        ...(rowData?.blackListobj || {}),
        name: rowData?.patName || undefined,
        credNo: rowData?.patCredNo || undefined,
        mobile: rowData?.patMobile || undefined,
        activeFlag: rowData?.activeFlag || 'Y',
      };
    }
    this.setState({ visible }, () => {
      setTimeout(() => {
        !visible && this.formRef && this.formRef.resetFields();
      }, 300)
    });
  }

  getFormData = async () => {
    try {
      let data = {
        params: [{
          type: 'C',
          compontName: 'PatientBlacklist',
        }]
      }
      let res = await React.$asyncPost(this, '01040073', data);
      let formData = res.result?.formData || [];
      this.setState({ formData });
    } catch (error) {
      console.log(error);
    }
  }

  // 保存
  handleSave = async () => {
    try {
      const { rowData, userData } = this.state;
      let values = await this.formRef.handleSave();
      if (values.error) {
        message.error('请完善必填信息');
        return;
      }
      let data = {
        params: [{
          startDate: moment().format(dayFormat),
          endDate: '', // 接口需要，如界面选择了就取界面选择的值
          ...values,
          ID: rowData?.ID || '',
          hospID: userData?.hospID || '',
          patientID: rowData?.patID || '',
        }]
      }
      let res = await React.$asyncPost(this, '03050186', data);
      message.success(res.errorMessage || '保存成功');
      this.modifyVisible(false);
      this.props.handleQuery && this.props.handleQuery(rowData?.admID || '');
    } catch (error) {
      console.log('error', error);
    }
  }

  render() {
    const { visible, rowData, formData, selectData } = this.state;
    return (
      <Modal
        width="800px"
        visible={visible}
        title={rowData && rowData?.ID ? '修改黑名单信息' : '添加至黑名单'}
        okText={rowData && rowData?.ID ? '确认修改' : '保存'}
        onOk={this.handleSave}
        onCancel={() => this.modifyVisible(false)}
      >
        <div className="patient-blacklist">
          <DynamicRenderingForm
            onRef={ref => this.formRef = ref}
            rowData={rowData}
            formData={formData}
            selectData={selectData}
          />
        </div>
      </Modal>
    )
  }
};

export default PatientBlacklist;