"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;
};
Object.defineProperty(exports, "__esModule", { value: true });
const react_1 = __importStar(require("react"));
const react_redux_1 = require("react-redux");
const antd_1 = require("antd");
const ACTIONS = __importStar(require("@/actions/store/list"));
const index_1 = require("@/constants/index");
const system_1 = require("@/pages/system");
const { Option } = antd_1.Select;
const PAGE_NUMBER = 1;
const PAGE_SIZE = 9999;
const mapStateToProps = (store) => ({
    applicationList: store.store.list.applicationList,
    buyIds: store.store.list.buyIds,
    visible: store.store.list.buyModalVisible,
    type: store.store.list.type,
    organizationId: store.system.user.organizationId,
});
const mapDispatchToProps = {
    updateBuyModalVisible: ACTIONS.updateBuyModalVisible,
    addGoods: ACTIONS.addGoods,
    fetchApplicationList: ACTIONS.fetchApplicationList,
};
const BuyModal = (props) => {
    const { applicationList, buyIds, organizationId, type, visible, updateBuyModalVisible, fetchApplicationList, addGoods, } = props;
    const [selectedAppIds, setSelectedAppIds] = (0, react_1.useState)([]);
    const [deliveryType, setDeliveryType] = (0, react_1.useState)();
    // i18n
    const { locale } = (0, react_1.useContext)(system_1.GlobalContext);
    const { application, store } = locale.business;
    const deliveryTypeMap = [
        {
            label: store.clone,
            value: index_1.StoreGoodsPurchaseType.clone,
        },
        {
            label: store.refer,
            value: index_1.StoreGoodsPurchaseType.reference,
        },
    ];
    (0, react_1.useEffect)(() => {
        if (!visible) {
            setSelectedAppIds([]);
            setDeliveryType(undefined);
        }
        else {
            const _deliveryType = type === 'package' ? index_1.StoreGoodsPurchaseType.reference : index_1.StoreGoodsPurchaseType.clone;
            setDeliveryType(_deliveryType);
        }
    }, [type, visible]);
    (0, react_1.useEffect)(() => {
        if (visible) {
            fetchApplicationList({ organizationId, page: PAGE_NUMBER, size: PAGE_SIZE });
        }
    }, [visible, fetchApplicationList, organizationId]);
    const handleClose = () => {
        updateBuyModalVisible(false);
    };
    const handleOnOk = () => {
        addGoods({
            appIds: selectedAppIds,
            goodsIds: buyIds,
            delivery: deliveryType || index_1.StoreGoodsPurchaseType.clone,
        });
    };
    return (react_1.default.createElement(antd_1.Modal, { title: store.buyModalTitle, destroyOnClose: true, maskClosable: false, visible: visible, onCancel: handleClose, onOk: handleOnOk },
        react_1.default.createElement(antd_1.Select, { mode: "multiple", placeholder: application.selectApplication, value: selectedAppIds, onChange: setSelectedAppIds, style: { width: '100%', marginBottom: 16 } }, applicationList === null || applicationList === void 0 ? void 0 : applicationList.map((application) => (react_1.default.createElement(Option, { key: application.id, value: application.id }, application.name)))),
        react_1.default.createElement(antd_1.Radio.Group, { disabled: type === index_1.FileType.variable, options: deliveryTypeMap, value: deliveryType, onChange: (e) => setDeliveryType(e.target.value) })));
};
exports.default = (0, react_redux_1.connect)(mapStateToProps, mapDispatchToProps)(BuyModal);
