import './menu-with-top-button.css'
import { IMenuListItem, IMenuWithTopButton } from "../../interfaces/menu/IMenuWithTopButton.ts";
import objImg from '../../static/left/obj.png'
import newImg from '../../static/left/new.png'
import { useNavigate } from 'react-router-dom';
import {
    RightOutlined
} from '@ant-design/icons';
import { getMenuActive, setMenuActive } from './../../util/cache'
import { useEffect, useState } from 'react'
export default function MenuWithTopButton(props: IMenuWithTopButton) {
    const navugate = useNavigate()
    const [menuItem, setMenuItem] = useState<IMenuListItem[]>([])
    useEffect(() => {
        setMenuItem(
            props.list.map((m) => {
                if (getMenuActive()) {
                    if (getMenuActive() == m.id) {
                        m['active'] = true
                    } else {
                        m['active'] = false
                    }
                }
                return m
            })
        )
    }, [getMenuActive()])

    const list = menuItem.map((item, index) => (
        // 创建项目下三个选项
        <li className={item.active ? 'active' : ''} key={item.id} onClick={(e) => {
            props.handleListItem(e, index, item);
            setMenuActive(item.id)
            navugate('/home')
        }}>

            {item.icon ? (<img src={item.icon} className="menu-icon" alt="加载失败" />) : <></>}
            <span className="menu-name">{item.name}</span>
            <span className='topIcon'><RightOutlined /></span>
        </li>
    ));

    return (
        <div className="menu-with-top-button">
            {/* button 是三个黄色按钮 */}
            <div className='top'>
                <div className='top-lift'>
                    <img src={objImg} alt="" />
                    <div> {props.button.name}</div>
                </div>
                <div className='top-right' onClick={(e) => {
                    props.button.handle(e);
                    // 当点击创建时其他按钮默认为不被选中
                    // props.list.forEach(item => item.active = false);
                    // console.log(props.list);
                    // setMenuActive('ALL')
                }}>
                    <img src={newImg} alt="" />
                    <div>创建</div>
                </div>
            </div>
            {/* <button className="btn btn-orange top-button"
                    onClick={(e) => {
                        props.button.handle(e);
                        console.log(e);
                        
                    }}
            >
                {props.button.name}
            </button> */}
            <ul style={{}}>{list}</ul>
        </div>
    )
}