/*
 * Creater:     shizi
 * CreateDate:  2022/05/24
 * Describe：   基本信息
 * */
import React, { Component } from 'react';
// import Moment from 'moment';
import { Util } from 'tools';
import DynamicRenderingForm from 'pages/common/DynamicRenderingForm';

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

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

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

  componentDidUpdate(prevProps, prevState) {
    if (('selectData' in prevProps && (JSON.stringify(prevProps.selectData) !== JSON.stringify(this.state.selectData))) || ('rowData' in prevProps && (JSON.stringify(prevProps.rowData) !== JSON.stringify(this.state.rowData)))) {
      this.getFormData();
    }
  }

  getFormData = () => {
    let { selectData, rowData } = this.state;
    let formData = [{
      dataIndex: 'medSubCatID',
      title: '医嘱子类',
      typeCode: 'Select',
      required: 'Y',
      detailItem: selectData && 'ordSubCat' in selectData ? selectData.ordSubCat : [],
      // changeCallBack: this.handleMedSubCatChange,
      // callBackResult: "Fn",
      col: 6
    }, {
      dataIndex: 'medCode',
      title: '医嘱代码',
      typeCode: 'Input',
      required: 'Y',
      // disabled: 'Y',
      onBlur: this.handleMedCodeBlur,
      col: 6
    }, {
      dataIndex: 'medDesc',
      title: '医嘱描述',
      typeCode: 'Input',
      required: 'Y',
      disabled: rowData && 'disabled' in rowData && rowData.disabled ? rowData.disabled : 'N',
      col: 6
    }, {
      dataIndex: 'medPriorityID',
      title: '医嘱优先级',
      typeCode: 'Select',
      // required: 'Y',
      detailItem: selectData && 'orderPriority' in selectData ? selectData.orderPriority : [],
      col: 6
    }, {
      dataIndex: 'medShortDesc',
      title: '医嘱简称',
      typeCode: 'Input',
      required: 'Y',
      col: 6
    }, {
      dataIndex: 'medUnitID',
      title: '计价单位',
      typeCode: 'Select',
      required: 'Y',
      detailItem: selectData && 'unit' in selectData ? selectData.unit : [],
      col: 6
    }, {
      dataIndex: 'applicationTypeID',
      title: '申请单分类',
      typeCode: 'Select',
      // required: 'Y',
      detailItem: selectData && 'application' in selectData ? selectData.application : [],
      col: 6
    }, {
      dataIndex: '',
      title: '',
      typeCode: '',
      col: 6
    }, {
      dataIndex: 'medNoAloneOrder',
      title: '非独立医嘱',
      typeCode: 'Switch',
      col: 4,
      formItemCol: { labelCol: 13, wrapperCol: 11 }
    }, {
      dataIndex: 'medEMFlag',
      title: '加急标志',
      typeCode: 'Switch',
      col: 4,
      formItemCol: { labelCol: 13, wrapperCol: 11 }
    }, {
      dataIndex: 'medPrivateFlag',
      title: '私有标志',
      typeCode: 'Switch',
      col: 4,
      formItemCol: { labelCol: 13, wrapperCol: 11 }
    }, {
      dataIndex: 'medNoChangeFlag',
      title: '不收费',
      typeCode: 'Switch',
      col: 4,
      formItemCol: { labelCol: 13, wrapperCol: 11 }
    },
    //  {
    //   dataIndex: 'medStatus',
    //   title: '是否生效',
    //   typeCode: 'Switch',
    //   defaultValue: 'Y',
    //   col: 4,
    //   formItemCol: { labelCol: 13, wrapperCol: 11 }
    // },
    {
      dataIndex: 'medCustomPriceFlag',
      title: '自定义价格标志',
      typeCode: 'Switch',
      col: 4,
      formItemCol: { labelCol: 13, wrapperCol: 11 }
    }];
    // {
    //   dataIndex: 'startDate',
    //   title: '生效日期',
    //   typeCode: 'Date',
    //   required: 'Y',
    //   default: 0,
    //   col: 6
    // }, {
    //   dataIndex: 'stopDate',
    //   title: '失效日期',
    //   typeCode: 'Date',
    //   col: 6
    // }, 
    this.setState({ formData })
  }

  // 医嘱子类change事件
  handleMedSubCatChange = async e => {
    // const { rowData } = this.state;
    // if (rowData && 'medID' in rowData && rowData.medID) return;
    let data = {
      params: [{
        subCatID: e
      }]
    }
    let res = await React.$asyncPost(this, '02020379', data);
    let code = res.result;
    // 设置医嘱项代码
    this.formRef && this.formRef.setFieldsValue({
      medCode: code
    });
    // 设置收费项代码
    this.props.setChargeItemValues && this.props.setChargeItemValues({
      chargeCode: code
    });
    this.props.getOrderCategory && this.props.getOrderCategory(e);
  }

  // 医嘱代码失去焦点
  handleMedCodeBlur = (e) => {
    let value = e?.target?.value || undefined;
    // 设置收费项代码
    this.props.setChargeItemValues && this.props.setChargeItemValues({
      chargeCode: value
    });
  }

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

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

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

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

  // 获取更改的数据
  getDiffData = () => {
    let { formData, rowData } = this.state;
    let newValues = this.getFieldsValue();
    let diffData = [];
    for (var i = 0; i < formData.length; i++) {
      let detailItem = 'detailItem' in formData[i] && formData[i].detailItem ? formData[i].detailItem : [];
      if (!(rowData[formData[i].dataIndex] === '' && newValues[formData[i].dataIndex] === undefined) && (rowData[formData[i].dataIndex] !== newValues[formData[i].dataIndex])) {
        diffData.push({
          dataIndex: formData[i].dataIndex,
          title: formData[i].title,
          new: formData[i].typeCode === 'Select' && newValues[formData[i].dataIndex] && detailItem ? Util.returnDataCccordingToAttributes(detailItem, newValues[formData[i].dataIndex], 'id').descripts : newValues[formData[i].dataIndex],
          old: formData[i].typeCode === 'Select' && rowData[formData[i].dataIndex] && detailItem ? Util.returnDataCccordingToAttributes(detailItem, rowData[formData[i].dataIndex], 'id').descripts : rowData[formData[i].dataIndex]
        })
      }
    }
    return diffData;
  }

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

export default BasicInformation;