/*
 * Creater:     shizi
 * CreateDate:  2022/03/31
 * Describe：   新增药品
 * */
import React from 'react';
import { Card, message, Tabs, Spin } from 'antd';
import { Util } from 'tools';
import '../style/new-drug.less';

import RequiredOrderItem from './RequiredOrderItem'; // 必填医嘱项
import RequiredInventoryItem from './RequiredInventoryItem'; // 必填库存项
import AllOrderItem from './AllOrderItem'; // 全部医嘱项
import AllInventoryItem from './AllInventoryItem'; // 全部库存项


const { TabPane } = Tabs;

class AddDrugs extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      activeKey: '1',
      loading: false,
      rowData: {
        medAliasArr: [], // 医嘱项别名
        medAutArr: [], // 限制用药
        doseUnitArr: [], // 剂量单位/等效单位
        dispUomArr: [], // 发药单位
        stockAliasArr: [], // 库存项别名
      }
    }
  }

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

  // tabs切换
  handleTabsChange = key => {
    let currentFormData = this.getCurrentFormData();
    if ('medAliasEditingKey' in currentFormData && currentFormData.medAliasEditingKey) { // 医嘱别名
      message.error("医嘱别名表格列处于编辑状态，请先保存或取消该操作。");
      return;
    }
    if ('medAutEditingKey' in currentFormData && currentFormData.medAutEditingKey) { // 限制用药
      message.error("限制用药表格列处于编辑状态，请先保存或取消该操作。");
      return;
    }
    if ('doseUnitEditingKey' in currentFormData && currentFormData.doseUnitEditingKey) { // 剂量单位
      message.error("剂量单位表格列处于编辑状态，请先保存或取消该操作。");
      return;
    }
    if ('dispUomEditingKey' in currentFormData && currentFormData.dispUomEditingKey) { // 更多发药单位
      message.error("更多发药单位表格列处于编辑状态，请先保存或取消该操作。");
      return;
    }
    if ('dispUomEditingKey' in currentFormData && currentFormData.dispUomEditingKey) { // 库存项别名
      message.error("更多发药单位表格列处于编辑状态，请先保存或取消该操作。");
      return;
    }
    if ('stockAliasEditingKey' in currentFormData && currentFormData.stockAliasEditingKey) { // 库存项别名
      message.error("库存项别名表格列处于编辑状态，请先保存或取消该操作。");
      return;
    }
    this.setState({ activeKey: key, rowData: { ...currentFormData } }, () => {
      this.setValuesForSubcomponents(true);
    });
  }

  // 查询医嘱维护的详细信息
  queryRowDataByID = async (id, isCopy) => {
    try {
      let data = {
        params: [{
          medID: id
        }]
      }
      let res = await React.$asyncPost(this, '02020378', data);
      let rowData = { ...this.state.rowData, ...res.result };
      let medObj = 'medObj' in rowData && rowData.medObj ? rowData.medObj : {};
      let stockObj = 'stockObj' in rowData && rowData.stockObj ? rowData.stockObj : {};
      let chargeObj = 'chargeObj' in rowData && rowData.chargeObj ? rowData.chargeObj : {};
      let disabled = 'disabledFlag' in rowData && rowData.disabledFlag ? rowData.disabledFlag : 'N'; // 部分输入域根据后台标志控制是否可编辑
      let moreHospFlag = 'moreHospFlag' in rowData && rowData.moreHospFlag ? rowData.moreHospFlag : 'N'; // 部分输入域根据后台标志控制是否可编辑
      let purUnitID = 'purUnitID' in stockObj && stockObj.purUnitID ? stockObj.purUnitID : ''; // 剂量单位
      let baseUnitID = 'baseUnitID' in stockObj && stockObj.baseUnitID ? stockObj.baseUnitID : ''; // 对照单位
      if (isCopy && 'medSubCatID' in medObj && medObj.medSubCatID) {
        let medCodeData = {
          params: [{
            subCatID: medObj.medSubCatID
          }]
        }
        let medCodeRes = await React.$asyncPost(this, '02020379', medCodeData);
        medObj.medCode = medCodeRes.result;
      }
      rowData.medObj = {
        ...medObj, // 医嘱项信息
        ...rowData.drugObj, // 药品信息
        medID: isCopy ? '' : ('medID' in medObj && medObj.medID ? medObj.medID : ''),
        disabled: isCopy ? 'N' : disabled,
        moreHospFlag: isCopy ? 'N' : moreHospFlag,
      };
      rowData.stockObj = {
        ...stockObj, // 库存项信息
        ...chargeObj, // 收费项信息
        stockID: isCopy ? '' : ('stockID' in stockObj && stockObj.stockID ? stockObj.stockID : ''),
        stockCode: 'stockCode' in stockObj && stockObj.stockCode ? stockObj.stockCode : medObj?.medCode, // 库存项代码
        // stockDesc: 'stockDesc' in stockObj && stockObj.stockDesc ? stockObj.stockDesc : medObj?.medDesc, // 库存项描述
        stockDesc: medObj?.medDesc, // 库存项描述
        itemCode: 'itemCode' in chargeObj && chargeObj.itemCode ? chargeObj.itemCode : medObj?.medCode, // 收费项代码
        // itemDesc: 'itemDesc' in chargeObj && chargeObj.itemDesc ? chargeObj.itemDesc : medObj?.medDesc, // 收费项描述
        itemDesc: medObj?.medDesc, // 收费项描述
        disabled: isCopy ? 'N' : disabled,
        moreHospFlag: isCopy ? 'N' : moreHospFlag,
      };
      rowData.chargeObj = {
        ...chargeObj, // 收费项信息
        itemID: isCopy ? '' : ('itemID' in chargeObj && chargeObj.itemID ? chargeObj.itemID : ''),
        itemCode: chargeObj && 'itemCode' in chargeObj && chargeObj.itemCode ? chargeObj.itemCode : medObj?.medCode, // 收费项代码
        // itemDesc: chargeObj && 'itemDesc' in chargeObj && chargeObj.itemDesc ? chargeObj.itemDesc : medObj?.medDesc, // 收费项描述
        itemDesc: medObj?.medDesc, // 收费项描述
        moreHospFlag: isCopy ? 'N' : moreHospFlag,
      };
      rowData.id = isCopy ? '' : id;
      rowData.medAliasArr = 'medAliasArr' in rowData && rowData.medAliasArr ? this.handleModifyRowID(isCopy, rowData.medAliasArr, 'aliasID') : []; // 医嘱别名
      rowData.medAutArr = 'medAutArr' in rowData && rowData.medAutArr ? this.handleModifyRowID(isCopy, rowData.medAutArr, 'medAutID') : []; // 限制用药
      rowData.doseUnitArr = 'doseUnitArr' in rowData && rowData.doseUnitArr ? this.handleModifyRowID(isCopy, this.handleUpDataTableData(rowData.doseUnitArr, stockObj?.baseUnitDesc), 'doseID') : []; // 剂量单位
      rowData.dispUomArr = 'dispUomArr' in rowData && rowData.dispUomArr ? this.handleModifyRowID(isCopy, this.handleUpDataTableData(rowData.dispUomArr, stockObj?.baseUnitDesc), 'dispUomID') : []; // 更多发药单位
      rowData.stockAliasArr = 'stockAliasArr' in rowData && rowData.stockAliasArr ? this.handleModifyRowID(isCopy, rowData.stockAliasArr, 'aliasID') : []; // 库存项别名
      rowData.billUnitSelectData = this.getBillUnitSelectData(purUnitID, baseUnitID);
      // console.log("rowData", rowData)
      this.setState({ rowData }, () => {
        this.setValuesForSubcomponents();
      });
    } catch (error) {
      this.props.resetFields && this.props.resetFields();
    }
  }

  // 根据返回的数据获取临时发药单位
  getBillUnitSelectData = (purUnitID, baseUnitID) => {
    const { selectData } = this.props;
    let unitSelectData = selectData && 'unit' in selectData ? selectData.unit : [];
    let billUnitSelectData = [];
    for (var i = 0; i < unitSelectData.length; i++) {
      let itemID = unitSelectData[i].id;
      if (itemID === purUnitID || itemID === baseUnitID) {
        billUnitSelectData.push({
          ...unitSelectData[i],
          dataIndex: itemID === purUnitID ? 'purUnitID' : 'baseUnitID'
        })
      }
    }
    return billUnitSelectData;
  }

  // 更新表格数据
  handleUpDataTableData = (data, value, dataIndex = 'baseUnitDesc') => {
    let dataSource = data && data.length > 0 ? JSON.parse(JSON.stringify(data)) : [];
    if (value) {
      for (var i = 0; i < dataSource.length; i++) {
        dataSource[i][dataIndex] = value || '';
      }
    }
    return dataSource;
  }

  // 另存则修改表格行id
  handleModifyRowID = (isModify, data, dataIndex) => {
    let dataSource = data && data.length > 0 ? JSON.parse(JSON.stringify(data)) : [];
    if (isModify) {
      for (var i = 0; i < dataSource.length; i++) {
        dataSource[i][dataIndex] = '';
      }
    }
    return dataSource;
  }

  // 设置子组件的值 - isgetMedicalOrder：tabs切换时
  setValuesForSubcomponents = (isgetMedicalOrder) => {
    let stockObj = 'stockObj' in this.state.rowData ? this.state.rowData.stockObj : {};
    let basicUnitObj = {
      purUnitID: stockObj && 'purUnitID' in stockObj ? stockObj.purUnitID : '', // 入库单位
      baseFac: stockObj && 'baseFac' in stockObj ? stockObj.baseFac : '', // 转换系数
      baseUnitID: stockObj && 'baseUnitID' in stockObj ? stockObj.baseUnitID : '', // 住院长期发药单位
    };
    let valuationUnitObj = {
      purUnitID: stockObj && 'purUnitID' in stockObj ? stockObj.purUnitID : '', // 入库单位
      billFac: stockObj && 'billFac' in stockObj ? stockObj.billFac : '', // 转换系数
      billUnitID: stockObj && 'billUnitID' in stockObj ? stockObj.billUnitID : '', // 门诊/住院临时发药单位
    };
    if (this.state.activeKey === '2') {
      this.AllInventoryChild && this.AllInventoryChild.setTableData();
      this.AllOrderItemChild && this.AllOrderItemChild.setTableData();
      this.AllInventoryChild && this.AllInventoryChild.basicUnitConversionPreview('', '', basicUnitObj); // 住院长期发药单位及系数预览计算
      this.AllInventoryChild && this.AllInventoryChild.valuationUnitConversionPreview('', '', valuationUnitObj); // 门诊/住院零时医嘱发药单位及系数预览计算
      isgetMedicalOrder && this.AllOrderItemChild && this.AllOrderItemChild.getMedicalOrder(); // 获取WHO单位下拉数据
    } else {
      this.RInventoryChild && this.RInventoryChild.setTableData();
      this.RInventoryChild && this.RInventoryChild.basicUnitConversionPreview('', '', basicUnitObj); // 住院长期发药单位及系数预览计算
      this.RInventoryChild && this.RInventoryChild.valuationUnitConversionPreview('', '', valuationUnitObj); // 门诊/住院零时医嘱发药单位及系数预览计算
    }
  }

  // 获取当前tabs表单数据
  getCurrentFormData = () => {
    const { selectData } = this.props;
    let { activeKey, rowData } = this.state;
    let currentFormData = {};
    let oldMedObj = rowData && 'medObj' in rowData ? rowData.medObj : {};
    let oldStockObj = rowData && 'stockObj' in rowData ? rowData.stockObj : {};
    let oldChargeObj = rowData && 'chargeObj' in rowData ? rowData.chargeObj : {};
    if (activeKey === '1') {
      let rOrderItem = this.ROrderItemChild && this.ROrderItemChild.getFieldsValue();
      let rInventory = this.RInventoryChild && this.RInventoryChild.getFieldsValue();
      let medObj = rOrderItem && 'medObj' in rOrderItem ? rOrderItem.medObj : {};
      let stockObj = rInventory && 'stockObj' in rInventory ? rInventory.stockObj : {};
      let baseUnitDesc = stockObj && stockObj.baseUnitID ? Util.returnDataCccordingToAttributes(selectData && 'unit' in selectData ? selectData.unit : [], stockObj.baseUnitID)?.descripts : '';
      currentFormData = {
        ...rowData,
        ...rOrderItem,
        ...rInventory,
        medAutArr: 'medAutArr' in rowData && rowData.medAutArr ? rowData.medAutArr : [],
        medAliasArr: 'medAliasArr' in rowData && rowData.medAliasArr ? rowData.medAliasArr : [],
        doseUnitArr: 'doseUnitArr' in rInventory && rInventory.doseUnitArr ? rInventory.doseUnitArr : ('doseUnitArr' in rowData && rowData.doseUnitArr ? rowData.doseUnitArr : []),
        dispUomArr: 'dispUomArr' in rInventory && rInventory.dispUomArr ? rInventory.dispUomArr : ('dispUomArr' in rowData && rowData.dispUomArr ? rowData.dispUomArr : []),
        stockAliasArr: 'stockAliasArr' in rowData && rowData.stockAliasArr ? rowData.stockAliasArr : [],
        medObj: {
          ...oldMedObj,
          ...medObj,
        },
        stockObj: {
          ...oldStockObj,
          ...stockObj,
          stockCode: rowData && rowData.id && oldStockObj && oldStockObj.stockCode ? oldStockObj.stockCode : (medObj?.medCode || undefined),
          // stockDesc: rowData && rowData.id && oldStockObj && oldStockObj.stockDesc ? oldStockObj.stockDesc : (medObj?.medDesc || undefined),
          stockDesc: medObj?.medDesc,
          itemCode: rowData && rowData.id && oldChargeObj && oldChargeObj.itemCode ? oldChargeObj.itemCode : (medObj?.medCode || undefined),
          // itemDesc: rowData && rowData.id && oldChargeObj && oldChargeObj.itemDesc ? oldChargeObj.itemDesc : (medObj?.medDesc || undefined),
          itemDesc: medObj?.medDesc,
          baseUnitDesc, // 住院长期发药单位描述
        },
        chargeObj: {
          ...oldChargeObj,
          itemCode: rowData && rowData.id && oldChargeObj && oldChargeObj.itemCode ? oldChargeObj.itemCode : (medObj?.medCode || undefined),
          // itemDesc: rowData && rowData.id && oldChargeObj && oldChargeObj.itemDesc ? oldChargeObj.itemDesc : (medObj?.medDesc || undefined),
          itemDesc: medObj?.medDesc
        }
      };
    } else {
      let allOrderItem = this.AllOrderItemChild && this.AllOrderItemChild.getFieldsValue();
      let allInventory = this.AllInventoryChild && this.AllInventoryChild.getFieldsValue();
      let medObj = allOrderItem && 'medObj' in allOrderItem ? allOrderItem.medObj : {};
      let stockObj = allInventory && 'stockObj' in allInventory ? allInventory.stockObj : {};
      let chargeObj = allInventory && 'chargeObj' in allInventory ? allInventory.chargeObj : {};
      let baseUnitDesc = stockObj && stockObj.baseUnitID ? Util.returnDataCccordingToAttributes(selectData && 'unit' in selectData ? selectData.unit : [], stockObj.baseUnitID)?.descripts : '';
      currentFormData = {
        ...rowData,
        ...allOrderItem,
        ...allInventory,
        medObj: {
          ...oldMedObj,
          ...medObj,
        },
        stockObj: {
          ...oldStockObj,
          ...stockObj,
          stockCode: rowData && rowData.id && oldStockObj && oldStockObj.stockCode ? oldStockObj.stockCode : (medObj?.medCode || undefined),
          // stockDesc: rowData && rowData.id && oldStockObj && oldStockObj.stockDesc ? oldStockObj.stockDesc : (medObj?.medDesc || undefined),
          stockDesc: medObj?.medDesc,
          baseUnitDesc, // 住院长期发药单位描述
        },
        chargeObj: {
          ...oldChargeObj,
          ...chargeObj,
          itemCode: rowData && rowData.id && oldChargeObj && oldChargeObj.itemCode ? oldChargeObj.itemCode : (medObj?.medCode || undefined),
          // itemDesc: rowData && rowData.id && oldChargeObj && oldChargeObj.itemDesc ? oldChargeObj.itemDesc : (medObj?.medDesc || undefined),
          itemDesc: medObj?.medDesc
        }
      };
    }
    return currentFormData;
  }

  // 保存
  handleSave = () => {
    let { activeKey, rowData } = this.state;
    let currentFormData = {};
    if (activeKey === '1') {
      this.ROrderItemChild && this.ROrderItemChild.handleSave();
      this.RInventoryChild && this.RInventoryChild.handleSave();
    } else {
      this.AllOrderItemChild && this.AllOrderItemChild.handleSave();
      this.AllInventoryChild && this.AllInventoryChild.handleSave();
    }
    setTimeout(() => {
      let oldMedObj = rowData && 'medObj' in rowData ? rowData.medObj : {}; // 医嘱项
      let oldDrugObj = rowData && 'drugObj' in rowData ? rowData.drugObj : {}; // 药学相关
      let oldStockObj = rowData && 'stockObj' in rowData ? rowData.stockObj : {}; // 库存项
      let oldChargeObj = rowData && 'chargeObj' in rowData ? rowData.chargeObj : {}; // 收费项信息
      let oldStoreObj = rowData && 'storeObj' in rowData ? rowData.storeObj : {}; // 存储条件
      let oldHospStockObj = rowData && 'hospStockObj' in rowData ? rowData.hospStockObj : {}; // 招投标信息
      let oldApprovalObj = rowData && 'approvalObj' in rowData ? rowData.approvalObj : {}; // 批准文号
      if (activeKey === '1') {
        const rOrderItem = this.ROrderItemChild && this.ROrderItemChild.getStaticFieldsValue();
        const rInventory = this.RInventoryChild && this.RInventoryChild.getStaticFieldsValue();
        let medObj = rOrderItem && 'medObj' in rOrderItem ? rOrderItem.medObj : {};
        let stockObj = rInventory && 'stockObj' in rInventory ? rInventory.stockObj : {};
        currentFormData = {
          ...rowData,
          ...rOrderItem,
          ...rInventory,
          medObj: {
            ...oldMedObj,
            ...medObj,
            medID: 'medID' in oldMedObj && oldMedObj.medID ? oldMedObj.medID : '',
            medUnitID: stockObj.billUnitID, // 计价单位/门诊/住院临时发药单位
          },
          drugObj: { // 药学相关
            medSpec: medObj.medSpec, // 规格含量
            ...oldDrugObj,
          },
          stockObj: {
            ...oldStockObj,
            ...stockObj,
            stockID: 'stockID' in oldStockObj && oldStockObj.stockID ? oldStockObj.stockID : '',
            stockCode: 'stockCode' in stockObj && stockObj.stockCode ? stockObj.stockCode : medObj?.medCode, // 库存项代码
            // stockDesc: 'stockDesc' in stockObj && stockObj.stockDesc ? stockObj.stockDesc : medObj?.medDesc, // 库存项描述
            stockDesc: medObj?.medDesc, // 库存项描述
          },
          chargeObj: { // 收费项信息
            ...oldChargeObj,
            itemTCID: stockObj.itemTCID, // 物价分类
            itemECID: stockObj.itemECID, // 核算项目分类
            itemICID: stockObj.itemICID, // 住院收据费用
            itemOCID: stockObj.itemOCID, // 门诊收据费用
            itemACID: stockObj.itemACID, // 会计科目
            itemMCID: stockObj.itemMCID, // 病案分类
            itemPCID: stockObj.itemPCID, // 收费分类
            itemStDate: stockObj.itemStDate, // 生效日期
            itemEndDate: stockObj.itemEndDate, // 生效日期
            itemCode: 'itemCode' in oldChargeObj && oldChargeObj.itemCode ? oldChargeObj.itemCode : medObj?.medCode, // 收费项代码
            // itemDesc: 'itemDesc' in oldChargeObj && oldChargeObj.itemDesc ? oldChargeObj.itemDesc : medObj?.medDesc, // 收费项描述
            itemDesc: medObj?.medDesc, // 收费项描述
          },
          storeObj: oldStoreObj, // 存储条件
          hospStockObj: { // 本院信息/招投标信息
            startDate: stockObj.itemStDate, // 生效日期
            ...oldHospStockObj,
          },
          approvalObj: oldApprovalObj, // 批准文号
        };
        if ('medObj' in currentFormData && 'medError' in currentFormData.medObj && currentFormData.medObj.medError) { // 医嘱项校验失败
          message.error("医嘱项必填数据校验失败。");
          return;
        }
        if ('stockObj' in currentFormData && 'stockError' in currentFormData.stockObj && currentFormData.stockObj.stockError) { // 库存项校验失败
          message.error("库存项必填数据校验失败。");
          return;
        }
        if ('stockObj' in currentFormData && 'basicError' in currentFormData.stockObj && currentFormData.stockObj.basicError) { // 住院长期发药单位及系数校验失败
          message.error("住院长期发药单位及系数必填数据校验失败。");
          return;
        }
        if ('stockObj' in currentFormData && 'valuationError' in currentFormData.stockObj && currentFormData.stockObj.valuationError) { // 门诊/住院零时医嘱发药单位及系数校验失败
          message.error("住门诊/住院零时医嘱发药单位及系数必填数据校验失败。");
          return;
        }
        if (('doseUnitEditingKey' in currentFormData) && currentFormData.doseUnitEditingKey) { // 剂量单位
          message.error("剂量单位表格列处于编辑状态，请先保存或取消该操作。");
          return;
        }
        if (('dispUomEditingKey' in currentFormData) && currentFormData.dispUomEditingKey) { // 更多发药单位
          message.error("更多发药单位表格列处于编辑状态，请先保存或取消该操作。");
          return;
        }
        // if (!('doseUnitArr' in currentFormData && currentFormData.doseUnitArr.length > 0)) {
        //   message.error("请新增至少一条剂量单位数据");
        //   return;
        // }
        // if (!('dispUomArr' in currentFormData && currentFormData.dispUomArr.length > 0)) {
        //   message.error("请新增至少一条更多发药单位数据");
        //   return;
        // }
      } else {
        let allOrderItem = this.AllOrderItemChild && this.AllOrderItemChild.getStaticFieldsValue();
        let allInventory = this.AllInventoryChild && this.AllInventoryChild.getStaticFieldsValue();
        let medObj = allOrderItem && 'medObj' in allOrderItem ? allOrderItem.medObj : {};
        let stockObj = allInventory && 'stockObj' in allInventory ? allInventory.stockObj : {};
        let chargeObj = allInventory && 'chargeObj' in allInventory ? allInventory.chargeObj : {};
        currentFormData = {
          ...allOrderItem,
          ...allInventory,
          medObj: {
            ...oldMedObj,
            ...medObj,
            medUnitID: stockObj.billUnitID, // 计价单位/门诊/住院临时发药单位
            medID: 'medID' in oldMedObj && oldMedObj.medID ? oldMedObj.medID : '',
          },
          stockObj: {
            ...oldStockObj,
            ...stockObj,
            stockID: 'stockID' in oldStockObj && oldStockObj.stockID ? oldStockObj.stockID : '',
            stockCode: 'stockCode' in stockObj && stockObj.stockCode ? stockObj.stockCode : medObj?.medCode, // 库存项代码
            stockDesc: 'stockDesc' in stockObj && stockObj.stockDesc ? stockObj.stockDesc : medObj?.medDesc, // 库存项描述
          },
          chargeObj: { // 收费项信息
            ...oldChargeObj,
            ...chargeObj,
            itemID: 'itemID' in oldChargeObj && oldChargeObj.itemID ? oldChargeObj.itemID : '',
            itemCode: 'itemCode' in chargeObj && chargeObj.itemCode ? chargeObj.itemCode : medObj?.medCode, // 收费项代码
            itemDesc: 'itemDesc' in chargeObj && chargeObj.itemDesc ? chargeObj.itemDesc : medObj?.medDesc, // 收费项描述
          }
        };
        if ('medObj' in currentFormData && 'medError' in currentFormData.medObj && currentFormData.medObj.medError) { // 医嘱项校验失败
          message.error("医嘱项必填数据校验失败。");
          return;
        }
        if ('medObj' in currentFormData && 'pharmacyError' in currentFormData.drugObj && currentFormData.drugObj.pharmacyError) { // 药学相关校验失败
          message.error("药学相关信息必填数据校验失败。");
          return;
        }
        if ('stockObj' in currentFormData && 'stockError' in currentFormData.stockObj && currentFormData.stockObj.stockError) { // 库存项校验失败
          message.error("库存项必填数据校验失败。");
          return;
        }
        if ('stockObj' in currentFormData && 'chargeItemError' in currentFormData.chargeObj && currentFormData.chargeObj.chargeItemError) { // 收费项信息
          message.error("收费项必填数据校验失败。");
          return;
        }
        if ('stockObj' in currentFormData && 'basicError' in currentFormData.stockObj && currentFormData.stockObj.basicError) { // 住院长期发药单位及系数校验失败
          message.error(" 住院长期发药单位及系数必填数据校验失败。");
          return;
        }
        if ('stockObj' in currentFormData && 'valuationError' in currentFormData.stockObj && currentFormData.stockObj.valuationError) { // 门诊/住院零时医嘱发药单位及系数校验失败
          message.error("住门诊/住院零时医嘱发药单位及系数必填数据校验失败。");
          return;
        }
        if ('medAliasEditingKey' in currentFormData && currentFormData.medAliasEditingKey) { // 医嘱别名
          message.error("医嘱别名表格列处于编辑状态，请先保存或取消该操作。");
          return;
        }
        if ('medAutEditingKey' in currentFormData && currentFormData.medAutEditingKey) { // 限制用药
          message.error("限制用药表格列处于编辑状态，请先保存或取消该操作。");
          return;
        }
        if ('doseUnitEditingKey' in currentFormData && currentFormData.doseUnitEditingKey) { // 剂量单位
          message.error("剂量单位表格列处于编辑状态，请先保存或取消该操作。");
          return;
        }
        if ('dispUomEditingKey' in currentFormData && currentFormData.dispUomEditingKey) { // 更多发药单位
          message.error("更多发药单位表格列处于编辑状态，请先保存或取消该操作。");
          return;
        }
        if ('dispUomEditingKey' in currentFormData && currentFormData.dispUomEditingKey) { // 库存项别名
          message.error("更多发药单位表格列处于编辑状态，请先保存或取消该操作。");
          return;
        }
        // if (!('doseUnitArr' in currentFormData && currentFormData.doseUnitArr.length > 0)) {
        //   message.error("请新增至少一条剂量单位数据");
        //   return;
        // }
        // if (!('dispUomArr' in currentFormData && currentFormData.dispUomArr.length > 0)) {
        //   message.error("请新增至少一条更多发药单位数据");
        //   return;
        // }
        if ('stockAliasEditingKey' in currentFormData && currentFormData.stockAliasEditingKey) { // 库存项别名
          message.error("库存项别名表格列处于编辑状态，请先保存或取消该操作。");
          return;
        }
      }
      this.setState({ loading: true });
      this.handleSaveOrderItems(currentFormData);
    }, 300)
  }

  // 保存医嘱项信息
  handleSaveOrderItems = async (currentFormData) => {
    try {
      let data = {
        params: [{
          ...currentFormData
        }]
      }
      let res = await React.$asyncPost(this, '02020380', data);
      message.success(res.errorMessage ? res.errorMessage : '保存成功');
      let rowID = res && res.result ? String(res.result) : '';
      this.setState({ loading: false, rowData: { ...this.state.rowData, id: rowID } });
      this.props.getTableData && this.props.getTableData(rowID);
    } catch (error) {
      this.setState({ loading: false });
    }
  }

  // 重置
  resetFields = () => {
    let { activeKey } = this.state;
    if (activeKey === '1') {
      this.ROrderItemChild && this.ROrderItemChild.resetFields();
      this.RInventoryChild && this.RInventoryChild.resetFields();
    } else {
      this.AllOrderItemChild && this.AllOrderItemChild.resetFields();
      this.AllInventoryChild && this.AllInventoryChild.resetFields();
    }
    this.setState({ rowData: {} });
  }

  // 设置库存项、收费项代码描述
  setMedData = (values, dataIndex) => {
    this.AllInventoryChild && this.AllInventoryChild.setMedData(values, dataIndex);
  }

  render() {
    let { height, selectData } = this.props;
    let { activeKey, rowData, loading } = this.state;
    return (
      <Spin spinning={loading} tip='数据保存中...'>
        <Tabs activeKey={activeKey} onChange={this.handleTabsChange}>
          <TabPane tab="基本信息" key="1">
            <div className='ndi-tab-content scroll-bar-style' style={{ height: height - 60 + 'px' }}>
              <Card
                type="inner"
                title={<div>医嘱项信息</div>}
                size="small"
              >
                <RequiredOrderItem
                  activeKey={activeKey}
                  rowData={'medObj' in rowData && rowData.medObj ? { ...rowData.medObj, id: rowData?.id || '' } : {}}
                  selectData={selectData}
                  onRef={ref => this.ROrderItemChild = ref}
                />
              </Card>
              <Card
                type="inner"
                title={<div>库存项信息</div>}
                size="small"
                style={{ marginTop: '16px' }}
              >
                <RequiredInventoryItem
                  activeKey={activeKey}
                  rowData={rowData}
                  selectData={selectData}
                  onRef={ref => this.RInventoryChild = ref}
                />
              </Card>
            </div>
          </TabPane>
          <TabPane tab="全部信息" key="2">
            <div className='ndi-tab-content scroll-bar-style' style={{ height: height - 60 + 'px' }}>
              <Card
                type="inner"
                title={<div>医嘱项信息</div>}
                size="small"
              >
                <AllOrderItem
                  activeKey={activeKey}
                  rowData={rowData}
                  selectData={selectData}
                  setMedData={this.setMedData}
                  onRef={ref => this.AllOrderItemChild = ref}
                />
              </Card>
              <Card
                type="inner"
                title={<div>库存项信息</div>}
                size="small"
                style={{ marginTop: '16px' }}
              >
                <AllInventoryItem
                  activeKey={activeKey}
                  rowData={rowData}
                  selectData={selectData}
                  onRef={ref => this.AllInventoryChild = ref}
                />
              </Card>
            </div>
          </TabPane>
        </Tabs>
      </Spin>
    )
  }
};
export default AddDrugs;