import "choerodon-ui/lib/input/style";
import _Input from "choerodon-ui/lib/input";
import "choerodon-ui/lib/tooltip/style";
import _Tooltip from "choerodon-ui/lib/tooltip";
import "choerodon-ui/lib/radio/style";
import _Radio from "choerodon-ui/lib/radio";
import "choerodon-ui/lib/select/style";
import _Select from "choerodon-ui/lib/select";
import "choerodon-ui/lib/form/style";
import _Form from "choerodon-ui/lib/form";

var _dec, _class, _class2, _temp;

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 _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; }

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); }

import React, { Component } from 'react';
import { injectIntl, FormattedMessage } from 'react-intl';
import _ from 'lodash';
import Sidebar from 'choerodon-ui/lib/modal/Sidebar';
import { Content } from '@choerodon/boot';
import { STAGE_FLOW_MANUAL, STAGE_FLOW_AUTO } from "../Constants";
import "../../../main.less";
import "./StageCreateModal.less";
var FormItem = _Form.Item;
var Option = _Select.Option;
var RadioGroup = _Radio.Group;
var formItemLayout = {
  labelCol: {
    xs: {
      span: 24
    },
    sm: {
      span: 100
    }
  },
  wrapperCol: {
    xs: {
      span: 24
    },
    sm: {
      span: 26
    }
  }
};
var StageCreateModal = (_dec = _Form.create({}), injectIntl(_class = _dec(_class = (_temp = _class2 = /*#__PURE__*/function (_Component) {
  _inherits(StageCreateModal, _Component);

  var _super = _createSuper(StageCreateModal);

  function StageCreateModal() {
    var _this;

    _classCallCheck(this, StageCreateModal);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _super.call.apply(_super, [this].concat(args));
    _this.state = {
      flowType: ''
    };

    _this.changeFlowType = function (e) {
      _this.setState({
        flowType: e.target.value
      });
    };

    _this.onSubmit = function (e) {
      e.preventDefault();
      var _this$props = _this.props,
          store = _this$props.store,
          prevId = _this$props.prevId,
          onClose = _this$props.onClose,
          stage = _this$props.stage,
          validateFieldsAndScroll = _this$props.form.validateFieldsAndScroll;
      validateFieldsAndScroll(function (err, _ref) {
        var stageName = _ref.stageName,
            triggerType = _ref.triggerType,
            users = _ref.users;

        if (!err) {
          var data = {
            stageName: stageName,
            triggerType: triggerType,
            stageUserRels: users ? _.map(users, function (item) {
              return item;
            }) : null
          };

          if (_.isEmpty(stage)) {
            var currentIndex = store.getStageIndex + 1;
            store.addStage(prevId, _objectSpread(_objectSpread({}, data), {}, {
              isParallel: 0,
              tempId: currentIndex
            }));
            store.setStageIndex(currentIndex);
          } else {
            store.editStage(stage.tempId, _objectSpread(_objectSpread({}, stage), data));
          }

          onClose();
        }
      });
    };

    return _this;
  }

  _createClass(StageCreateModal, [{
    key: "render",
    value: function render() {
      var _this$props2 = this.props,
          formatMessage = _this$props2.intl.formatMessage,
          getFieldDecorator = _this$props2.form.getFieldDecorator,
          _this$props2$stage = _this$props2.stage,
          stageName = _this$props2$stage.stageName,
          triggerType = _this$props2$stage.triggerType,
          stageUserRels = _this$props2$stage.stageUserRels,
          prevId = _this$props2.prevId,
          visible = _this$props2.visible,
          onClose = _this$props2.onClose,
          store = _this$props2.store;
      var flowType = this.state.flowType;
      var createOrEdit = prevId || prevId === 0 ? 'create' : 'edit';

      var user = _.map(store.getUser, function (_ref2) {
        var id = _ref2.id,
            realName = _ref2.realName,
            loginName = _ref2.loginName;
        return /*#__PURE__*/React.createElement(Option, {
          key: id,
          value: String(id)
        }, /*#__PURE__*/React.createElement(_Tooltip, {
          title: loginName
        }, realName || loginName));
      });

      var initUsers;

      if (stageUserRels) {
        initUsers = _.map(stageUserRels.slice(), function (item) {
          return String(item);
        });
      }

      return /*#__PURE__*/React.createElement(Sidebar, {
        visible: visible,
        title: formatMessage({
          id: "pipeline.stage.".concat(createOrEdit)
        }),
        width: 380,
        onOk: this.onSubmit,
        onCancel: onClose,
        okText: /*#__PURE__*/React.createElement(FormattedMessage, {
          id: createOrEdit
        }),
        cancelText: /*#__PURE__*/React.createElement(FormattedMessage, {
          id: "cancel"
        })
      }, /*#__PURE__*/React.createElement(_Form, {
        layout: "vertical"
      }, /*#__PURE__*/React.createElement(FormItem, formItemLayout, getFieldDecorator('stageName', {
        rules: [{
          required: true,
          message: formatMessage({
            id: 'required'
          })
        }],
        initialValue: stageName
      })( /*#__PURE__*/React.createElement(_Input, {
        label: /*#__PURE__*/React.createElement(FormattedMessage, {
          id: "name"
        }),
        type: "text",
        maxLength: 30
      }))), /*#__PURE__*/React.createElement(FormItem, Object.assign({
        className: "c7ncd-stage-modal-from"
      }, formItemLayout), getFieldDecorator('triggerType', {
        initialValue: triggerType || STAGE_FLOW_AUTO
      })( /*#__PURE__*/React.createElement(RadioGroup, {
        label: formatMessage({
          id: 'pipeline.flow'
        }),
        onChange: this.changeFlowType
      }, /*#__PURE__*/React.createElement(_Radio, {
        value: STAGE_FLOW_AUTO
      }, /*#__PURE__*/React.createElement(FormattedMessage, {
        id: "pipeline.flow.auto"
      })), /*#__PURE__*/React.createElement(_Radio, {
        value: STAGE_FLOW_MANUAL
      }, /*#__PURE__*/React.createElement(FormattedMessage, {
        id: "pipeline.flow.manual"
      }))))), (flowType || triggerType) === STAGE_FLOW_MANUAL && /*#__PURE__*/React.createElement(FormItem, formItemLayout, getFieldDecorator('users', {
        rules: [{
          required: true,
          message: formatMessage({
            id: 'required'
          })
        }],
        initialValue: initUsers
      })( /*#__PURE__*/React.createElement(_Select, {
        filter: true,
        allowClear: true,
        mode: "multiple",
        optionFilterProp: "children",
        label: formatMessage({
          id: 'pipeline.flow.member'
        }) // getPopupContainer={(triggerNode) => triggerNode.parentNode}
        ,
        filterOption: function filterOption(input, option) {
          return option.props.children.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0;
        }
      }, user)))));
    }
  }]);

  return StageCreateModal;
}(Component), _class2.defaultProps = {
  stage: {}
}, _temp)) || _class) || _class);
export { StageCreateModal as default };