import './list-proj.css'
import CardProj from "../card/CardProj.tsx";
import { useRef, MutableRefObject, useState, useEffect, useContext } from "react";
import { Input, Pagination, message, Spin, Image } from 'antd';
import { get } from "../../util/AjaxUtils.ts";
import { IndexListContext } from "../../context/IndexListContext.ts";
import { IListPage } from "../../interfaces/listpage/IListPage.ts";
import { IProj } from "../../interfaces/proj/IProj.ts";
import NoData from "../../assets/no-data.png";
import gpsImg from '../../static/right/gps.png'
import { Link } from "react-router-dom";
import { Breadcrumb } from 'antd';
import backImg from '../../static/right/back.png'
const { Search } = Input;

export default function ListProj() {
    const indexListContext = useContext(IndexListContext);
    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<IProj[]>([]);
    const [isLoading, setIsLoading] = useState(false);
    const [keywords, setKeywords] = useState('');

    const domHeight = window.innerHeight - 280;

    const reqData = (currentPage: number) => {
        get<IListPage<IProj>>({
            messageApi: messageApi,
            url: '/api/proj/listpage/self',
            config: {
                params: {
                    page: currentPage,
                    rows: 20,
                    keywords: keywords,
                    projCategoryId: indexListContext.category,
                    status: indexListContext.status ? indexListContext.status : ''
                }
            },
            onBefore() {
                setIsLoading(true);
            },
            onSuccess({ data }) {
                setPage(data.page);
                setTotal(data.total);
                setProjs(data.rows);
            },
            onFinally() {
                setIsLoading(false);
            }
        })
    }

    const renderList = () => {
        if (projs.length == 0) {
            return (
                <div className="no-data" style={{
                    width: '100%',
                    height: '100%',
                    backgroundColor: 'var(--color-light)',
                    display: 'flex',
                    flexDirection: 'column',
                    justifyContent: 'center',
                    alignItems: 'center'
                }}>
                    <Image src={NoData} preview={false} />
                    <span>暂无数据</span>
                </div>
            )
        }
        return projs.map((item) => {
            return <CardProj item={item} key={new Date().getTime() + ':' + item.projId} />;
        });
    }

    const renderCategory = () => {
    }

    useEffect(() => {
        if (indexListContext.categorys) {
            reqData(page);
            renderCategory();
        }
    }, [indexListContext.status, indexListContext.categoryChangeCount, indexListContext.category, keywords, page])

    // const renderStatus = () => {
    //     if (indexListContext.status == 'ALL') {
    //         return <Tag color="blue">项目：全部项目</Tag>
    //     } else if (indexListContext.status == 'PROCESSING') {
    //         return <Tag color="blue">项目：进行中的</Tag>
    //     } else if (indexListContext.status == 'COMPLETE') {
    //         return <Tag color="blue">项目：已完成的</Tag> 
    //     }
    //     return <></>
    // }

    return (
        <>
            {contextHolder}
            <div className="list-proj" ref={listProjRef}>
                <div className="top">
                    {/* 标签 */}
                    {/* {renderStatus()} */}
                    <div className='gps'>
                        <img src={gpsImg} alt="" />
                        <div>当前位置:首页</div>
                    </div>
                    <div className='line'></div>
                    <Search placeholder="输入项目名称" onSearch={(value) => {
                        setKeywords(value)
                    }} style={{
                        width: '253px',
                        height: '31px',
                    }} />
                    <div className='nowPosition'>
                        <img src={backImg} alt="" />
                        <div>
                            <Breadcrumb
                                separator="|"
                                items={[
                                    { title: <Link to={'/'}>首页</Link> },
                                ]}
                            />
                        </div>
                    </div>
                </div>
                <div className="body">
                    <Spin tip="加载中..." spinning={isLoading}>
                        <div className="list" ref={listRef} style={{ height: `${domHeight}px` }}>
                            {renderList()}
                        </div>
                        <div className="page">
                            <Pagination defaultCurrent={page} total={total} onChange={(page) => {
                                setPage(page);
                            }} />
                        </div>
                    </Spin>
                </div>
            </div>
        </>
    )
}