/*
 * Creater:     shizi
 * CreateDate:  2022/03/31
 * Describe：   全部医嘱项
 * */
import React, { Component } from 'react';
import { $http } from 'containers/config/https';
import { message } from 'antd';
import { Util } from 'tools';
import DynamicRenderingForm from 'pages/common/DynamicRenderingForm';
import UnitMaintenance from './UnitMaintenance'; // 子table列表维护
import '../style/new-drug.less';

class AllOrderItem extends Component {
  constructor(props) {
    super(props);
    this.state = {
      activeKey: '',
      rowData: {},
      selectData: {},
      basicFormData: [], // 基本信息表单数据
      approvalNumberFormData: [], // 批准文号表单数据
      pharmacyFormData: [], // 药学相关表单数据
      medAliasArr: [], // 医嘱项别名列表数据
      orderItemAliasColumns: [],
      medAutArr: [], // 限制用药列表数据
      limitColumns: [],
      pointerSelectData: [], // 类型值下拉数据
      pointerSelectDataAll: {}, // 类型值下拉集合
      WHOUnitSelectData: [], // WHO单位
      manfSelectData: [], // 厂家下拉数据
    };
    this.staticFieldsValue = {};
    this.timeout = null;
  }

  static getDerivedStateFromProps(nextProps, prevState) {
    //该方法内禁止访问this
    if (('selectData' in nextProps && (JSON.stringify(nextProps.selectData) !== JSON.stringify(prevState.selectData))) || ('rowData' in nextProps && (JSON.stringify(nextProps.rowData) !== JSON.stringify(prevState.rowData)))) {
      //通过对比nextProps和prevState，返回一个用于更新状态的对象
      return {
        rowData: nextProps.rowData,
        activeKey: nextProps.activeKey,
        selectData: nextProps.selectData,
        medAliasArr: nextProps && 'rowData' in nextProps && 'medAliasArr' in nextProps.rowData ? [...nextProps.rowData.medAliasArr] : [], // 医嘱项别名
        medAutArr: nextProps && 'rowData' in nextProps && 'medAutArr' in nextProps.rowData ? [...nextProps.rowData.medAutArr] : [], // 限制用药
      }
    }
    //不需要更新状态，返回null
    return null
  }

  componentDidMount() {
    this.getBasicFormData(); // 获取基本信息表单数据
    this.getPharmacyFormData(); // 获取药学相关表单数据
    this.getLimitColumns(); // 获取限制用药表头数据
    this.getOrderItemAliasColumns(); // 医嘱项别名表头获取
    this.getApprovalNumberFormData(); // 批准文号表单数据
    this.props.onRef && this.props.onRef(this);
  }

