import 'antd/es/row/style';
import _Row from 'antd/es/row';
import 'antd/es/col/style';
import _Col from 'antd/es/col';
import 'antd/es/button/style';
import _Button from 'antd/es/button';
import 'antd/es/modal/style';
import _Modal from 'antd/es/modal';
import 'antd/es/input/style';
import _Input from 'antd/es/input';
import 'antd/es/tooltip/style';
import _Tooltip from 'antd/es/tooltip';
import _map from 'lodash/map';
import _isUndefined from 'lodash/isUndefined';
import _isEmpty from 'lodash/isEmpty';
import _includes from 'lodash/includes';
import _get from 'lodash/get';
import _forEach from 'lodash/forEach';
import _concat from 'lodash/concat';
import { s as styleInject, _ as __extends, b as __awaiter, c as __generator, d as __assign } from './index.js';
import { PlusCircleOutlined, EditOutlined, DeleteOutlined } from '@ant-design/icons';
import { request } from '@lm_fe/utils';
import React__default, { Component } from 'react';
import { LazyAntd } from '@lm_fe/components';
import '@lm_fe/env';
import 'antd/es/tabs/style';
import 'antd/es/tabs';
import 'lodash/size';
import 'lodash/orderBy';
import 'antd/es/collapse/style';
import 'antd/es/collapse';
import 'antd/es/badge/style';
import 'antd/es/badge';
import 'lodash/assign';
import '@lm_fe/components_m';
import '@lm_fe/service';
import '@lm_fe/pages';
var css_248z = ".template-tree-wrapper .add-btn {\n  position: absolute;\n  top: 18px;\n  right: 42px;\n  z-index: 2;\n  text-align: right;\n}\n.template-tree-wrapper .ant-tree .ant-tree-list .ant-tree-title {\n  display: inline-block;\n  max-width: 320px;\n  overflow: hidden;\n  white-space: nowrap;\n  text-overflow: ellipsis;\n}\n.template-tree-wrapper .handle-icon {\n  margin-left: 10px;\n  cursor: pointer;\n}\n";
styleInject(css_248z);
var operationMapping = {
  "new": '新增模板',
  add: '新增子模板',
  edit: '编辑模板',
  "delete": '请注意!'
};
var Tree = LazyAntd.Tree;
LazyAntd.TreeSelect;
LazyAntd.Select;
LazyAntd.Table;
LazyAntd.Dropdown;
LazyAntd.Pagination;
var Index = function (_super) {
  __extends(Index, _super);
  function Index(props) {
    var _this = _super.call(this, props) || this;
    _this.getTemplateData = function (value) {
      return __awaiter(_this, void 0, void 0, function () {
        var _a, type, depid, userid, pregnancyid, paramsArr, paramsStr, newValue, res;
        return __generator(this, function (_b) {
          switch (_b.label) {
            case 0:
              _a = this.props, type = _a.type, depid = _a.depid, userid = _a.userid, pregnancyid = _a.pregnancyid;
              paramsArr = {
                type: type,
                depid: depid,
                userid: userid
              };
              paramsStr = '';
              _forEach(paramsArr, function (value, key) {
                if (!_isUndefined(value)) {
                  paramsStr += "".concat(key, ".equals=").concat(value, "&");
                }
              });
              if (pregnancyid) {
                paramsStr += "pregnancyid.equals=".concat(pregnancyid, "&");
              }
              newValue = value == undefined ? this.props.searchValue : value;
              if (newValue) {
                paramsStr += "code.contains=".concat(newValue, "&mnemonic.contains=").concat(newValue, "&val.contains=").concat(newValue, "&");
              }
              return [4, request.get("/api/template-trees?".concat(paramsStr, "page=0&size=500"))];
            case 1:
              res = _b.sent().data;
              res = res || [];
              this.setState({
                templateData: res
              });
              this.setState({
                nodeTreeItem: null,
                operation: null,
                newTitle: null,
                addTitle: null
              });
              return [2];
          }
        });
      });
    };
    _this.handleTreeItem = function (operate) {
      _this.setState({
        modalVisible: true,
        operation: operate
      });
    };
    _this.handleTreeSort = function (sort) {
      return __awaiter(_this, void 0, void 0, function () {
        return __generator(this, function (_a) {
          console.log('移动');
          return [2];
        });
      });
    };
    _this.handleIptChange = function (e, operation) {
      var nodeTreeItem = _this.state.nodeTreeItem;
      if (operation === 'new') _this.setState({
        newTitle: e.target.value
      });
      if (operation === 'add') _this.setState({
        addTitle: e.target.value
      });
      if (operation === 'edit') {
        var data = {
          title: e.target.value
        };
        _this.setState({
          nodeTreeItem: __assign(__assign({}, nodeTreeItem), data)
        });
      }
    };
    _this.handleOk = function () {
      return __awaiter(_this, void 0, void 0, function () {
        var _a, type, depid, userid, _b, nodeTreeItem, addTitle, operation, newTitle, newItem, addItem, editItem;
        return __generator(this, function (_c) {
          switch (_c.label) {
            case 0:
              _a = this.props, type = _a.type, depid = _a.depid, userid = _a.userid;
              _b = this.state, nodeTreeItem = _b.nodeTreeItem, addTitle = _b.addTitle, operation = _b.operation, newTitle = _b.newTitle;
              if (!(operation === 'new')) return [3, 2];
              newItem = {
                pid: 0,
                val: newTitle,
                type: type,
                depid: depid,
                userid: userid
              };
              return [4, request.post("/api/template-trees", newItem)];
            case 1:
              _c.sent();
              this.getTemplateData();
              _c.label = 2;
            case 2:
              if (!(operation === 'add')) return [3, 4];
              addItem = {
                pid: Number(nodeTreeItem.id),
                val: addTitle,
                type: type,
                depid: depid,
                userid: userid
              };
              return [4, request.post("/api/template-trees", addItem)];
            case 3:
              _c.sent();
              this.getTemplateData();
              _c.label = 4;
            case 4:
              if (!(operation === 'edit')) return [3, 6];
              editItem = {
                pid: Number(nodeTreeItem.pid),
                val: nodeTreeItem.title,
                type: type,
                depid: depid,
                userid: userid,
                id: nodeTreeItem.id
              };
              return [4, request.put("/api/template-trees", editItem)];
            case 5:
              _c.sent();
              this.getTemplateData();
              _c.label = 6;
            case 6:
              if (!(operation === 'delete')) return [3, 8];
              return [4, request["delete"]("/api/template-trees/".concat(nodeTreeItem.id))];
            case 7:
              _c.sent();
              this.getTemplateData();
              _c.label = 8;
            case 8:
              this.setState({
                modalVisible: false,
                nodeTreeItem: null,
                operation: null
              });
              return [2];
          }
        });
      });
    };
    _this.handleCancel = function () {
      _this.setState({
        modalVisible: false,
        nodeTreeItem: null,
        operation: null
      });
    };
    _this.handleAddBtnClick = function () {
      _this.setState({
        operation: 'new',
        modalVisible: true
      });
    };
    _this.onMouseEnter = function (_a, area) {
      var event = _a.event,
        node = _a.node;
      var x = event.currentTarget.offsetLeft + event.currentTarget.clientWidth;
      var y = event.currentTarget.offsetTop;
      _this.setState({
        nodeTreeItem: {
          pageX: x,
          pageY: y,
          pid: node.props.pid,
          id: Number(node.props.eventKey),
          title: node.props.title,
          sort: node.props.sort
        },
        currentArea: area
      });
    };
    _this.handleCheck = function (keys) {
      return __awaiter(_this, void 0, void 0, function () {
        var onChecked, _a, templateData;
        return __generator(this, function (_b) {
          onChecked = this.props.onChecked;
          _a = this.state, templateData = _a.templateData, _a.checkedKeys;
          this.setState({
            checkedKeys: keys
          });
          _forEach(templateData, function (item) {
            if (_includes(keys, "".concat(item.id))) {
              item.checked = true;
            } else {
              item.checked = false;
            }
          });
          onChecked && onChecked(templateData);
          return [2];
        });
      });
    };
    _this.handleCheckLeft = function (keys) {
      var onChecked = _this.props.onChecked;
      var _a = _this.state,
        templateData = _a.templateData,
        checkedKeysRight = _a.checkedKeysRight;
      var allCheckedKays = _concat(keys, checkedKeysRight);
      _this.setState({
        checkedKeysLeft: keys
      });
      _forEach(templateData, function (item) {
        if (_includes(allCheckedKays, "".concat(item.id))) {
          item.checked = true;
        } else {
          item.checked = false;
        }
      });
      onChecked && onChecked(templateData);
    };
    _this.handleCheckRight = function (keys) {
      var onChecked = _this.props.onChecked;
      var _a = _this.state,
        templateData = _a.templateData,
        checkedKeysLeft = _a.checkedKeysLeft;
      var allCheckedKays = _concat(keys, checkedKeysLeft);
      _this.setState({
        checkedKeysRight: keys
      });
      _forEach(templateData, function (item) {
        if (_includes(allCheckedKays, "".concat(item.id))) {
          item.checked = true;
        } else {
          item.checked = false;
        }
      });
      onChecked && onChecked(templateData);
    };
    _this.transferTemplateData = function (data, pid) {
      if (pid === void 0) {
        pid = 0;
      }
      var showIcd = _this.props.showIcd;
      var treeData = [];
      _map(data, function (item) {
        if (item.pid === pid) {
          item.title = showIcd && item.code ? "\uFF08icd\uFF09".concat(item.val) : item.val;
          item.key = String(item.id);
          item.children = _this.transferTemplateData(data, item.id);
          if (_isEmpty(item.children)) {
            item.isLeaf = true;
          } else {
            item.isLeaf = false;
          }
          treeData.push(item);
        }
      });
      console.log({
        treeData: treeData
      });
      return treeData;
    };
    _this.handleSelect = function (selectedKeys, _a) {
      var node = _a.node;
      var onSelected = _this.props.onSelected;
      onSelected && onSelected(node);
    };
    _this.state = {
      templateData: null,
      checkedKeys: [],
      checkedKeysLeft: [],
      checkedKeysRight: [],
      currentArea: null,
      operation: null,
      nodeTreeItem: null,
      modalVisible: false,
      newTitle: null,
      addTitle: null
    };
    return _this;
  }
  Index.prototype.componentDidMount = function () {
    this.getTemplateData(_get(this.props, "searchValue", ''));
  };
  Index.prototype.componentWillReceiveProps = function (nextPorps) {
    if (_get(nextPorps, "searchValue") != _get(this.props, "searchValue")) {
      this.getTemplateData(_get(nextPorps, "searchValue"));
    }
  };
  Index.prototype.renderNodeTreeMenu = function () {
    var _this = this;
    var editable = this.props.editable;
    var nodeTreeItem = this.state.nodeTreeItem;
    var _a = __assign({}, nodeTreeItem),
      pageX = _a.pageX,
      pageY = _a.pageY,
      pid = _a.pid;
    var menuStyle = {
      position: 'absolute',
      maxHeight: 40,
      textAlign: 'center',
      left: "".concat(pageX + 10, "px"),
      top: "".concat(pageY, "px"),
      display: 'flex',
      flexDirection: 'row'
    };
    return nodeTreeItem && editable ? /*#__PURE__*/React__default.createElement("div", {
      style: menuStyle
    }, pid === 0 ? /*#__PURE__*/React__default.createElement("div", {
      className: "handle-icon",
      onClick: function onClick() {
        return _this.handleTreeItem('add');
      }
    }, /*#__PURE__*/React__default.createElement(_Tooltip, {
      placement: "bottom",
      title: "\u6DFB\u52A0\u5B50\u6A21\u677F"
    }, /*#__PURE__*/React__default.createElement(PlusCircleOutlined, null))) : null, /*#__PURE__*/React__default.createElement("div", {
      className: "handle-icon",
      onClick: function onClick() {
        return _this.handleTreeItem('edit');
      }
    }, /*#__PURE__*/React__default.createElement(_Tooltip, {
      placement: "bottom",
      title: "\u4FEE\u6539"
    }, /*#__PURE__*/React__default.createElement(EditOutlined, null))), /*#__PURE__*/React__default.createElement("div", {
      className: "handle-icon",
      onClick: function onClick() {
        return _this.handleTreeItem('delete');
      }
    }, /*#__PURE__*/React__default.createElement(_Tooltip, {
      placement: "bottom",
      title: "\u5220\u9664"
    }, /*#__PURE__*/React__default.createElement(DeleteOutlined, null)))) : null;
  };
  Index.prototype.renderHandleModal = function () {
    var _this = this;
    var _a = this.state,
      modalVisible = _a.modalVisible,
      nodeTreeItem = _a.nodeTreeItem,
      addTitle = _a.addTitle,
      operation = _a.operation,
      newTitle = _a.newTitle;
    var title = operationMapping[operation];
    return /*#__PURE__*/React__default.createElement(_Modal, {
      title: title,
      closable: true,
      visible: modalVisible,
      onCancel: this.handleCancel,
      onOk: this.handleOk
    }, operation === 'new' && /*#__PURE__*/React__default.createElement("div", null, "\u65B0\u589E\u6A21\u677F\u540D\u79F0\uFF1A", /*#__PURE__*/React__default.createElement(_Input, {
      value: newTitle,
      onChange: function onChange(e) {
        return _this.handleIptChange(e, 'new');
      },
      style: {
        width: '50%'
      }
    })), operation === 'add' && /*#__PURE__*/React__default.createElement("div", null, "\u65B0\u589E\u5B50\u6A21\u677F\uFF1A", /*#__PURE__*/React__default.createElement(_Input, {
      value: addTitle,
      onChange: function onChange(e) {
        return _this.handleIptChange(e, 'add');
      },
      style: {
        width: '50%'
      }
    })), operation === 'edit' && /*#__PURE__*/React__default.createElement("div", null, "\u7F16\u8F91\u6A21\u677F\u540D\u79F0\uFF1A", /*#__PURE__*/React__default.createElement(_Input, {
      value: nodeTreeItem.title,
      onChange: function onChange(e) {
        return _this.handleIptChange(e, 'edit');
      },
      style: {
        width: '50%'
      }
    })), operation === 'delete' && /*#__PURE__*/React__default.createElement("div", null, "\u662F\u5426\u5220\u9664\u6A21\u677F\uFF1A", nodeTreeItem.title));
  };
  Index.prototype.renderTreatment = function () {
    var _this = this;
    var _a = this.props,
      checkable = _a.checkable,
      divide = _a.divide,
      editable = _a.editable;
    var _b = this.state,
      templateData = _b.templateData,
      checkedKeys = _b.checkedKeys,
      checkedKeysLeft = _b.checkedKeysLeft,
      checkedKeysRight = _b.checkedKeysRight,
      currentArea = _b.currentArea,
      nodeTreeItem = _b.nodeTreeItem;
    var treeData = this.transferTemplateData(templateData);
    return /*#__PURE__*/React__default.createElement("div", {
      style: {
        minHeight: 160,
        padding: '12px 0'
      }
    }, editable ? /*#__PURE__*/React__default.createElement("div", {
      className: "add-btn"
    }, /*#__PURE__*/React__default.createElement(_Button, {
      size: "small",
      onClick: this.handleAddBtnClick
    }, "\u6DFB\u52A0\u6A21\u677F")) : null, divide ? /*#__PURE__*/React__default.createElement(_Row, null, /*#__PURE__*/React__default.createElement(_Col, {
      span: 12
    }, /*#__PURE__*/React__default.createElement(Tree, {
      style: {
        maxHeight: '90%'
      },
      checkable: checkable,
      defaultExpandAll: true,
      checkedKeys: checkedKeysLeft.length === 0 ? [] : checkedKeysLeft,
      onCheck: this.handleCheckLeft,
      onSelect: this.handleSelect,
      onMouseEnter: function onMouseEnter(e) {
        return _this.onMouseEnter(e, 'treeLeft');
      },
      treeData: treeData.slice(0, Math.ceil(treeData.length / 2))
    }), nodeTreeItem && currentArea === 'treeLeft' ? this.renderNodeTreeMenu() : null), /*#__PURE__*/React__default.createElement(_Col, {
      span: 12
    }, /*#__PURE__*/React__default.createElement(Tree, {
      style: {
        maxHeight: '90%'
      },
      checkable: checkable,
      defaultExpandAll: true,
      checkedKeys: checkedKeysRight.length === 0 ? [] : checkedKeysRight,
      onCheck: this.handleCheckRight,
      onSelect: this.handleSelect,
      onMouseEnter: function onMouseEnter(e) {
        return _this.onMouseEnter(e, 'treeright');
      },
      treeData: treeData.slice(Math.ceil(treeData.length / 2))
    }), nodeTreeItem && currentArea === 'treeright' ? this.renderNodeTreeMenu() : null)) : /*#__PURE__*/React__default.createElement(_Row, null, /*#__PURE__*/React__default.createElement(_Col, {
      span: 24
    }, /*#__PURE__*/React__default.createElement(Tree, {
      style: {
        maxHeight: '90%'
      },
      checkable: checkable,
      defaultExpandAll: true,
      checkedKeys: checkedKeys,
      onCheck: this.handleCheck,
      onSelect: this.handleSelect,
      onMouseEnter: function onMouseEnter(e) {
        return _this.onMouseEnter(e, '');
      },
      treeData: treeData
    }), nodeTreeItem ? this.renderNodeTreeMenu() : null)));
  };
  Index.prototype.render = function () {
    var templateData = this.state.templateData;
    return /*#__PURE__*/React__default.createElement("div", {
      className: "template-tree-wrapper"
    }, templateData && this.renderTreatment(), this.renderHandleModal());
  };
  return Index;
}(Component);
export { Index as default };
//# sourceMappingURL=prenatal-visit.pregnancy.doctor-end4.js.map
