"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/applications/detail/resources/groups"));
const index_1 = require("@/components/index");
const file_1 = require("@/constants/file");
const system_1 = require("@/pages/system");
const EditDrawer_1 = __importDefault(require("./components/EditDrawer"));
const OptionsBox = styled_components_1.default.div `
  display: flex;
  justify-content: flex-end;
  margin-bottom: 12px;
`;
const GroupsBox = styled_components_1.default.div ``;
const LoadingBox = styled_components_1.default.div `
  display: flex;
  justify-content: center;
  align-items: center;
  height: 300px;
  width: 100%;
`;
const GroupCard = (0, styled_components_1.default)(antd_1.Card) `
  position: relative;
  display: inline-block;
  width: 100%;
  text-align: center;
  background-color: #fafafa;
  :hover .foxpage-group-card-operate {
    display: block;
  }
  .ant-card-body {
    display: flex;
    height: 160px;
    align-items: center;
    justify-content: center;
    text-align: center;
  }
`;
const OperateGroup = styled_components_1.default.div `
  position: absolute;
  display: none;
  bottom: 10px;
  right: 10px;
  :hover {
    color: #ff4d4f;
    border-color: #ff4d4f;
  }
`;
const ColAutoSpan = {
    span: 8,
    sx: 24,
    sm: 24,
    md: 12,
    lg: 8,
    xl: 6,
};
const mapStateToProps = (store) => ({
    storeApplicationId: store.applications.detail.settings.app.applicationId,
    loading: store.applications.detail.resources.groups.loading,
    groupList: store.applications.detail.resources.groups.groupList,
});
const mapDispatchToProps = {
    fetchGroups: ACTIONS.fetchResourcesGroups,
    openDrawer: ACTIONS.openEditDrawer,
    deleteGroup: ACTIONS.deleteResourcesGroup,
};
const ResourceGroups = (props) => {
    const { storeApplicationId, loading, groupList, fetchGroups, openDrawer, deleteGroup } = props;
    const [applicationId, setApplicationId] = (0, react_1.useState)();
    // i18n
    const { locale } = (0, react_1.useContext)(system_1.GlobalContext);
    const { global, resource, application: applicationI18n } = locale.business;
    const isInit = (0, react_1.useRef)(false);
    (0, react_1.useEffect)(() => {
        if (storeApplicationId)
            setApplicationId(storeApplicationId);
    }, [storeApplicationId]);
    (0, react_1.useEffect)(() => {
        if (applicationId) {
            fetchGroups({
                applicationId,
            });
            isInit.current = true;
        }
    }, [applicationId]);
    const onDeleteGroup = (id) => {
        antd_1.Modal.confirm({
            title: resource.deleteTitle,
            content: resource.deleteMsg,
            okText: global.yes,
            okType: 'danger',
            cancelText: global.no,
            onOk() {
                if (applicationId)
                    deleteGroup({
                        applicationId,
                        id,
                        status: true,
                    });
            },
        });
    };
    return (react_1.default.createElement(react_1.default.Fragment, null,
        react_1.default.createElement(index_1.FoxPageContent, { breadcrumb: react_1.default.createElement(index_1.FoxPageBreadcrumb, { breadCrumb: [
                    {
                        name: applicationI18n.applicationList,
                        link: '/#/workspace/applications',
                    },
                    { name: resource.resourceGroup },
                ] }) },
            react_1.default.createElement(react_1.default.Fragment, null,
                react_1.default.createElement(OptionsBox, null,
                    react_1.default.createElement(antd_1.Button, { type: "primary", onClick: () => openDrawer(true) },
                        react_1.default.createElement(icons_1.PlusOutlined, null),
                        " ",
                        resource.addResourceGroup)),
                react_1.default.createElement(GroupsBox, null, loading || !isInit.current ? (react_1.default.createElement(LoadingBox, null,
                    react_1.default.createElement(antd_1.Spin, { size: "large" }))) : groupList && groupList.length > 0 ? (react_1.default.createElement(antd_1.Row, { gutter: [16, 16] }, groupList.map((group) => {
                    var _a, _b;
                    const resourceDetail = (_b = (_a = group.tags) === null || _a === void 0 ? void 0 : _a.find) === null || _b === void 0 ? void 0 : _b.call(_a, (item) => item.type === file_1.ResourceTagEnum.ResourceGroup);
                    const ribbonText = resourceDetail === null || resourceDetail === void 0 ? void 0 : resourceDetail.origin;
                    return (react_1.default.createElement(antd_1.Col, Object.assign({}, ColAutoSpan, { key: group.id }),
                        react_1.default.createElement(react_router_dom_1.Link, { to: `/applications/${applicationId}/resources/detail/${group.folderPath}` },
                            react_1.default.createElement(antd_1.Badge.Ribbon, { text: ribbonText || 'No Type', color: ribbonText ? '' : 'red' },
                                react_1.default.createElement(GroupCard, { hoverable: true },
                                    react_1.default.createElement(OperateGroup, { className: "foxpage-group-card-operate" },
                                        react_1.default.createElement(antd_1.Button, { type: "default", size: "small", shape: "circle", icon: react_1.default.createElement(icons_1.EditOutlined, null), style: { marginRight: 4 }, onClick: (e) => {
                                                e.stopPropagation();
                                                e.preventDefault();
                                                openDrawer(true, group);
                                            } }),
                                        react_1.default.createElement(antd_1.Button, { type: "default", size: "small", shape: "circle", icon: react_1.default.createElement(icons_1.DeleteOutlined, null), onClick: (e) => {
                                                e.stopPropagation();
                                                e.preventDefault();
                                                onDeleteGroup(group.id);
                                            } })),
                                    react_1.default.createElement("div", null, group.name))))));
                }))) : (react_1.default.createElement(antd_1.Empty, { image: antd_1.Empty.PRESENTED_IMAGE_SIMPLE }))))),
        react_1.default.createElement(EditDrawer_1.default, null)));
};
exports.default = (0, react_redux_1.connect)(mapStateToProps, mapDispatchToProps)(ResourceGroups);
