import "choerodon-ui/lib/upload/style";
import _Upload from "choerodon-ui/lib/upload";
import "choerodon-ui/lib/icon/style";
import _Icon from "choerodon-ui/lib/icon";
import "choerodon-ui/lib/form/style";
import _Form from "choerodon-ui/lib/form";

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

/**
 * @author ale0720@163.com
 * @date 2019-05-30 15:37
 */
import React, { PureComponent, Fragment } from 'react';
import { injectIntl, FormattedMessage } from 'react-intl';
import classnames from 'classnames';
import "./CertUploader.less";
var FormItem = _Form.Item;
var formItemLayout = {
  labelCol: {
    xs: {
      span: 24
    },
    sm: {
      span: 100
    }
  },
  wrapperCol: {
    xs: {
      span: 24
    },
    sm: {
      span: 26
    }
  }
};
var uploadProps = {
  listType: 'text',
  // TODO: 安全起见，该处应该替换为系统内地址，而不是使用公共地址
  action: '//jsonplaceholder.typicode.com/posts/',
  multiple: false
};

var CertUploader = injectIntl(_class = /*#__PURE__*/function (_PureComponent) {
  _inherits(CertUploader, _PureComponent);

  var _super = _createSuper(CertUploader);

  function CertUploader() {
    var _this;

    _classCallCheck(this, CertUploader);

    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 = {
      keyDisabled: false,
      crtDisabled: false
    };

    _this.beforeUpload = function (file, type) {
      _this.setState(_defineProperty({}, "".concat(type, "Disabled"), true)); // beforeUpload 需要返回一个bool值


      return false;
    };

    _this.checkKeyFile = function () {
      var _this2;

      for (var _len2 = arguments.length, arg = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
        arg[_key2] = arguments[_key2];
      }

      (_this2 = _this).checkFile.apply(_this2, ['key'].concat(arg));
    };

    _this.checkCrtFile = function () {
      var _this3;

      for (var _len3 = arguments.length, arg = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
        arg[_key3] = arguments[_key3];
      }

      (_this3 = _this).checkFile.apply(_this3, ['crt'].concat(arg));
    };

    _this.checkFile = function (type, rule, value, callback) {
      var formatMessage = _this.props.intl.formatMessage;

      if (!value) {
        callback(formatMessage({
          id: "ctf.".concat(type, ".required")
        }));
      } else {
        var name = value.file.name,
            fileList = value.fileList;

        if (!fileList.length) {
          callback(formatMessage({
            id: "ctf.".concat(type, ".required")
          }));
        } else if (fileList.length > 1) {
          callback(formatMessage({
            id: 'file.type.multiple'
          }));
        } else if (!name.endsWith(".".concat(type))) {
          callback(formatMessage({
            id: 'file.type.error'
          }));
        } else {
          callback();
        }
      }
    };

    _this.removeCert = function () {
      _this.setState({
        crtDisabled: false
      });
    };

    _this.removeKey = function () {
      _this.setState({
        keyDisabled: false
      });
    };

    return _this;
  }

  _createClass(CertUploader, [{
    key: "render",
    value: function render() {
      var _this4 = this;

      var getFieldDecorator = this.props.propsForm.getFieldDecorator;
      var _this$state = this.state,
          keyDisabled = _this$state.keyDisabled,
          crtDisabled = _this$state.crtDisabled;
      var crtUploadClass = classnames({
        'c7ncd-upload-select': !crtDisabled,
        'c7ncd-upload-disabled': crtDisabled
      });
      var keyUploadClass = classnames({
        'c7ncd-upload-select': !keyDisabled,
        'c7ncd-upload-disabled': keyDisabled
      });
      var uploadBtn = /*#__PURE__*/React.createElement(Fragment, null, /*#__PURE__*/React.createElement(_Icon, {
        className: "c7ncd-cert-upload-icon",
        type: "add"
      }), /*#__PURE__*/React.createElement("div", {
        className: "c7n-upload-text"
      }, "Upload"));
      return /*#__PURE__*/React.createElement("div", {
        className: "c7ncd-cert-upload"
      }, /*#__PURE__*/React.createElement("div", {
        className: "c7ncd-cert-upload-item"
      }, /*#__PURE__*/React.createElement("h4", null, /*#__PURE__*/React.createElement(FormattedMessage, {
        id: "ctf.certFile"
      })), /*#__PURE__*/React.createElement(FormItem, formItemLayout, getFieldDecorator('cert', {
        rules: [{
          validator: this.checkCrtFile
        }]
      })( /*#__PURE__*/React.createElement(_Upload, Object.assign({}, uploadProps, {
        disabled: crtDisabled,
        onChange: this.fileChange,
        beforeUpload: function beforeUpload(file) {
          return _this4.beforeUpload(file, 'crt');
        },
        onRemove: this.removeCert,
        accept: ".crt"
      }), /*#__PURE__*/React.createElement("div", {
        className: crtUploadClass
      }, uploadBtn))))), /*#__PURE__*/React.createElement("div", {
        className: "c7ncd-cert-upload-item"
      }, /*#__PURE__*/React.createElement("h4", null, /*#__PURE__*/React.createElement(FormattedMessage, {
        id: "ctf.keyFile"
      })), /*#__PURE__*/React.createElement(FormItem, formItemLayout, getFieldDecorator('key', {
        rules: [{
          validator: this.checkKeyFile
        }]
      })( /*#__PURE__*/React.createElement(_Upload, Object.assign({}, uploadProps, {
        disabled: keyDisabled,
        beforeUpload: function beforeUpload(file) {
          return _this4.beforeUpload(file, 'key');
        },
        onRemove: this.removeKey,
        accept: ".key"
      }), /*#__PURE__*/React.createElement("div", {
        className: keyUploadClass
      }, uploadBtn))))));
    }
  }]);

  return CertUploader;
}(PureComponent)) || _class;

export { CertUploader as default };