import "choerodon-ui/lib/modal/style";
import _Modal from "choerodon-ui/lib/modal";
import "choerodon-ui/lib/button/style";
import _Button from "choerodon-ui/lib/button";

var _dec, _class;

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, Fragment } from 'react';
import { withRouter } from 'react-router-dom';
import { observer, inject } from 'mobx-react';
import { FormattedMessage, injectIntl } from 'react-intl';
import { Choerodon } from '@choerodon/boot';
import _ from 'lodash';
var PendingCheckModal = (_dec = inject('AppState'), injectIntl(_class = withRouter(_class = _dec(_class = observer(_class = /*#__PURE__*/function (_Component) {
  _inherits(PendingCheckModal, _Component);

  var _super = _createSuper(PendingCheckModal);

  function PendingCheckModal() {
    var _this;

    _classCallCheck(this, PendingCheckModal);

    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 = {
      stopLoading: false,
      passLoading: false,
      canCheck: false,
      checkTips: null,
      show: false
    };

    _this.handleSubmit = function (flag) {
      var _this$props = _this.props,
          _this$props$AppState = _this$props.AppState,
          projectId = _this$props$AppState.currentMenuType.projectId,
          userId = _this$props$AppState.userInfo.id,
          formatMessage = _this$props.intl.formatMessage,
          id = _this$props.id,
          _this$props$checkData = _this$props.checkData,
          stageRecordId = _this$props$checkData.stageRecordId,
          taskRecordId = _this$props$checkData.taskRecordId,
          type = _this$props$checkData.type,
          PipelineRecordStore = _this$props.PipelineRecordStore;
      var postData = {
        pipelineRecordId: id,
        userId: userId,
        isApprove: flag,
        type: type,
        stageRecordId: stageRecordId,
        taskRecordId: taskRecordId
      };

      _this.setState(_defineProperty({}, flag ? 'passLoading' : 'stopLoading', true));

      PipelineRecordStore.checkData(projectId, postData).then(function (data) {
        if (data) {
          if (data.failed) {
            Choerodon.prompt(data.message);
          } else if (data.length) {
            // 会签，非最后一人审核，返回数据：[{ audit: true 已审核 | false 未审核, loginName: "工号", realName: "姓名"}]
            var users = {
              check: [],
              unCheck: []
            };

            _.forEach(data, function (_ref) {
              var audit = _ref.audit,
                  loginName = _ref.loginName,
                  realName = _ref.realName;
              users[audit ? 'check' : 'unCheck'].push(realName);
            });

            _this.setState({
              canCheck: false,
              checkTips: formatMessage({
                id: 'pipeline.check.tips.text'
              }, {
                checkUsers: users.check.join('，'),
                unCheckUsers: users.unCheck.join('，')
              })
            });
          } else {
            // 或签、会签最后一人，返回数据[]
            _this.handClose(true);
          }
        }

        _this.setState(_defineProperty({}, flag ? 'passLoading' : 'stopLoading', false));
      });
    };

    _this.handClose = function (flag) {
      var onClose = _this.props.onClose;
      onClose(flag);

      _this.setState({
        show: false,
        canCheck: false,
        checkTips: null
      });
    };

    return _this;
  }

  _createClass(PendingCheckModal, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      var _this2 = this;

      var _this$props2 = this.props,
          _this$props2$AppState = _this$props2.AppState,
          projectId = _this$props2$AppState.currentMenuType.projectId,
          userId = _this$props2$AppState.userInfo.id,
          formatMessage = _this$props2.intl.formatMessage,
          id = _this$props2.id,
          _this$props2$checkDat = _this$props2.checkData,
          type = _this$props2$checkDat.type,
          stageRecordId = _this$props2$checkDat.stageRecordId,
          taskRecordId = _this$props2$checkDat.taskRecordId,
          PipelineRecordStore = _this$props2.PipelineRecordStore;
      var postData = {
        pipelineRecordId: id,
        userId: userId,
        type: type,
        stageRecordId: stageRecordId,
        taskRecordId: taskRecordId
      };
      PipelineRecordStore.canCheck(projectId, postData).then(function (data) {
        if (data) {
          if (data.failed) {
            Choerodon.prompt(data.message);
          } else if ((data.isCountersigned || data.isCountersigned === 0) && data.userName) {
            // 会签已被终止、或签已被审核，返回数据：{ isCountersigned: 0 或签 | 1 会签, userName: "string"}
            _this2.setState({
              show: true,
              canCheck: false,
              checkTips: formatMessage({
                id: "pipeline.canCheck.tips.".concat(data.isCountersigned)
              }, {
                userName: data.userName
              })
            });
          } else {
            // 预检通过，返回数据：{ isCountersigned: null, userName: null }
            _this2.setState({
              show: true,
              canCheck: true
            });
          }
        }
      });
    }
    /**
     * 中止或通过人工审核
     * @param flag 是否通过
     */

  }, {
    key: "render",
    value: function render() {
      var _this$props3 = this.props,
          formatMessage = _this$props3.intl.formatMessage,
          name = _this$props3.name,
          _this$props3$checkDat = _this$props3.checkData,
          type = _this$props3$checkDat.type,
          stageName = _this$props3$checkDat.stageName;
      var _this$state = this.state,
          stopLoading = _this$state.stopLoading,
          passLoading = _this$state.passLoading,
          canCheck = _this$state.canCheck,
          checkTips = _this$state.checkTips,
          show = _this$state.show;
      return /*#__PURE__*/React.createElement(_Modal, {
        visible: show,
        title: formatMessage({
          id: 'pipelineRecord.check.manual'
        }),
        closable: false,
        footer: canCheck ? [/*#__PURE__*/React.createElement(_Button, {
          key: "back",
          onClick: this.handClose.bind(this, false),
          disabled: stopLoading || passLoading
        }, /*#__PURE__*/React.createElement(FormattedMessage, {
          id: "pipelineRecord.check.cancel"
        })), /*#__PURE__*/React.createElement(_Button, {
          key: "stop",
          loading: stopLoading,
          type: "primary",
          onClick: this.handleSubmit.bind(this, false),
          disabled: passLoading
        }, /*#__PURE__*/React.createElement(FormattedMessage, {
          id: "pipelineRecord.check.stop"
        })), /*#__PURE__*/React.createElement(_Button, {
          key: "pass",
          loading: passLoading,
          type: "primary",
          onClick: this.handleSubmit.bind(this, true),
          disabled: stopLoading
        }, /*#__PURE__*/React.createElement(FormattedMessage, {
          id: "pipelineRecord.check.pass"
        }))] : [/*#__PURE__*/React.createElement(_Button, {
          key: "back",
          type: "primary",
          onClick: this.handClose.bind(this, true)
        }, /*#__PURE__*/React.createElement(FormattedMessage, {
          id: "pipelineRecord.check.tips.button"
        }))]
      }, /*#__PURE__*/React.createElement("div", {
        className: "c7n-padding-top_8"
      }, canCheck ? /*#__PURE__*/React.createElement(FormattedMessage, {
        id: "pipelineRecord.check.".concat(type, ".des"),
        values: {
          name: name,
          stage: stageName
        }
      }) : /*#__PURE__*/React.createElement("span", null, checkTips)));
    }
  }]);

  return PendingCheckModal;
}(Component)) || _class) || _class) || _class) || _class);
export { PendingCheckModal as default };