"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = ModalAddBookBorrow;
const tslib_1 = require("tslib");
const react_1 = require("react");
const react_hook_form_1 = require("react-hook-form");
const __1 = require("../../../../..");
const react_2 = tslib_1.__importDefault(require("react"));
const query_1 = require("../query");
function ModalAddBookBorrow({ onChange, setShow, show, warehouseId, }) {
    const [selectedValues, setSelectedValues] = (0, react_1.useState)([]);
    const [disableSelectBook, setDisableSelectBook] = (0, react_1.useState)(true);
    const userInfo = (0, __1.PHXFuncGetLoggedInfo)();
    const { register, setValue, watch } = (0, react_hook_form_1.useForm)();
    const keySearchBook = 'book_name';
    const [inputText, setInputText] = (0, react_1.useState)('');
    const [options, setOptions] = (0, react_1.useState)([]);
    const handleSubmit = () => {
        onChange(selectedValues);
        onHide();
    };
    function onHide() {
        setShow(false);
        setDisableSelectBook(true);
        setOptions([]);
        setInputText('');
        setSelectedValues([]);
    }
    const handleClickSearchItem = (searchItem) => {
        var _a, _b;
        setInputText((_a = searchItem === null || searchItem === void 0 ? void 0 : searchItem.library_book) === null || _a === void 0 ? void 0 : _a.title);
        if (disableSelectBook) {
            setDisableSelectBook(false);
        }
        const { library_book, library_book_images, library_dang_ki_ca_biets } = searchItem;
        const image = (_b = library_book_images === null || library_book_images === void 0 ? void 0 : library_book_images[0]) === null || _b === void 0 ? void 0 : _b.url;
        const title = library_book === null || library_book === void 0 ? void 0 : library_book.title;
        const newOptions = (library_dang_ki_ca_biets || []).map((book) => ({
            id: book.id,
            title,
            image,
            code: book.code,
            stateId: book.state_id,
        }));
        setOptions(newOptions);
        setSelectedValues([]);
    };
    (0, react_1.useEffect)(() => {
        if (!watch(keySearchBook)) {
            setSelectedValues([]);
        }
    }, [watch(keySearchBook)]);
    return (react_2.default.createElement(__1.PHXModal, { disableSubmit: selectedValues.length === 0, inCard: false, onHide: onHide, onSubmit: handleSubmit, primaryActionText: 'Th\u00EAm m\u1EDBi', show: show, title: 'Th\u00EAm s\u00E1ch m\u01B0\u1EE3n' },
        react_2.default.createElement("div", { className: 'space-y-5' },
            react_2.default.createElement(__1.PHXSearchResultListV3, { defaultInputText: inputText, filterArray: [{ keyParent: 'library_book', keyChild: 'title' }], getSearchResult: handleClickSearchItem, handleCustomRender: (item) => { var _a; return (_a = item === null || item === void 0 ? void 0 : item.library_book) === null || _a === void 0 ? void 0 : _a.title; }, isFillInput: true, isUnaccentSearchValue: true, label: 'T\u00EAn s\u00E1ch', name: keySearchBook, placeholder: 'Nh\u1EADp t\u00EAn s\u00E1ch', register: register(keySearchBook), search: (0, query_1.searchBookForModalBorrowQuery)(userInfo === null || userInfo === void 0 ? void 0 : userInfo.school_id, warehouseId), setValue: setValue }),
            react_2.default.createElement(__1.PHXCombobox, { defaultValue: selectedValues.map((item) => ({
                    id: item.id,
                    name: item.code,
                    ...item,
                })), disabled: !watch(keySearchBook) || disableSelectBook, id: 'book_code', label: 'M\u00E3 s\u00E1ch', listOptions: options.map((item) => ({
                    id: item.id,
                    name: item.code,
                    ...item,
                })), onChange: (values) => {
                    setSelectedValues(values);
                }, optionHeight: 200, placeholder: 'Ch\u1ECDn m\u00E3 s\u00E1ch' }))));
}
//# sourceMappingURL=modal-add-book.js.map