import React, { useState, useRef, useEffect, useMemo, useCallback } from "react"; 
 | 
import request from '@/utils/request'; 
 | 
import { 
 | 
    SavedQueriesList, 
 | 
    FilterLiveSearch, 
 | 
    useNotify, 
 | 
    useListContext, 
 | 
    SearchInput 
 | 
} from 'react-admin'; 
 | 
import BookmarkIcon from '@mui/icons-material/BookmarkBorder'; 
 | 
import { Box, Typography, Card, CardContent, useTheme, Input } from '@mui/material'; 
 | 
import { RichTreeView } from "@mui/x-tree-view/RichTreeView"; 
 | 
import { TreeItem2 } from "@mui/x-tree-view/TreeItem2"; 
 | 
import { useTreeViewApiRef } from '@mui/x-tree-view/hooks'; 
 | 
  
 | 
const LocListAside = () => { 
 | 
    const theme = useTheme(); 
 | 
    const notify = useNotify(); 
 | 
    const { setFilters } = useListContext(); // 获取列表上下文 
 | 
    const [selectedOption, setSelectedOption] = useState(null); 
 | 
    const [treeData, setTreeData] = useState([]); 
 | 
    const [defaultIds, setDefaultIds] = useState([]); 
 | 
    const [condition, setCondition] = useState(''); 
 | 
  
 | 
    const haveChildren = (item) => { 
 | 
        if (Array.isArray(item)) { 
 | 
            return item.map((k) => haveChildren(k)); 
 | 
        } 
 | 
  
 | 
        if (item && typeof item === 'object') { 
 | 
            if (item.index !== undefined) { 
 | 
                item.index = item.index.toString(); 
 | 
            } 
 | 
  
 | 
            if (item.children && Array.isArray(item.children)) { 
 | 
                item.children = haveChildren(item.children); 
 | 
            } 
 | 
        } 
 | 
  
 | 
        return item; 
 | 
    }; 
 | 
    useEffect(() => { 
 | 
        http() 
 | 
    }, [condition]); 
 | 
  
 | 
    const http = () => { 
 | 
        request.post('/warehouse/areas', { condition }) 
 | 
            .then(res => { 
 | 
                if (res?.data?.code === 200) { 
 | 
                    let data = res.data.data; 
 | 
                    let items = haveChildren(data) 
 | 
                    setTreeData(items) 
 | 
                    // setDefaultIds([items.at(0).id]) 
 | 
  
 | 
                } else { 
 | 
                    notify(res.data.msg); 
 | 
                } 
 | 
            }) 
 | 
            .catch(error => { 
 | 
                notify('Error fetching tree data'); 
 | 
            }); 
 | 
  
 | 
    } 
 | 
    const handleNodeSelect = (event, nodeId) => { 
 | 
        const row = apiRef.current.getItem(nodeId); 
 | 
  
 | 
        if (row.flagWare === 1) { 
 | 
            setFilters({ warehouseId: row.id, areaId: '' }); 
 | 
        } else if (row.flagWare === 0) { 
 | 
            setFilters({ areaId: row.id, warehouseId: row.warehouseId }); 
 | 
        } 
 | 
  
 | 
    }; 
 | 
    const handleSearch = (e) => { 
 | 
        setCondition(e.target.value) 
 | 
    }; 
 | 
  
 | 
    const apiRef = useTreeViewApiRef(); 
 | 
  
 | 
  
 | 
    const CustomCheckbox = React.forwardRef(function CustomCheckbox(props, ref) { 
 | 
        return <input type="checkbox" ref={ref} {...props} />; 
 | 
    }); 
 | 
  
 | 
    const CustomTreeItem = React.forwardRef(function CustomTreeItem(props, ref) { 
 | 
        return ( 
 | 
            <TreeItem2 
 | 
                {...props} 
 | 
                ref={ref} 
 | 
                slots={{ 
 | 
                    checkbox: CustomCheckbox, 
 | 
                }} 
 | 
            /> 
 | 
        ); 
 | 
    }); 
 | 
  
 | 
  
 | 
    return ( 
 | 
        <Card 
 | 
            sx={{ 
 | 
                order: -1, 
 | 
                mr: 2, 
 | 
                mt: 8, 
 | 
                alignSelf: 'flex-start', 
 | 
                border: theme.palette.mode === 'light' && '1px solid #e0e0e3', 
 | 
                width: 250, 
 | 
                minWidth: 150, 
 | 
                height: `100%`, 
 | 
            }} 
 | 
        > 
 | 
            <CardContent> 
 | 
                {/* <Input 
 | 
                    placeholder="搜索库位" 
 | 
                    sx={{ '--Input-focused': 1, marginBottom: '10px' }} 
 | 
                    onChange={handleSearch} 
 | 
                /> */} 
 | 
                <RichTreeView 
 | 
                    defaultExpandedItems={defaultIds} 
 | 
                    expansionTrigger="iconContainer" 
 | 
                    items={treeData} 
 | 
                    slots={CustomTreeItem} 
 | 
                    apiRef={apiRef} 
 | 
                    getItemId={(item) => item.index} 
 | 
                    getItemLabel={(item) => item.name} 
 | 
                    onItemClick={handleNodeSelect} // 监听节点点击事件 
 | 
                /> 
 | 
  
 | 
            </CardContent> 
 | 
        </Card> 
 | 
    ) 
 | 
} 
 | 
  
 | 
export default LocListAside; 
 |