import './index.css';
import {MouseEvent, Reducer, useEffect, useReducer, useState} from "react";
import {Link, useNavigate, useSearchParams} from "react-router-dom";
import {IMenuListItem, IMenuWithTopButton} from "../../interfaces/menu/IMenuWithTopButton.ts";
import MenuWithTopButton from "../../components/menu/MenuWithTopButton.tsx";
import MenuTreeWithTopButton from "../../components/menu/MenuTreeWithTopButton.tsx";
import ListProj from "../../components/list/ListProj.tsx";
import ListProjAgent from "../../components/list/ListProjAgent.tsx";
import {Breadcrumb, Button, MenuProps} from 'antd';
import {
    IndexListContext,
    IndexListDataType,
    IndexListDispatchContext,
    ListAction,
    ListData,
} from "../../context/IndexListContext.ts";

export default function Index() {
    const nav = useNavigate();
    const [searchParams] = useSearchParams();

    const listReducer = (state: ListData, action: ListAction) => {
        if (action.type == IndexListDataType.PROJ) {
            state.type = IndexListDataType.PROJ;
            state.status = action.value as string;
        } else if (action.type == IndexListDataType.AGENT) {
            state.type = IndexListDataType.AGENT;
            state.status = action.value as string;
        } else if (action.type == IndexListDataType.CATEGORY) {
            state.categorys = action.value as MenuProps['items'];
            state.categoryChangeCount++;
        } else if (action.type == IndexListDataType.CATEGORY_CHANGE) {
            state.category = action.value as string;
            state.categoryChangeCount++;
        } else if (action.type == IndexListDataType.CATEGORY_DELETE) {
            state.categorys = action.value as MenuProps['items'];
            state.category = '';
            state.categoryChangeCount++;
        }
        return {
            ...state
        };
    }

    const [listData, dispatch] = useReducer<Reducer<ListData, ListAction>>(listReducer, {
        type: IndexListDataType.PROJ,
        categoryChangeCount: 0
    });

    const [projMenu, setProjMenu] = useState<IMenuWithTopButton>({
        button: {
            name: '创建项目',
            handle() {
                nav('/proj-create')
            }
        },
        list: [
            {id: 'ALL', name: '全部项目', active: true},
            {id: 'PROCESSING', name: '进行中的'},
            {id: 'COMPLETE', name: '已完成的'}
        ],
        handleListItem(_e, _index, item: IMenuListItem) {
            projMenu.list.forEach(item => item.active = false);
            agentMenu.list.forEach(item => item.active = false);
            item.active = true;
            setProjMenu({
                ...projMenu
            })
            dispatch({
                type: IndexListDataType.PROJ,
                value: item.id
            })
        }
    });

    const [agentMenu, setAgentMenu] = useState<IMenuWithTopButton>({
        button: {
            name: '代理服务',
            handle() {
                dispatch({
                    type: IndexListDataType.PROJ,
                    value: 'COMPLETE'
                })
            }
        },
        list: [
            {id: 'ALL', name: '全部项目'},
            {id: 'PROCESSING', name: '进行中的'},
            {id: 'COMPLETE', name: '已完成的'},
        ],
        handleListItem(_e: MouseEvent<HTMLLIElement>, _index: number, item: IMenuListItem) {
            projMenu.list.forEach(item => item.active = false);
            agentMenu.list.forEach(item => item.active = false);
            item.active = true;
            setAgentMenu({
                ...agentMenu
            })
            dispatch({
                type: IndexListDataType.AGENT,
                value: item.id
            })
        }
    })

    useEffect(() => {
        if (searchParams.get('type') == 'agent') {
            dispatch({
                type: IndexListDataType.AGENT,
                value: 'ALL'
            })
        }
    }, []);

    return (
        <>
            <Breadcrumb
                items={[
                    {title: <Link to={'/'}>首页</Link>}
                ]}
            />
            <IndexListContext.Provider value={listData}>
                <IndexListDispatchContext.Provider value={dispatch}>
                    <div className="index">
                        <div className="left">
                            <MenuWithTopButton
                                button={projMenu.button}
                                list={projMenu.list}
                                handleListItem={projMenu.handleListItem}
                            />
                            <MenuTreeWithTopButton/>
                            <MenuWithTopButton
                                button={agentMenu.button}
                                list={agentMenu.list}
                                handleListItem={agentMenu.handleListItem}
                            />
                            <Button>发票管理</Button>
                        </div>
                        <div className="right">
                            {
                                listData.type === IndexListDataType.PROJ ? <ListProj/> : (
                                    listData.type == IndexListDataType.AGENT ? <ListProjAgent/> : <></>
                                )
                            }
                        </div>
                    </div>
                </IndexListDispatchContext.Provider>
            </IndexListContext.Provider>
        </>
    )
}