"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports["default"] = void 0;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _form = _interopRequireDefault(require("@alifd/next/lib/form"));
var _dialog = _interopRequireDefault(require("@alifd/next/lib/dialog"));
var _upload = _interopRequireDefault(require("@alifd/next/lib/upload"));
var _button = _interopRequireDefault(require("@alifd/next/lib/button"));
var _box = _interopRequireDefault(require("@alifd/next/lib/box"));
var _icon = _interopRequireDefault(require("@alifd/next/lib/icon"));
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 _switch = _interopRequireDefault(require("@alifd/next/lib/switch"));
var _rating = _interopRequireDefault(require("@alifd/next/lib/rating"));
var _timePicker = _interopRequireDefault(require("@alifd/next/lib/time-picker2"));
var _numberPicker = _interopRequireDefault(require("@alifd/next/lib/number-picker"));
var _datePicker = _interopRequireDefault(require("@alifd/next/lib/date-picker2"));
var _input = _interopRequireDefault(require("@alifd/next/lib/input"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));
var _react = _interopRequireDefault(require("react"));
require("./index.scss");
var _data2 = require("./data");
var _events = _interopRequireDefault(require("../../utils/events"));
var _api = require("./api");
//这是表单项组件
var FormItem = /*#__PURE__*/function (_React$Component) {
  (0, _inheritsLoose2["default"])(FormItem, _React$Component);
  function FormItem(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.getData = /*#__PURE__*/function () {
      var _ref2 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2(commonVOs) {
        return _regenerator["default"].wrap(function _callee2$(_context2) {
          while (1) switch (_context2.prev = _context2.next) {
            case 0:
              // 清空数据
              _this.setState({
                selectData: ''
              });
            case 1:
            case "end":
              return _context2.stop();
          }
        }, _callee2);
      }));
      return function (_x) {
        return _ref2.apply(this, arguments);
      };
    }();
    _this.onChange = function (value) {
      _this.props.onChange && _this.props.onChange(value);
    };
    // 文件下载
    _this.onFileDownLoad = /*#__PURE__*/function () {
      var _ref3 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee3(id) {
        return _regenerator["default"].wrap(function _callee3$(_context3) {
          while (1) switch (_context3.prev = _context3.next) {
            case 0:
              _context3.next = 2;
              return (0, _api.downloadImage)(id);
            case 2:
            case "end":
              return _context3.stop();
          }
        }, _callee3);
      }));
      return function (_x2) {
        return _ref3.apply(this, arguments);
      };
    }();
    // 删除文件
    _this.onFileDelete = /*#__PURE__*/function () {
      var _ref4 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee4(id) {
        var formData;
        return _regenerator["default"].wrap(function _callee4$(_context4) {
          while (1) switch (_context4.prev = _context4.next) {
            case 0:
              formData = new FormData();
              formData.append('id', id);
              _context4.next = 4;
              return (0, _api.delImage)(formData);
            case 4:
            case "end":
              return _context4.stop();
          }
        }, _callee4);
      }));
      return function (_x3) {
        return _ref4.apply(this, arguments);
      };
    }();
    // 图片附件上传
    _this.onFileChange = /*#__PURE__*/function () {
      var _ref5 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee5(info, type) {
        var uids, fileTypes, _this$props, thumbnailQuality, thumbnailWidth, fileId, newData, fileObject, fileUid, fileType, formData, newImg, id, fileUrl, fileName;
        return _regenerator["default"].wrap(function _callee5$(_context5) {
          while (1) switch (_context5.prev = _context5.next) {
            case 0:
              uids = info.map(function (item) {
                return item.uid;
              });
              fileTypes = ['image/png', 'image/jpg', 'image/jpeg', 'image/gif', 'image/bmp'];
              _this$props = _this.props, thumbnailQuality = _this$props.thumbnailQuality, thumbnailWidth = _this$props.thumbnailWidth, fileId = _this$props.id;
              newData = type === 'file' ? _this.state.fileData : _this.state.imgData;
              if (!(uids.length > _this.state.fileData.length)) {
                _context5.next = 19;
                break;
              }
              fileObject = info[info.length - 1].originFileObj;
              fileUid = info[info.length - 1].uid;
              fileType = info[info.length - 1].type;
              formData = new FormData();
              formData.append('fileId', fileId);
              formData.append('file', fileObject);
              if (fileTypes.indexOf(fileType) !== -1) {
                formData.append('thumbnailQuality', thumbnailQuality ? thumbnailQuality : 0.7);
                formData.append('thumbnailWidth', thumbnailWidth ? thumbnailWidth : 80);
              }
              _context5.next = 14;
              return (0, _api.saveImage)(formData);
            case 14:
              newImg = _context5.sent;
              id = newImg.id, fileUrl = newImg.fileUrl, fileName = newImg.fileName;
              newData.push({
                uid: fileUid,
                id: id,
                name: fileName,
                url: fileUrl,
                downloadURL: fileUrl,
                imgURL: fileUrl,
                state: "done",
                size: 100
              });
              _context5.next = 20;
              break;
            case 19:
              newData = newData.filter(function (item) {
                return uids.indexOf(item.uid) !== -1;
              });
            case 20:
              if (type === 'file') {
                _this.setState({
                  fileData: newData
                });
              } else {
                _this.setState({
                  imgData: newData
                });
              }
            case 21:
            case "end":
              return _context5.stop();
          }
        }, _callee5);
      }));
      return function (_x4, _x5) {
        return _ref5.apply(this, arguments);
      };
    }();
    // 删除图片
    _this.onRemove = /*#__PURE__*/function () {
      var _ref6 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee6(info, data) {
        var newData, currentData, formData;
        return _regenerator["default"].wrap(function _callee6$(_context6) {
          while (1) switch (_context6.prev = _context6.next) {
            case 0:
              newData = data;
              currentData = newData.filter(function (item) {
                return item.uid === info.uid;
              });
              formData = new FormData();
              formData.append('id', currentData[0].id);
              _context6.next = 6;
              return (0, _api.delImage)(formData);
            case 6:
            case "end":
              return _context6.stop();
          }
        }, _callee6);
      }));
      return function (_x6, _x7) {
        return _ref6.apply(this, arguments);
      };
    }();
    // 控件类型
    _this.renderColumns = function () {
      var _this$props2 = _this.props,
        componentType = _this$props2.componentType,
        placeholder = _this$props2.placeholder,
        fieldName = _this$props2.fieldName,
        isPreview = _this$props2.isPreview,
        defaultValue = _this$props2.defaultValue;
      if (!isPreview) {
        defaultValue = _data2.data[fieldName] ? _data2.data[fieldName] : defaultValue ? defaultValue : '';
      }
      if (componentType === 'Input') {
        return /*#__PURE__*/_react["default"].createElement(_input["default"], {
          onChange: function onChange(e) {
            return _this.onChange(e);
          },
          placeholder: placeholder,
          defaultValue: defaultValue
        });
      }
      if (componentType === 'CommonInput') {
        var _this$props3 = _this.props,
          InputLength = _this$props3.InputLength,
          hasLimitHint = _this$props3.hasLimitHint;
        return /*#__PURE__*/_react["default"].createElement(_input["default"], {
          onChange: function onChange(e) {
            return _this.onChange(e);
          },
          maxLength: InputLength,
          hasLimitHint: hasLimitHint,
          placeholder: placeholder,
          defaultValue: defaultValue
        });
      }
      if (componentType === 'CommonTextArea') {
        var _this$props4 = _this.props,
          row = _this$props4.row,
          _InputLength = _this$props4.InputLength,
          _hasLimitHint = _this$props4.hasLimitHint;
        return /*#__PURE__*/_react["default"].createElement(_input["default"].TextArea, {
          onChange: function onChange(e) {
            return _this.onChange(e);
          },
          rows: row,
          maxLength: _InputLength,
          hasLimitHint: _hasLimitHint,
          placeholder: placeholder,
          defaultValue: defaultValue
        });
      }
      if (componentType === 'DatePicker') {
        return /*#__PURE__*/_react["default"].createElement(_datePicker["default"], {
          onChange: function onChange(e) {
            return _this.onChange(e);
          },
          outputFormat: "YYYY-MM-DD",
          placeholder: placeholder,
          defaultValue: defaultValue
        });
      }
      if (componentType === 'NumberPicker') {
        return /*#__PURE__*/_react["default"].createElement(_numberPicker["default"], {
          onChange: function onChange(e) {
            return _this.onChange(e);
          },
          defaultValue: defaultValue
        });
      }
      if (componentType === 'TimePicker') {
        return /*#__PURE__*/_react["default"].createElement(_timePicker["default"], {
          onChange: function onChange(e) {
            return _this.onChange(e);
          },
          placeholder: placeholder,
          defaultValue: defaultValue
        });
      }
      if (componentType === 'Rating') {
        return /*#__PURE__*/_react["default"].createElement(_rating["default"], {
          onChange: function onChange(e) {
            return _this.onChange(e);
          },
          defaultValue: defaultValue
        });
      }
      if (componentType === 'Switch') {
        return /*#__PURE__*/_react["default"].createElement(_switch["default"], {
          onChange: function onChange(e) {
            return _this.onChange(e);
          },
          defaultChecked: defaultValue
        });
      }
      if (componentType === 'CommonOption') {
        var _this$props5 = _this.props,
          isMultiple = _this$props5.isMultiple,
          isDeployment = _this$props5.isDeployment,
          isEdit = _this$props5.isEdit;
        var optionData = _this.state.optionData;
        var childList = optionData.length > 0 ? optionData.map(function (item) {
          return (0, _extends2["default"])({}, item, {
            dictValue: item.dictValue,
            dictKey: item.dictKey,
            label: item.dictValue,
            value: item.dictKey
          });
        }) : _data2.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,
            placeholder: placeholder,
            disabled: isEdit,
            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
            });
          }
        }
      }
      if (componentType === 'CommonAttachment') {
        if (!isPreview) {
          return defaultValue ? /*#__PURE__*/_react["default"].createElement(_box["default"], {
            style: {
              display: 'flex',
              flexDirection: 'column',
              width: 200
            }
          }, defaultValue.map(function (item, index) {
            return /*#__PURE__*/_react["default"].createElement("div", {
              key: item.id,
              className: "attachment"
            }, /*#__PURE__*/_react["default"].createElement("div", {
              className: "attachment-box"
            }, /*#__PURE__*/_react["default"].createElement(_icon["default"], {
              size: "small",
              className: "attachment-box-icon",
              type: "attachment"
            }), /*#__PURE__*/_react["default"].createElement("span", {
              className: "attachment-box-title"
            }, item.name)), /*#__PURE__*/_react["default"].createElement("a", {
              className: "attachment-download",
              href: item.imgURL
            }, "\u4E0B\u8F7D"));
          })) : null;
        } else {
          return /*#__PURE__*/_react["default"].createElement(_upload["default"], {
            listType: "text",
            autoUpload: false,
            onChange: function onChange(e) {
              _this.onFileChange(e, 'file');
            },
            onRemove: function onRemove(e) {
              return _this.onRemove(e, _this.state.fileData);
            }
          }, /*#__PURE__*/_react["default"].createElement(_button["default"], {
            style: {
              margin: "0 0 10px"
            }
          }, /*#__PURE__*/_react["default"].createElement(_icon["default"], {
            type: "upload"
          }), /*#__PURE__*/_react["default"].createElement(_box["default"], null, "\u4E0A\u4F20\u6587\u4EF6")));
        }
      }
      if (componentType === 'CommonImage') {
        if (!isPreview) {
          return defaultValue ? /*#__PURE__*/_react["default"].createElement(_upload["default"].Card, {
            shape: "card",
            style: {
              display: 'none'
            },
            onPreview: _this.onPreview,
            defaultValue: defaultValue
          }) : null;
        } else {
          return /*#__PURE__*/_react["default"].createElement(_upload["default"].Card, {
            shape: "card",
            useDataURL: true,
            autoUpload: false,
            accept: "image/png, image/jpg, image/jpeg, image/gif, image/bmp",
            style: {
              display: 'inline-block'
            },
            onPreview: _this.onPreview,
            onRemove: function onRemove(e) {
              return _this.onRemove(e, _this.state.imgData);
            },
            onChange: function onChange(e) {
              return _this.onFileChange(e, 'img');
            }
          });
        }
      }
      if (componentType === 'CommonSwitch') {
        var _this$props6 = _this.props,
          _isEdit = _this$props6.isEdit,
          defaultChecked = _this$props6.defaultChecked;
        return /*#__PURE__*/_react["default"].createElement(_switch["default"], {
          checkedChildren: "\u5F00",
          defaultChecked: defaultChecked,
          unCheckedChildren: "\u5173",
          onChange: function onChange(e) {
            return _this.onChange(e);
          },
          disabled: _isEdit
        });
      }
      if (componentType === 'CommonNumberInput') {
        var _this$props7 = _this.props,
          _isEdit2 = _this$props7.isEdit,
          precision = _this$props7.precision,
          _defaultValue = _this$props7.defaultValue;
        return /*#__PURE__*/_react["default"].createElement(_numberPicker["default"], {
          precision: precision,
          onChange: function onChange(e) {
            return _this.onChange(e);
          },
          defaultValue: _defaultValue,
          disabled: _isEdit2
        });
      }
      return /*#__PURE__*/_react["default"].createElement(_input["default"], {
        onChange: function onChange(e) {
          return _this.onChange(e);
        },
        placeholder: placeholder,
        defaultValue: defaultValue
      });
    };
    _this.state = {
      childList: [],
      selectData: [],
      imgData: [],
      //图片
      fileData: [],
      //附件
      optionData: []
    };
    return _this;
  }
  var _proto = FormItem.prototype;
  _proto.componentDidMount = /*#__PURE__*/function () {
    var _componentDidMount = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee7() {
      return _regenerator["default"].wrap(function _callee7$(_context7) {
        while (1) switch (_context7.prev = _context7.next) {
          case 0:
            _context7.next = 2;
            return this.initData();
          case 2:
          case "end":
            return _context7.stop();
        }
      }, _callee7, this);
    }));
    function componentDidMount() {
      return _componentDidMount.apply(this, arguments);
    }
    return componentDidMount;
  }();
  _proto.componentWillUnmount = function componentWillUnmount() {
    var _this2 = this;
    _events["default"].off('getData', function () {
      return _this2.getData;
    });
  };
  // 预览图片
  _proto.onPreview = function onPreview(info) {
    _dialog["default"].show({
      title: "图片预览",
      content: /*#__PURE__*/_react["default"].createElement("img", {
        src: info.imgURL,
        style: {
          width: 500,
          height: 500
        }
      }),
      footer: false
    });
  };
  _proto.render = function render() {
    var _this$props8 = this.props,
      displayName = _this$props8.displayName,
      fieldName = _this$props8.fieldName,
      isEdit = _this$props8.isEdit,
      isEmpty = _this$props8.isEmpty,
      componentType = _this$props8.componentType;
    // 表单项
    var formItemLayoutItem = {
      labelCol: {
        span: 8
      },
      wrapperCol: {
        span: 14
      }
    };
    var onterProps = componentType === 'CommonAttachment' ? {
      help: '支持扩展名：.rar .zip .doc .docx .pdf .jpg...'
    } : {};
    return /*#__PURE__*/_react["default"].createElement(_form["default"].Item, (0, _extends2["default"])({
      colon: true,
      name: fieldName
    }, formItemLayoutItem, onterProps, {
      requiredMessage: "\u8BF7\u8F93\u5165" + displayName,
      required: isEmpty,
      disabled: isEdit,
      labelAlign: "left",
      label: displayName
    }), this.renderColumns());
  };
  return FormItem;
}(_react["default"].Component);
FormItem.displayName = void 0;
FormItem.displayName = 'FormItem';
var _default = FormItem;
exports["default"] = _default;