"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/packages/fast"));
const index_1 = require("@/components/index");
const system_1 = require("@/pages/system");
const detail_1 = require("../detail");
const { Option } = antd_1.Select;
const GroupContainer = styled_components_1.default.div `
  margin-bottom: 12px;
  display: flex;
  flex-wrap: wrap;
  padding-left: 20%;
`;
const Tips = styled_components_1.default.div `
  margin-bottom: 8px;
  font-style: italic;
  font-size: 12px;
  padding: 0 20px;
  color: #656565;
  text-align: right;
  display: flex;
  flex-direction: row;
  line-height: 24px;
  span {
    color: #f90;
    font-size: 16px;
    padding-right: 12px;
  }
`;
const List = styled_components_1.default.ul `
  list-style-type: none;
  margin: 0;
  padding: 0 20px;
  max-height: 400px;
  overflow: auto;
`;
const Row = styled_components_1.default.li `
  padding: 8px;
  margin-bottom: 8px;
  background-color: #fafafa;
  &:hover {
    background-color: #e6f7ff;
  }
`;
const Header = styled_components_1.default.div ``;
const Name = styled_components_1.default.span `
  padding: 0 12px;
  font-weight: 500;
  line-height: 40px;
`;
const ExpendBtn = styled_components_1.default.span `
  float: right;
  line-height: 40px;
  display: inline-block;
  padding: 0 10px;
  color: #797979;
  &:hover {
    cursor: pointer;
  }
`;
const GoResource = styled_components_1.default.div `
  font-size: 12px;
  line-height: 32px;
  margin-left: 20px;
  a {
    color: #656565;
    &:hover {
      color: #1890ff;
    }
  }
`;
const Version = styled_components_1.default.span `
  font-size: 12px;
  color: #f90;
  background-color: #fff6e5;
  padding: 0 4px;
  border-radius: 4px;
`;
const mapStateToProps = (store) => ({
    applicationId: store.applications.detail.settings.app.applicationId,
    groups: store.applications.detail.resources.groups.groupList,
    packages: store.applications.detail.packages.fast.packages,
    pageInfo: store.applications.detail.packages.fast.pageInfo,
    groupId: store.applications.detail.packages.fast.groupId,
    loading: store.applications.detail.packages.fast.loading,
    checkedList: store.applications.detail.packages.fast.checkedList,
});
const mapDispatchToProps = {
    clearAll: ACTIONS.clearAll,
    fetchPackages: ACTIONS.fetchPackages,
    update: ACTIONS.updateChanges,
    updateSelected: ACTIONS.updateSelected,
    selectGroup: ACTIONS.selectGroup,
    updateSearchName: ACTIONS.updateSearchName,
};
const Main = (props) => {
    const { applicationId, groups = [], packages = [], pageInfo, loading, checkedList, clearAll, fetchPackages, update, updateSelected, selectGroup, updateSearchName, } = props;
    const [groupId, setGroupId] = (0, react_1.useState)('');
    const [expendIds, setExpendIds] = (0, react_1.useState)([]);
    const [name, setName] = (0, react_1.useState)('');
    // i18n
    const { locale } = (0, react_1.useContext)(system_1.GlobalContext);
    const { global, package: packageI18n } = locale.business;
    (0, react_1.useEffect)(() => {
        return () => {
            clearAll();
        };
    }, []);
    const handleSearch = (page = 1, size = pageInfo.size) => {
        if (!groupId) {
            antd_1.message.warning(packageI18n.groupTips);
            return;
        }
        selectGroup(groupId);
        updateSearchName(name);
        if (applicationId)
            fetchPackages({
                applicationId,
                groupId,
                page,
                size,
                name: name,
            });
    };
    const handleExpend = (id) => {
        const idx = expendIds.findIndex((item) => item === id);
        if (idx < 0) {
            expendIds.push(id);
        }
        else {
            expendIds.splice(idx, 1);
        }
        setExpendIds(expendIds.concat([]));
    };
    const handleCheck = (id) => {
        const newList = checkedList.concat([]);
        const idx = newList.findIndex((item) => item === id);
        if (idx < 0) {
            newList.push(id);
        }
        else {
            newList.splice(idx, 1);
        }
        updateSelected(newList);
    };
    const handleChange = (componentName, value) => {
        update(componentName, value);
    };
    const Item = (pkg) => {
        var _a;
        const component = pkg.components[0];
        const lastVersion = pkg.lastVersion;
        const key = (component === null || component === void 0 ? void 0 : component.resource.name) || '';
        const expend = expendIds.findIndex((item) => item === key) > -1;
        const checked = checkedList.findIndex((item) => item === key) > -1;
        const isRegistered = !!((_a = pkg.lastVersion) === null || _a === void 0 ? void 0 : _a.id);
        const isUpdate = isRegistered && component.resource.isNew;
        return (react_1.default.createElement(Row, { key: key },
            react_1.default.createElement(Header, { onClick: () => {
                    handleCheck(key);
                } },
                react_1.default.createElement(antd_1.Checkbox, { checked: checked }),
                react_1.default.createElement(Name, null, component === null || component === void 0 ? void 0 : component.resource.name),
                !isRegistered && (react_1.default.createElement(antd_1.Tag, { color: "volcano", style: { zoom: 0.8 } }, "New")),
                isUpdate && (react_1.default.createElement(antd_1.Tag, { color: "green", style: { zoom: 0.8 } }, "Update")),
                !isRegistered && checked && react_1.default.createElement(Version, null, component.component.version),
                isRegistered && checked && (react_1.default.createElement(Version, null,
                    (lastVersion === null || lastVersion === void 0 ? void 0 : lastVersion.version) || '-',
                    react_1.default.createElement(icons_1.SwapRightOutlined, null),
                    component.component.version)),
                react_1.default.createElement(ExpendBtn, { onClick: (e) => {
                        handleExpend(key);
                        e.stopPropagation();
                    } }, expend ? react_1.default.createElement(icons_1.DownOutlined, null) : react_1.default.createElement(icons_1.UpOutlined, null))),
            expend && (react_1.default.createElement("div", null,
                react_1.default.createElement(detail_1.ComponentResource, { lastVersion: pkg.lastVersion, componentRemote: component, onChange: (value) => handleChange(key, value) })))));
    };
    return (react_1.default.createElement("div", null,
        react_1.default.createElement(GroupContainer, null,
            react_1.default.createElement(antd_1.Select, { placeholder: packageI18n.groupTips, style: { width: 150 }, onChange: setGroupId }, groups.map((group) => (react_1.default.createElement(Option, { key: group.id, value: group.id }, group.name)))),
            react_1.default.createElement(antd_1.Input, { placeholder: packageI18n.inputNameTips, style: { width: 250, marginLeft: 8 }, onChange: (e) => setName(e.target.value) }),
            react_1.default.createElement(antd_1.Button, { style: { marginLeft: 8 }, onClick: () => {
                    handleSearch();
                } },
                react_1.default.createElement(icons_1.SearchOutlined, null),
                " ",
                global.search),
            react_1.default.createElement(GoResource, null,
                react_1.default.createElement(react_router_dom_1.Link, { to: {
                        pathname: `/applications/${applicationId}/resources/list`,
                    } }, packageI18n.noGroupTips))),
        react_1.default.createElement(antd_1.Spin, { spinning: loading }, packages.length > 0 ? (react_1.default.createElement(react_1.default.Fragment, null,
            react_1.default.createElement(Tips, null,
                react_1.default.createElement("div", { style: { color: '#f90' } },
                    "* ",
                    packageI18n.batchSaveTips),
                react_1.default.createElement("div", { style: { flexGrow: 1 } },
                    packageI18n.selectCount,
                    " : ",
                    react_1.default.createElement("span", null, checkedList.length || 0),
                    packageI18n.total,
                    " : ",
                    react_1.default.createElement("span", null, pageInfo.total || 0))),
            react_1.default.createElement(List, null, packages.map(Item)),
            react_1.default.createElement(index_1.Pagination, { hideOnSinglePage: true, current: pageInfo.page, total: pageInfo.total, pageSize: pageInfo.size, onChange: (page, pageSize) => handleSearch(page, pageSize), style: { textAlign: 'center', padding: '24px 0 0 0' } }))) : (react_1.default.createElement(antd_1.Empty, { image: antd_1.Empty.PRESENTED_IMAGE_SIMPLE })))));
};
exports.default = (0, react_redux_1.connect)(mapStateToProps, mapDispatchToProps)(Main);
