import 'antd/es/row/style';
import _Row from 'antd/es/row';
import 'antd/es/col/style';
import _Col from 'antd/es/col';
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 _get from 'lodash/get';
import { s as styleInject, _ as __extends, d as __assign, a as __spreadArray, b as __awaiter, c as __generator } from './index.js';
import React__default from 'react';
import { BaseTableOld, generateModalForm, PermissionSelect, ApiPermissionSelect, BaseListOld } from '@lm_fe/components_m';
import _map from 'lodash/map';
import 'antd/es/tooltip/style';
import _Tooltip from 'antd/es/tooltip';
import { APP_CONFIG } from '@lm_fe/env';
import { EditOutlined, DeleteOutlined } from '@ant-design/icons';
import 'antd/es/card/style';
import _Card from 'antd/es/card';
import _isEmpty from 'lodash/isEmpty';
import { request } from '@lm_fe/utils';
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/service';
import '@lm_fe/pages';
var css_248z$1 = ".role-permission-card {\n  height: 100%;\n}\n.role-permission-card .ant-card {\n  height: 100%;\n  display: flex;\n  flex-direction: column;\n}\n.role-permission-card .ant-card .ant-card-head {\n  margin-bottom: 0;\n}\n.role-permission-card .ant-card .ant-card-head .ant-card-extra {\n  padding: 0;\n}\n.role-permission-card .ant-card .ant-card-head .ant-btn {\n  height: 30px;\n}\n.role-permission-card .ant-card .ant-card-body {\n  flex: 1;\n  overflow: auto;\n  padding-top: 0;\n}\n.role-permission-card__header {\n  display: flex;\n  justify-content: space-between;\n  align-items: center;\n  flex-flow: row nowrap;\n}\n.role-wrap #table-scrollContainer {\n  overflow: auto !important;\n}\n";
styleInject(css_248z$1);
var RoleTable = function (_super) {
  __extends(RoleTable, _super);
  function RoleTable() {
    return _super !== null && _super.apply(this, arguments) || this;
  }
  return RoleTable;
}(BaseTableOld);
var modalFormDescriptions = {
  name: {
    key: 'name',
    label: '角色代码',
    rules: [{
      required: true,
      message: '角色代码是必填项'
    }],
    inputType: 'input',
    inputProps: {}
  },
  nickname: {
    key: 'nickname',
    label: '角色名称',
    rules: [{
      required: true,
      message: '角色名称是必填项'
    }],
    inputType: 'input',
    inputProps: {}
  },
  groupdesc: {
    key: 'groupdesc',
    label: '角色描述',
    rules: [{
      required: true,
      message: '角色描述是必填项'
    }],
    inputType: 'text_area',
    inputProps: {}
  },
  groupRanks: {
    key: 'groupRanks',
    label: '等级分类',
    inputType: 'MyEditTable',
    inputProps: {
      showEdit: true,
      formDescriptions: [{
        dataIndex: 'name',
        title: '名称',
        inputType: 'MyInput',
        inputProps: {}
      }, {
        dataIndex: 'rankSort',
        title: '等级',
        inputType: 'input_number',
        inputProps: {}
      }, {
        dataIndex: 'administrator',
        title: '管理',
        inputType: 'MC',
        inputProps: {
          options: '是,否',
          marshal: 0
        }
      }]
    }
  }
};
var fromApi = function fromApi(item) {
  return __assign(__assign({}, item), {
    permissions: _get(item, 'permissions') && _map(_get(item, 'permissions'), function (permission) {
      return _get(permission, 'id');
    }),
    authorities: _get(item, 'authorities') && _map(_get(item, 'authorities'), function (permission) {
      return _get(permission, 'name');
    })
  });
};
var toApi = function toApi(item) {
  return __assign(__assign({}, item), {
    authorities: _get(item, 'authorities') && _map(_get(item, 'authorities'), function (auth) {
      return {
        name: auth
      };
    }),
    permissions: _get(item, 'authorities') && _map(_get(item, 'permissions'), function (id) {
      return {
        id: id
      };
    })
  });
};
var processFromApi = function processFromApi(data) {
  return _map(data, function (item) {
    return fromApi(item);
  });
};
var RolesModal = generateModalForm({
  formDescriptions: modalFormDescriptions,
  url: '/api/groups',
  title: '角色',
  fromApi: fromApi,
  toApi: toApi,
  modalProps: {
    width: 600
  },
  formItemLayout: {
    labelCol: {
      span: 6
    },
    wrapperCol: {
      span: 16
    }
  }
});
var tableColumns = [{
  title: '角色代码',
  dataIndex: 'name',
  ellipsis: true,
  width: APP_CONFIG.CELL_WIDTH_SMALL
}, {
  title: '角色名称',
  dataIndex: 'nickname',
  ellipsis: true,
  width: APP_CONFIG.CELL_WIDTH_SMALL
}, {
  title: '角色描述',
  dataIndex: 'groupdesc',
  ellipsis: true,
  width: APP_CONFIG.CELL_WIDTH_MIDDLE,
  render: function render(text, record) {
    return /*#__PURE__*/React__default.createElement(_Tooltip, {
      placement: "topLeft",
      title: text
    }, /*#__PURE__*/React__default.createElement("span", {
      className: "text-overflow",
      style: {
        width: 185
      }
    }, text));
  }
}];
var MenuPermissionCard$1 = function (_super) {
  __extends(MenuPermissionCard, _super);
  function MenuPermissionCard() {
    var _this = _super !== null && _super.apply(this, arguments) || this;
    _this.state = {
      checkedData: []
    };
    _this.handleSaveMenu = function () {
      var onSaveMenuPermission = _this.props.onSaveMenuPermission;
      var checkedData = _this.state.checkedData;
      onSaveMenuPermission && onSaveMenuPermission(checkedData);
    };
    _this.handleChange = function (checkedData) {
      _this.setState({
        checkedData: checkedData
      });
    };
    return _this;
  }
  MenuPermissionCard.prototype.render = function () {
    var role = this.props.role;
    return /*#__PURE__*/React__default.createElement("div", {
      className: "role-permission-card"
    }, /*#__PURE__*/React__default.createElement(_Card, {
      size: "small",
      title: "\u83DC\u5355/\u6743\u9650",
      extra: /*#__PURE__*/React__default.createElement(_Button, {
        type: "primary",
        onClick: this.handleSaveMenu,
        disabled: _isEmpty(role)
      }, "\u4FDD\u5B58")
    }, /*#__PURE__*/React__default.createElement(PermissionSelect, {
      value: _get(role, 'permissions'),
      defaultExpandAll: false,
      disabled: _isEmpty(role),
      onChange: this.handleChange
    })));
  };
  return MenuPermissionCard;
}(React__default.PureComponent);
var MenuPermissionCard = function (_super) {
  __extends(MenuPermissionCard, _super);
  function MenuPermissionCard() {
    var _this = _super !== null && _super.apply(this, arguments) || this;
    _this.state = {
      checkedData: []
    };
    _this.handleSaveApiPermission = function () {
      var onSaveApiPermission = _this.props.onSaveApiPermission;
      var checkedData = _this.state.checkedData;
      if (!_isEmpty(checkedData)) {
        onSaveApiPermission && onSaveApiPermission(checkedData);
      }
    };
    _this.handleChange = function (checkedData) {
      _this.setState({
        checkedData: checkedData
      });
    };
    return _this;
  }
  MenuPermissionCard.prototype.render = function () {
    var role = this.props.role;
    return /*#__PURE__*/React__default.createElement("div", {
      className: "role-permission-card"
    }, /*#__PURE__*/React__default.createElement(_Card, {
      size: "small",
      title: "API\u6743\u9650",
      extra: /*#__PURE__*/React__default.createElement(_Button, {
        type: "primary",
        onClick: this.handleSaveApiPermission,
        disabled: _isEmpty(role)
      }, "\u4FDD\u5B58")
    }, /*#__PURE__*/React__default.createElement(ApiPermissionSelect, {
      value: _get(role, 'authorities'),
      disabled: _isEmpty(role),
      onChange: this.handleChange
    })));
  };
  return MenuPermissionCard;
}(React__default.PureComponent);
var css_248z = ".role-wrap {\n  height: 100%;\n}\n.role-wrap .ant-col {\n  height: 100%;\n  overflow: auto;\n}\n.role-wrap .ant-col .ant-table-title {\n  background-color: #fff;\n}\n.role-wrap .ant-col .ant-card-extra {\n  padding: 8px;\n}\n";
styleInject(css_248z);
var Roles = function (_super) {
  __extends(Roles, _super);
  function Roles() {
    var _this = _super !== null && _super.apply(this, arguments) || this;
    _this.state = {
      dataSource: [],
      menuDataSource: [],
      visible: false,
      editable: false,
      id: undefined,
      defaultCheckedMenu: [],
      activeRole: {}
    };
    _this.roleColumns = __spreadArray(__spreadArray([], _this.props.tableColumns, true), [{
      title: '操作',
      align: 'center',
      width: 136,
      render: function render(value, rowData) {
        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: function onClick() {
            _this.setState({
              visible: true,
              editable: true,
              id: rowData.id
            });
          }
        }, "\u7F16\u8F91"), /*#__PURE__*/React__default.createElement(_Divider, {
          type: "vertical"
        }), /*#__PURE__*/React__default.createElement(_Popconfirm, {
          title: "\u786E\u5B9A\u8981\u5220\u9664\u8FD9\u4E2A".concat(_get(_this.props, 'baseTitle'), "\u5417?"),
          onConfirm: _this.handleDelete(rowData),
          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.handleSearch = function () {
      return __awaiter(_this, void 0, void 0, function () {
        var _a, baseUrl, needPagination, dataSource, _b, total;
        return __generator(this, function (_c) {
          switch (_c.label) {
            case 0:
              _a = this.props, baseUrl = _a.baseUrl, needPagination = _a.needPagination;
              _b = processFromApi;
              return [4, request.get(baseUrl)];
            case 1:
              dataSource = _b.apply(void 0, [_c.sent().data]);
              total = 0;
              if (!needPagination) return [3, 3];
              return [4, request.get("".concat(baseUrl, "/count"))];
            case 2:
              total = _c.sent().data;
              _c.label = 3;
            case 3:
              this.setState({
                dataSource: dataSource,
                total: total
              });
              return [2];
          }
        });
      });
    };
    _this.handleRowClick = function (rowData) {
      return function (e) {
        _this.setState({
          defaultCheckedMenu: _get(rowData, 'permissions'),
          activeRole: rowData
        });
      };
    };
    _this.setRowClassName = function (rowData) {
      var activeRole = _this.state.activeRole;
      if (_get(rowData, 'id') === _get(activeRole, 'id')) {
        return 'table-row-active';
      }
      return '';
    };
    _this.handleSaveApiPermission = function (checkedData) {
      return __awaiter(_this, void 0, void 0, function () {
        var activeRole, baseUrl, error_1;
        return __generator(this, function (_a) {
          switch (_a.label) {
            case 0:
              activeRole = this.state.activeRole;
              baseUrl = this.props.baseUrl;
              _a.label = 1;
            case 1:
              _a.trys.push([1, 4,, 5]);
              return [4, request.put(baseUrl, toApi(__assign(__assign({}, activeRole), {
                authorities: checkedData
              })))];
            case 2:
              _a.sent();
              _message.success('保存 API 权限成功');
              return [4, this.handleSearch()];
            case 3:
              _a.sent();
              return [3, 5];
            case 4:
              error_1 = _a.sent();
              console.log(error_1);
              return [3, 5];
            case 5:
              return [2];
          }
        });
      });
    };
    _this.handleSaveMenuPermission = function (checkedData) {
      return __awaiter(_this, void 0, void 0, function () {
        var activeRole, baseUrl, error_2;
        return __generator(this, function (_a) {
          switch (_a.label) {
            case 0:
              activeRole = this.state.activeRole;
              baseUrl = this.props.baseUrl;
              _a.label = 1;
            case 1:
              _a.trys.push([1, 4,, 5]);
              return [4, request.put(baseUrl, toApi(__assign(__assign({}, activeRole), {
                permissions: checkedData
              })))];
            case 2:
              _a.sent();
              _message.success('保存菜单/权限成功');
              return [4, this.handleSearch()];
            case 3:
              _a.sent();
              return [3, 5];
            case 4:
              error_2 = _a.sent();
              console.log(error_2);
              return [3, 5];
            case 5:
              return [2];
          }
        });
      });
    };
    return _this;
  }
  Roles.prototype.render = function () {
    var _this = this;
    var baseTitle = this.props.baseTitle;
    var _a = this.state,
      dataSource = _a.dataSource,
      visible = _a.visible,
      editable = _a.editable,
      id = _a.id,
      activeRole = _a.activeRole;
    return /*#__PURE__*/React__default.createElement(React__default.Fragment, null, /*#__PURE__*/React__default.createElement(_Row, {
      gutter: 12,
      className: "role-wrap"
    }, /*#__PURE__*/React__default.createElement(_Col, {
      flex: "700px"
    }, /*#__PURE__*/React__default.createElement(RoleTable, {
      columns: this.roleColumns,
      dataSource: dataSource,
      onAdd: function onAdd() {
        _this.setState({
          visible: true,
          editable: true
        });
      },
      pagination: false,
      rowClassName: this.setRowClassName,
      onRow: function onRow(record) {
        return {
          onClick: _this.handleRowClick(record)
        };
      },
      baseTitle: baseTitle,
      scroll: {}
    })), /*#__PURE__*/React__default.createElement(_Col, {
      flex: "1"
    }, /*#__PURE__*/React__default.createElement(MenuPermissionCard$1, {
      key: _get(activeRole, 'id'),
      role: activeRole,
      onSaveMenuPermission: this.handleSaveMenuPermission
    })), /*#__PURE__*/React__default.createElement(_Col, {
      flex: "1"
    }, /*#__PURE__*/React__default.createElement(MenuPermissionCard, {
      key: _get(activeRole, 'id'),
      role: activeRole,
      onSaveApiPermission: this.handleSaveApiPermission
    }))), visible && /*#__PURE__*/React__default.createElement(RolesModal, {
      visible: visible,
      editable: editable,
      id: id,
      onCancel: this.handleCancel,
      onSearch: this.handleSearch
    }));
  };
  Roles.defaultProps = {
    baseUrl: '/api/groups',
    baseTitle: '角色1',
    needPagination: false,
    showQuery: false,
    tableColumns: tableColumns,
    rowKey: 'id',
    showAdd: false
  };
  return Roles;
}(BaseListOld);
export { Roles as default };
//# sourceMappingURL=pages.system.role.js.map
