import {PageHeader} from "../../../component/PageHeader.tsx";
import {ListSearchForm} from "../../../component/ListSearchForm.tsx";
import {BoardListTable} from "../components/master/BoardListTable.tsx";
import {useLoadingToast} from "../../../hook/useLoadingToast.ts";
import {Pagination} from "../../../component/pagination/Pagination.tsx";
import {ActionButtonListGroup} from "../../../component/button/ActionButtonListGroup.tsx";
import {useBoardListPage} from "../hook/page/useBoardListPage.ts";

export const BoardListPage = () => {
    const {
        list,
        totalItems,
        totalPages,
        currentPage,
        size,
        isLoading,
        error,
        title,
        breadcrumb,
        homeUrl,
        searchOptions,
        pageSizeOptions,
        successMessage,
        searchParams,
        setSearchParams,
        handleDetail,
        handleArticleList,
        handlePreview,
        handlePageChange,
        handleDeleteBatch,
        handleCreate,
        isAllChecked,
        isPartiallyChecked,
        isChecked,
        handleCheck,
        handleCheckAll,
    } = useBoardListPage();

    useLoadingToast({
        isLoading,
        error,
        successMessage
    });

    return (
        <>
            <PageHeader
                title={title}
                breadcrumb={breadcrumb}
                homeUrl={homeUrl}
            />
            <ListSearchForm
                totalItems={totalItems}
                searchParams={searchParams}
                onChange={setSearchParams}
                searchOptions={searchOptions}
                pageSizeOptions={pageSizeOptions}
                totalLabel={"게시판"}
            />
            {isLoading && <p>Loading...</p>}

            <BoardListTable
                items={list}
                params={searchParams}
                onChange={setSearchParams}
                totalItems={totalItems}
                currentPage={currentPage}
                totalPages={totalPages}
                isAllChecked={isAllChecked}
                isPartiallyChecked={isPartiallyChecked}
                isChecked={isChecked}
                onCheck={handleCheck}
                onCheckAll={handleCheckAll}
                onDetail={handleDetail}
                onArticleList={handleArticleList}
                onPreview={handlePreview}
            />
            <ActionButtonListGroup
                onDelete={handleDeleteBatch}
                onCreate={handleCreate}
                deleteLabel={"미사용"}
            />
            <Pagination
                totalItems={totalItems}
                totalPages={totalPages}
                currentPage={currentPage}
                size={size}
                onPageChange={handlePageChange}
            />
        </>
    )
};
