import {useBoardListQuery} from "../hook/useBoardList.ts";
import {PageHeader} from "../../../component/PageHeader.tsx";
import {useState} from "react";
import {ListSearchForm} from "../../../component/ListSearchForm.tsx";
import {BoardListTable} from "../components/master/BoardListTable.tsx";
import {useNavigate} from "react-router-dom";
import type {BoardSearchParams} from "../type/board.types.ts";
import {useLoadingToast} from "../../../hook/useLoadingToast.ts";

const initSearchParam: BoardSearchParams = {
    pageIndex: 1,
    pageUnit: 10,
    searchCnd: "0",
    searchKeyword: "",
    searchSortCnd: "BBS_NM",
    searchSortOrd: "ASC"
}

export const BoardListPage = () => {
    const searchOptions = [
        {value: '0', label: '게시판명/연결메뉴'},
        {value: '1', label: '게시판유형'},
    ]

    const pageSizeOptions = [
        {value: '10', label: '10건씩'},
        {value: '20', label: '20건씩'},
        {value: '30', label: '30건씩'},
    ]
    const [searchParams, setSearchParams] = useState<BoardSearchParams>(initSearchParam);
    const {
        list,
        totalItems,
        currentPage,
        totalPages,
        isLoading,
        error
    } = useBoardListQuery(searchParams);
    const navigate = useNavigate();

    const handleDetail = (bbsId: string) => {
        navigate(`/admin/cop/bbs/detail/${bbsId}`);
    }
    const handleArticleList = (bbsId: string) => {
        navigate(`/detail/cop/bbs/article/${bbsId}`);
    }
    const handlePreview = (bbsId: string) => {
        navigate(`/preview/${bbsId}`);
    }

    useLoadingToast({
        isLoading,
        error,
        successMessage : '게시판 마스터 목록을 조회하였습니다.'
    });

    const title = '게시판 관리'
    const breadcrumb = [{label: '게시판 관리'}]
    const homeUrl = '#'

    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}
                onDetail={handleDetail}
                onArticleList={handleArticleList}
                onPreview={handlePreview}
            />
        </>
    )
};
