"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));

var _objectSpread3 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));

var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));

var _react = _interopRequireDefault(require("react"));

var _r2g7rm = _interopRequireDefault(require("./assets/r2g7rm.jpg"));

var _documentTitle = _interopRequireDefault(require("@uiw-admin/document-title"));

var _uiw = require("uiw");

var _swr = _interopRequireDefault(require("swr"));

var _utils = require("@uiw-admin/utils");

var _jsxRuntime = require("react/jsx-runtime");

var _excluded = ["name", "required", "labelFor"],
    _excluded2 = ["title"];

var _default = function _default(props) {
  var _props$align = props.align,
      align = _props$align === void 0 ? 'center' : _props$align,
      _props$classNameWarp = props.classNameWarp,
      classNameWarp = _props$classNameWarp === void 0 ? '' : _props$classNameWarp,
      _props$styleWarp = props.styleWarp,
      styleWarp = _props$styleWarp === void 0 ? {} : _props$styleWarp,
      _props$classNameBody = props.classNameBody,
      classNameBody = _props$classNameBody === void 0 ? '' : _props$classNameBody,
      _props$styleBody = props.styleBody,
      styleBody = _props$styleBody === void 0 ? {} : _props$styleBody,
      _props$footer = props.footer,
      footer = _props$footer === void 0 ? true : _props$footer,
      _props$bg = props.bg,
      bg = _props$bg === void 0 ? _r2g7rm["default"] : _props$bg,
      _props$logo = props.logo,
      logo = _props$logo === void 0 ? _r2g7rm["default"] : _props$logo,
      children = props.children,
      _props$projectName = props.projectName,
      projectName = _props$projectName === void 0 ? 'UIW Admin' : _props$projectName,
      _props$onSuccess = props.onSuccess,
      _onSuccess = _props$onSuccess === void 0 ? function () {
    return null;
  } : _props$onSuccess,
      _props$btnProps = props.btnProps,
      btnProps = _props$btnProps === void 0 ? {} : _props$btnProps,
      api = props.api,
      onBefore = props.onBefore,
      requestConfig = props.requestConfig,
      buttons = props.buttons,
      saveField = props.saveField,
      fields = props.fields,
      _props$isDefaultField = props.isDefaultFields,
      isDefaultFields = _props$isDefaultField === void 0 ? true : _props$isDefaultField,
      defaultFieldsConfig = props.defaultFieldsConfig,
      _props$styleContainer = props.styleContainer,
      styleContainer = _props$styleContainer === void 0 ? {} : _props$styleContainer;

  var _ref = saveField || {},
      _ref$userName = _ref.userName,
      userName = _ref$userName === void 0 ? 'username' : _ref$userName,
      _ref$passWord = _ref.passWord,
      passWord = _ref$passWord === void 0 ? 'password' : _ref$passWord;

  var userNameLabel = ((defaultFieldsConfig || {})['userName'] || {})['label'] || '账号';
  var passWordLabel = ((defaultFieldsConfig || {})['passWord'] || {})['label'] || '密码';
  userName = ((defaultFieldsConfig || {})['userName'] || {})['name'] || userName;
  passWord = ((defaultFieldsConfig || {})['passWord'] || {})['name'] || passWord;

  var _React$useState = _react["default"].useState(),
      _React$useState2 = (0, _slicedToArray2["default"])(_React$useState, 2),
      store = _React$useState2[0],
      setStore = _React$useState2[1];

  var _useSWR = (0, _swr["default"])(store ? [api, (0, _objectSpread3["default"])({
    method: 'POST',
    body: store
  }, requestConfig || {})] : null, _utils.request, {
    revalidateOnFocus: false,
    onSuccess: function onSuccess(resp) {
      return _onSuccess(resp, store);
    }
  }),
      isValidating = _useSWR.isValidating;

  var defaultFields = [(0, _objectSpread3["default"])({
    name: userName,
    label: "".concat(userNameLabel),
    labelFor: userName,
    required: true,
    children: /*#__PURE__*/(0, _jsxRuntime.jsx)("input", {
      type: "text",
      disabled: !!isValidating,
      id: userName,
      placeholder: "\u8BF7\u8F93\u5165".concat(userNameLabel),
      className: "form-field"
    })
  }, (defaultFieldsConfig === null || defaultFieldsConfig === void 0 ? void 0 : defaultFieldsConfig.userName) || {}), (0, _objectSpread3["default"])({
    name: passWord,
    label: "".concat(passWordLabel),
    labelFor: passWord,
    required: true,
    children: /*#__PURE__*/(0, _jsxRuntime.jsx)("input", {
      disabled: !!isValidating,
      id: passWord,
      type: "password",
      placeholder: "\u8BF7\u8F93\u5165".concat(passWordLabel),
      className: "form-field"
    })
  }, (defaultFieldsConfig === null || defaultFieldsConfig === void 0 ? void 0 : defaultFieldsConfig.passWord) || {})];
  var fieldArr = defaultFields;

  if (fields && Array.isArray(fields)) {
    if (isDefaultFields) {
      fieldArr = defaultFields.concat(fields);
    } else {
      fieldArr = fields;
    }
  }

  var FooterRender = _react["default"].useMemo(function () {
    if (footer === false || footer === '') {
      return null;
    } else if (footer === true) {
      return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        className: "copyright-footer",
        children: "\u7248\u6743\u6240\u6709 copyright \xA9 2022 uiw admin"
      });
    }

    return footer;
  }, []);

  return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
    className: "uiw-loayout-login",
    style: (0, _objectSpread3["default"])({
      background: "url(".concat(bg, ")")
    }, styleContainer),
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_documentTitle["default"], {
      title: projectName
    }), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      style: styleWarp,
      className: "uiw-loayout-login-warp ".concat(classNameWarp, " uiw-loayout-login-warp-").concat(align),
      children: /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
        className: "uiw-loayout-login-body ".concat(classNameBody),
        style: styleBody,
        children: [logo && /*#__PURE__*/(0, _jsxRuntime.jsx)("img", {
          className: "logo",
          src: logo
        }), projectName && /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
          className: "uiw-title",
          children: projectName
        }), children ? children : /*#__PURE__*/(0, _jsxRuntime.jsx)(_uiw.Form, {
          resetOnSubmit: false,
          onSubmit: function onSubmit(_ref2) {
            var current = _ref2.current;
            var errorObj = {};
            fieldArr.forEach(function (item) {
              if (item.verification && typeof item.verification === 'function') {
                var result = item.verification(current[item.name], current);

                if (typeof result === 'string' && result) {
                  errorObj[item.name] = result;
                }
              } else if (item.required && !current[userName]) {
                errorObj[item.name] = "".concat(item.label, "\u4E0D\u80FD\u4E3A\u7A7A");
              }
            });

            if (Object.keys(errorObj).length > 0) {
              var err = new Error();
              err.filed = errorObj;
              throw err;
            } else {
              if (typeof onBefore === 'function') {
                var result = onBefore(current);

                if ((0, _typeof2["default"])(result) === 'object') {
                  setStore((0, _objectSpread3["default"])((0, _objectSpread3["default"])((0, _objectSpread3["default"])({}, current), result || {}), {}, {
                    swr_Rest_Time: new Date().getTime()
                  }));
                  return;
                }

                if (!result) {
                  return;
                }
              }

              setStore((0, _objectSpread3["default"])((0, _objectSpread3["default"])({}, current), {}, {
                swr_Rest_Time: new Date().getTime()
              }));
            }
          },
          onSubmitError: function onSubmitError(error) {
            if (error.filed) {
              return (0, _objectSpread3["default"])({}, error.filed);
            }

            return null;
          },
          fields: fieldArr.reduce(function (pre, current) {
            if (!current || current && !current.name) {
              return (0, _objectSpread3["default"])({}, pre);
            }

            var name = current.name,
                required = current.required,
                labelFor = current.labelFor,
                rest = (0, _objectWithoutProperties2["default"])(current, _excluded);
            return (0, _objectSpread3["default"])((0, _objectSpread3["default"])({}, pre), {}, (0, _defineProperty2["default"])({}, name, (0, _objectSpread3["default"])({
              labelFor: name || labelFor
            }, rest)));
          }, {}),
          children: function children(_ref3) {
            var fields = _ref3.fields,
                canSubmit = _ref3.canSubmit;
            return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
              children: [fieldArr.map(function (item, index) {
                return /*#__PURE__*/(0, _jsxRuntime.jsx)(_uiw.Row, {
                  children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_uiw.Col, {
                    children: fields[item.name]
                  })
                }, index);
              }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_uiw.Row, {
                children: buttons && buttons.length > 0 ? buttons.map(function (item, idx) {
                  var title = item.title,
                      rest = (0, _objectWithoutProperties2["default"])(item, _excluded2);
                  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_uiw.Button, (0, _objectSpread3["default"])((0, _objectSpread3["default"])({
                    loading: !!isValidating && item.htmlType === 'submit',
                    disabled: !canSubmit(),
                    className: "btns",
                    type: "danger"
                  }, rest), {}, {
                    children: title
                  }), idx);
                }) : /*#__PURE__*/(0, _jsxRuntime.jsx)(_uiw.Button, (0, _objectSpread3["default"])((0, _objectSpread3["default"])({
                  disabled: !canSubmit(),
                  loading: !!isValidating,
                  className: "btns",
                  block: true,
                  style: {
                    marginTop: 20
                  },
                  htmlType: "submit",
                  type: "dark"
                }, btnProps), {}, {
                  children: "\u767B\u5F55"
                }))
              })]
            });
          }
        })]
      })
    }), FooterRender]
  });
};

exports["default"] = _default;
module.exports = exports.default;
//# sourceMappingURL=index.js.map