import _set from 'lodash/set';
import _get from 'lodash/get';
import { b as __awaiter, c as __generator, _ as __extends, d as __assign } from './index.js';
import 'antd/es/message/style';
import _message from 'antd/es/message';
import _size from 'lodash/size';
import { request } from '@lm_fe/utils';
import { BaseEditPanelForm, formDescriptionsWithoutSectionApi, observePatientData, BaseEditPanel, fromApi } from '@lm_fe/components_m';
import { SLocal_History, SLocal_State, SMchc_FormDescriptions } from '@lm_fe/service';
import 'react';
import '@lm_fe/env';
import 'antd/es/tabs/style';
import 'antd/es/tabs';
import 'lodash/orderBy';
import 'lodash/map';
import '@lm_fe/components';
import 'antd/es/collapse/style';
import 'antd/es/collapse';
import 'antd/es/badge/style';
import 'antd/es/badge';
import 'antd/es/button/style';
import 'antd/es/button';
import 'lodash/assign';
import '@ant-design/icons';
import '@lm_fe/pages';
var getPregnancyByOutpatientNO = function getPregnancyByOutpatientNO(outpatientNO) {
  return __awaiter(void 0, void 0, void 0, function () {
    return __generator(this, function (_a) {
      switch (_a.label) {
        case 0:
          return [4, request.get("/api/pregnancies?outpatientNO.equals=".concat(outpatientNO))];
        case 1:
          return [2, _a.sent().data];
      }
    });
  });
};
var AdmissionForm = function (_super) {
  __extends(AdmissionForm, _super);
  function AdmissionForm() {
    var _this = _super !== null && _super.apply(this, arguments) || this;
    _this.pregnancy = {};
    _this.handleItemChange = function (changedValues, allValues) {
      return __awaiter(_this, void 0, void 0, function () {
        var outpatientNO, pregnancyData, dataSize, tempData, referralOrganization, organization, currentOrganization, organizationGrade, currentOrganizationGrade, direction;
        var _a, _b, _c;
        return __generator(this, function (_d) {
          switch (_d.label) {
            case 0:
              if (!_get(changedValues, 'outpatientNO')) return [3, 5];
              outpatientNO = _get(changedValues, 'outpatientNO');
              return [4, getPregnancyByOutpatientNO(outpatientNO)];
            case 1:
              pregnancyData = _d.sent();
              dataSize = _size(pregnancyData);
              tempData = {};
              if (!(dataSize > 0)) return [3, 3];
              this.pregnancy = _get(pregnancyData, '0');
              _set(tempData, 'name', _get(this.pregnancy, 'name'));
              _set(tempData, 'checkupNO', _get(this.pregnancy, 'checkupNO'));
              _set(tempData, 'telephone', _get(this.pregnancy, 'telephone'));
              _set(tempData, 'currentGestationalWeek', _get(this.pregnancy, 'currentGestationalWeek'));
              _set(tempData, 'age', _get(this.pregnancy, 'age'));
              return [4, (_a = this.form) === null || _a === void 0 ? void 0 : _a.setFieldsValue(tempData)];
            case 2:
              _d.sent();
              return [3, 5];
            case 3:
              this.pregnancy = {};
              _set(tempData, 'name', '');
              _set(tempData, 'checkupNO', '');
              _set(tempData, 'telephone', '');
              _set(tempData, 'currentGestationalWeek', '');
              _set(tempData, 'age', '');
              return [4, (_b = this.form) === null || _b === void 0 ? void 0 : _b.setFieldsValue(tempData)];
            case 4:
              _d.sent();
              _d.label = 5;
            case 5:
              if (!_get(changedValues, 'referralOrganization')) return [3, 8];
              referralOrganization = _get(changedValues, 'referralOrganization');
              return [4, request.get("/api/referral-organizations?name.equals=".concat(referralOrganization))];
            case 6:
              organization = _d.sent().data;
              return [4, request.get("/api/getCurrReferralOrganization")];
            case 7:
              currentOrganization = _d.sent().data;
              organizationGrade = _get(organization, '0.grade');
              currentOrganizationGrade = _get(currentOrganization, 'grade');
              direction = currentOrganizationGrade === organizationGrade ? 1 : currentOrganizationGrade > organizationGrade ? 2 : 3;
              (_c = this.form) === null || _c === void 0 ? void 0 : _c.setFieldsValue({
                referralDirection: direction,
                referralOrganization: _get(organization, 0)
              });
              _d.label = 8;
            case 8:
              return [2];
          }
        });
      });
    };
    _this.handleFinish = function () {
      return __awaiter(_this, void 0, void 0, function () {
        var form, _a, onFinish, data;
        var _this = this;
        return __generator(this, function (_b) {
          form = this.form;
          _a = this.props, onFinish = _a.onFinish, data = _a.data;
          form && form.validateFields().then(function () {
            var params = __assign(__assign({}, form.getFieldsValue()), {
              id: _get(data, 'id')
            });
            if (!_get(data, 'id')) {
              if (_get(_this.pregnancy, 'id')) {
                _set(params, 'pregnancy.id', _get(_this.pregnancy, 'id'));
                _set(params, 'referralType', 2);
              } else {
                _message.error('请输入正确的就诊卡号！');
                return;
              }
            }
            onFinish && onFinish(params);
            setTimeout(function () {
              SLocal_History.safe_history_push('/referral-management/referral-in/list', this.props);
              form.resetFields();
            }, 100);
          })["catch"](function (error) {
            _message.error(_get(error, 'errorFields.0.errors.0'));
            form.scrollToField(_get(error, 'errorFields.0.name.0'));
          });
          return [2];
        });
      });
    };
    return _this;
  }
  return AdmissionForm;
}(BaseEditPanelForm);
var ReferralOut = function (_super) {
  __extends(ReferralOut, _super);
  function ReferralOut() {
    return _super !== null && _super.apply(this, arguments) || this;
  }
  ReferralOut.prototype.componentDidMount = function () {
    return __awaiter(this, void 0, void 0, function () {
      var _a, routerQuery, moduleName, baseUrl, _b, fromApi$1, id, formDescriptions, formDescriptionsWithoutSection, data, _c, _d, formKey;
      var _this = this;
      var _e;
      return __generator(this, function (_f) {
        switch (_f.label) {
          case 0:
            _a = this.props, routerQuery = _a.routerQuery, moduleName = _a.moduleName, baseUrl = _a.baseUrl, _b = _a.fromApi, fromApi$1 = _b === void 0 ? fromApi : _b;
            observePatientData.subscribe(function (data) {
              var prevData = _this.state.data;
              _this.setState({
                data: __assign(__assign({}, prevData), data)
              });
            });
            id = _get(this.props, 'id') || _get(routerQuery, 'id');
            return [4, SMchc_FormDescriptions.getModuleParse(moduleName)];
          case 1:
            formDescriptions = _f.sent();
            this.setState({
              spinning: false
            });
            formDescriptionsWithoutSection = formDescriptionsWithoutSectionApi(formDescriptions);
            if (!id) return [3, 3];
            _d = fromApi$1;
            return [4, request.get("".concat(baseUrl, "/").concat(id))];
          case 2:
            _c = _d.apply(void 0, [_f.sent().data, formDescriptionsWithoutSection]);
            return [3, 4];
          case 3:
            _c = {};
            _f.label = 4;
          case 4:
            data = _c;
            formKey = _get(data, 'id') || Math.random();
            if (!_get(data, 'recorder')) _set(data, 'recorder', (_e = SLocal_State.userData) === null || _e === void 0 ? void 0 : _e.firstName);
            this.setState({
              formDescriptions: formDescriptions,
              formDescriptionsWithoutSection: formDescriptionsWithoutSection,
              data: data,
              formKey: formKey
            });
            return [2];
        }
      });
    });
  };
  ReferralOut.defaultProps = {
    baseUrl: '/api/referrals',
    moduleName: 'referral-in',
    title: '转入登记',
    Form: AdmissionForm
  };
  return ReferralOut;
}(BaseEditPanel);
export { ReferralOut as default };
//# sourceMappingURL=referral-management.referral-in.add.js.map
