"use strict";
'use client';
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = ScanToReturn;
const tslib_1 = require("tslib");
const react_1 = tslib_1.__importDefault(require("react"));
const scan_ticket_info_1 = tslib_1.__importDefault(require("./scan-ticket-info"));
const __1 = require("../../../../..");
function ScanToReturn({ data, onChange, route, schoolYearId, warehouseId }) {
    const onChangeHistory = (newHistory, isDeleted) => {
        if (isDeleted) {
            const newData = data
                .map((ticket) => ({
                ...ticket,
                listborrowBook: ticket.listborrowBook.filter((book) => book.code !== newHistory.code),
            }))
                .filter((ticket) => ticket.listborrowBook.length > 0);
            onChange(newData);
            return;
        }
        if (!newHistory.ticketId) {
            return;
        }
        const existTicketIndex = data.findIndex((item) => item.ticketId === newHistory.ticketId);
        if (existTicketIndex === -1) {
            const newList = [
                {
                    title: newHistory.title,
                    ticketId: newHistory.ticketId,
                    borrowerName: newHistory.borrowerName,
                    roleName: newHistory.roleName,
                    classroom: newHistory.classroomName,
                    borrowDate: newHistory.borrowDate,
                    returnDate: '',
                    userCode: newHistory.userCode,
                    expectReturnDate: newHistory.expectReturnDate,
                    image: newHistory.image,
                    listborrowBook: [
                        {
                            bookBorrowId: newHistory.bookBorrowId,
                            code: newHistory.code,
                            scanCount: 1,
                            bookId: newHistory.bookId,
                            preStateId: newHistory.preStateId,
                            title: newHistory.title,
                            image: newHistory.image,
                        },
                    ],
                    totalReturned: newHistory.totalReturned,
                    totalBorrowed: newHistory.totalBorrowed,
                },
                ...data.map((ticket) => ({
                    ...ticket,
                    listborrowBook: ticket.listborrowBook.filter((book) => book.code !== newHistory.code),
                })),
            ].filter((item) => item.listborrowBook.length > 0);
            onChange(newList);
            return;
        }
        const clone = [...data];
        clone[existTicketIndex].expectReturnDate = newHistory.expectReturnDate;
        const existTicket = clone[existTicketIndex];
        const existBookIndex = existTicket.listborrowBook.findIndex((item) => item.code === newHistory.code);
        if (existBookIndex === -1) {
            clone[existTicketIndex].listborrowBook.push({
                bookBorrowId: newHistory.bookBorrowId,
                code: newHistory.code,
                scanCount: 1,
                bookId: newHistory.bookId,
                preStateId: newHistory.preStateId,
                title: newHistory.title,
                image: newHistory.image,
            });
            onChange(clone);
            return;
        }
        clone[existTicketIndex].listborrowBook[existBookIndex].scanCount++;
        onChange(clone);
    };
    const handleDeleteBook = (ticketId, bookCode) => {
        const clone = [...data];
        const ticketIndex = data.findIndex((item) => item.ticketId === ticketId);
        // nếu chỉ còn 1 quyển thì xoá luôn phiếu mượn
        if (clone[ticketIndex].listborrowBook.length === 1) {
            clone.splice(ticketIndex, 1);
        }
        else {
            clone[ticketIndex].listborrowBook = clone[ticketIndex].listborrowBook.filter((item) => item.code !== bookCode);
        }
        onChange(clone);
    };
    return (react_1.default.createElement("div", { className: 'gap-4 space-y-4 bg-[#F1F1F1] p-4 lg:grid-cols-3 xl:grid xl:space-y-0' },
        react_1.default.createElement("div", { className: 'col-span-1 h-full' },
            react_1.default.createElement(__1.PHXCard, null,
                react_1.default.createElement("p", { className: 'pb-2 text-xs text-gray-900' }, "Qu\u00E9t m\u00E3 s\u00E1ch"),
                react_1.default.createElement(scan_ticket_info_1.default, { onChangeHistory: onChangeHistory, schoolYearId: schoolYearId, warehouseId: warehouseId }))),
        react_1.default.createElement("div", { className: 'col-span-2' },
            react_1.default.createElement(__1.PHXCard, null,
                react_1.default.createElement("p", { className: 'pb-2 text-xs font-[650] text-gray-900' }, "Danh s\u00E1ch phi\u1EBFu"),
                data.length > 0 ? (react_1.default.createElement("div", { className: 'overflow-y-auto xl:max-h-[35.8rem]' },
                    react_1.default.createElement(__1.PHXTableStatic, { bodyData: data.map((ticket) => ({
                            ticket_id: ticket.ticketId,
                            user_full_name: ticket.borrowerName,
                            target: ticket.roleName,
                            borrow_date: ticket.borrowDate,
                            return_date: ticket.expectReturnDate,
                            action: '',
                            returned_count: `${ticket.totalReturned}/${ticket.totalBorrowed}`,
                            ...ticket,
                            child: {
                                headers: ['Hình ảnh', 'Tên sách', 'Mã sách', 'Số lần quét', 'Trạng thái', 'Thao tác', ''],
                                dataKeys: ['image', 'title', 'code', 'scan_count', 'status', 'action', ''],
                                data: ticket.listborrowBook.map((book) => ({
                                    image: book.image,
                                    title: book.title,
                                    code: book.code,
                                    scan_count: book.scanCount,
                                    status: '',
                                    action: '',
                                    ticketId: ticket.ticketId,
                                    bookCode: book.code,
                                })),
                                customRenderKey: {
                                    image(value, itemBody) {
                                        return (react_1.default.createElement("div", { className: `flex h-10 w-10 shrink-0 items-center justify-center overflow-hidden rounded-lg border border-gray-200 ${!value && 'bg-[#F7F7F7]'}` }, value ? (react_1.default.createElement("img", { alt: itemBody.userCode, className: 'h-full w-auto', src: value })) : (react_1.default.createElement("svg", { fill: 'none', height: '13', viewBox: '0 0 13 13', width: '13', xmlns: 'http://www.w3.org/2000/svg' },
                                            react_1.default.createElement("path", { d: 'M9 5.5C9.82843 5.5 10.5 4.82843 10.5 4C10.5 3.17157 9.82843 2.5 9 2.5C8.17157 2.5 7.5 3.17157 7.5 4C7.5 4.82843 8.17157 5.5 9 5.5Z', fill: '#8A8A8A' }),
                                            react_1.default.createElement("path", { clipRule: 'evenodd', d: 'M5.5179 1.86555e-07H7.48211C8.29494 -7.05843e-06 8.95056 -1.29007e-05 9.48147 0.043364C10.0281 0.088026 10.5082 0.182385 10.9525 0.408726C11.6581 0.768251 12.2317 1.34193 12.5913 2.04754C12.8176 2.49175 12.912 2.9719 12.9566 3.51853C13 4.04944 13 4.70505 13 5.51788V7.48212C13 8.29496 13 8.95056 12.9566 9.48147C12.912 10.0281 12.8176 10.5082 12.5913 10.9525C12.2317 11.6581 11.6581 12.2317 10.9525 12.5913C10.5082 12.8176 10.0281 12.912 9.48147 12.9566C8.95056 13 8.29495 13 7.48212 13H5.51788C4.70505 13 4.04944 13 3.51853 12.9566C2.9719 12.912 2.49175 12.8176 2.04754 12.5913C1.34193 12.2317 0.768251 11.6581 0.408726 10.9525C0.182385 10.5082 0.088026 10.0281 0.043364 9.48147C-1.29007e-05 8.95056 -7.05843e-06 8.29495 1.86555e-07 7.48212V5.5179C-7.05843e-06 4.70506 -1.29007e-05 4.04944 0.043364 3.51853C0.088026 2.9719 0.182385 2.49175 0.408726 2.04754C0.768251 1.34193 1.34193 0.768251 2.04754 0.408726C2.49175 0.182385 2.9719 0.088026 3.51853 0.043364C4.04944 -1.29007e-05 4.70506 -7.05843e-06 5.5179 1.86555e-07ZM3.64068 1.53838C3.18681 1.57547 2.92604 1.6446 2.72852 1.74524C2.30516 1.96095 1.96095 2.30516 1.74524 2.72852C1.6446 2.92604 1.57547 3.18681 1.53838 3.64068C1.50058 4.10331 1.5 4.69755 1.5 5.55V6.9785L2.2228 6.11114C2.88159 5.32059 4.07697 5.26637 4.80462 5.99402L7.49999 8.68939L8.67963 7.50974C9.41716 6.77222 10.632 6.8395 11.2836 7.65396L11.4997 7.92404C11.5 7.77538 11.5 7.61768 11.5 7.45V5.55C11.5 4.69755 11.4994 4.10331 11.4616 3.64068C11.4245 3.18681 11.3554 2.92604 11.2548 2.72852C11.039 2.30516 10.6948 1.96095 10.2715 1.74524C10.074 1.6446 9.8132 1.57547 9.35932 1.53838C8.89669 1.50058 8.30245 1.5 7.45 1.5H5.55C4.69755 1.5 4.10331 1.50058 3.64068 1.53838ZM1.53838 9.35932C1.53622 9.33282 1.53417 9.30589 1.53225 9.2785C1.54743 9.26317 1.56209 9.24707 1.57615 9.23019L3.37513 7.07141C3.46924 6.95848 3.64001 6.95073 3.74396 7.05468L6.96966 10.2804C7.26255 10.5733 7.73742 10.5733 8.03032 10.2804L9.74029 8.5704C9.84565 8.46504 10.0192 8.47465 10.1123 8.59101L11.3252 10.1071C11.3037 10.1674 11.2803 10.2214 11.2548 10.2715C11.039 10.6948 10.6948 11.039 10.2715 11.2548C10.074 11.3554 9.8132 11.4245 9.35932 11.4616C8.89669 11.4994 8.30245 11.5 7.45 11.5H5.55C4.69755 11.5 4.10331 11.4994 3.64068 11.4616C3.18681 11.4245 2.92604 11.3554 2.72852 11.2548C2.30516 11.039 1.96095 10.6948 1.74524 10.2715C1.6446 10.074 1.57547 9.8132 1.53838 9.35932Z', fill: '#8A8A8A', fillRule: 'evenodd' })))));
                                    },
                                    status() {
                                        return react_1.default.createElement(__1.PHXBadge, { text: '\u0110\u00E3 m\u01B0\u1EE3n', type: 'success' });
                                    },
                                    action(_value, itemBody) {
                                        return (react_1.default.createElement(__1.PHXDropdown, { options: [
                                                {
                                                    content: 'Xoá sách mượn',
                                                    destructive: true,
                                                    onClick: () => {
                                                        handleDeleteBook(itemBody.ticketId, itemBody.bookCode);
                                                    },
                                                },
                                            ], origin: 'right', type: 'ellipsis-icon' }));
                                    },
                                    title(value) {
                                        return (react_1.default.createElement(__1.PHXTooltip, { content: value },
                                            react_1.default.createElement("p", { className: 'w-[20ch] cursor-pointer truncate' }, value)));
                                    },
                                },
                            },
                        })), onClick: () => null, numericalOrder: {
                            enable: true,
                            title: 'STT',
                        }, readonly: false, thBody: [
                            'ticket_id',
                            'user_full_name',
                            'target',
                            'borrow_date',
                            'return_date',
                            'returned_count',
                            'action',
                        ], thComponent: {
                            ticket_id(value) {
                                return `#${value}`;
                            },
                            user_full_name(value, itemBody) {
                                return (react_1.default.createElement("div", { className: 'w-[10rem] space-y-1' },
                                    react_1.default.createElement("p", null, value),
                                    react_1.default.createElement("p", { className: 'text-gray-500' }, itemBody.userCode || __1.EMPTY_VALUE)));
                            },
                            target(value, itemBody) {
                                return (react_1.default.createElement("div", { className: 'space-y-1' },
                                    react_1.default.createElement("p", null, value),
                                    itemBody.classroom && react_1.default.createElement("p", { className: 'text-gray-500' }, itemBody.classroom)));
                            },
                            child_action() {
                                return '';
                            },
                            action(_value, itemBody) {
                                return (react_1.default.createElement("a", { 
                                    // eslint-disable-next-line prettier/prettier
                                    className: 'flex items-center gap-x-1 text-link-500 hover:text-link-600 hover:underline', href: `/library/${route}/borrow-ticket/edit/${itemBody.ticketId}`, onClick: (e) => {
                                        e.stopPropagation();
                                    }, rel: 'noreferrer', target: '_blank' },
                                    react_1.default.createElement("span", null, "M\u01B0\u1EE3n s\u00E1ch"),
                                    react_1.default.createElement("svg", { fill: 'none', height: '20', viewBox: '0 0 20 20', width: '20', xmlns: 'http://www.w3.org/2000/svg' },
                                        react_1.default.createElement("path", { d: 'M11.75 3.5C11.3358 3.5 11 3.83579 11 4.25C11 4.66421 11.3358 5 11.75 5H13.9393L8.96967 9.96967C8.67678 10.2626 8.67678 10.7374 8.96967 11.0303C9.26256 11.3232 9.73744 11.3232 10.0303 11.0303L15 6.06066V8.25C15 8.66421 15.3358 9 15.75 9C16.1642 9 16.5 8.66421 16.5 8.25V4.25C16.5 3.83579 16.1642 3.5 15.75 3.5H11.75Z', fill: '#6A52FA' }),
                                        react_1.default.createElement("path", { d: 'M15 10.9674C15 10.5532 14.6642 10.2174 14.25 10.2174C13.8358 10.2174 13.5 10.5532 13.5 10.9674V13.75C13.5 14.4404 12.9404 15 12.25 15H6.25C5.55964 15 5 14.4404 5 13.75L5 7.75C5 7.05964 5.55965 6.5 6.25 6.5L9.03261 6.5C9.44682 6.5 9.78261 6.16421 9.78261 5.75C9.78261 5.33579 9.44682 5 9.03261 5L6.25 5C4.73122 5 3.5 6.23122 3.5 7.75V13.75C3.5 15.2688 4.73122 16.5 6.25 16.5H12.25C13.7688 16.5 15 15.2688 15 13.75V10.9674Z', fill: '#6A52FA' }))));
                            },
                        }, thHeader: [
                            'Mã phiếu',
                            'Người mượn',
                            'Đối tượng mượn',
                            'Ngày mượn',
                            'Ngày trả (dự kiến)',
                            'Số sách đã trả',
                            'Xem phiếu',
                        ] }))) : (react_1.default.createElement("div", { className: 'rounded-lg border' },
                    react_1.default.createElement(__1.PHXEmptyRecord, { className: 'h-[23.9rem]' })))))));
}
//# sourceMappingURL=scan-to-return.js.map