import 'antd/es/row/style';
import _Row from 'antd/es/row';
import 'antd/es/radio/style';
import _Radio from 'antd/es/radio';
import 'antd/es/form/style';
import _Form from 'antd/es/form';
import 'antd/es/col/style';
import _Col from 'antd/es/col';
import 'antd/es/card/style';
import _Card from 'antd/es/card';
import 'antd/es/collapse/style';
import _Collapse from 'antd/es/collapse';
import 'antd/es/list/style';
import _List from 'antd/es/list';
import 'antd/es/button/style';
import _Button from 'antd/es/button';
import 'antd/es/message/style';
import _message from 'antd/es/message';
import _size from 'lodash/size';
import _set from 'lodash/set';
import _map from 'lodash/map';
import _keys from 'lodash/keys';
import _keyBy from 'lodash/keyBy';
import _isEmpty from 'lodash/isEmpty';
import _get from 'lodash/get';
import _compact from 'lodash/compact';
import { b as __awaiter, c as __generator, s as styleInject, _ as __extends, d as __assign } from './index.js';
import React__default, { Component } from 'react';
import { request, getSearchParamsValue, getFutureDate } from '@lm_fe/utils';
import { LazyAntd, formatTimeToUTC, getTemplateById, CaseTempleteEdit } from '@lm_fe/components_m';
import { SelectTip } from '@lm_fe/pages';
import dayjs from 'dayjs';
import '@lm_fe/env';
import 'antd/es/tabs/style';
import 'antd/es/tabs';
import 'lodash/orderBy';
import '@lm_fe/components';
import 'antd/es/badge/style';
import 'antd/es/badge';
import 'lodash/assign';
import '@ant-design/icons';
import '@lm_fe/service';
var getInformedConsents = function getInformedConsents(pregnancyId) {
  return __awaiter(void 0, void 0, void 0, function () {
    var result;
    return __generator(this, function (_a) {
      switch (_a.label) {
        case 0:
          return [4, request.get("/api/pregnancy-documents?pregnancyId.equals=".concat(pregnancyId))];
        case 1:
          result = _a.sent();
          return [2, result.data];
      }
    });
  });
};
var getDocumentTemplates = function getDocumentTemplates() {
  return __awaiter(void 0, void 0, void 0, function () {
    var result;
    return __generator(this, function (_a) {
      switch (_a.label) {
        case 0:
          return [4, request.get("/api/document-templates?moduleType.equals=3&page=0&size=9999")];
        case 1:
          result = _a.sent();
          return [2, result.data];
      }
    });
  });
};
var createInformedConsent = function createInformedConsent(data) {
  return __awaiter(void 0, void 0, void 0, function () {
    var result;
    return __generator(this, function (_a) {
      switch (_a.label) {
        case 0:
          return [4, request.post('/api/pregnancy-documents', data)];
        case 1:
          result = _a.sent();
          return [2, result.data];
      }
    });
  });
};
var updateInformedConsent = function updateInformedConsent(data) {
  return __awaiter(void 0, void 0, void 0, function () {
    var result;
    return __generator(this, function (_a) {
      switch (_a.label) {
        case 0:
          return [4, request.put('/api/pregnancy-documents', data)];
        case 1:
          result = _a.sent();
          return [2, result.data];
      }
    });
  });
};
var freeAllowance = {
  1: {
    account: '地贫复筛（血红蛋白成分分析）',
    province: 22.9,
    city: 7.6,
    county: 7.6
  },
  2: {
    account: '地贫基因检测',
    province: 310.2,
    city: 103.4,
    county: 103.4
  },
  3: {
    account: '孕早期血清学唐氏筛查',
    province: 70.2,
    city: 23.4,
    county: 23.4
  },
  4: {
    account: '孕中期血清学唐氏筛查',
    province: 79.6,
    city: 26.55,
    county: 26.55
  },
  5: {
    account: '孕妇外周血游离DNA产前筛查（NIPT）',
    province: 360,
    city: 120,
    county: 120
  },
  6: {
    account: '孕早期严重致死残致残性结构畸形超声筛查（含NT）',
    province: 95.6,
    city: 31.85,
    county: 31.85
  },
  7: {
    account: '严重致死残致残性结构畸形超声筛查',
    province: 95.6,
    city: 31.85,
    county: 31.85
  },
  8: {
    account: '地贫',
    province: 1110,
    city: 370,
    county: 370
  },
  9: {
    account: '严重单基因病',
    province: 1110,
    city: 370,
    county: 370
  },
  10: {
    account: '致死残致残性染色体病',
    province: 1110,
    city: 370,
    county: 370
  },
  11: {
    account: '致死残致残性结构畸形',
    province: 1110,
    city: 370,
    county: 370
  },
  12: {
    account: '新生儿遗传代谢病（初筛）',
    province: 61.1,
    city: 20.4,
    county: 20.4
  },
  13: {
    account: '新生儿甲状腺功能减低症（复筛）',
    province: 68.9,
    city: 22.95,
    county: 22.95
  },
  14: {
    account: '新生儿苯丙酮尿症（复筛）',
    province: 130.6,
    city: 43.5,
    county: 43.5
  },
  15: {
    account: '新生儿G6pd缺乏症（复筛）',
    province: 5,
    city: 1.65,
    county: 1.65
  },
  16: {
    account: '新生儿先天性肾上腺皮质增多症（复筛）',
    province: 58.1,
    city: 19.35,
    county: 19.35
  },
  17: {
    account: '新生儿听力筛查（初筛）',
    province: 45.5,
    city: 15.15,
    county: 15.15
  },
  18: {
    account: '新生儿听力筛查（复筛）',
    province: 161.7,
    city: 53.95,
    county: 53.95
  },
  19: {
    account: '早产儿视网膜病变（初筛）',
    province: 174.4,
    city: 58.15,
    county: 58.15
  },
  20: {
    account: '早产儿视网膜病变（复筛）',
    province: 174.4,
    city: 58.15,
    county: 58.15
  },
  21: {
    account: 'HIV+梅毒+乙肝筛查（免费）',
    province: 0,
    city: 0,
    county: 0
  }
};
var allowanceAccount = [{
  label: '地贫',
  value: '地贫',
  disabled: true,
  children: [{
    label: '复筛（血红蛋白成分分析）',
    value: 1
  }, {
    label: '地贫基因检测',
    value: 2
  }]
}, {
  label: '孕期筛查',
  value: '孕期筛查',
  disabled: true,
  children: [{
    label: '孕早期血清学唐氏筛查',
    value: 3
  }, {
    label: '孕中期血清学唐氏筛查',
    value: 4
  }, {
    label: '孕妇外周血游离DNA产前筛查（NIPT）',
    value: 5
  }, {
    label: '孕早期严重致死残致残性结构畸形超声筛查（含NT）',
    value: 6
  }, {
    label: '严重致死残致残性结构畸形超声筛查',
    value: 7
  }]
}, {
  label: '产前诊断',
  value: '产前诊断',
  disabled: true,
  children: [{
    label: '地贫',
    value: 8
  }, {
    label: '严重单基因病',
    value: 9
  }, {
    label: '致死残致残性染色体病',
    value: 10
  }, {
    label: '致死残致残性结构畸形',
    value: 11
  }]
}, {
  label: '疾病筛查',
  value: '疾病筛查',
  disabled: true,
  children: [{
    label: '新生儿遗传代谢病（初筛）',
    value: 12
  }, {
    label: '新生儿甲状腺功能减低症（复筛）',
    value: 13
  }, {
    label: '新生儿苯丙酮尿症（复筛）',
    value: 14
  }, {
    label: '新生儿G6pd缺乏症（复筛）',
    value: 15
  }, {
    label: '新生儿先天性肾上腺皮质增多症（复筛）',
    value: 16
  }, {
    label: '新生儿听力筛查（初筛）',
    value: 17
  }, {
    label: '新生儿听力筛查（复筛）',
    value: 18
  }, {
    label: '早产儿视网膜病变（初筛）',
    value: 19
  }, {
    label: '早产儿视网膜病变（复筛）',
    value: 20
  }]
}, {
  label: '传染病',
  value: '传染病',
  disabled: true,
  children: [{
    label: 'HIV+梅毒+乙肝筛查（免费）',
    value: 21
  }]
}];
var css_248z = ".pregnancy-informed-consent {\n  height: 100%;\n}\n.pregnancy-informed-consent-list {\n  background-color: #fafafa;\n}\n.pregnancy-informed-consent-list .ant-card {\n  height: 100%;\n}\n.pregnancy-informed-consent-list .ant-collapse {\n  height: 100%;\n  overflow-y: scroll;\n}\n.pregnancy-informed-consent-list .ant-collapse .ant-collapse-content > .ant-collapse-content-box {\n  padding: 0;\n}\n.pregnancy-informed-consent-list-item {\n  cursor: pointer;\n}\n.pregnancy-informed-consent-detail {\n  background-color: #ffffff;\n  padding: 8px;\n}\n";
styleInject(css_248z);
var TreeSelect = LazyAntd.TreeSelect;
var groupOptions = [{
  label: '女方',
  value: 1
}, {
  label: '男方',
  value: 2
}, {
  label: '男女双方',
  value: 3
}];
var InformedConsent = function (_super) {
  __extends(InformedConsent, _super);
  function InformedConsent() {
    var _this = _super !== null && _super.apply(this, arguments) || this;
    _this.state = {
      informedConsentList: [],
      informedConsent: {},
      isNew: false,
      templateId: null,
      partnerTemplateId: null,
      commonplateId: null,
      allowanceData: {},
      partnerAllowanceData: {},
      radioValue: 1
    };
    _this.handleSearchInfomedConsentList = function () {
      return __awaiter(_this, void 0, void 0, function () {
        var id, pregnancyId, informedConsentList, _a;
        return __generator(this, function (_b) {
          switch (_b.label) {
            case 0:
              id = this.props.id;
              pregnancyId = id || getSearchParamsValue('id');
              _a = this.mergeInformedConsentByDate;
              return [4, getInformedConsents(pregnancyId)];
            case 1:
              informedConsentList = _a.apply(this, [_b.sent()]);
              this.setState({
                informedConsentList: informedConsentList
              });
              return [2];
          }
        });
      });
    };
    _this.mergeInformedConsentByDate = function (informedConsents) {
      var createDateKeys = _compact(_keys(_keyBy(informedConsents, 'createDate')));
      var mergedInformedConsents = {};
      _map(createDateKeys, function (createDateKey) {
        _set(mergedInformedConsents, createDateKey, []);
      });
      _map(informedConsents, function (informedConsent) {
        if (createDateKeys.indexOf(informedConsent.createDate) === -1) {
          return;
        }
        mergedInformedConsents[informedConsent.createDate].push(informedConsent);
      });
      return mergedInformedConsents;
    };
    _this.handleSave = function (content) {
      return __awaiter(_this, void 0, void 0, function () {
        var pregnancyData, informedConsent, data;
        return __generator(this, function (_a) {
          switch (_a.label) {
            case 0:
              pregnancyData = this.props.pregnancyData;
              informedConsent = this.state.informedConsent;
              data = __assign(__assign({}, informedConsent), {
                content: content,
                pregnancy: pregnancyData,
                createDate: formatTimeToUTC(dayjs()),
                documentTemplate: _get(informedConsent, 'documentTemplate')
              });
              if (!informedConsent.id) return [3, 2];
              return [4, updateInformedConsent(data)];
            case 1:
              informedConsent = _a.sent();
              return [3, 4];
            case 2:
              return [4, createInformedConsent(data)];
            case 3:
              informedConsent = _a.sent();
              _a.label = 4;
            case 4:
              _message.success('操作成功');
              this.setState({
                informedConsent: informedConsent
              });
              this.handleSearchInfomedConsentList();
              return [2];
          }
        });
      });
    };
    _this.handleAdd = function () {
      _this.setState({
        isNew: true,
        informedConsent: {}
      });
    };
    _this.handleClickListItem = function (oldInformedConsent) {
      return function () {
        return __awaiter(_this, void 0, void 0, function () {
          var oldContent, newContent;
          return __generator(this, function (_a) {
            oldContent = _get(oldInformedConsent, 'content');
            newContent = this.transferContent(oldContent);
            this.setState({
              informedConsent: __assign(__assign({}, oldInformedConsent), {
                content: newContent,
                documentTemplate: _get(oldInformedConsent, 'documentTemplate')
              })
            });
            return [2];
          });
        });
      };
    };
    _this.transferContent = function (oldContent, allowanceObj) {
      var pregnancyData = _this.props.pregnancyData;
      _set(pregnancyData, 'freeAllowance', allowanceObj);
      var reg = /\[\{\{.+?\}\}\]/g;
      var result = oldContent.matchAll(reg);
      var newContent = oldContent;
      for (var _i = 0, result_1 = result; _i < result_1.length; _i++) {
        var item = result_1[_i];
        if (item) {
          newContent = newContent.replace(item[0], _get(pregnancyData, item[0].slice(3, -3)) || '   ');
        }
      }
      return newContent;
    };
    _this.handleConsentChange = function (value) {
      return __awaiter(_this, void 0, void 0, function () {
        var basicInfo, _a, templateId, partnerTemplateId, commonplateId, allowanceData, partnerAllowanceData, id, allowanceObj, informedConsent, documentTemplate, oldContent, newContent;
        return __generator(this, function (_b) {
          switch (_b.label) {
            case 0:
              basicInfo = this.props.basicInfo;
              _a = this.state, templateId = _a.templateId, partnerTemplateId = _a.partnerTemplateId, commonplateId = _a.commonplateId, allowanceData = _a.allowanceData, partnerAllowanceData = _a.partnerAllowanceData;
              console.log('aax', basicInfo, this.state, value);
              allowanceObj = {};
              this.setState({
                radioValue: value
              });
              if (value === 1) {
                id = templateId;
                allowanceObj = __assign({}, allowanceData);
              } else if (value === 2) {
                id = partnerTemplateId;
                allowanceObj = __assign({}, partnerAllowanceData);
              } else {
                id = commonplateId;
                allowanceObj = __assign(__assign({}, allowanceData), partnerAllowanceData);
              }
              _set(allowanceObj, 'date', getFutureDate(0));
              _set(allowanceObj, 'doctor', _get(basicInfo, 'firstName'));
              informedConsent = this.state.informedConsent;
              return [4, getTemplateById(id)];
            case 1:
              documentTemplate = _b.sent();
              oldContent = _get(documentTemplate, 'content');
              newContent = this.transferContent(oldContent, allowanceObj);
              this.setState({
                informedConsent: __assign(__assign({}, informedConsent), {
                  content: newContent,
                  documentTemplate: documentTemplate
                })
              });
              return [2];
          }
        });
      });
    };
    _this.handleTreeChange = function (value, type) {
      return __awaiter(_this, void 0, void 0, function () {
        var radioValue, arr, obj, account, province, city, county;
        return __generator(this, function (_a) {
          switch (_a.label) {
            case 0:
              radioValue = this.state.radioValue;
              if (_size(value) === 0) return [2];
              arr = [];
              _map(value, function (item) {
                return arr.push(freeAllowance[item]);
              });
              obj = {};
              account = '';
              province = 0;
              city = 0;
              county = 0;
              _map(arr, function (item) {
                account += "".concat(item.account, "  ");
                province += item.province;
                city += item.city;
                county += item.county;
              });
              if (!(type === 1)) return [3, 2];
              _set(obj, 'account', account);
              _set(obj, 'province', province.toFixed(2));
              _set(obj, 'city', city.toFixed(2));
              _set(obj, 'county', county.toFixed(2));
              return [4, this.setState({
                allowanceData: obj
              })];
            case 1:
              _a.sent();
              return [3, 4];
            case 2:
              _set(obj, 'partnerAccount', account);
              _set(obj, 'partnerProvince', province.toFixed(2));
              _set(obj, 'partnerCity', city.toFixed(2));
              _set(obj, 'partnerCounty', county.toFixed(2));
              return [4, this.setState({
                partnerAllowanceData: obj
              })];
            case 3:
              _a.sent();
              _a.label = 4;
            case 4:
              this.handleConsentChange(radioValue);
              return [2];
          }
        });
      });
    };
    _this.renderInformedConsentList = function () {
      var informedConsentList = _this.state.informedConsentList;
      return /*#__PURE__*/React__default.createElement(_Card, {
        size: "small",
        title: "\u8865\u52A9\u5238\u5217\u8868",
        extra: /*#__PURE__*/React__default.createElement(_Button, {
          type: "primary",
          onClick: _this.handleAdd
        }, "\u65B0\u5EFA")
      }, /*#__PURE__*/React__default.createElement(_Collapse, null, _map(informedConsentList, function (item, key) {
        return /*#__PURE__*/React__default.createElement(_Collapse.Panel, {
          header: key,
          key: key
        }, /*#__PURE__*/React__default.createElement(_List, {
          size: "small",
          dataSource: item,
          renderItem: function renderItem(informedConsent) {
            return /*#__PURE__*/React__default.createElement(_List.Item, {
              className: "pregnancy-informed-consent-list-item",
              onClick: _this.handleClickListItem(informedConsent)
            }, _get(informedConsent, 'documentTemplate.title'));
          }
        }));
      })));
    };
    return _this;
  }
  InformedConsent.prototype.componentDidMount = function () {
    return __awaiter(this, void 0, void 0, function () {
      var template;
      return __generator(this, function (_a) {
        switch (_a.label) {
          case 0:
            this.handleSearchInfomedConsentList();
            return [4, getDocumentTemplates()];
          case 1:
            template = _a.sent();
            if (_isEmpty(template)) _message.warning('document-templates 为空');
            this.setState({
              templateId: _get(template, '0.id'),
              partnerTemplateId: _get(template, '1.id'),
              commonplateId: _get(template, '2.id')
            });
            return [2];
        }
      });
    });
  };
  InformedConsent.prototype.render = function () {
    var _this = this;
    var _a;
    var _b = this.state,
      informedConsent = _b.informedConsent,
      isNew = _b.isNew,
      radioValue = _b.radioValue;
    var _c = this.props.containerProps,
      containerProps = _c === void 0 ? {
        height: (_a = document.getElementsByClassName('prenatal-visit-main')[0]) === null || _a === void 0 ? void 0 : _a.clientHeight
      } : _c;
    return /*#__PURE__*/React__default.createElement(_Row, {
      className: "pregnancy-informed-consent"
    }, /*#__PURE__*/React__default.createElement(_Col, {
      className: "pregnancy-informed-consent-list",
      span: 4
    }, this.renderInformedConsentList()), /*#__PURE__*/React__default.createElement(_Col, {
      className: "pregnancy-informed-consent-detail",
      span: 20
    }, _isEmpty(informedConsent) && !isNew ? /*#__PURE__*/React__default.createElement(SelectTip, null) : /*#__PURE__*/React__default.createElement(React__default.Fragment, null, /*#__PURE__*/React__default.createElement(_Row, null, /*#__PURE__*/React__default.createElement(_Col, {
      span: 8
    }, /*#__PURE__*/React__default.createElement(_Form.Item, {
      label: "\u5973\u65B9\u8865\u52A9\u9879\u76EE",
      labelCol: {
        span: 8
      },
      wrapperCol: {
        span: 16
      }
    }, /*#__PURE__*/React__default.createElement(TreeSelect, {
      style: {
        width: '100%'
      },
      placeholder: "\u8BF7\u9009\u62E9\u8865\u52A9\u9879\u76EE",
      treeData: allowanceAccount,
      treeDefaultExpandAll: true,
      multiple: true,
      onChange: function onChange(v) {
        return _this.handleTreeChange(v, 1);
      }
    }))), /*#__PURE__*/React__default.createElement(_Col, {
      span: 8
    }, /*#__PURE__*/React__default.createElement(_Form.Item, {
      label: "\u7537\u65B9\u8865\u52A9\u9879\u76EE",
      labelCol: {
        span: 8
      },
      wrapperCol: {
        span: 16
      }
    }, /*#__PURE__*/React__default.createElement(TreeSelect, {
      style: {
        width: '100%'
      },
      placeholder: "\u8BF7\u9009\u62E9\u8865\u52A9\u9879\u76EE",
      treeData: allowanceAccount,
      treeDefaultExpandAll: true,
      multiple: true,
      onChange: function onChange(v) {
        return _this.handleTreeChange(v, 2);
      }
    }))), /*#__PURE__*/React__default.createElement(_Col, {
      span: 8
    }, /*#__PURE__*/React__default.createElement(_Form.Item, {
      label: "\u8865\u52A9\u5BF9\u8C61",
      labelCol: {
        span: 8
      },
      wrapperCol: {
        span: 16
      }
    }, /*#__PURE__*/React__default.createElement(_Radio.Group, {
      options: groupOptions,
      value: radioValue,
      onChange: function onChange(e) {
        return _this.handleConsentChange(e.target.value);
      }
    })))), /*#__PURE__*/React__default.createElement(CaseTempleteEdit, {
      key: _get(informedConsent, 'id') || Math.random(),
      containerProps: __assign(__assign({}, containerProps), {
        height: containerProps.height - 88
      }),
      content: _get(informedConsent, 'content'),
      onSave: this.handleSave,
      toolbars: false,
      mode: "STRICT"
    }))));
  };
  return InformedConsent;
}(Component);
export { InformedConsent as default };
//# sourceMappingURL=pregnancy.nurse-end.InformedConsent.js.map
