"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = ModalReturnBook;
const tslib_1 = require("tslib");
const react_1 = require("react");
const scan_to_return_1 = tslib_1.__importDefault(require("./scan-to-return"));
const use_borrow_book_1 = tslib_1.__importDefault(require("./use-borrow-book"));
const react_2 = tslib_1.__importDefault(require("react"));
const __1 = require("../../../../..");
const use_book_state_1 = tslib_1.__importDefault(require("./use-book-state"));
const helper_1 = require("../helper");
const query_1 = require("../query");
const constant_1 = require("../../../constant");
function ModalReturnBook({ educationalCodes, route, setShow, show, }) {
    var _a, _b;
    const userInfo = (0, __1.PHXFuncGetLoggedInfo)();
    const { listBookState } = (0, use_book_state_1.default)();
    const [warehouseId, setWarehouseId] = (0, react_1.useState)(null);
    const [initLoading, setInitLoading] = (0, react_1.useState)(false);
    const { handleReturnBook } = (0, use_borrow_book_1.default)();
    const [listBookReturn, setListBookReturn] = (0, react_1.useState)([]);
    const [showModalConfirm, setShowModalConfirm] = (0, react_1.useState)(false);
    const [returnLoading, setReturnLoading] = (0, react_1.useState)(false);
    const schoolYearInfo = (0, __1.PHXGetCurrentYearWithTermV3)();
    const handleReturn = async () => {
        const listBorrowBookLength = listBookReturn.length;
        if (listBorrowBookLength === 0)
            return;
        setReturnLoading(true);
        try {
            const { inBookshelfBookArray, inWarehouseBookArray, returnAllTicketIdArray, returnBorrowBook } = (0, helper_1.processBorrowBook)(listBookReturn, listBookState);
            await handleReturnBook(inBookshelfBookArray, inWarehouseBookArray, returnBorrowBook, returnAllTicketIdArray);
            (0, helper_1.triggerMessage)({
                type: constant_1.FORM_TYPE.CUSTOM,
                message: 'Trả sách thành công',
            });
            setListBookReturn([]);
        }
        catch (e) {
            console.log(e);
            (0, helper_1.triggerMessage)({
                type: constant_1.FORM_TYPE.CUSTOM,
                message: 'Đã xảy ra lỗi. Vui lòng thử lại',
                isFailure: true,
            });
        }
        finally {
            setReturnLoading(false);
            setShowModalConfirm(false);
        }
    };
    function onHide() {
        setShow(false);
        setShowModalConfirm(false);
        setListBookReturn([]);
    }
    async function getInitData() {
        var _a;
        setInitLoading(true);
        try {
            const { data: { library_warehouse_educational_level }, } = await (0, __1.PHXClientQueryV3)({
                query: query_1.getWarehouseIdByEducationalLevelCodeQuery,
                variables: {
                    educationalLevelCode: educationalCodes[0],
                    schoolId: userInfo === null || userInfo === void 0 ? void 0 : userInfo.school_id,
                },
            });
            setWarehouseId((_a = library_warehouse_educational_level === null || library_warehouse_educational_level === void 0 ? void 0 : library_warehouse_educational_level[0]) === null || _a === void 0 ? void 0 : _a.warehouse_id);
        }
        catch (e) {
            console.log(e);
        }
        finally {
            setInitLoading(false);
        }
    }
    (0, react_1.useEffect)(() => {
        if (show) {
            getInitData();
        }
    }, [show]);
    return (react_2.default.createElement(__1.PHXModal, { key: 'scanModal', customWidth: 'max-w-[calc(100vw-2rem)] xl:max-w-[70rem]', disableOutsideClick: true, disableSubmit: listBookReturn.length === 0, headerClassName: 'bg-white', inCard: false, onHide: onHide, onSubmit: () => {
            setShowModalConfirm(true);
        }, primaryActionText: 'Tr\u1EA3 t\u1EA5t c\u1EA3', show: show, title: 'Phi\u1EBFu tr\u1EA3 s\u00E1ch', type: 'no-padding' }, !initLoading ? (react_2.default.createElement(react_2.default.Fragment, null,
        react_2.default.createElement(__1.PHXModal, { key: 'confirmModal', disableCloseButton: returnLoading, inCard: false, onHide: () => {
                setShowModalConfirm(false);
            }, onSubmit: handleReturn, primaryActionText: 'Tr\u1EA3 s\u00E1ch', primaryLoading: returnLoading, show: showModalConfirm, title: 'X\u00E1c nh\u1EADn tr\u1EA3 s\u00E1ch' },
            react_2.default.createElement("p", { className: '  text-xs text-gray-600' }, "B\u1EA1n c\u00F3 ch\u1EAFc ch\u1EAFn mu\u1ED1n tr\u1EA3 t\u1EA5t c\u1EA3 c\u00E1c s\u00E1ch \u0111\u00E3 ch\u1ECDn kh\u00F4ng? Sau khi x\u00E1c nh\u1EADn, t\u1EA5t c\u1EA3 s\u00E1ch s\u1EBD \u0111\u01B0\u1EE3c c\u1EADp nh\u1EADt v\u1EC1 tr\u1EA1ng th\u00E1i m\u1EB7c \u0111\u1ECBnh v\u00E0 c\u00E1c phi\u1EBFu m\u01B0\u1EE3n s\u00E1ch s\u1EBD chuy\u1EC3n sang tr\u1EA1ng th\u00E1i \u0110\u00E3 tr\u1EA3 v\u00E0 kh\u00F4ng th\u1EC3 thay \u0111\u1ED5i.")),
        warehouseId && (react_2.default.createElement(scan_to_return_1.default, { data: listBookReturn, onChange: (data) => setListBookReturn(data), route: route, schoolYearId: (_b = (_a = schoolYearInfo === null || schoolYearInfo === void 0 ? void 0 : schoolYearInfo.current_school_year) === null || _a === void 0 ? void 0 : _a.schoolYear) === null || _b === void 0 ? void 0 : _b.id, warehouseId: warehouseId })))) : (react_2.default.createElement(__1.PHXSkeleton, { type: 'content-modal' }))));
}
//# sourceMappingURL=modal-return-book.js.map