"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 react_router_dom_1 = require("react-router-dom");
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("@/components/index");
const global_1 = require("@/constants/global");
const system_1 = require("@/pages/system");
const period_format_1 = require("@/utils/period-format");
const EditDrawer_1 = __importDefault(require("./components/EditDrawer"));
const MemberManagement_1 = __importDefault(require("./components/MemberManagement"));
const Table = (0, styled_components_1.default)(antd_1.Table) `
  .ant-table-pagination.ant-pagination {
    margin: 36px 0 0;
  }
`;
const mapStateToProps = (store) => ({
    organizationId: store.system.user.organizationId,
    fetching: store.teams.list.fetching,
    list: store.teams.list.list,
    pageInfo: store.teams.list.pageInfo,
});
const mapDispatchToProps = {
    clearAll: ACTIONS.clearAll,
    fetchTeamList: ACTIONS.fetchTeamList,
    deleteTeam: ACTIONS.deleteTeam,
    openDrawer: ACTIONS.openDrawer,
    openUserManagementDrawer: ACTIONS.openUserManagementDrawer,
};
const Main = (props) => {
    const { organizationId, fetching, list, pageInfo, fetchTeamList, deleteTeam, openDrawer, openUserManagementDrawer, clearAll, } = props;
    // i18n
    const { locale } = (0, react_1.useContext)(system_1.GlobalContext);
    const { global, team } = locale.business;
    // go back to login page if organization id undefined
    const history = (0, react_router_dom_1.useHistory)();
    if (!organizationId) {
        history.push({
            pathname: '/login',
        });
        return null;
    }
    (0, react_1.useEffect)(() => {
        if (organizationId) {
            fetchTeamList({ organizationId, page: pageInfo.page, size: pageInfo.size });
        }
        return () => {
            clearAll();
        };
    }, [fetchTeamList, organizationId]);
    const columns = [
        {
            title: global.nameLabel,
            dataIndex: 'name',
        },
        {
            title: team.userCount,
            dataIndex: 'memberCount',
        },
        {
            title: global.createTime,
            dataIndex: 'createTime',
            width: 200,
            render: (text) => (0, period_format_1.periodFormat)(text, 'unknown'),
        },
        {
            title: global.actions,
            dataIndex: 'updateTime',
            width: 130,
            render: (_text, record) => (react_1.default.createElement(react_1.default.Fragment, null,
                react_1.default.createElement(antd_1.Button, { type: "default", size: "small", shape: "circle", title: team.userManagement, onClick: () => openUserManagementDrawer(true, record) },
                    react_1.default.createElement(icons_1.TeamOutlined, null)),
                react_1.default.createElement(antd_1.Button, { type: "default", size: "small", shape: "circle", title: global.edit, onClick: () => openDrawer(true, record), style: { marginLeft: 8 } },
                    react_1.default.createElement(icons_1.EditOutlined, null)),
                react_1.default.createElement(antd_1.Popconfirm, { title: `${global.deleteMsg} ${record.name}?`, onConfirm: () => {
                        deleteTeam(organizationId, record);
                    }, 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), style: { marginLeft: 8 } })))),
        },
    ];
    return (react_1.default.createElement(react_1.default.Fragment, null,
        react_1.default.createElement(index_1.Content, null,
            react_1.default.createElement(index_1.FoxPageContent, { breadcrumb: react_1.default.createElement(index_1.FoxPageBreadcrumb, { breadCrumb: [
                        {
                            name: global.team,
                        },
                    ] }), style: { maxWidth: global_1.WIDTH_DEFAULT, margin: '0 auto', overflow: 'unset' } },
                react_1.default.createElement("div", { style: { marginBottom: 12, display: 'flex', justifyContent: 'flex-end' } },
                    react_1.default.createElement(antd_1.Button, { type: "primary", onClick: () => {
                            openDrawer(true);
                        } },
                        react_1.default.createElement(icons_1.PlusOutlined, null),
                        " ",
                        team.add)),
                react_1.default.createElement(Table, { rowKey: "id", loading: fetching, columns: columns, dataSource: list, pagination: pageInfo.total > pageInfo.size
                        ? {
                            position: ['bottomCenter'],
                            current: pageInfo.page,
                            pageSize: pageInfo.size,
                            total: pageInfo.total,
                        }
                        : false, onChange: (pagination) => {
                        fetchTeamList({
                            organizationId,
                            page: pagination.current || 1,
                            size: pagination.pageSize || 10,
                        });
                    } }))),
        react_1.default.createElement(EditDrawer_1.default, null),
        react_1.default.createElement(MemberManagement_1.default, { organizationId: organizationId })));
};
exports.default = (0, react_redux_1.connect)(mapStateToProps, mapDispatchToProps)(Main);