  componentDidUpdate(prevProps, prevState) {
    if (this.state.activeKey === '1') return;
    if (JSON.stringify(prevProps.selectData) !== JSON.stringify(this.state.selectData)) {
      this.getLimitColumns(); // 获取限制用药表头数据
    }
    if (JSON.stringify(prevProps.rowData) !== JSON.stringify(this.state.rowData)) {
      this.getMedicalOrder();
    }
    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.getBasicFormData(); // 获取基本信息表单数据
      this.getPharmacyFormData(); // 获取药学相关表单数据
      this.getApprovalNumberFormData(); // 批准文号表单数据
    }
    if ((prevProps.rowData.medAliasArr !== undefined) && (JSON.stringify(prevProps.rowData.medAliasArr) !== JSON.stringify(this.state.medAliasArr))) {
      this.orderItemAliasyRef && this.orderItemAliasyRef.getTableData(this.state.medAliasArr); // 医嘱项别名
    }
    if ((this.state.medAutArr !== undefined) && (JSON.stringify(prevProps.rowData.medAutArr) !== JSON.stringify(this.state.medAutArr))) {
      this.limitRef && this.limitRef.getTableData(this.state.medAutArr); // 限制用药
    }
  }

  // 获取医嘱项名table数据
  getMedicalOrder = async () => {
    try {
      let { rowData } = this.state;
      let medObj = 'medObj' in rowData && rowData.medObj ? rowData.medObj : {};
      if ('medID' in medObj && medObj.medID) {
        let data = {
          params: [{
            medID: medObj?.medID
          }],
        }
        let res = await React.$asyncPost(this, '02020323', data);
        let WHOUnitSelectData = 'row' in res.result && res.result.row.length > 0 ? res.result.row : [];
        for (var i = 0; i < WHOUnitSelectData.length; i++) {
          WHOUnitSelectData[i].key = i
        }
        this.setState({ WHOUnitSelectData }, () => {
          this.getPharmacyFormData();
        });
      }
    } catch (error) {
      console.log(error)
    }
  }

  // 切换tabs进行表单赋值
  setTableData = async () => {
    let { medAliasArr, medAutArr } = this.state;
    this.orderItemAliasyRef && this.orderItemAliasyRef.getTableData(medAliasArr); // 医嘱项别名
    this.limitRef && this.limitRef.getTableData(medAutArr); // 限制用药
    for (var i = 0; i < medAutArr.length; i++) {
      let operTypeDesc = medAutArr[i].operTypeDesc;
      await this.handleOperTypeChange(operTypeDesc, 'no');
    }
  }

  // 获取基本信息表单数据
  getBasicFormData = () => {
    let { selectData, rowData } = this.state;
    const medObj = rowData && 'medObj' in rowData ? rowData.medObj : {};
    const disabled = 'disabled' in medObj && medObj.disabled ? medObj.disabled : 'N';
    const moreHospFlag = 'moreHospFlag' in medObj && medObj.moreHospFlag ? medObj.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',
      // onBlur: (e) => this.handleMedBlur(e, 'medCode'),
      col: 8
    }, {
      dataIndex: 'medDesc',
      title: '描述',
      typeCode: 'Input',
      required: 'Y',
      disabled: disabled === 'Y' || moreHospFlag === 'Y' ? 'Y' : 'N',
      onBlur: (e) => this.handleMedBlur(e, 'desc'),
      col: 8
    }, {
      dataIndex: 'medENDesc',
      title: '英文描述',
      typeCode: 'Input',
      disabled: moreHospFlag,
      col: 8
    }, {
      dataIndex: 'medShortDesc',
      title: '简称',
      typeCode: 'Input',
      required: 'Y',
      disabled: moreHospFlag,
      col: 8
    }, {
      dataIndex: 'medPriorityID',
      title: '医嘱优先级',
      typeCode: 'Select',
      required: 'Y',
      disabled: moreHospFlag,
      detailItem: selectData && 'priority' in selectData ? selectData.priority : [],
      col: 8
    }, {
      dataIndex: 'generID',
      title: '处方通用名',
      typeCode: 'Select',
      disabled: moreHospFlag,
      detailItem: selectData && 'generic' in selectData ? selectData.generic : [],
      col: 8
    }, {
      dataIndex: 'maxDurQty',
      title: '最大疗程',
      typeCode: 'inputNumber',
      disabled: moreHospFlag,
      col: 8
    }, {
      typeCode: '',
      col: 8
    }, {
      dataIndex: 'medMaxDose',
      title: '单次最大剂量',
      typeCode: 'inputNumber',
      disabled: moreHospFlag,
      col: 8
    }, {
      dataIndex: 'medQdMaxQty',
      title: '每天最大剂量',
      typeCode: 'inputNumber',
      disabled: moreHospFlag,
      col: 8
    }, {
      dataIndex: 'medSexID',
      title: '性别',
      typeCode: 'Select',
      disabled: moreHospFlag,
      detailItem: selectData && 'sex' in selectData ? selectData.sex : [],
      col: 8
    }, {
      dataIndex: 'medAgeFrom',
      title: '年龄范围',
      typeCode: 'inputNumber',
      disabled: moreHospFlag,
      col: 8
    }, {
      dataIndex: 'medAgeTo',
      title: '到',
      typeCode: 'inputNumber',
      disabled: moreHospFlag,
      col: 8
    }, {
      dataIndex: 'medMessage',
      title: '医嘱提示',
      typeCode: 'Input',
      disabled: moreHospFlag,
      col: 8
    }, {
      dataIndex: 'medStartDate',
      title: '生效日期',
      typeCode: 'Date',
      required: 'Y',
      default: 0,
      disabled: moreHospFlag,
      col: 8
    }, {
      dataIndex: 'medStopDate',
      title: '失效日期',
      typeCode: 'Date',
      disabled: moreHospFlag,
      col: 8
    }, {
      col: 8,
      typeCode: '',
    }, {
      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({ basicFormData: 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.basicInfoRef && this.basicInfoRef.setFieldsValue(values); // 设置表单的值
    // 如果是修改的时候不改变收费项和库存项代码描述
    !(rowData && rowData.id) && this.props.setMedData && this.props.setMedData(res.result, 'code'); // 设置库存项、收费项代码描述
  }

  // 医嘱代码、项描述失焦事件
  handleMedBlur = (e, dataIndex) => {
    // let { rowData } = this.state;
    // let values = {
    //   [dataIndex]: e.target.value
    // };
    // !(rowData && rowData.id) && this.props.setMedData && this.props.setMedData(e.target.value, dataIndex); // 设置库存项、收费项代码描述
    this.props.setMedData && this.props.setMedData(e.target.value, dataIndex); // 收费项库存项描述跟医嘱项保持一致
  }

  // 获取药学相关表单数据
  getPharmacyFormData = () => {
    let { selectData, WHOUnitSelectData, rowData } = this.state;
    const medObj = rowData && 'medObj' in rowData ? rowData.medObj : {};
    const moreHospFlag = 'moreHospFlag' in medObj && medObj.moreHospFlag ? medObj.moreHospFlag : 'N';
    let formData = [
      //   {
      //   dataIndex: 'medSpec',
      //   title: '含量规格',
      //   typeCode: 'Input',
      //   required: 'Y',
      //   col: 8
      // }, 
      {
        dataIndex: 'medUsageID',
        title: '默认用法',
        typeCode: 'Select',
        detailItem: selectData && 'usage' in selectData ? selectData.usage : [],
        // disabled: moreHospFlag,
        col: 8
      }, {
        dataIndex: 'medFreqID',
        title: '默认频次',
        typeCode: 'Select',
        detailItem: selectData && 'freq' in selectData ? selectData.freq : [],
        // disabled: moreHospFlag,
        col: 8
      }, {
        dataIndex: 'formID',
        title: '剂型',
        typeCode: 'Select',
        detailItem: selectData && 'form' in selectData ? selectData.form : [],
        // disabled: moreHospFlag,
        col: 8
      }, {
        dataIndex: 'tradeName',
        title: '商品名',
        typeCode: 'Input',
        // disabled: moreHospFlag,
        col: 8
      }, {
        dataIndex: 'poisonID',
        title: '管制分类',
        typeCode: 'Select',
        detailItem: selectData && 'poison' in selectData ? selectData.poison : [],
        // disabled: moreHospFlag,
        col: 8
      }, {
        dataIndex: 'criLevID',
        title: '危重级别',
        typeCode: 'Select',
        detailItem: selectData && 'critical' in selectData ? selectData.critical : [],
        // disabled: moreHospFlag,
        col: 8
      }, {
        dataIndex: 'medDurQty',
        title: '默认疗程数',
        typeCode: 'inputNumber',
        // disabled: moreHospFlag,
        col: 8
      }, {
        dataIndex: 'medDurationID',
        title: '默认疗程单位',
        typeCode: 'Select',
        detailItem: selectData && 'duration' in selectData ? selectData.duration : [],
        // disabled: moreHospFlag,
        col: 8
      }, {
        dataIndex: 'WHODDD',
        title: 'WHODDD值',
        typeCode: 'Input',
        onBlur: this.handleWHODDDBlur,
        // disabled: moreHospFlag,
        col: 8
      }, {
        dataIndex: 'WHOUnitID',
        title: 'WHO单位',
        typeCode: 'Select',
        detailItem: WHOUnitSelectData,
        valueIndex: 'doseUnitID',
        descindex: 'doseUnitDesc',
        // disabled: moreHospFlag,
        col: 8
      }, {
        dataIndex: 'DDD',
        title: 'DDD值',
        typeCode: 'Input',
        // disabled: moreHospFlag,
        col: 8
      }, {
        dataIndex: 'WHONET',
        title: 'WHONET码',
        typeCode: 'Select',
        detailItem: selectData && 'WHONET' in selectData ? selectData.WHONET : [],
        // disabled: moreHospFlag,
        col: 8
      }, {
        dataIndex: 'YPID',
        title: 'YPID',
        typeCode: 'Input',
        // disabled: moreHospFlag,
        col: 8
      }, {
        typeCode: '',
        col: 16
      }, {
        dataIndex: 'countryFlag',
        title: '是否为国家基本药物',
        typeCode: 'Switch',
        col: 7,
        // disabled: moreHospFlag,
        formItemCol: { labelCol: 13, wrapperCol: 11 }
      }, {
        dataIndex: 'provFlag',
        title: '是否为省级基本药物',
        typeCode: 'Switch',
        col: 7,
        // disabled: moreHospFlag,
        formItemCol: { labelCol: 13, wrapperCol: 11 }
      }, {
        dataIndex: 'cityFlag',
        title: '是否为市级基本药物',
        typeCode: 'Switch',
        col: 7,
        // disabled: moreHospFlag,
        formItemCol: { labelCol: 13, wrapperCol: 11 }
      }, {
        dataIndex: 'monitFlag',
        title: '是否为重点监控标志',
        typeCode: 'Switch',
        col: 7,
        // disabled: moreHospFlag,
        formItemCol: { labelCol: 13, wrapperCol: 11 }
      }, {
        dataIndex: 'antidFlag',
        title: '是否为抗菌药物标志',
        typeCode: 'Switch',
        col: 7,
        // disabled: moreHospFlag,
        formItemCol: { labelCol: 13, wrapperCol: 11 }
      }, {
        dataIndex: 'assistFlag',
        title: '是否为辅助用药标志',
        typeCode: 'Switch',
        col: 7,
        // disabled: moreHospFlag,
        formItemCol: { labelCol: 13, wrapperCol: 11 }
      }, {
        dataIndex: 'negotiatedFlag',
        title: '是否为国家谈判药品',
        typeCode: 'Switch',
        col: 7,
        // disabled: negotiatedFlag,
        formItemCol: { labelCol: 13, wrapperCol: 11 }
      }, {
        dataIndex: 'centralPurchFlag',
        title: '是否为集中采购药品',
        typeCode: 'Switch',
        col: 7,
        // disabled: centralPurchFlag,
        formItemCol: { labelCol: 13, wrapperCol: 11 }
      }, {
        dataIndex: 'twoDiseasesFlag',
        title: '是否为"两病"药品',
        typeCode: 'Switch',
        col: 7,
        // disabled: twoDiseasesFlag,
        formItemCol: { labelCol: 13, wrapperCol: 11 }
      }];
    this.setState({ pharmacyFormData: formData })
  }

  // WHODDD值失焦事件
  handleWHODDDBlur = (e) => {
    let { WHOUnitSelectData } = this.state;
    if (WHOUnitSelectData && WHOUnitSelectData.length > 0) {
      let WHOUnitID = this.pharmacyRef && this.pharmacyRef.getFieldValue('WHOUnitID'); // WHO单位
      if (WHOUnitID) {
        let op = WHOUnitSelectData.filter(item => { return item.doseUnitID == WHOUnitID })
        let WHODDD = e.target.value;
        let Num = op.length > 0 ? op[0].doseQty ? op[0].doseQty : 1 : 1;
        let tatol = (WHODDD ? Number(WHODDD) : 0) / Num
        let number = Math.ceil(tatol);
        number !== '' && this.pharmacyRef && this.pharmacyRef.setFieldsValue({
          DDD: number
        })
      }
    }
  }

  // 医嘱项别名表头获取
  getOrderItemAliasColumns = () => {
    let columns = [{
      title: '医嘱项别名',
      dataIndex: 'medAlias',
      editable: true,
      align: 'center',
      inputType: 'Input',
      required: 'Y',
    }];
    this.setState({ orderItemAliasColumns: columns })
  }

  // 获取限制用药表头数据
  getLimitColumns = () => {
    let { selectData, pointerSelectData } = this.state;
    let columns = [{
      title: '范围',
      dataIndex: 'operateDesc',
      editable: true,
      required: 'Y',
      width: '150px',
      align: 'center',
      inputType: 'Select',
      detailitem: selectData && 'operate' in selectData ? selectData.operate : [],
    }, {
      title: '类型',
      dataIndex: 'operTypeDesc',
      editable: true,
      required: 'Y',
      width: '150px',
      align: 'center',
      inputType: 'Select',
      detailitem: selectData && 'operType' in selectData ? selectData.operType : [],
      onSelect: this.handleOperTypeChange
    }, {
      title: '类型值',
      dataIndex: 'pointerDesc',
      editable: true,
      required: 'Y',
      width: '150px',
      align: 'center',
      inputType: 'Select',
      detailitem: pointerSelectData,
    }, {
      title: '管控级别',
      dataIndex: 'levelDesc',
      editable: true,
      required: 'Y',
      width: '150px',
      align: 'center',
      inputType: 'Select',
      detailitem: selectData && 'level' in selectData ? selectData.level : [],
    }];
    this.setState({ limitColumns: columns }, () => {
      // isUpData && this.limitRef && this.limitRef.handleUpdataColumns(this.state.limitColumns);
    })
  }

  // 编辑行操作
  editCurrentLine = record => {
    record && 'operTypeDesc' in record && record.operTypeDesc && this.handleOperTypeChange(record.operTypeDesc);
  }

  // 类型下拉操作 - isUpDataPointerSelectData - 用于循环获取类型下拉数据
  handleOperTypeChange = async (e, isUpDataPointerSelectData) => {
    try {
      let { selectData, pointerSelectDataAll } = this.state;
      let pointerSelectData = [];
      let operTypeID = Util.returnDataCccordingToAttributes(selectData && 'operType' in selectData ? selectData.operType : [], e, 'descripts')?.id; // 类型
      if (pointerSelectDataAll[operTypeID] !== undefined) { // 优化接口调用次数查询
        pointerSelectData = pointerSelectDataAll[operTypeID];
      } else {
        if (!operTypeID) return;
        let data = {
          params: [{
            operTypeID
          }]
        }
        let res = await React.$asyncPost(this, '02020232', data);
        pointerSelectData = res.result;
        this.savePointerSelectDataAll(pointerSelectData, operTypeID); // 保存数据
      }
      if (isUpDataPointerSelectData !== 'no') {
        this.setState({
          pointerSelectData
        }, () => {
          this.getLimitColumns();
        })
      }
    } catch (error) {
      console.log(error)
    }
  }

  // 将获取到的类型值下拉数据全部保存起来
  savePointerSelectDataAll = (data, dataIndex) => {
    let { pointerSelectDataAll } = this.state;
    if (pointerSelectDataAll[dataIndex] === undefined) {
      pointerSelectDataAll[dataIndex] = data;
    }
    this.setState({ pointerSelectDataAll })
  }

  // 获取数据
  getFieldsValue = () => {
    let medObj = this.basicInfoRef && this.basicInfoRef.getFieldsValue(); // 医嘱项基本信息
    let approvalObj = this.approvalNumberRef && this.approvalNumberRef.getFieldsValue(); // 批准文号
    let drugObj = this.pharmacyRef && this.pharmacyRef.getFieldsValue(); // 药学相关
    let medAliasData = this.orderItemAliasyRef && this.orderItemAliasyRef.handleSave(); // 医嘱别名
    let medAliasArr = medAliasData && 'tableData' in medAliasData ? medAliasData.tableData : [];
    let medAliasEditingKey = medAliasData && 'editingKey' in medAliasData ? medAliasData.editingKey : '';
    let medAutData = this.limitRef && this.limitRef.handleSave(); // 限制用药
    let medAutArr = medAutData && 'tableData' in medAutData ? medAutData.tableData : [];
    let medAutEditingKey = medAutData && 'editingKey' in medAutData ? medAutData.editingKey : '';
    return {
      medObj: {
        ...medObj,
        ...drugObj,
      },
      approvalObj,
      drugObj,
      medAliasArr,
      medAliasEditingKey,
      medAutArr,
      medAutEditingKey
    }
  }

  // 重置
  resetFields = () => {
    this.basicInfoRef && this.basicInfoRef.resetFields(); // 医嘱项基本信息
    this.approvalNumberRef && this.approvalNumberRef.resetFields(); // 批准文号
    this.pharmacyRef && this.pharmacyRef.resetFields(); // 药学相关
    this.orderItemAliasyRef && this.orderItemAliasyRef.resetFields(); // 医嘱别名
    this.limitRef && this.limitRef.resetFields(); // 限制用药
  }

  // 保存
  handleSave = async () => {
    let { selectData, pointerSelectDataAll } = this.state;
    // 医嘱项基本信息
    this.basicInfoRef && this.basicInfoRef.handleSave().then(result => {
      const medObj = this.staticFieldsValue && 'medObj' in this.staticFieldsValue ? this.staticFieldsValue.medObj : {};
      this.staticFieldsValue = {
        ...this.staticFieldsValue,
        medObj: {
          ...medObj,
          ...result,
          medError: result?.error
        }
      }
    });
    // 批准文号
    this.approvalNumberRef && this.approvalNumberRef.handleSave().then(result => {
      const approvalObj = this.staticFieldsValue && 'approvalObj' in this.staticFieldsValue ? this.staticFieldsValue.approvalObj : {};
      this.staticFieldsValue = {
        ...this.staticFieldsValue,
        approvalObj: {
          ...approvalObj,
          ...result
        }
      }
    });
    // 药学相关
    this.pharmacyRef && this.pharmacyRef.handleSave().then(result => {
      this.staticFieldsValue = {
        ...this.staticFieldsValue,
        drugObj: {
          ...result,
          pharmacyError: result?.error
        }
      }
    });
    // 医嘱别名
    let medAliasData = this.orderItemAliasyRef && this.orderItemAliasyRef.handleSave();
    let medAliasArr = medAliasData && 'tableData' in medAliasData ? medAliasData.tableData : [];
    let medAliasEditingKey = medAliasData && 'editingKey' in medAliasData ? medAliasData.editingKey : '';
    // 限制用药
    let medAutData = this.limitRef && this.limitRef.handleSave();
    let medAutArr = medAutData && 'tableData' in medAutData ? medAutData.tableData : [];

    let medAutEditingKey = medAutData && 'editingKey' in medAutData ? medAutData.editingKey : '';
    for (var i = 0; i < medAutArr.length; i++) {
      let pointerSelectData = [];
      let operTypeID = Util.returnDataCccordingToAttributes(selectData && 'operType' in selectData ? selectData.operType : [], medAutArr[i].operTypeDesc, 'descripts')?.id; // 类型
      if (pointerSelectDataAll[operTypeID] !== undefined) { // 取存的下拉数据
        pointerSelectData = pointerSelectDataAll[operTypeID]
      } else {
        let data = {
          params: [{
            operTypeID
          }]
        }
        let res = await React.$asyncPost(this, '02020232', data);
        pointerSelectData = res.result;
      }
      let pointerID = Util.returnDataCccordingToAttributes(pointerSelectData, medAutArr[i].pointerDesc, 'descripts')?.id
      medAutArr[i].operateID = Util.returnDataCccordingToAttributes(selectData && 'operate' in selectData ? selectData.operate : [], medAutArr[i].operateDesc, 'descripts')?.id; // 范围
      medAutArr[i].operTypeID = operTypeID; // 类型
      medAutArr[i].pointerID = pointerID; // 类型值
      medAutArr[i].levelID = Util.returnDataCccordingToAttributes(selectData && 'level' in selectData ? selectData.level : [], medAutArr[i].levelDesc, 'descripts')?.id; // 管控级别
    }
    this.staticFieldsValue = {
      ...this.staticFieldsValue,
      medAliasArr,
      medAliasEditingKey,
      medAutArr,
      medAutEditingKey
    }
  }

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

  // 批准文号表单数据
  getApprovalNumberFormData = () => {
    let { selectData, manfSelectData, rowData } = this.state;
    const medObj = rowData && 'medObj' in rowData ? rowData.medObj : {};
    const moreHospFlag = 'moreHospFlag' in medObj && medObj.moreHospFlag ? medObj.moreHospFlag : 'N';
    let formData = [{
      dataIndex: 'lastApprNo',
      title: '原批准文号',
      typeCode: 'Input',
      disabled: moreHospFlag,
      col: 8
    }, {
      dataIndex: 'manfDesc',
      title: '生产厂商',
      typeCode: 'SearchSelect',
      valueindex: 'descripts', // 下拉value对应的值
      valueID: 'manfID', // 选择的valueID
      onSearch: this.handleChargeSearch,
      detailItem: manfSelectData,
      disabled: moreHospFlag,
      loading: 'N',
      col: 8,
      trigger: 'enter', // 搜索框触发查询数据方式
    }, {
      dataIndex: 'appPreFixID',
      title: '批准文号前缀',
      typeCode: 'Select',
      detailItem: selectData && 'approvalPrefix' in selectData ? selectData.approvalPrefix : [],
      disabled: moreHospFlag,
      col: 8
    }, {
      dataIndex: 'apprNo',
      title: '批准文号',
      typeCode: 'Input',
      disabled: moreHospFlag,
      col: 8
    }, {
      dataIndex: 'apprExpDate',
      title: '批准文号有效期',
      typeCode: 'Date',
      default: 0,
      disabled: moreHospFlag,
      col: 8
    }, {
      dataIndex: 'standardCode',
      title: '药品本位码',
      typeCode: 'Input',
      disabled: moreHospFlag,
      col: 8
    }, {
      dataIndex: 'regCertNo',
      title: '进口药品注册证号',
      typeCode: 'Input',
      // disabled: moreHospFlag,
      col: 8,
    }, {
      dataIndex: 'regCertExpDate',
      title: '注册证有效期',
      typeCode: 'Date',
      default: 0,
      // disabled: moreHospFlag,
      col: 8
    }, {
      dataIndex: 'regLabel',
      title: '注册商标',
      typeCode: 'Input',
      // disabled: moreHospFlag,
      col: 8
    }, {
      dataIndex: 'certificat',
      title: '认证情况',
      typeCode: 'Select',
      detailItem: selectData && 'pubCertification' in selectData ? selectData.pubCertification : [],
      // disabled: moreHospFlag,
      col: 8
    }];
    this.setState({ approvalNumberFormData: formData })
  }

  // 厂家搜索事件
  handleChargeSearch = (value) => {
    this.state.approvalNumberFormData[1].loading = 'Y';
    this.setState({});
    this.fetchCharge(value, manfSelectData => this.setState({ manfSelectData }, () => {
      this.state.approvalNumberFormData[1].loading = 'N';
      this.getApprovalNumberFormData();
    }));
  }

  fetchCharge = (value, callback) => {
    value = value.replace(/^\s+/, '').replace(/\s+$/, '');
    if (value === '') {
      let resData = [];
      callback(resData);
      return;
    }
    let data = {
      params: [{
        desc: value
      }],
      pagination: [{ // 默认只查询600条
        pageSize: 500,
        currentPage: 1
      }]
    }
    $http.post('urlS', this, {
      code: '02020352',
      data,
      success: function (res) {
        if (res.errorCode === '0') {
          let manfData = res?.manf || [];
          callback(res.manf);
          if (!(manfData && manfData.length > 0)) {
            message.error('未查询到相关数据');
          }
        }
      }
    })
  }

  render() {
    let { basicFormData, approvalNumberFormData, pharmacyFormData, medAliasArr, orderItemAliasColumns, medAutArr, limitColumns, rowData } = this.state;
    return (
      <div className='required-inventory-item'>
        <div className="card-title-left-icon rii-card-title"><div></div>医嘱项基本信息</div>
        <DynamicRenderingForm
          formItemCol={{ labelCol: 8, wrapperCol: 16 }}
          rowData={rowData && 'medObj' in rowData ? rowData.medObj : {}}
          formData={basicFormData}
          onRef={ref => this.basicInfoRef = ref}
        />
        <div className="card-title-left-icon rii-card-title"><div></div>批准文号</div>
        <DynamicRenderingForm
          formItemCol={{ labelCol: 8, wrapperCol: 16 }}
          rowData={rowData && 'approvalObj' in rowData ? rowData.approvalObj : {}}
          formData={approvalNumberFormData}
          onRef={ref => this.approvalNumberRef = ref}
        />
        <div className="card-title-left-icon rii-card-title"><div></div>药学相关</div>
        <DynamicRenderingForm
          formItemCol={{ labelCol: 8, wrapperCol: 16 }}
          rowData={rowData && 'medObj' in rowData ? rowData.medObj : {}}
          formData={pharmacyFormData}
          onRef={ref => this.pharmacyRef = ref}
        />
        <div className="card-title-left-icon rii-card-title"><div></div>医嘱别名</div>
        <UnitMaintenance
          dataSource={medAliasArr}
          columns={orderItemAliasColumns}
          title="医嘱项别名"
          desc=""
          result=""
          deleteCode="02020228"
          recordID="medAliasID" // 删除传给后台的字段
          rowIDName="aliasID" // 对应的数据ID
          onRef={ref => this.orderItemAliasyRef = ref}
        />
        <div className="card-title-left-icon rii-card-title"><div></div>限制用药</div>
        <UnitMaintenance
          dataSource={medAutArr}
          columns={limitColumns}
          title="限制用药"
          desc=""
          result=""
          deleteCode="02020229"
          recordID="medAutID"
          onRef={ref => this.limitRef = ref}
          editCurrentLine={this.editCurrentLine}
        />
      </div>
    )
  }
};

export default AllOrderItem;