import 'antd/es/message/style';
import _message from 'antd/es/message';
import 'antd/es/popconfirm/style';
import _Popconfirm from 'antd/es/popconfirm';
import 'antd/es/divider/style';
import _Divider from 'antd/es/divider';
import 'antd/es/button/style';
import _Button from 'antd/es/button';
import 'antd/es/switch/style';
import _Switch from 'antd/es/switch';
import { _ as __extends, d as __assign, a as __spreadArray, b as __awaiter, c as __generator } from './index.js';
import React__default, { useEffect } from 'react';
import { EditOutlined, RedoOutlined, DeleteOutlined } from '@ant-design/icons';
import { BaseTableOld, BaseQuery, formatTimeToStandard, formatTimeToDate, generateModalForm, BaseListOld } from '@lm_fe/components_m';
import _omit from 'lodash/omit';
import _isEmpty from 'lodash/isEmpty';
import _reduce from 'lodash/reduce';
import _map from 'lodash/map';
import _get from 'lodash/get';
import dayjs from 'dayjs';
import 'antd/es/modal/style';
import _Modal from 'antd/es/modal';
import { _ as _extends } from './extends.js';
import 'antd/es/input/style';
import _Input from 'antd/es/input';
import 'antd/es/form/style';
import _Form from 'antd/es/form';
import { SLocal_State } from '@lm_fe/service';
import { request } from '@lm_fe/utils';
import { APP_CONFIG } from '@lm_fe/env';
import 'antd/es/tabs/style';
import 'antd/es/tabs';
import 'lodash/size';
import 'lodash/orderBy';
import '@lm_fe/components';
import 'antd/es/collapse/style';
import 'antd/es/collapse';
import 'antd/es/badge/style';
import 'antd/es/badge';
import 'lodash/assign';
import '@lm_fe/pages';
var Table = function (_super) {
  __extends(Table, _super);
  function Table() {
    return _super !== null && _super.apply(this, arguments) || this;
  }
  return Table;
}(BaseTableOld);
var modalFormDescriptions = {
  userType: {
    key: 'userType',
    label: '类型',
    rules: [{
      required: true,
      message: '登录账号是必填项'
    }],
    inputType: 'normal_select',
    special_config: '{"type":"userTypeMapping"}',
    inputProps: {},
    layout: '1/2'
  },
  overdueDate: {
    key: 'overdueDate',
    label: '过期',
    inputType: 'single_date_picker',
    inputProps: {},
    layout: '1/2'
  },
  login: {
    key: 'login',
    label: '账号',
    rules: [{
      required: true,
      message: '登录账号是必填项'
    }],
    inputType: 'input',
    inputProps: {},
    layout: '1/2'
  },
  password: {
    key: 'password',
    label: '登录密码',
    inputType: 'password',
    rules: [],
    inputProps: {},
    layout: '1/2'
  },
  firstName: {
    key: 'firstName',
    label: '姓名',
    rules: [{
      required: true,
      message: '姓名是必填项'
    }],
    inputType: 'input',
    inputProps: {},
    layout: '1/2'
  },
  groups: {
    key: 'groups',
    label: '角色',
    rules: [{
      required: true,
      message: '角色是必选项'
    }],
    inputType: 'roles',
    inputProps: {},
    layout: '1/1'
  }
};
var queryFormDescriptions = {
  login: {
    label: '账号',
    inputType: 'input',
    filterType: 'contains'
  },
  firstName: {
    label: '姓名',
    inputType: 'input',
    filterType: ''
  },
  role: {
    label: '角色',
    inputType: 'select',
    url: '/api/groups',
    filterType: ''
  },
  score: {
    label: '分数',
    inputType: 'rangeInputNumber'
  }
};
var Query = function (_super) {
  __extends(Query, _super);
  function Query() {
    var _this = _super !== null && _super.apply(this, arguments) || this;
    _this.state = {
      queryFormDescriptions: queryFormDescriptions
    };
    return _this;
  }
  return Query;
}(BaseQuery);
var processFromApi = function processFromApi(data) {
  return _map(data, function (item) {
    return __assign(__assign({}, item), {
      overdueDate: dayjs(_get(item, 'overdueDate')),
      createdDate: formatTimeToStandard(_get(item, 'createdDate')),
      lastModifiedDate: formatTimeToStandard(_get(item, 'lastModifiedDate')),
      roles: _map(_get(item, 'groups'), function (group) {
        return _get(group, 'id');
      }),
      role: _reduce(_get(item, 'groups'), function (sum, group) {
        return "".concat(_isEmpty(sum) ? '' : sum + '、').concat(_get(group, 'nickname'));
      }, '')
    });
  });
};
var fromApi = function fromApi(item) {
  _get(item, 'groups');
  return __assign(__assign({}, item), {
    overdueDate: _get(item, 'overdueDate') ? dayjs(_get(item, 'overdueDate')) : undefined,
    createdDate: formatTimeToStandard(_get(item, 'createdDate')),
    lastModifiedDate: formatTimeToStandard(_get(item, 'lastModifiedDate'))
  });
};
var toApi = function toApi(item) {
  return __assign(__assign({}, _omit(item, ['createdDate', 'lastModifiedDate', 'roles', 'role'])), {
    overdueDate: formatTimeToDate(_get(item, 'overdueDate'))
  });
};
var UsersModal = generateModalForm({
  formDescriptions: modalFormDescriptions,
  url: '/api/users',
  title: '用户',
  toApi: toApi,
  fromApi: fromApi,
  modalProps: {
    width: 700
  }
});
var formItemLayout = {
  labelCol: {
    span: 6
  },
  wrapperCol: {
    span: 16
  }
};
function ResetPasswordModal(_a) {
  var visible = _a.visible,
    onCancel = _a.onCancel,
    dataSource = _a.dataSource;
  _a.roles;
  var user = SLocal_State.getUserData();
  var form = _Form.useForm()[0];
  useEffect(function () {
    visible && form.resetFields();
  }, [visible]);
  var handleSubmit = function handleSubmit() {
    form.validateFields().then(function (values) {
      if (!(user === null || user === void 0 ? void 0 : user.groups.some(function (_) {
        return _.nickname === 'ADMIN';
      }))) {
        _message.info('您没有权限修改其他用户的密码，请联系管理员...');
      } else {
        request.post('/api/account/reset-password', {
          password: values.password,
          id: dataSource.id,
          login: dataSource.login
        }).then(function (r) {
          _message.success('重置密码成功');
          onCancel();
        })["catch"](function (error) {
          _message.error('重置密码失败...');
        });
      }
    })["catch"](function (error) {});
  };
  return /*#__PURE__*/React__default.createElement(_Modal, {
    centered: true,
    destroyOnClose: true,
    width: 400,
    visible: visible,
    onCancel: onCancel,
    onOk: handleSubmit,
    title: "\u91CD\u7F6E\u5BC6\u7801"
  }, /*#__PURE__*/React__default.createElement(_Form, _extends({}, formItemLayout, {
    form: form,
    autoComplete: "off"
  }), /*#__PURE__*/React__default.createElement(_Form.Item, {
    name: "password",
    label: "\u91CD\u7F6E\u5BC6\u7801",
    rules: [{
      required: true
    }]
  }, /*#__PURE__*/React__default.createElement(_Input, {
    allowClear: true,
    placeholder: "\u8BF7\u8F93\u5165\u91CD\u7F6E\u5BC6\u7801"
  }))));
}
var tableColumns = [{
  title: '序号',
  dataIndex: 'index',
  width: 40,
  align: 'center',
  render: function render(text, record, index) {
    return index + 1;
  }
}, {
  title: '账号',
  dataIndex: 'login',
  ellipsis: true,
  width: APP_CONFIG.CELL_WIDTH_SMALL
}, {
  title: '姓名',
  dataIndex: 'firstName',
  ellipsis: true,
  width: APP_CONFIG.CELL_WIDTH_SMALL
}, {
  title: '角色',
  dataIndex: 'role',
  width: APP_CONFIG.CELL_WIDTH_SMALL,
  ellipsis: true,
  render: function render(value, rowData) {
    var groups = rowData.groups;
    var res = _reduce(groups, function (sum, group) {
      return "".concat(_isEmpty(sum) ? '' : "".concat(sum, "\u3001")).concat(_get(group, 'nickname'));
    }, '');
    return /*#__PURE__*/React__default.createElement("span", null, res);
  }
}, {
  title: '创建时间',
  dataIndex: 'createdDate',
  ellipsis: true,
  width: APP_CONFIG.CELL_WIDTH_MIDDLE,
  render: function render(value) {
    return formatTimeToStandard(value);
  }
}, {
  title: '创建者',
  dataIndex: 'createdBy',
  ellipsis: true,
  width: APP_CONFIG.CELL_WIDTH_SMALL
}, {
  title: '最近修改时间',
  dataIndex: 'lastModifiedDate',
  ellipsis: true,
  width: APP_CONFIG.CELL_WIDTH_MIDDLE,
  render: function render(value) {
    return formatTimeToStandard(value);
  }
}, {
  title: '最近修改者',
  dataIndex: 'lastModifiedBy',
  ellipsis: true,
  width: APP_CONFIG.CELL_WIDTH_SMALL
}];
var UserList = function (_super) {
  __extends(UserList, _super);
  function UserList(props) {
    var _this = _super.call(this, props) || this;
    _this.handleAdd = function () {
      _this.setState({
        visible: true,
        editable: true
      });
    };
    _this.columns = __spreadArray(__spreadArray([], _this.props.tableColumns, true), [{
      title: '活跃状态',
      dataIndex: 'activated',
      showSorter: false,
      showFilter: false,
      key: 'activated',
      align: 'center',
      width: 65,
      render: function render(value, record) {
        return /*#__PURE__*/React__default.createElement(_Switch, {
          size: "small",
          defaultChecked: value,
          onChange: _this.handleDisableUser(record)
        });
      }
    }, {
      title: '操作',
      align: 'center',
      showSorter: false,
      showFilter: false,
      fixed: 'right',
      width: 208,
      render: function render(value, record, index) {
        return /*#__PURE__*/React__default.createElement(React__default.Fragment, null, /*#__PURE__*/React__default.createElement(_Button, {
          type: "link",
          size: "small",
          icon: /*#__PURE__*/React__default.createElement(EditOutlined, {
            className: "global-table-action-icon"
          }),
          onClick: _this.handleEdit(record)
        }, "\u7F16\u8F91"), /*#__PURE__*/React__default.createElement(_Divider, {
          type: "vertical"
        }), /*#__PURE__*/React__default.createElement(_Button, {
          type: "link",
          size: "small",
          icon: /*#__PURE__*/React__default.createElement(RedoOutlined, {
            className: "global-table-action-icon"
          }),
          onClick: _this.handleResetEdit(record)
        }, "\u91CD\u7F6E"), /*#__PURE__*/React__default.createElement(_Divider, {
          type: "vertical"
        }), /*#__PURE__*/React__default.createElement(_Popconfirm, {
          title: "\u786E\u5B9A\u8981\u5220\u9664\u8FD9\u4E2A".concat(_this.props.baseTitle, "\u5417?"),
          onConfirm: _this.handleDelete(record),
          okText: "\u786E\u5B9A",
          cancelText: "\u53D6\u6D88"
        }, /*#__PURE__*/React__default.createElement(_Button, {
          type: "link",
          size: "small",
          icon: /*#__PURE__*/React__default.createElement(DeleteOutlined, {
            className: "global-table-action-icon"
          })
        }, "\u5220\u9664")));
      }
    }], false);
    _this.handleEdit = function (record) {
      return function () {
        _this.setState({
          visible: true,
          editable: true,
          id: record.id,
          primaryKey: record.login
        });
      };
    };
    _this.handleResetCancel = function () {
      _this.setState({
        resetVisible: false,
        resetEditable: false
      });
    };
    _this.handleResetEdit = function (record) {
      return function () {
        _this.setState({
          resetVisible: true,
          resetEditable: true,
          id: record.id,
          currentRecord: record
        });
      };
    };
    _this.handleDisableUser = function (record) {
      return function () {
        return __awaiter(_this, void 0, void 0, function () {
          var baseUrl, error_1;
          return __generator(this, function (_a) {
            switch (_a.label) {
              case 0:
                baseUrl = this.props.baseUrl;
                _a.label = 1;
              case 1:
                _a.trys.push([1, 3,, 4]);
                return [4, request.put(baseUrl, toApi(__assign(__assign({}, record), {
                  roles: record.roles,
                  activated: !record.activated
                })))];
              case 2:
                _a.sent();
                _message.success('切换用户状态成功');
                return [3, 4];
              case 3:
                error_1 = _a.sent();
                console.log(error_1);
                return [3, 4];
              case 4:
                this.handleSearch();
                return [2];
            }
          });
        });
      };
    };
    _this.renderOthersModal = function () {
      var _a = _this.state,
        visible = _a.visible,
        editable = _a.editable,
        id = _a.id,
        resetVisible = _a.resetVisible,
        currentRecord = _a.currentRecord,
        primaryKey = _a.primaryKey;
      return /*#__PURE__*/React__default.createElement(React__default.Fragment, null, /*#__PURE__*/React__default.createElement(UsersModal, {
        id: id,
        primaryKey: primaryKey,
        visible: visible,
        editable: editable,
        onCancel: _this.handleCancel,
        onSearch: _this.handleQuerySearch,
        onSubmit: _this.handleSubmit
      }), /*#__PURE__*/React__default.createElement(ResetPasswordModal, {
        id: id,
        visible: resetVisible,
        dataSource: currentRecord,
        onCancel: _this.handleResetCancel,
        onSearch: _this.handleQuerySearch
      }));
    };
    _this.handleSubmit = function (data) {
      return __awaiter(_this, void 0, void 0, function () {
        var tip, method, error_2;
        return __generator(this, function (_a) {
          switch (_a.label) {
            case 0:
              _a.trys.push([0, 2,, 3]);
              tip = '';
              method = '';
              if (data.id) {
                tip = '修改用户成功';
                method = 'put';
              } else {
                tip = '添加用户成功';
                method = 'post';
              }
              return [4, request[method]('/api/users', data)];
            case 1:
              _a.sent();
              _message.success(tip);
              this.handleCancel();
              this.handleQuerySearch();
              return [3, 3];
            case 2:
              error_2 = _a.sent();
              if (error_2.status === 400) {
                _message.error('该用户名已存在，请重新输入');
              }
              return [3, 3];
            case 3:
              return [2];
          }
        });
      });
    };
    _this.handleDelete = function (record) {
      return function () {
        return __awaiter(_this, void 0, void 0, function () {
          var _a, baseUrl, baseTitle;
          return __generator(this, function (_b) {
            switch (_b.label) {
              case 0:
                _a = this.props, baseUrl = _a.baseUrl, baseTitle = _a.baseTitle;
                return [4, request["delete"]("".concat(baseUrl, "/").concat(record.login))];
              case 1:
                _b.sent();
                _message.success("\u5220\u9664".concat(baseTitle, "\u6210\u529F"));
                this.handleSearch();
                return [2];
            }
          });
        });
      };
    };
    _this.state = __assign(__assign({}, _this.state), {
      visible: false,
      resetEditable: false,
      resetVisible: false
    });
    return _this;
  }
  UserList.defaultProps = {
    baseUrl: '/api/users',
    baseTitle: '用户',
    needPagination: true,
    processFromApi: processFromApi,
    showAdd: true,
    showQuery: true,
    rowKey: 'id',
    tableColumns: tableColumns,
    Table: Table,
    Query: Query
  };
  return UserList;
}(BaseListOld);
export { UserList as default };
//# sourceMappingURL=pages.system.user.js.map
