"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/applications/detail/file/conditions"));
const index_1 = require("@/components/index");
const index_2 = require("@/constants/index");
const system_1 = require("@/pages/system");
const index_3 = require("@/utils/index");
const Table = (0, styled_components_1.default)(antd_1.Table) `
  .ant-table-pagination.ant-pagination {
    margin: 24px 0 0;
  }
`;
const mapStateToProps = (store) => ({
    applicationId: store.applications.detail.settings.app.applicationId,
    loading: store.applications.detail.file.conditions.loading,
    pageInfo: store.applications.detail.file.conditions.pageInfo,
    list: store.applications.detail.file.conditions.list,
});
const mapDispatchToProps = {
    fetchList: ACTIONS.fetchList,
    openDrawer: ACTIONS.openEditDrawer,
    deleteCondition: ACTIONS.deleteCondition,
    publishCondition: ACTIONS.publishCondition,
};
function ConditionList(props) {
    const { applicationId, loading, pageInfo, list, fetchList, deleteCondition, openDrawer, publishCondition, } = props;
    // i18n
    const { locale } = (0, react_1.useContext)(system_1.GlobalContext);
    const { folder, global } = locale.business;
    const handlePublish = (contentId) => {
        if (contentId) {
            publishCondition({
                applicationId,
                contentId,
                status: 'release',
            }, () => fetchList({
                applicationId,
                page: pageInfo.page,
                size: pageInfo.size,
            }));
        }
    };
    const handleDelete = (id) => {
        if (applicationId && id) {
            deleteCondition({
                applicationId,
                id,
                status: true,
            }, () => {
                fetchList({
                    applicationId,
                    page: pageInfo.page,
                    size: pageInfo.size,
                });
            });
        }
    };
    const columns = [
        {
            title: global.nameLabel,
            dataIndex: 'name',
            key: 'name',
        },
        {
            title: folder.name,
            dataIndex: 'folderName',
            key: 'folderName',
            render: (folderName) => folderName === '_condition' ? (react_1.default.createElement(antd_1.Tag, { color: "blue" }, global.application)) : (react_1.default.createElement("span", null, folderName)),
        },
        {
            title: global.type,
            dataIndex: 'type',
            key: 'type',
            render: (_text, record) => {
                var _a;
                const schemas = (_a = record.content) === null || _a === void 0 ? void 0 : _a.schemas;
                if ((schemas === null || schemas === void 0 ? void 0 : schemas.length) > 0) {
                    const type = schemas[0].type;
                    if (type) {
                        return index_2.ConditionTypeEnum[type] || '';
                    }
                }
                return '';
            },
        },
        {
            title: global.creator,
            dataIndex: 'creator',
            key: 'creator',
            render: (_text, record) => {
                var _a;
                return ((_a = record === null || record === void 0 ? void 0 : record.creator) === null || _a === void 0 ? void 0 : _a.account) || '--';
            },
        },
        {
            title: global.createTime,
            dataIndex: 'createTime',
            key: 'createTime',
            width: 200,
            render: (text) => {
                return (0, index_3.periodFormat)(text, 'unknown');
            },
        },
        {
            title: global.actions,
            dataIndex: '',
            key: '',
            width: 130,
            render: (_text, record) => (react_1.default.createElement(react_1.default.Fragment, null,
                react_1.default.createElement(antd_1.Button, { size: "small", shape: "circle", icon: react_1.default.createElement(icons_1.EditOutlined, null), onClick: () => {
                        openDrawer(true, record);
                    }, style: { marginRight: 8 } }),
                react_1.default.createElement(antd_1.Button, { type: "default", size: "small", shape: "circle", title: global.publish, onClick: () => handlePublish(record.contentId), style: { marginRight: 8 } },
                    react_1.default.createElement(index_1.PublishIcon, null)),
                react_1.default.createElement(antd_1.Popconfirm, { title: `${global.deleteMsg}${record.name}?`, okText: global.yes, cancelText: global.no, onConfirm: () => handleDelete(record.id) },
                    react_1.default.createElement(antd_1.Button, { size: "small", shape: "circle", icon: react_1.default.createElement(icons_1.DeleteOutlined, null) })))),
        },
    ];
    return (react_1.default.createElement(Table, { rowKey: "id", loading: loading, columns: columns, dataSource: list, pagination: pageInfo.total > pageInfo.size
            ? {
                position: ['bottomCenter'],
                current: pageInfo.page,
                pageSize: pageInfo.size,
                total: pageInfo.total,
            }
            : false, onChange: (pagination) => {
            fetchList({
                applicationId,
                page: pagination.current || 1,
                size: pagination.pageSize || 10,
            });
        } }));
}
exports.default = (0, react_redux_1.connect)(mapStateToProps, mapDispatchToProps)(ConditionList);
