"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const react_1 = __importStar(require("react"));
const react_redux_1 = require("react-redux");
const icons_1 = require("@ant-design/icons");
const antd_1 = require("antd");
const styled_components_1 = __importDefault(require("styled-components"));
const ACTIONS = __importStar(require("@/actions/teams/list"));
const index_1 = require("@/pages/system/index");
const period_format_1 = require("@/utils/period-format");
const { Option } = antd_1.Select;
const PAGE_SIZE = 10000;
const Modal = (0, styled_components_1.default)(antd_1.Modal) `
  .ant-modal-content {
    height: 100% !important;
    .ant-modal-body {
      overflow: auto !important;
      max-height: calc(100% - 55px) !important;
    }
  }
`;
const mapStateToProps = (store) => ({
    pageInfo: store.teams.list.pageInfo,
    managementTeam: store.teams.list.managementTeam,
    managementTeamLoading: store.teams.list.managementTeamLoading,
    userManagementDrawerOpen: store.teams.list.userManagementDrawerOpen,
    users: store.teams.list.organizationUsers,
});
const mapDispatchToProps = {
    fetchTeamList: ACTIONS.fetchTeamList,
    fetchTeamUsers: ACTIONS.fetchTeamUsers,
    fetchOrganizationUsers: ACTIONS.fetchOrganizationUsers,
    deleteTeamUsers: ACTIONS.deleteTeamUsers,
    addTeamUsers: ACTIONS.addTeamUsers,
    openUserManagementDrawer: ACTIONS.openUserManagementDrawer,
};
const MemberManagement = (props) => {
    const { organizationId, users, managementTeam, userManagementDrawerOpen, pageInfo, managementTeamLoading, fetchTeamList, fetchTeamUsers, fetchOrganizationUsers, addTeamUsers, deleteTeamUsers, openUserManagementDrawer, } = props;
    const [selectedUserIds, setSelectedUserIds] = (0, react_1.useState)([]);
    // i18n
    const { locale } = (0, react_1.useContext)(index_1.GlobalContext);
    const { global, team } = locale.business;
    (0, react_1.useEffect)(() => {
        if (managementTeam === null || managementTeam === void 0 ? void 0 : managementTeam.id) {
            fetchTeamUsers({ teamId: managementTeam === null || managementTeam === void 0 ? void 0 : managementTeam.id, page: 1, size: PAGE_SIZE });
        }
    }, [managementTeam === null || managementTeam === void 0 ? void 0 : managementTeam.id]);
    (0, react_1.useEffect)(() => {
        if (organizationId)
            fetchOrganizationUsers({ organizationId, page: 1, size: PAGE_SIZE });
    }, [fetchOrganizationUsers, organizationId]);
    const handleSelectUserChange = (value) => {
        setSelectedUserIds(value);
    };
    const handleClose = () => {
        openUserManagementDrawer(false);
        fetchTeamList({ organizationId, page: pageInfo.page, size: pageInfo.size });
    };
    const handleAdd = () => {
        if (selectedUserIds.length === 0) {
            antd_1.message.warning(team.selectUserPlaceHolder);
            return;
        }
        addTeamUsers({
            teamId: managementTeam === null || managementTeam === void 0 ? void 0 : managementTeam.id,
            users: users === null || users === void 0 ? void 0 : users.filter((user) => selectedUserIds.includes(user.userId)),
        }, () => {
            setSelectedUserIds([]);
        });
    };
    return (react_1.default.createElement(Modal, { title: team.userManagement, centered: true, visible: userManagementDrawerOpen, onCancel: handleClose, width: "60%", style: { height: '60%' }, footer: null, destroyOnClose: true, maskClosable: false },
        react_1.default.createElement(antd_1.Spin, { spinning: managementTeamLoading },
            react_1.default.createElement("div", { style: { display: 'flex', marginBottom: 12 } },
                react_1.default.createElement(antd_1.Select, { mode: "multiple", placeholder: team.selectUserPlaceHolder, value: selectedUserIds, onChange: handleSelectUserChange, style: { width: '100%' } }, users === null || users === void 0 ? void 0 : users.map((user) => {
                    var _a;
                    return (react_1.default.createElement(Option, { key: user.userId, value: user.userId, disabled: !!((_a = managementTeam === null || managementTeam === void 0 ? void 0 : managementTeam.members) === null || _a === void 0 ? void 0 : _a.find((item) => item.userId === user.userId)) }, user.account));
                })),
                react_1.default.createElement(antd_1.Button, { type: "primary", onClick: () => {
                        handleAdd();
                    } },
                    react_1.default.createElement(icons_1.PlusOutlined, null),
                    " ",
                    team.addUser)),
            react_1.default.createElement(antd_1.Table, { size: "small", columns: [
                    {
                        title: team.account,
                        dataIndex: 'account',
                    },
                    {
                        title: team.userId,
                        dataIndex: 'userId',
                    },
                    {
                        title: team.joinTime,
                        key: 'joinTime',
                        width: 200,
                        render: (_text, user) => (0, period_format_1.periodFormat)(user.joinTime, 'unknown'),
                    },
                    {
                        title: global.actions,
                        key: 'status',
                        width: 80,
                        render: (_text, user) => {
                            return (react_1.default.createElement(antd_1.Popconfirm, { title: `${global.deleteMsg} ${user.account}?`, onConfirm: () => {
                                    deleteTeamUsers({ teamId: managementTeam.id, users: [user] });
                                }, okText: global.yes, cancelText: global.no },
                                react_1.default.createElement(antd_1.Button, { size: "small", shape: "circle", icon: react_1.default.createElement(icons_1.DeleteOutlined, null) })));
                        },
                    },
                ], rowKey: (record) => record.userId.toString(), dataSource: (managementTeam === null || managementTeam === void 0 ? void 0 : managementTeam.members) || [], pagination: false }))));
};
exports.default = (0, react_redux_1.connect)(mapStateToProps, mapDispatchToProps)(MemberManagement);
