"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _react = _interopRequireDefault(require("react"));

var _mkuiFd = require("mkui-fd");

var _classnames = _interopRequireDefault(require("classnames"));

var _PropTypes = require("./PropTypes");

var _util = require("./util");

var _SelectedItem = _interopRequireDefault(require("./SelectedItem"));

var _ListItem = _interopRequireDefault(require("./ListItem"));

var _CategoryItem = _interopRequireDefault(require("./CategoryItem"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var Search = _mkuiFd.Input.Search;

var TransferModal = /*#__PURE__*/function (_React$Component) {
  _inherits(TransferModal, _React$Component);

  var _super = _createSuper(TransferModal);

  /** *******************static************************** */

  /** *******************life cycle************************** */
  function TransferModal(props) {
    var _this;

    _classCallCheck(this, TransferModal);

    _this = _super.call(this, props);
    _this.state = _this.getInitState();
    return _this;
  }
  /** 处理 modalManager open 场景  */


  _createClass(TransferModal, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      if (this.props.visible && this.props.api) {
        this.fetchData();
      }
    }
  }, {
    key: "componentDidUpdate",
    value: function componentDidUpdate(prevProps) {
      if (this.props.visible !== prevProps.visible && this.props.visible) {
        if (this.props.api) {
          this.setState({
            loading: true
          });
          this.fetchData();
        }

        this.synStateFromValue();
      }

      if (!this.props.api && this.props.data !== prevProps.data) {
        this.setState({
          data: this.props.data
        });
      }
    }
    /** *******************handle************************** */

    /**
     * 选择后触发
     * @param item
     */

  }, {
    key: "selectHandle",
    value: function selectHandle(item) {
      var choosed = this.state.choosed;
      var _this$props = this.props,
          keyName = _this$props.keyName,
          multiple = _this$props.multiple;

      if ((0, _util.isSelect)(choosed, item, keyName)) {
        return;
      }

      this.setChoosedState((0, _util.addSelected)(choosed, item, multiple));
    }
    /**
     * 更多点击触发
     * @param e
     */

  }, {
    key: "moreClickHandle",
    value: function moreClickHandle(e) {}
  }, {
    key: "okHandle",
    value: function okHandle() {
      var onOk = this.props.onOk;

      if (onOk) {
        onOk(this.buildReturnData());
      }
    }
  }, {
    key: "cancelHandle",
    value: function cancelHandle() {
      var onCancle = this.props.onCancle;

      if (onCancle) {
        onCancle();
      }
    }
  }, {
    key: "clearHandle",
    value: function clearHandle(e) {
      this.setChoosedState([]);
    }
  }, {
    key: "removeHandle",
    value: function removeHandle(item) {
      var choosed = this.state.choosed;
      var keyName = this.props.keyName;
      this.setChoosedState((0, _util.remove)(choosed, item, keyName));
    }
  }, {
    key: "batchAddHandle",
    value: function batchAddHandle(category) {
      var keyName = this.props.keyName;
      var choosed = this.state.choosed;
      this.setChoosedState((0, _util.batchAdd)(choosed, category, keyName));
    }
  }, {
    key: "searchHandle",
    value: function searchHandle(keyword) {
      this.searchData(keyword);
    }
    /** *******************inner method************************** */

  }, {
    key: "getInitState",
    value: function getInitState(props) {
      var _this$props2 = this.props,
          value = _this$props2.value,
          api = _this$props2.api,
          data = _this$props2.data;

      if (api) {
        return {
          choosed: value || [],
          loading: true
        };
      } else {
        return {
          data: data || null,
          choosed: value || [],
          loading: false
        };
      }
    }
    /**
     * 同步state
     */

  }, {
    key: "synStateFromValue",
    value: function synStateFromValue() {
      var value = this.props.value;
      this.setChoosedState(value);
    }
  }, {
    key: "fetchData",
    value: function fetchData() {
      var _this2 = this;

      var _this$props3 = this.props,
          api = _this$props3.api,
          params = _this$props3.params;
      api(params).then(function (data) {
        _this2.setState({
          data: data,
          loading: false
        });

        _this2.loaded = true;
      });
    }
  }, {
    key: "searchData",
    value: function searchData(keyword) {
      var _this3 = this;

      var _this$props4 = this.props,
          api = _this$props4.api,
          params = _this$props4.params,
          keyName = _this$props4.keyName,
          keyWordName = _this$props4.keyWordName;
      var queryParams = keyWordName ? _defineProperty({}, keyWordName, keyword) : _defineProperty({}, keyName, keyword);
      api(_objectSpread(_objectSpread({}, params), queryParams)).then(function (data) {
        _this3.setState({
          data: data
        });
      });
    }
  }, {
    key: "fireChange",
    value: function fireChange(choosed) {
      var onChange = this.props.onChange;
      onChange(values);
    }
  }, {
    key: "setChoosedState",
    value: function setChoosedState(choosed) {
      this.setState({
        choosed: choosed
      });
    }
  }, {
    key: "buildReturnData",
    value: function buildReturnData() {
      var choosed = this.state.choosed;
      return choosed;
    }
    /** *******************render method************************** */

  }, {
    key: "renderSearch",
    value: function renderSearch() {
      var _this4 = this;

      var _this$props5 = this.props,
          prefixCls = _this$props5.prefixCls,
          searchPlaceholder = _this$props5.searchPlaceholder;
      return /*#__PURE__*/_react["default"].createElement("div", {
        className: "".concat(prefixCls, "-left-panel-search")
      }, /*#__PURE__*/_react["default"].createElement(Search, {
        placeholder: searchPlaceholder,
        onSearch: function onSearch(value) {
          return _this4.searchHandle(value);
        }
      }));
    }
  }, {
    key: "renderLeftPanel",
    value: function renderLeftPanel() {
      var _this$props6 = this.props,
          prefixCls = _this$props6.prefixCls,
          subTitle = _this$props6.subTitle,
          showSearch = _this$props6.showSearch;
      return /*#__PURE__*/_react["default"].createElement("div", {
        className: "".concat(prefixCls, "-panel ").concat(prefixCls, "-left-panel")
      }, /*#__PURE__*/_react["default"].createElement("div", {
        className: "".concat(prefixCls, "-panel-head ").concat(prefixCls, "-left-panel-head")
      }, /*#__PURE__*/_react["default"].createElement("span", {
        className: "".concat(prefixCls, "-right-panel-head-title")
      }, subTitle)), showSearch ? this.renderSearch() : null, /*#__PURE__*/_react["default"].createElement("div", {
        className: "".concat(prefixCls, "-panel-body ").concat(prefixCls, "-left-panel-body")
      }, this.renderLeftContent()));
    }
  }, {
    key: "renderLeftContent",
    value: function renderLeftContent() {
      var _this5 = this;

      var prefixCls = this.props.prefixCls;
      var _this$state = this.state,
          loading = _this$state.loading,
          data = _this$state.data;

      if (loading) {
        return /*#__PURE__*/_react["default"].createElement(_mkuiFd.Icon, {
          type: "loading",
          className: "".concat(prefixCls, "-left-panel-loading")
        });
      } else {
        return /*#__PURE__*/_react["default"].createElement("ul", {
          className: "".concat(prefixCls, "-panel-body-content ").concat(prefixCls, "-left-panel-body-content")
        }, data.map(function (item) {
          return _this5.renderLeftItem(item);
        }));
      }
    }
  }, {
    key: "renderLeftItem",
    value: function renderLeftItem(item) {
      var _this6 = this;

      var choosed = this.state.choosed;
      var _this$props7 = this.props,
          keyName = _this$props7.keyName,
          categoryKeyName = _this$props7.categoryKeyName,
          render = _this$props7.render,
          categoryRender = _this$props7.categoryRender,
          multiple = _this$props7.multiple;

      if (item.children && item.children.length) {
        return /*#__PURE__*/_react["default"].createElement(_CategoryItem["default"], {
          key: item[categoryKeyName],
          name: categoryRender(item),
          onBatchClick: function onBatchClick() {
            return _this6.batchAddHandle(item);
          },
          batchAddBtn: multiple
        }, item.children.map(function (item) {
          return /*#__PURE__*/_react["default"].createElement(_ListItem["default"], {
            name: render(item),
            key: item[keyName],
            disabled: (0, _util.isSelect)(choosed, item, keyName),
            onSelect: function onSelect() {
              _this6.selectHandle(item);
            }
          });
        }));
      } else {
        return /*#__PURE__*/_react["default"].createElement(_ListItem["default"], {
          name: render(item),
          key: item[keyName],
          disabled: (0, _util.isSelect)(choosed, item, keyName),
          onSelect: function onSelect() {
            _this6.selectHandle(item);
          }
        });
      }
    }
  }, {
    key: "renderRightPanel",
    value: function renderRightPanel() {
      var _this7 = this;

      var _this$props8 = this.props,
          prefixCls = _this$props8.prefixCls,
          keyName = _this$props8.keyName,
          render = _this$props8.render,
          multiple = _this$props8.multiple;
      var choosed = this.state.choosed;
      return /*#__PURE__*/_react["default"].createElement("div", {
        className: "".concat(prefixCls, "-panel ").concat(prefixCls, "-right-panel")
      }, /*#__PURE__*/_react["default"].createElement("div", {
        className: "".concat(prefixCls, "-panel-head ").concat(prefixCls, "-right-panel-head")
      }, /*#__PURE__*/_react["default"].createElement("span", {
        className: "".concat(prefixCls, "-right-panel-head-info")
      }, /*#__PURE__*/_react["default"].createElement("span", {
        className: "".concat(prefixCls, "-right-panel-head-info-prefix")
      }, "\u5DF2\u9009:"), /*#__PURE__*/_react["default"].createElement("span", {
        className: "".concat(prefixCls, "-right-panel-head-info-count")
      }, choosed.length), /*#__PURE__*/_react["default"].createElement("span", {
        className: "".concat(prefixCls, "-right-panel-head-info-suffix")
      }, "\u9879")), /*#__PURE__*/_react["default"].createElement(_mkuiFd.Button, {
        className: "".concat(prefixCls, "-right-panel-head-clear"),
        onClick: function onClick(event) {
          _this7.clearHandle(event);
        },
        type: "link"
      }, "\u5168\u90E8\u6E05\u7A7A")), /*#__PURE__*/_react["default"].createElement("div", {
        className: "".concat(prefixCls, "-panel-body ").concat(prefixCls, "-right-panel-body")
      }, !multiple ? /*#__PURE__*/_react["default"].createElement("span", {
        className: "".concat(prefixCls, "-right-panel-body-info")
      }, /*#__PURE__*/_react["default"].createElement(_mkuiFd.Icon, {
        type: "info-circle",
        theme: "filled"
      }), " \u5355\u9009\u6A21\u5F0F\uFF0C\u53EA\u80FD\u9009\u5B9A\u4E00\u9879") : null, /*#__PURE__*/_react["default"].createElement("ul", {
        className: "".concat(prefixCls, "-panel-body-content ").concat(prefixCls, "-right-panel-body-content")
      }, choosed.map(function (item) {
        return /*#__PURE__*/_react["default"].createElement(_SelectedItem["default"], {
          name: render(item),
          key: item[keyName],
          onRemove: function onRemove() {
            _this7.removeHandle(item);
          }
        });
      }))));
    }
  }, {
    key: "renderOperation",
    value: function renderOperation() {
      var prefixCls = this.props.prefixCls;
      return /*#__PURE__*/_react["default"].createElement("div", {
        className: "".concat(prefixCls, "-middle-operation")
      }, /*#__PURE__*/_react["default"].createElement(_mkuiFd.Icon, {
        type: "right"
      }));
    }
  }, {
    key: "render",
    value: function render() {
      var _this8 = this;

      var _this$props9 = this.props,
          visible = _this$props9.visible,
          prefixCls = _this$props9.prefixCls,
          title = _this$props9.title,
          showSearch = _this$props9.showSearch,
          zIndex = _this$props9.zIndex;
      var wrapperCls = (0, _classnames["default"])("".concat(prefixCls), _defineProperty({}, "".concat(prefixCls, "-has-search"), showSearch));
      return /*#__PURE__*/_react["default"].createElement(_mkuiFd.Modal, {
        title: title,
        visible: visible,
        zIndex: zIndex,
        cancelText: "\u53D6\u6D88",
        okText: "\u786E\u5B9A",
        onOk: function onOk() {
          _this8.okHandle();
        },
        width: 800,
        wrapClassName: wrapperCls,
        onCancel: function onCancel() {
          _this8.cancelHandle();
        }
      }, this.renderLeftPanel(), this.renderOperation(), this.renderRightPanel());
    }
  }]);

  return TransferModal;
}(_react["default"].Component);

_defineProperty(TransferModal, "propTypes", _PropTypes.propTypes);

_defineProperty(TransferModal, "defaultProps", _PropTypes.defaultPropTypes);

var _default = TransferModal;
exports["default"] = _default;
//# sourceMappingURL=TransferModal.js.map
