import type {BoardListItem} from "../type/boardMaster.types.ts";
import type {SearchParams} from "../../../../../type/searchParams.ts";
import {EmptyRow} from "../../../../component/EmptyRow.tsx";
import {BoardListTableHeader} from "./BoardListTableHeader.tsx";
import {BoardListTableRow} from "./BoardListTableRow.tsx";
import type {CheckableTableModel, RowActionsModel} from "../../../../../type/viewModel.ts";

type BoardListTableProps =
    CheckableTableModel<BoardListItem, SearchParams> &
    RowActionsModel<{
        onDetail: (bbsId: string) => void
        onArticleList: (bbsId: string) => void
        onPreview: (bbsId: string) => void
    }>;

export function BoardListTable({
                                   items,
                                   params,
                                   onChange,
                                   pagination,
                                   check,
                                   rowActions
                               }: BoardListTableProps) {

    return (
        <div className="table table_type_cols">
            <table>
                <BoardListTableHeader
                    params={params}
                    onChange={onChange}
                    checked={check.isAllChecked}
                    indeterminate={check.isPartiallyChecked}
                    onCheckAll={check.onCheckAll}
                />
                <tbody>
                {items.length > 0 ?
                    items.map((item, index) => (
                        <BoardListTableRow
                            key={item.bbsId}
                            item={item}
                            index={index}
                            searchParams={params}
                            totalItems={pagination.totalItems}
                            currentPage={pagination.currentPage}
                            checked={check.isChecked(item.bbsId)}
                            onCheck={check.onCheck}
                            onDetail={rowActions.onDetail}
                            onArticleList={rowActions.onArticleList}
                            onPreview={rowActions.onPreview}
                        />))
                    : (<EmptyRow colSpan={9}/>)
                }
                </tbody>
            </table>
        </div>
    )
}
