"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports["default"] = void 0;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _box = _interopRequireDefault(require("@alifd/next/lib/box"));
var _radio = _interopRequireDefault(require("@alifd/next/lib/radio"));
var _checkbox = _interopRequireDefault(require("@alifd/next/lib/checkbox"));
var _select = _interopRequireDefault(require("@alifd/next/lib/select"));
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));
var _react = _interopRequireDefault(require("react"));
var _variables = require("../../variables");
require("./index.scss");
var _data = require("../form-item/data");
var _api = require("../../utils/api");
var CommonOption = /*#__PURE__*/function (_React$Component) {
  (0, _inheritsLoose2["default"])(CommonOption, _React$Component);
  function CommonOption(props) {
    var _this;
    _this = _React$Component.call(this, props) || this;
    // 渲染数据
    _this.initData = /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee() {
      var enumList, data;
      return _regenerator["default"].wrap(function _callee$(_context) {
        while (1) switch (_context.prev = _context.next) {
          case 0:
            enumList = _this.props.enumList;
            if (!(enumList !== undefined)) {
              _context.next = 6;
              break;
            }
            _context.next = 4;
            return (0, _api.getChildList)(enumList.id);
          case 4:
            data = _context.sent;
            _this.setState({
              optionData: data
            });
          case 6:
          case "end":
            return _context.stop();
        }
      }, _callee);
    }));
    _this.onChange = function (value) {
      _this.props.onChange && _this.props.onChange(value);
      _this.setState({
        selectData: value
      });
    };
    _this.renderColumn = function () {
      var _this$props = _this.props,
        isMultiple = _this$props.isMultiple,
        isDeployment = _this$props.isDeployment,
        enumList = _this$props.enumList,
        defaultValue = _this$props.defaultValue,
        placeholder = _this$props.placeholder,
        isEdit = _this$props.isEdit;
      var childList = enumList ? enumList === null || enumList === void 0 ? void 0 : enumList.childList.map(function (item) {
        return (0, _extends2["default"])({}, item, {
          dictValue: item.dictValue,
          dictKey: item.dictKey,
          label: item.dictValue,
          value: item.dictKey
        });
      }) : _data.selectList;
      isDeployment = isDeployment === undefined ? 'primary' : isDeployment;
      var mode = isMultiple ? 'multiple' : 'single';
      if (isDeployment === 'primary') {
        defaultValue = defaultValue === '' ? [] : defaultValue;
        return /*#__PURE__*/_react["default"].createElement(_select["default"], {
          onChange: function onChange(e) {
            return _this.onChange(e);
          },
          defaultValue: defaultValue,
          hasClear: true,
          disabled: !isEdit,
          placeholder: placeholder,
          mode: mode,
          dataSource: childList
        });
      } else {
        if (mode === 'multiple') {
          return /*#__PURE__*/_react["default"].createElement(_checkbox["default"].Group, {
            onChange: function onChange(e) {
              return _this.onChange(e);
            },
            defaultValue: defaultValue,
            disabled: !isEdit,
            dataSource: childList
          });
        } else {
          return /*#__PURE__*/_react["default"].createElement(_radio["default"].Group, {
            onChange: function onChange(e) {
              return _this.onChange(e);
            },
            defaultValue: defaultValue,
            disabled: !isEdit,
            dataSource: childList
          });
        }
      }
    };
    _this.state = {
      childList: [],
      selectData: [],
      optionData: []
    };
    return _this;
  }
  var _proto = CommonOption.prototype;
  _proto.componentDidMount = /*#__PURE__*/function () {
    var _componentDidMount = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2() {
      return _regenerator["default"].wrap(function _callee2$(_context2) {
        while (1) switch (_context2.prev = _context2.next) {
          case 0:
            _context2.next = 2;
            return this.initData();
          case 2:
          case "end":
            return _context2.stop();
        }
      }, _callee2, this);
    }));
    function componentDidMount() {
      return _componentDidMount.apply(this, arguments);
    }
    return componentDidMount;
  }();
  _proto.render = function render() {
    return /*#__PURE__*/_react["default"].createElement(_box["default"], {
      direction: "row",
      align: "center",
      className: _variables.bizCssPrefix + '-option'
    }, this.renderColumn());
  };
  return CommonOption;
}(_react["default"].Component);
CommonOption.displayName = 'CommonOption';
CommonOption.displayName = 'CommonOption';
var _default = CommonOption;
exports["default"] = _default;