import './list-proj.css'
import CardProj from "../card/CardProj.tsx";
import {useRef, MutableRefObject, useState, useEffect} from "react";
import {Input, Pagination, message} from 'antd';
import type {SearchProps} from 'antd/es/input/Search';
import {get} from "../../util/AjaxUtils.ts";

const {Search} = Input;

const onSearch: SearchProps['onSearch'] = (value, _e, info) => {
    console.log(info?.source, value)
};

export default function ListProj() {

    const listProjRef: MutableRefObject<HTMLDivElement | null> = useRef(null);
    const listRef: MutableRefObject<HTMLDivElement | null> = useRef(null);

    const [messageApi, contextHolder] = message.useMessage();
    const [page, setPage] = useState(1);
    const [total, setTotal] = useState(0);
    const [projs, setProjs] = useState([]);

    const domHeight = window.innerHeight - 280;

    const reqData = (currentPage: number) => {
        get({
            messageApi: messageApi,
            url: '/api/proj/listpage/self',
            config: {
                params: {
                    page: currentPage,
                    rows: 20
                }
            },
            onSuccess({data}) {
                setProjs(data.page);
                setTotal(data.total);
                setProjs(data.rows);
            }
        })
    }

    const renderList = () => {
        return projs.map((item, index) => <CardProj item={item} key={`proj${index}`}/>);
    }

    useEffect(() => {
        reqData(page);
    }, [])

    return (
        <>
            {contextHolder}
            <div className="list-proj" ref={listProjRef}>
                <div className="top">
                    <Search placeholder="按项目名搜索" onSearch={onSearch} style={{width: 200}}/>
                </div>
                <div className="body">
                    <div className="list" ref={listRef} style={{height: `${domHeight}px`}}>
                        {renderList()}
                    </div>
                    <div className="page">
                        <Pagination defaultCurrent={page} total={total} onChange={(page) => {
                            setPage(page);
                            reqData(page);
                        }}/>
                    </div>
                </div>
            </div>
        </>
    )
}