/*
 * Creater:     shizi
 * CreateDate:  2022/03/31
 * Describe：   全部库存项
 * */
import React, { Component } from 'react';
import Moment from 'moment';
import { Util } from 'tools';
import { $http } from 'containers/config/https';
import '../style/new-drug.less';

import DynamicRenderingForm from 'pages/common/DynamicRenderingForm';
import UnitSetting from './UnitSetting'; // 单位设置
import UnitMaintenance from './UnitMaintenance'; // 子table列表维护

class AllInventoryItem extends Component {
  constructor(props) {
    super(props);
    this.state = {
      rowData: {},
      selectData: {},
      activeKey: '',
      formData: [], // 库存项基本信息表单数据
      chargeItemFormData: [], // 收费项信息表单数据
      basicUnitFormData: [], // 入库单位与基本单位转换系数
      valuationUnitFormData: [], // 入库单位与计价单位转换系数
      basicPreview: '入库单位=系数+发药单位', // 住院长期预览
      valuationPreview: '入库单位=系数+发药单位', // 门诊/住院临时预览
      equivalentColumns: [], // 剂量单位表头数据
      doseUnitArr: [], // 剂量单位表格数据
      dispUomArr: [], // 发药单位表头数据
      dispensingColumns: [], // 发药单位表格数据
      storageConditionFormData: [], // 存储条件表单数据
      biddingFormData: [], // 招投标表单数据
      stockAliasArr: [], // 库存项列表数据
      inventoryItemAliasColumns: [], // 库存项表头数据
      baseUnitDesc: '', // 住院长期发药单位描述 - 对照单位
      chargeSelectData: [], // 关联收费项下拉数据
      equivalentPreview: '等效数量+剂量单位=对照单位', // 剂量单位预览
      dispensingPreview: '发药单位+系数=对照单位', // 更多发药单位预览
      billUnitSelectData: [], // 临时发药单位下拉数据
    };
    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 {
        baseUnitDesc: nextProps && 'rowData' in nextProps && 'stockObj' in nextProps.rowData && 'baseUnitDesc' in nextProps.rowData.stockObj ? nextProps.rowData.stockObj.baseUnitDesc : '',
        billUnitSelectData: nextProps && 'rowData' in nextProps && 'billUnitSelectData' in nextProps.rowData ? nextProps.rowData.billUnitSelectData : [], // 临时发药单位
        rowData: nextProps.rowData,
        selectData: nextProps.selectData,
        activeKey: nextProps.activeKey,
        doseUnitArr: nextProps && 'rowData' in nextProps && 'doseUnitArr' in nextProps.rowData ? nextProps.rowData.doseUnitArr : [], // 剂量单位/等效单位
        dispUomArr: nextProps && 'rowData' in nextProps && 'dispUomArr' in nextProps.rowData ? nextProps.rowData.dispUomArr : [], // 发药单位
        stockAliasArr: nextProps && 'rowData' in nextProps && 'stockAliasArr' in nextProps.rowData ? nextProps.rowData.stockAliasArr : [], // 库存项别名
      }
    }
    //不需要更新状态，返回null
    return null
  }

  componentDidMount() {
    this.getFormData(); // 基本信息form数据
    this.getChargeItemFormData(); // 收费项信息
    this.getBasicUnitFormData(); // 入库单位与基本单位转换系数form数据
    this.getValuationUnitFormData(); // 入库单位与计价单位转换系数form数据
    this.getEquivalentColumnsData(); // 获取剂量单位表头数据
    this.getDispensingColumns(); // 获取发药单位表格数据
    this.getBiddingFormData(); // 招投标信息表单数据
    this.getStorageConditionFormData(); // 存储条件表单数据
    this.getInventoryItemAliasColumns(); // 招投标信息表单数据
    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.getEquivalentColumnsData(); // 获取剂量单位表头数据
      this.getDispensingColumns(); // 获取发药单位表格数据
    }
    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(); // 基本信息form数据
      this.getChargeItemFormData(); // 收费项信息
      this.getBasicUnitFormData(); // 入库单位与基本单位转换系数form数据
      this.getValuationUnitFormData(); // 入库单位与计价单位转换系数form数据
      this.getBiddingFormData(); // 招投标信息表单数据
      this.getStorageConditionFormData(); // 存储条件表单数据
      this.getInventoryItemAliasColumns(); // 招投标信息表单数据
    }
    if ((prevProps.rowData.doseUnitArr !== undefined) && (JSON.stringify(prevProps.rowData.doseUnitArr) !== JSON.stringify(this.state.doseUnitArr))) {
      this.equivalentRef && this.equivalentRef.getTableData(this.state.doseUnitArr); // 计量单位/等效单位
    }
    if ((prevProps.rowData.dispUomArr !== undefined) && (JSON.stringify(prevProps.rowData.dispUomArr) !== JSON.stringify(this.state.dispUomArr))) {
      this.dispensingRef && this.dispensingRef.getTableData(this.state.dispUomArr); // 发药单位
    }
    if ((prevProps.rowData.stockAliasArr !== undefined) && (JSON.stringify(prevProps.rowData.stockAliasArr) !== JSON.stringify(this.state.stockAliasArr))) {
      this.inventoryItemAliasRef && this.inventoryItemAliasRef.getTableData(this.state.stockAliasArr); // 库存项别名
    }
  }

  // 切换tabs进行表单赋值
  setTableData = () => {
    let { doseUnitArr, dispUomArr, stockAliasArr } = this.state;
    this.setState({ equivalentPreview: '等效数量+剂量单位=对照单位', dispensingPreview: '发药单位+系数=对照单位' }, () => {
      this.equivalentRef && this.equivalentRef.getTableData(doseUnitArr); // 计量单位/等效单位
      this.dispensingRef && this.dispensingRef.getTableData(dispUomArr); // 发药单位
      this.inventoryItemAliasRef && this.inventoryItemAliasRef.getTableData(stockAliasArr); // 库存项别名
    })
  }

  // 基本信息form数据
  getFormData = () => {
    let { selectData, rowData } = this.state;
    const stockObj = rowData && 'stockObj' in rowData ? rowData.stockObj : {};
    const moreHospFlag = 'moreHospFlag' in stockObj && stockObj.moreHospFlag ? stockObj.moreHospFlag : 'N';
    let formData = [{
      dataIndex: 'stockCode',
      title: '库存项代码',
      typeCode: 'Input',
      required: 'Y',
      disabled: 'Y',
      col: 8
    }, {
      dataIndex: 'stockDesc',
      title: '库存项描述',
      typeCode: 'Input',
      required: 'Y',
      disabled: 'Y',
      col: 8
    }, {
      dataIndex: 'stockSpec',
      title: '包装规格',
      typeCode: 'Input',
      required: 'Y',
      disabled: moreHospFlag,
      col: 8
    }, {
      dataIndex: 'importTypeID',
      title: '进口类型',
      typeCode: 'Select',
      detailItem: selectData && 'importType' in selectData ? selectData.importType : [],
      disabled: moreHospFlag,
      col: 8
    }, {
      dataIndex: 'stkSubCatID',
      title: '库存分类',
      typeCode: 'Select',
      required: 'Y',
      detailItem: selectData && 'stkSubCat' in selectData ? selectData.stkSubCat : [],
      disabled: moreHospFlag,
      col: 8
    }, {
      dataIndex: 'barCode',
      title: '条码',
      typeCode: 'Input',
      disabled: moreHospFlag,
      col: 8
    }, {
      dataIndex: 'sp',
      title: '售价（入库单位）',
      typeCode: 'Input',
      disabled: 'Y',
      col: 8
    }, {
      dataIndex: 'rp',
      title: '进价（入库单位）',
      typeCode: 'Input',
      disabled: 'Y',
      col: 8
    }, {
      dataIndex: 'quaLevID',
      title: '质量层次',
      typeCode: 'Select',
      detailItem: selectData && 'qualityLevel' in selectData ? selectData.qualityLevel : [],
      disabled: moreHospFlag,
      col: 8
    }, {
      dataIndex: 'qualityNo',
      title: '质标编号',
      typeCode: 'Input',
      disabled: moreHospFlag,
      col: 8
    }, {
      dataIndex: 'priceTypeID',
      title: '定价类型',
      typeCode: 'Select',
      detailItem: selectData && 'priceType' in selectData ? selectData.priceType : [],
      disabled: moreHospFlag,
      col: 8
    }, {
      dataIndex: 'maxSp',
      title: '最高售价',
      typeCode: 'inputNumber',
      disabled: moreHospFlag,
      col: 8
    }, {
      dataIndex: 'priceFileNo',
      title: '物价文件号',
      typeCode: 'Input',
      disabled: moreHospFlag,
      col: 8
    }, {
      dataIndex: 'drugUseInfo',
      title: '用药说明',
      typeCode: 'Input',
      formItemCol: { labelCol: 4, wrapperCol: 20 },
      disabled: moreHospFlag,
      col: 16
    }];
    this.setState({ formData })
  }

  // 收费项信息
  getChargeItemFormData = () => {
    let { selectData, rowData } = this.state;
    const stockObj = rowData && 'stockObj' in rowData ? rowData.stockObj : {};
    const moreHospFlag = 'moreHospFlag' in stockObj && stockObj.moreHospFlag ? stockObj.moreHospFlag : 'N';
    let formData = [{
      dataIndex: 'itemCode',
      title: '收费项代码',
      typeCode: 'Input',
      required: 'Y',
      disabled: 'Y',
      col: 8
    }, {
      dataIndex: 'itemDesc',
      title: '收费项描述',
      typeCode: 'Input',
      required: 'Y',
      disabled: 'Y',
      col: 8
    },
    // {
    //   dataIndex: 'itemDesc',
    //   title: '关联收费项',
    //   placeholder: '保存后自动关联收费项',
    //   typeCode: 'SearchSelect',
    //   valueindex: 'descripts', // 下拉value对应的值
    //   valueID: 'itemID', // 选择的valueID
    //   onSearch: this.handleChargeSearch,
    //   changeCallBack: this.handleItemChange,
    //   callBackResult: 'Fn',
    //   detailItem: chargeSelectData,
    //   disabled: 'Y',
    //   col: 8
    // }, 
    {
      dataIndex: 'itemPCID',
      title: '物价分类',
      typeCode: 'Select',
      required: 'Y',
      detailItem: selectData && 'PC' in selectData ? selectData.PC : [],
      disabled: moreHospFlag,
      col: 8
    }, {
      dataIndex: 'itemECID',
      title: '核算项目分类',
      typeCode: 'Select',
      required: 'Y',
      detailItem: selectData && 'EC' in selectData ? selectData.EC : [],
      disabled: moreHospFlag,
      col: 8
    }, {
      dataIndex: 'itemICID',
      title: '住院收据费用',
      typeCode: 'Select',
      required: 'Y',
      detailItem: selectData && 'IC' in selectData ? selectData.IC : [],
      disabled: moreHospFlag,
      col: 8
    }, {
      dataIndex: 'itemOCID',
      title: '门诊收据费用',
      typeCode: 'Select',
      required: 'Y',
      detailItem: selectData && 'OC' in selectData ? selectData.OC : [],
      disabled: moreHospFlag,
      col: 8
    }, {
      dataIndex: 'itemACID',
      title: '会计科目',
      typeCode: 'Select',
      required: 'Y',
      detailItem: selectData && 'AC' in selectData ? selectData.AC : [],
      disabled: moreHospFlag,
      col: 8
    }, {
      dataIndex: 'itemMCID',
      title: '病案分类',
      typeCode: 'Select',
      required: 'Y',
      detailItem: selectData && 'MC' in selectData ? selectData.MC : [],
      disabled: moreHospFlag,
      col: 8
    }, {
      dataIndex: 'itemTCID',
      title: '收费分类',
      typeCode: 'Select',
      required: 'Y',
      detailItem: selectData && 'TC' in selectData ? selectData.TC : [],
      disabled: moreHospFlag,
      col: 8
    }, {
      dataIndex: 'itemStDate',
      title: '生效日期',
      typeCode: 'Date',
      required: 'Y',
      default: 0,
      disabled: moreHospFlag,
      col: 8
    }, {
      dataIndex: 'itemEndDate',
      title: '失效日期',
      typeCode: 'Date',
      disabled: moreHospFlag,
      col: 8
    }];
    this.setState({ chargeItemFormData: formData })
  }

  // 关联收费项change事件
  handleItemChange = async e => {
    try {
      let data = {
        params: [{
          chargeDr: e,
        }],
      }
      let res = await React.$asyncPost(this, '02020071', data);
      let values = {
        itemTCID: res.result[0].TCDr,
        itemPCID: res.result[0].PCDr,
        itemICID: res.result[0].ICDr,
        itemOCID: res.result[0].OCDr,
        itemECID: res.result[0].ECDr,
        itemACID: res.result[0].ACDr,
        itemMCID: res.result[0].MCDr,
        itemStDate: res.result[0].chargeStDate ? Moment(res.result[0].chargeStDate) : null,
        itemEndDate: res.result[0].chargeEndDate ? Moment(res.result[0].chargeEndDate) : null,
      }
      this.chargeItemRef && this.chargeItemRef.setFieldsValue(values);
    } catch (error) {
      console.log(error)
    }
  }

  // 关联收费项搜索事件
  handleChargeSearch = (value) => {
    this.fetchCharge(value, chargeSelectData => this.setState({ chargeSelectData }, () => {
      this.getChargeItemFormData();
    }));
  }

  fetchCharge = (value, callback) => {
    value = value.replace(/^\s+/, "").replace(/\s+$/, "");
    if (value === "") {
      let data = [];
      callback(data);
      return;
    }
    if (this.timeout) {
      clearTimeout(this.timeout);
      this.timeout = null;
    }
    function fakeCharge() {
      let data = {
        params: [{
          Code: '',
          Descripts: '',
          FindType: '',
          TypeID: '',
          alias: value,
          include: "",
          hospID: '',
          include: "combo",
        }],
        pagination: [{
          pageSize: 0,
          currentPage: 0,
          sortColumn: "Code",
          sortOrder: "asc"
        }]
      }
      $http.post('urlS', this, {
        code: "05100010",
        data: data,
        success: function (res) {
          if (res.errorCode === '0') {
            let selectData = [];
            for (var j = 0; j < res.result.Data.length; j++) {
              selectData.push({
                key: res.result.Data[j].ID,
                id: res.result.Data[j].ID,
                descripts: res.result.Data[j].itemDesc,
              });
            }
            callback(selectData);
          }
        }
      });
    }
    this.timeout = setTimeout(fakeCharge, 300);
  }

  // 入库单位与基本单位转换系数form数据
  getBasicUnitFormData = () => {
    let { selectData, rowData } = this.state;
    const stockObj = rowData && 'stockObj' in rowData ? rowData.stockObj : {};
    const disabled = 'disabled' in stockObj && stockObj.disabled ? stockObj.disabled : 'N';
    const moreHospFlag = 'moreHospFlag' in stockObj && stockObj.moreHospFlag ? stockObj.moreHospFlag : 'N';
    let formData = [{
      dataIndex: 'purUnitID',
      title: '入库单位',
      typeCode: 'Select',
      required: 'Y',
      detailItem: selectData && 'unit' in selectData ? selectData.unit : [],
      changeCallBack: this.handlePurUnitChange,
      callBackResult: "Fn",
      disabled: disabled === 'Y' || moreHospFlag === 'Y' ? 'Y' : 'N',
      col: 8
    }, {
      dataIndex: 'baseFac',
      title: '转换系数',
      typeCode: 'inputNumber',
      required: 'Y',
      changeCallBack: this.basicUnitConversionPreview,
      callBackResult: "Fn",
      disabled: disabled === 'Y' || moreHospFlag === 'Y' ? 'Y' : 'N',
      col: 8
    }, {
      dataIndex: 'baseUnitID',
      title: '住院长期发药单位',
      typeCode: 'Select',
      required: 'Y',
      formItemCol: { labelCol: 12, wrapperCol: 12 },
      detailItem: selectData && 'unit' in selectData ? selectData.unit : [],
      changeCallBack: this.handleBaseUnitChange,
      callBackResult: "Fn",
      disabled: disabled === 'Y' || moreHospFlag === 'Y' ? 'Y' : 'N',
      col: 8
    }];
    this.setState({ basicUnitFormData: formData })
  }

  // 单位换算预览
  basicUnitConversionPreview = (e, dataIndex, values) => {
    const { selectData } = this.state;
    let isFlag = Util.judgeValue(values);
    if (isFlag) {
      let purUnitItem = Util.returnDataCccordingToAttributes(selectData && 'unit' in selectData ? selectData.unit : [], values.purUnitID);
      let baseUnitItem = Util.returnDataCccordingToAttributes(selectData && 'unit' in selectData ? selectData.unit : [], values.baseUnitID);
      let basicPreview = '1' + purUnitItem?.unitDesc + '=' + values?.baseFac + baseUnitItem?.unitDesc;
      this.setState({ basicPreview })
    }
  }

  // 入库单位选择
  handlePurUnitChange = (e, dataIndex, values) => {
    let { selectData } = this.state;
    this.basicUnitConversionPreview(e, dataIndex, values);
    // 门诊/住院临时医嘱发药单位及系数
    this.valuationUnitRef && this.valuationUnitRef.setFieldsValue({
      purUnitID: e
    });
    let purUnitObj = Util.returnDataCccordingToAttributes(selectData && 'unit' in selectData ? selectData.unit : [], e);
    purUnitObj.dataIndex = 'purUnitID';
    this.getBillUnitSelectData(purUnitObj);
  }

  // 判断临时发药单位是否包含选择选择的数据，不包含则插入到临时发药单位下拉数据中
  getBillUnitSelectData = (currentData) => {
    let { billUnitSelectData } = this.state;
    let isFlag = false;
    for (var i = 0; i < billUnitSelectData.length; i++) {
      if (billUnitSelectData[i].dataIndex === currentData.dataIndex) {
        billUnitSelectData[i] = currentData;
        isFlag = true;
        break;
      }
    }
    if (!isFlag) {
      billUnitSelectData.push(currentData);
    }
    let billUnitFlag = false;
    let billUnitID = this.valuationUnitRef && this.valuationUnitRef.getFieldValue('billUnitID'); // 门诊/住院临时发药单位
    for (var i = 0; i < billUnitSelectData.length; i++) {
      if (billUnitSelectData[i].id === billUnitID) {
        billUnitFlag = true;
        break;
      }
    }
    this.setState({ billUnitSelectData }, () => {
      this.getValuationUnitFormData();
      if (!billUnitFlag) { // 判断临时发药单位在下拉数据中是否存在，不存在则情况清除选择的数据
        this.valuationUnitRef && this.valuationUnitRef.setFieldsValue({
          billUnitID: undefined
        });
      }
    })
  }

  // 住院长期发药单位change事件
  handleBaseUnitChange = (e, dataIndex, values) => {
    let { selectData } = this.state;
    this.basicUnitConversionPreview(e, dataIndex, values);
    this.modifyTableControlUnit(e, dataIndex, values)
    let baseUnitObj = Util.returnDataCccordingToAttributes(selectData && 'unit' in selectData ? selectData.unit : [], e);
    baseUnitObj.dataIndex = 'baseUnitID';
    this.getBillUnitSelectData(baseUnitObj);
  }

  // 修改table中的对照单位
  modifyTableControlUnit = (e, dataIndex, values) => {
    let { selectData } = this.state;
    let doseUnitData = this.equivalentRef && this.equivalentRef.handleSave(false); // 剂量单位
    let doseUnitArr = 'tableData' in doseUnitData && doseUnitData.tableData ? doseUnitData.tableData : [];
    let dispUomData = this.dispensingRef && this.dispensingRef.handleSave(false); // 更多发药单位
    let dispUomArr = 'tableData' in dispUomData && dispUomData.tableData ? dispUomData.tableData : [];
    let baseUnitDesc = Util.returnDataCccordingToAttributes(selectData && 'unit' in selectData ? selectData.unit : [], e)?.unitDesc;
    this.setState({ baseUnitDesc })
    if (doseUnitArr && doseUnitArr.length > 0) {
      for (var i = 0; i < doseUnitArr.length; i++) {
        doseUnitArr[i].baseUnitDesc = baseUnitDesc;
      }
      this.equivalentRef && this.equivalentRef.getTableData(doseUnitArr); // 计量单位/等效单位
    }
    if (dispUomArr && dispUomArr.length > 0) {
      for (var i = 0; i < dispUomArr.length; i++) {
        dispUomArr[i].baseUnitDesc = baseUnitDesc;
      }
      this.dispensingRef && this.dispensingRef.getTableData(dispUomArr); // 发药单位
    }
  }

  // 入库单位与计价单位转换系数form数据
  getValuationUnitFormData = () => {
    let { selectData, rowData, billUnitSelectData } = this.state;
    const stockObj = rowData && 'stockObj' in rowData ? rowData.stockObj : {};
    const disabled = 'disabled' in stockObj && stockObj.disabled ? stockObj.disabled : 'N';
    const moreHospFlag = 'moreHospFlag' in stockObj && stockObj.moreHospFlag ? stockObj.moreHospFlag : 'N';
    let formData = [{
      dataIndex: 'purUnitID',
      title: '入库单位',
      typeCode: 'Select',
      required: 'Y',
      disabled: 'Y',
      detailItem: selectData && 'unit' in selectData ? selectData.unit : [],
      changeCallBack: this.valuationUnitConversionPreview,
      callBackResult: "Fn",
      col: 8
    }, {
      dataIndex: 'billFac',
      title: '转换系数', // 转换系数(计价)
      typeCode: 'inputNumber',
      required: 'Y',
      changeCallBack: this.valuationUnitConversionPreview,
      callBackResult: "Fn",
      disabled: disabled === 'Y' || moreHospFlag === 'Y' ? 'Y' : 'N',
      col: 8
    }, {
      dataIndex: 'billUnitID',
      title: '门诊/住院临时发药单位', // 计价单位
      typeCode: 'Select',
      required: 'Y',
      formItemCol: { labelCol: 12, wrapperCol: 12 },
      detailItem: billUnitSelectData,
      changeCallBack: this.valuationUnitConversionPreview,
      callBackResult: "Fn",
      disabled: disabled === 'Y' || moreHospFlag === 'Y' ? 'Y' : 'N',
      col: 8
    }];
    this.setState({ valuationUnitFormData: formData })
  }

  // 单位换算预览
  valuationUnitConversionPreview = (e, dataIndex, values) => {
    const { selectData, billUnitSelectData } = this.state;
    let isFlag = Util.judgeValue(values);
    if (isFlag) {
      let purUnitItem = Util.returnDataCccordingToAttributes(selectData && 'unit' in selectData ? selectData.unit : [], values.purUnitID);
      let billUnitItem = Util.returnDataCccordingToAttributes(billUnitSelectData, values.billUnitID);
      let valuationPreview = '1' + purUnitItem?.unitDesc + '=' + values?.billFac + billUnitItem?.unitDesc;
      this.setState({ valuationPreview })
    }
  }

  // 获取剂量单位表头数据
  getEquivalentColumnsData = () => {
    let { selectData } = this.state;
    let columns = [{
      title: '剂量单位',
      dataIndex: 'doseUnitDesc',
      editable: true,
      width: '150px',
      align: 'center',
      inputType: 'Select',
      required: 'Y',
      detailitem: selectData && 'unit' in selectData ? selectData.unit : []
    }, {
      title: '等效数量',
      dataIndex: 'doseQty',
      editable: true,
      width: '150px',
      align: 'center',
      required: 'Y',
    }, {
      title: '默认数量',
      dataIndex: 'defaultQty',
      editable: true,
      width: '150px',
      align: 'center'
    }, {
      title: '对照单位',
      dataIndex: 'baseUnitDesc',
      editable: true,
      disabled: 'Y',
      width: '150px',
      align: 'center',
      inputType: 'Input'
    }];
    this.setState({ equivalentColumns: columns });
  }

  // 获取发药单位表格数据
  getDispensingColumns = () => {
    let { selectData } = this.state;
    let columns = [{
      title: '发药科室',
      dataIndex: 'dispLocDesc',
      editable: true,
      width: '150px',
      align: 'center',
      inputType: 'Select',
      required: 'Y',
      detailitem: selectData && 'loc' in selectData ? selectData.loc : [],
    }, {
      title: '发药单位',
      dataIndex: 'dispUnitDesc',
      editable: true,
      width: '150px',
      align: 'center',
      inputType: 'Select',
      required: 'Y',
      detailitem: selectData && 'unit' in selectData ? selectData.unit : [],
    }, {
      title: '转换系数',
      dataIndex: 'factor',
      editable: true,
      width: '150px',
      align: 'center',
      required: 'Y',
      inputType: 'inputNumber'
    }, {
      title: '对照单位',
      dataIndex: 'baseUnitDesc',
      editable: true,
      disabled: 'Y',
      width: '150px',
      align: 'center',
      inputType: 'Input'
    }];
    this.setState({ dispensingColumns: columns })
  }

  // 获取存储条件表单数据
  getStorageConditionFormData = () => {
    // let { rowData } = this.state;
    // const stockObj = rowData && 'stockObj' in rowData ? rowData.stockObj : {};
    // const moreHospFlag = 'moreHospFlag' in stockObj && stockObj.moreHospFlag ? stockObj.moreHospFlag : 'N';
    let formData = [{
      dataIndex: 'minTemp',
      title: '最低温度(℃)',
      typeCode: 'inputNumber',
      // disabled: moreHospFlag,
      col: 8
    }, {
      dataIndex: 'commonTemp',
      title: '常温',
      typeCode: 'Switch',
      col: 4,
      // disabled: moreHospFlag,
      formItemCol: { labelCol: 13, wrapperCol: 11 }
    }, {
      dataIndex: 'radiation',
      title: '防辐射',
      typeCode: 'Switch',
      col: 4,
      // disabled: moreHospFlag,
      formItemCol: { labelCol: 13, wrapperCol: 11 }
    }, {
      dataIndex: 'coldST',
      title: '冷藏',
      typeCode: 'Switch',
      col: 4,
      // disabled: moreHospFlag,
      formItemCol: { labelCol: 13, wrapperCol: 11 }
    }, {
      dataIndex: 'maxTemp',
      title: '最高温度(℃)',
      typeCode: 'inputNumber',
      // disabled: moreHospFlag,
      col: 8
    }, {
      dataIndex: 'dry',
      title: '干燥',
      typeCode: 'Switch',
      col: 4,
      // disabled: moreHospFlag,
      formItemCol: { labelCol: 13, wrapperCol: 11 }
    }, {
      dataIndex: 'seal',
      title: '熔封',
      typeCode: 'Switch',
      col: 4,
      // disabled: moreHospFlag,
      formItemCol: { labelCol: 13, wrapperCol: 11 }
    }, {
      dataIndex: 'freeze',
      title: '冷冻',
      typeCode: 'Switch',
      col: 4,
      // disabled: moreHospFlag,
      formItemCol: { labelCol: 13, wrapperCol: 11 }
    }, {
      dataIndex: 'minHumidity',
      title: '最低湿度',
      typeCode: 'inputNumber',
      // disabled: moreHospFlag,
      col: 8
    }, {
      dataIndex: 'airtight',
      title: '封闭',
      typeCode: 'Switch',
      col: 4,
      // disabled: moreHospFlag,
      formItemCol: { labelCol: 13, wrapperCol: 11 }
    }, {
      dataIndex: 'gloomyCool',
      title: '凉暗',
      typeCode: 'Switch',
      col: 4,
      // disabled: moreHospFlag,
      formItemCol: { labelCol: 13, wrapperCol: 11 }
    }, {
      dataIndex: 'dampProof',
      title: '防潮',
      typeCode: 'Switch',
      col: 4,
      // disabled: moreHospFlag,
      formItemCol: { labelCol: 13, wrapperCol: 11 }
    }, {
      dataIndex: 'maxHumidity',
      title: '最高湿度',
      typeCode: 'inputNumber',
      // disabled: moreHospFlag,
      col: 8
    }, {
      dataIndex: 'dark',
      title: '遮光',
      typeCode: 'Switch',
      col: 4,
      // disabled: moreHospFlag,
      formItemCol: { labelCol: 13, wrapperCol: 11 }
    }, {
      dataIndex: 'meltPack',
      title: '密封',
      typeCode: 'Switch',
      col: 4,
      // disabled: moreHospFlag,
      formItemCol: { labelCol: 13, wrapperCol: 11 }
    }, {
      dataIndex: 'shade',
      title: '阴凉',
      typeCode: 'Switch',
      col: 4,
      // disabled: moreHospFlag,
      formItemCol: { labelCol: 13, wrapperCol: 11 }
    }];
    this.setState({ storageConditionFormData: formData });
  }

  // 招投标信息表单数据
  getBiddingFormData = () => {
    let { selectData, rowData } = this.state;
    // const stockObj = rowData && 'stockObj' in rowData ? rowData.stockObj : {};
    // const moreHospFlag = 'moreHospFlag' in stockObj && stockObj.moreHospFlag ? stockObj.moreHospFlag : 'N';
    let formData = [{
      dataIndex: 'packUnitID',
      title: '大包装单位',
      typeCode: 'Select',
      detailItem: selectData && 'unit' in selectData ? selectData.unit : [],
      // disabled: moreHospFlag,
      col: 8
    }, {
      dataIndex: 'packFac',
      title: '大包装系数',
      typeCode: 'Input',
      // disabled: moreHospFlag,
      col: 8
    }, {
      dataIndex: 'packPicPath',
      title: '包装图片路径',
      typeCode: 'Input',
      // disabled: moreHospFlag,
      col: 8
    }, {
      dataIndex: 'PBLID',
      title: '招标名称',
      typeCode: 'Select',
      detailItem: selectData && 'pubBidList' in selectData ? selectData.pubBidList : [],
      // disabled: moreHospFlag,
      col: 8
    }, {
      dataIndex: 'PBRp',
      title: '招标进价',
      typeCode: 'Input',
      // disabled: moreHospFlag,
      col: 8
    }, {
      dataIndex: 'PBVendorID',
      title: '招标供应商',
      typeCode: 'Select',
      detailItem: selectData && 'pubBidVendor' in selectData ? selectData.pubBidVendor : [],
      // disabled: moreHospFlag,
      col: 8
    }, {
      dataIndex: 'PBLevID',
      title: '招标级别',
      typeCode: 'Select',
      detailItem: selectData && 'pubBidLevel' in selectData ? selectData.pubBidLevel : [],
      // disabled: moreHospFlag,
      col: 8
    }, {
      dataIndex: 'refRetReaID',
      title: '不可退原因',
      typeCode: 'Select',
      detailItem: selectData && 'refRetReason' in selectData ? selectData.refRetReason : [],
      // disabled: moreHospFlag,
      col: 8
    }, {
      dataIndex: 'inMedBasis',
      title: '进药依据',
      typeCode: 'Input',
      // disabled: moreHospFlag,
      col: 8
    }, {
      dataIndex: 'startDate',
      title: '生效日期',
      typeCode: 'Date',
      default: 0,
      // disabled: moreHospFlag,
      col: 8
    }, {
      dataIndex: 'stopDate',
      title: '失效日期',
      typeCode: 'Date',
      // disabled: moreHospFlag,
      col: 8
    }, {
      dataIndex: 'stopReaID',
      title: '失效原因',
      typeCode: 'Select',
      detailItem: selectData && 'stopReason' in selectData ? selectData.stopReason : [],
      // disabled: moreHospFlag,
      col: 8
    }, {
      dataIndex: 'purPlanCode',
      title: '采购编码',
      typeCode: 'Input',
      // disabled: moreHospFlag,
      col: 8
    }, {
      dataIndex: 'isHospFlag',
      title: '院内目录',
      typeCode: 'Switch',
      // disabled: moreHospFlag,
      col: 8
    }];
    this.setState({ biddingFormData: formData })
  }

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

  // 获取数据
  getFieldsValue = () => {
    let { billUnitSelectData } = this.state;
    let basicInfoData = this.basicInfoRef && this.basicInfoRef.getFieldsValue(); // 库存项基本信息数据
    let chargeObj = this.chargeItemRef && this.chargeItemRef.getFieldsValue(); // 收费项信息
    let basicUnitData = this.basicUnitRef && this.basicUnitRef.getFieldsValue(); // 住院长期发药单位及系数
    let aluationUnitData = this.valuationUnitRef && this.valuationUnitRef.getFieldsValue(); // 门诊/住院临时医嘱发药单位及系数
    let doseUnitData = this.equivalentRef && this.equivalentRef.handleSave(); // 剂量单位
    let doseUnitArr = doseUnitData && 'tableData' in doseUnitData ? doseUnitData.tableData : [];
    let doseUnitEditingKey = doseUnitData && 'editingKey' in doseUnitData ? doseUnitData.editingKey : '';
    let dispUomData = this.dispensingRef && this.dispensingRef.handleSave(); // 更多发药单位
    let dispUomArr = dispUomData && 'tableData' in dispUomData ? dispUomData.tableData : [];
    let dispUomEditingKey = dispUomData && 'editingKey' in dispUomData ? dispUomData.editingKey : '';
    let storeObj = this.storageConditionRef && this.storageConditionRef.getFieldsValue(); // 存储条件
    let hospStockObj = this.biddingtRef && this.biddingtRef.getFieldsValue(); // 招投标信息
    let stockAliasData = this.inventoryItemAliasRef && this.inventoryItemAliasRef.handleSave(); // 库存项别名
    let stockAliasArr = stockAliasData && 'tableData' in stockAliasData ? stockAliasData.tableData : [];
    let stockAliasEditingKey = stockAliasData && 'editingKey' in stockAliasData ? stockAliasData.editingKey : '';
    return {
      stockObj: {
        ...basicInfoData,
        ...chargeObj,
        ...basicUnitData,
        ...aluationUnitData
      },
      doseUnitArr,
      doseUnitEditingKey,
      chargeObj,
      storeObj,
      hospStockObj,
      dispUomArr,
      dispUomEditingKey,
      stockAliasArr,
      stockAliasEditingKey,
      billUnitSelectData
    }
  }

  // 重置
  resetFields = () => {
    this.basicInfoRef && this.basicInfoRef.resetFields(); // 库存项基本信息数据
    this.chargeItemRef && this.chargeItemRef.resetFields(); // 收费项信息
    this.basicUnitRef && this.basicUnitRef.resetFields(); // 住院长期发药单位及系数
    this.valuationUnitRef && this.valuationUnitRef.resetFields(); // 门诊/住院临时医嘱发药单位及系数
    this.equivalentRef && this.equivalentRef.resetFields(); // 剂量单位
    this.dispensingRef && this.dispensingRef.resetFields(); // 更多发药单位
    this.storageConditionRef && this.storageConditionRef.resetFields(); // 存储条件
    this.biddingtRef && this.biddingtRef.resetFields(); // 招投标信息
    this.inventoryItemAliasRef && this.inventoryItemAliasRef.resetFields(); // 库存项别名
  }

  // 保存
  handleSave = () => {
    let { selectData } = this.state;
    // 库存项基本信息数据
    this.basicInfoRef && this.basicInfoRef.handleSave().then(result => {
      const stockObj = this.staticFieldsValue && 'stockObj' in this.staticFieldsValue ? this.staticFieldsValue.stockObj : {};
      this.staticFieldsValue = {
        ...this.staticFieldsValue,
        stockObj: {
          ...stockObj,
          ...result,
          stockError: result?.error
        }
      }
    });
    // 收费项信息
    this.chargeItemRef && this.chargeItemRef.handleSave().then(result => {
      this.staticFieldsValue = {
        ...this.staticFieldsValue,
        chargeObj: {
          ...result,
          chargeItemError: result?.error
        }
      }
    });
    // 住院长期发药单位及系数
    this.basicUnitRef && this.basicUnitRef.handleSave().then(result => {
      const stockObj = this.staticFieldsValue && 'stockObj' in this.staticFieldsValue ? this.staticFieldsValue.stockObj : {};
      this.staticFieldsValue = {
        ...this.staticFieldsValue,
        stockObj: {
          ...stockObj,
          ...result,
          basicError: result?.error
        }
      }
    });
    // 门诊/住院临时医嘱发药单位及系数
    this.valuationUnitRef && this.valuationUnitRef.handleSave().then(result => {
      const stockObj = this.staticFieldsValue && 'stockObj' in this.staticFieldsValue ? this.staticFieldsValue.stockObj : {};
      this.staticFieldsValue = {
        ...this.staticFieldsValue,
        stockObj: {
          ...stockObj,
          ...result,
          valuationError: result?.error
        }
      }
    });
    // 存储条件
    this.storageConditionRef && this.storageConditionRef.handleSave().then(result => {
      const storeObj = this.staticFieldsValue && 'storeObj' in this.staticFieldsValue ? this.staticFieldsValue.storeObj : {};
      this.staticFieldsValue = {
        ...this.staticFieldsValue,
        storeObj: {
          ...storeObj,
          ...result
        }
      }
    });
    // 招投标信息
    this.biddingtRef && this.biddingtRef.handleSave().then(result => {
      const hospStockObj = this.staticFieldsValue && 'hospStockObj' in this.staticFieldsValue ? this.staticFieldsValue.hospStockObj : {};
      this.staticFieldsValue = {
        ...this.staticFieldsValue,
        hospStockObj: {
          ...hospStockObj,
          ...result
        }
      }
    });
    // 剂量单位
    let doseUnitData = this.equivalentRef && this.equivalentRef.handleSave();
    let doseUnitArr = doseUnitData && 'tableData' in doseUnitData ? doseUnitData.tableData : [];
    let doseUnitEditingKey = doseUnitData && 'editingKey' in doseUnitData ? doseUnitData.editingKey : '';
    // 更多发药单位
    let dispUomData = this.dispensingRef && this.dispensingRef.handleSave();
    let dispUomArr = dispUomData && 'tableData' in dispUomData ? dispUomData.tableData : [];
    let dispUomEditingKey = dispUomData && 'editingKey' in dispUomData ? dispUomData.editingKey : '';
    // 库存项别名
    let stockAliasData = this.inventoryItemAliasRef && this.inventoryItemAliasRef.handleSave();
    let stockAliasArr = stockAliasData && 'tableData' in stockAliasData ? stockAliasData.tableData : [];
    let stockAliasEditingKey = stockAliasData && 'editingKey' in stockAliasData ? stockAliasData.editingKey : '';
    for (var i = 0; i < doseUnitArr.length; i++) {
      doseUnitArr[i].doseUnitID = Util.returnDataCccordingToAttributes(selectData && 'unit' in selectData ? selectData.unit : [], doseUnitArr[i].doseUnitDesc, 'descripts')?.id; // 剂量单位
    }
    for (var i = 0; i < dispUomArr.length; i++) {
      dispUomArr[i].dispLocID = Util.returnDataCccordingToAttributes(selectData && 'loc' in selectData ? selectData.loc : [], dispUomArr[i].dispLocDesc, 'descripts')?.id; // 发药科室
      dispUomArr[i].dispUnitID = Util.returnDataCccordingToAttributes(selectData && 'unit' in selectData ? selectData.unit : [], dispUomArr[i].dispUnitDesc, 'descripts')?.id; // 发药单位
    }
    this.staticFieldsValue = {
      ...this.staticFieldsValue,
      doseUnitArr,
      doseUnitEditingKey,
      dispUomArr,
      dispUomEditingKey,
      stockAliasArr,
      stockAliasEditingKey
    }
  }

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

  // 设置库存项、收费项代码描述
  setMedData = (val, dataIndex) => {
    // 库存项基本信息
    this.basicInfoRef && this.basicInfoRef.setFieldsValue({
      [dataIndex === 'code' ? 'stockCode' : 'stockDesc']: val
    });
    // 收费项信息
    this.chargeItemRef && this.chargeItemRef.setFieldsValue({
      [dataIndex === 'code' ? 'itemCode' : 'itemDesc']: val
    });
  }

  // 剂量单位选中行操作
  handleEquivalentRowClick = record => {
    if ('doseQty' in record && 'doseUnitDesc' in record && 'baseUnitDesc' in record && record.doseUnitDesc && record.baseUnitDesc) {
      let equivalentPreview = (record.doseQty || 0) + record.doseUnitDesc + '=1' + record.baseUnitDesc;
      this.setState({ equivalentPreview })
    }
  }

  // 更多发药单位选中行操作
  handleDispensingRowClick = record => {
    if ('dispUnitDesc' in record && 'factor' in record && 'baseUnitDesc' in record && record.dispUnitDesc && record.baseUnitDesc) {
      let dispensingPreview = record.factor + record.dispUnitDesc + '=1' + record.baseUnitDesc;
      this.setState({ dispensingPreview })
    }
  }

  render() {
    let { formData, chargeItemFormData, basicUnitFormData, valuationUnitFormData, doseUnitArr, equivalentColumns, dispUomArr, dispensingColumns, storageConditionFormData,
      biddingFormData, stockAliasArr, inventoryItemAliasColumns, basicPreview, valuationPreview, baseUnitDesc, equivalentPreview, dispensingPreview,
      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 && 'stockObj' in rowData ? rowData.stockObj : {}}
          formData={formData}
          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 && 'stockObj' in rowData ? rowData.stockObj : {}}
          formData={chargeItemFormData}
          onRef={ref => this.chargeItemRef = ref}
        />
        <div className="card-title-left-icon rii-card-title"><div></div>库存项单位维护</div>
        <UnitSetting
          title="1-住院长期发药单位及系数"
          desc=""
          result={basicPreview}
          formData={basicUnitFormData}
          rowData={rowData && 'stockObj' in rowData ? rowData.stockObj : {}}
          onRef={ref => this.basicUnitRef = ref}
        />
        <UnitSetting
          title="2-门诊/住院临时医嘱发药单位及系数"
          desc=""
          result={valuationPreview}
          formData={valuationUnitFormData}
          rowData={rowData && 'stockObj' in rowData ? rowData.stockObj : {}}
          onRef={ref => this.valuationUnitRef = ref}
        />
        <UnitMaintenance
          dataSource={doseUnitArr}
          columns={equivalentColumns}
          baseUnitDesc={baseUnitDesc}
          result={equivalentPreview}
          title="3-剂量单位"
          desc="与基本单位转换"
          deleteCode="02020227"
          recordID="doseID"
          onRef={ref => this.equivalentRef = ref}
          setParentTableData={data => this.setState({ doseUnitArr: data })}
          handleRowClick={this.handleEquivalentRowClick}
        />
        <UnitMaintenance
          dataSource={dispUomArr}
          columns={dispensingColumns}
          baseUnitDesc={baseUnitDesc}
          result={dispensingPreview}
          title="4-更多发药单位"
          desc="用于增加门诊、住院发药单位"
          deleteCode="02020239"
          recordID="dispUomID"
          onRef={ref => this.dispensingRef = ref}
          setParentTableData={data => this.setState({ dispUomArr: data })}
          handleRowClick={this.handleDispensingRowClick}
        />
        <div className="card-title-left-icon rii-card-title"><div></div>存储条件</div>
        <DynamicRenderingForm
          formItemCol={{ labelCol: 8, wrapperCol: 16 }}
          rowData={rowData && 'storeObj' in rowData ? rowData.storeObj : {}}
          formData={storageConditionFormData}
          onRef={ref => this.storageConditionRef = ref}
        />
        <div className="card-title-left-icon rii-card-title"><div></div>招投标信息</div>
        <DynamicRenderingForm
          formItemCol={{ labelCol: 8, wrapperCol: 16 }}
          rowData={rowData && 'hospStockObj' in rowData ? rowData.hospStockObj : {}}
          formData={biddingFormData}
          onRef={ref => this.biddingtRef = ref}
        />
        <div className="card-title-left-icon rii-card-title"><div></div>库存项别名</div>
        <UnitMaintenance
          dataSource={stockAliasArr}
          columns={inventoryItemAliasColumns}
          title="库存项别名"
          desc=""
          result=""
          deleteCode="02020238"
          recordID="aliasID"
          onRef={ref => this.inventoryItemAliasRef = ref}
          setParentTableData={data => this.setState({ stockAliasArr: data })}
        />
      </div>
    )
  }
};

export default AllInventoryItem;