/*
 * Creater:     shizi
 * CreateDate:  2022/03/31
 * Describe：   必填医嘱项
 * */
import React, { Component } from 'react';
import DynamicRenderingForm from 'pages/common/DynamicRenderingForm';

class RequiredOrderItem extends Component {
  constructor(props) {
    super(props);
    this.state = {
      rowData: {},
      selectData: {},
      formData: [],
      activeKey: ''
    };
    this.staticFieldsValue = {};
  }

  static getDerivedStateFromProps(nextProps, prevState) {
    // 该方法内禁止访问this
    if (('selectData' in nextProps && (JSON.stringify(nextProps.selectData) !== JSON.stringify(prevState.selectData)))) {
      // 通过对比nextProps和prevState，返回一个用于更新状态的对象
      return {
        selectData: nextProps.selectData
      }
    }
    if (('rowData' in nextProps && (JSON.stringify(nextProps.rowData) !== JSON.stringify(prevState.rowData)))) {
      return {
        activeKey: nextProps.activeKey,
        rowData: nextProps.rowData
      }
    }
    // 不需要更新状态，返回null
    return null
  }

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

  componentDidUpdate(prevProps, prevState) {
    if (this.state.activeKey === '2') return; // 防止无用加载
    if (('selectData' in prevProps && (JSON.stringify(prevProps.selectData) !== JSON.stringify(this.state.selectData))) || ('rowData' in prevProps && (JSON.stringify(prevProps.rowData) !== JSON.stringify(this.state.rowData)))) {
      this.getFormData();
    }
  }

  getFormData = () => {
    let { selectData, rowData } = this.state;
    const disabled = 'disabled' in rowData && rowData.disabled ? rowData.disabled : 'N';
    const moreHospFlag = 'moreHospFlag' in rowData && rowData.moreHospFlag ? rowData.moreHospFlag : 'N';
    let formData = [{
      dataIndex: 'medSubCatID',
      title: '医嘱子类',
      typeCode: 'Select',
      required: 'Y',
      detailItem: selectData && 'ordSubCat' in selectData ? selectData.ordSubCat : [],
      changeCallBack: this.handleMedSubCatChange,
      callBackResult: "Fn",
      disabled: moreHospFlag,
      col: 8
    }, {
      dataIndex: 'medCode',
      title: '代码',
      typeCode: 'Input',
      required: 'Y',
      disabled: 'Y',
      col: 8
    }, {
      dataIndex: 'medDesc',
      title: '描述',
      typeCode: 'Input',
      required: 'Y',
      disabled: disabled === 'Y' || moreHospFlag === 'Y' ? 'Y' : 'N',
      col: 8
    }, {
      dataIndex: 'medPriorityID',
      title: '医嘱优先级',
      typeCode: 'Select',
      required: 'Y',
      detailItem: selectData && 'priority' in selectData ? selectData.priority : [],
      disabled: moreHospFlag,
      col: 8
    }, 
    // {
    //   dataIndex: 'medSpec',
    //   title: '含量规格',
    //   typeCode: 'Input',
    //   required: 'Y',
    //   col: 8
    // }, 
    {
      dataIndex: 'medShortDesc',
      title: '简称',
      typeCode: 'Input',
      required: 'Y',
      disabled: moreHospFlag,
      col: 8
    }, {
      dataIndex: 'medStartDate',
      title: '生效日期',
      typeCode: 'Date',
      required: 'Y',
      default: 0,
      disabled: moreHospFlag,
      col: 8
    }, 
    // {
    //   dataIndex: 'medStopDate',
    //   title: '失效日期',
    //   typeCode: 'Date',
    //   col: 8
    // }, 
    {
      dataIndex: 'medNoChangeFlag',
      title: '收费',
      typeCode: 'Switch',
      col: 5,
      // disabled: moreHospFlag,
      formItemCol: { labelCol: 13, wrapperCol: 11 }
    }, {
      dataIndex: 'medMergeUseFlag',
      title: '合并用药',
      typeCode: 'Switch',
      col: 5,
      // disabled: moreHospFlag,
      formItemCol: { labelCol: 13, wrapperCol: 11 }
    }, {
      dataIndex: 'medEMFlag',
      title: '加急标志',
      typeCode: 'Switch',
      col: 5,
      // disabled: moreHospFlag,
      formItemCol: { labelCol: 13, wrapperCol: 11 }
    }, {
      dataIndex: 'medPrivateFlag',
      title: '私有标志',
      typeCode: 'Switch',
      col: 5,
      // disabled: moreHospFlag,
      formItemCol: { labelCol: 13, wrapperCol: 11 }
    }, {
      dataIndex: 'medSkinTestFlag',
      title: '皮试标志',
      typeCode: 'Switch',
      col: 5,
      // disabled: moreHospFlag,
      formItemCol: { labelCol: 13, wrapperCol: 11 }
    }, {
      dataIndex: 'medToalCountO',
      title: '门诊按总量',
      typeCode: 'Switch',
      col: 5,
      defaultValue: 'Y',
      // disabled: moreHospFlag,
      formItemCol: { labelCol: 13, wrapperCol: 11 }
    }, {
      dataIndex: 'medToalCountI',
      title: '住院按总量',
      typeCode: 'Switch',
      col: 5,
      // disabled: moreHospFlag,
      formItemCol: { labelCol: 13, wrapperCol: 11 }
    }];
    this.setState({ formData })
  }

  // 医嘱子类change事件
  handleMedSubCatChange = async e => {
    let { rowData } = this.state;
    if (rowData && rowData.id) return;
    let data = {
      params: [{
        subCatID: e
      }]
    }
    let res = await React.$asyncPost(this, '02020379', data);
    let values = {
      medCode: res.result
    };
    this.formRef && this.formRef.setFieldsValue(values); // 设置表单的值
  }

  // 获取数据
  getFieldsValue = () => {
    let medObj = this.formRef && this.formRef.getFieldsValue();
    return {
      medObj
    };
  }

  // 重置
  resetFields = () => {
    this.formRef && this.formRef.resetFields()
  }

  // 保存
  handleSave = () => {
    this.formRef && this.formRef.handleSave().then(result => {
      this.staticFieldsValue = {
        medObj: result,
        medError: result?.error
      };
    });
  }

  // 获取表单数据
  getStaticFieldsValue = () => {
    return this.staticFieldsValue;
  }

  render() {
    let { rowData } = this.props;
    let { formData } = this.state;
    return (
      <div>
        <DynamicRenderingForm
          formItemCol={{ labelCol: 8, wrapperCol: 16 }}
          rowData={rowData}
          formData={formData}
          onRef={ref => this.formRef = ref}
        />
      </div>
    )
  }
};

export default RequiredOrderItem;