import React, { useState, useRef, useEffect, useMemo, useCallback } from "react"; 
 | 
import { useNavigate } from 'react-router-dom'; 
 | 
import request from '@/utils/request'; 
 | 
  
 | 
import { 
 | 
    List, 
 | 
    DatagridConfigurable, 
 | 
    SearchInput, 
 | 
    TopToolbar, 
 | 
    SelectColumnsButton, 
 | 
    FilterButton, 
 | 
    BulkDeleteButton, 
 | 
    WrapperField, 
 | 
    useRecordContext, 
 | 
    useTranslate, 
 | 
    useNotify, 
 | 
    useRefresh, 
 | 
    TextField, 
 | 
    NumberField, 
 | 
    DateField, 
 | 
    AutocompleteInput, 
 | 
    BooleanField, 
 | 
    TextInput, 
 | 
    DateInput, 
 | 
    SelectInput, 
 | 
    NumberInput, 
 | 
    Button, 
 | 
    EditButton, 
 | 
} from 'react-admin'; 
 | 
import { Box, Typography, Card, Stack, Drawer } from '@mui/material'; 
 | 
import { styled } from '@mui/material/styles'; 
 | 
import SwapVertIcon from '@mui/icons-material/SwapVert'; 
 | 
import AlignVerticalTopIcon from '@mui/icons-material/AlignVerticalTop'; 
 | 
import TaskAltIcon from '@mui/icons-material/TaskAlt'; 
 | 
import CancelIcon from '@mui/icons-material/Cancel'; 
 | 
import PageDrawer from "../components/PageDrawer"; 
 | 
import TaskPanel from "./TaskPanel"; 
 | 
import MyField from "../components/MyField"; 
 | 
import ConfirmButton from "../components/ConfirmButton"; 
 | 
import { PAGE_DRAWER_WIDTH, OPERATE_MODE, DEFAULT_PAGE_SIZE, AUTO_RUN_CHECK_ORDERS } from '@/config/setting'; 
 | 
import PlayArrowOutlinedIcon from '@mui/icons-material/PlayArrowOutlined'; 
 | 
import PauseIcon from '@mui/icons-material/Pause'; 
 | 
import PauseCircleOutlineIcon from '@mui/icons-material/PauseCircleOutline'; 
 | 
import * as Common from '@/utils/common'; 
 | 
import ColorizeOutlinedIcon from '@mui/icons-material/ColorizeOutlined'; 
 | 
import GradingOutlinedIcon from '@mui/icons-material/GradingOutlined'; 
 | 
  
 | 
const StyledDatagrid = styled(DatagridConfigurable)(({ theme }) => ({ 
 | 
    '& .css-1vooibu-MuiSvgIcon-root': { 
 | 
        height: '.9em' 
 | 
    }, 
 | 
    '& .RaDatagrid-row': { 
 | 
        cursor: 'auto' 
 | 
    }, 
 | 
    '& .column-name': { 
 | 
    }, 
 | 
    '& .opt': { 
 | 
        width: 248 
 | 
    }, 
 | 
    '& .MuiTableCell-root': { 
 | 
    whiteSpace: 'nowrap', 
 | 
    overflow: 'visible', 
 | 
    textOverflow: 'unset' 
 | 
    } 
 | 
  
 | 
})); 
 | 
  
 | 
  
 | 
const TaskList = (props) => { 
 | 
    const translate = useTranslate(); 
 | 
    const refresh = useRefresh(); 
 | 
    const [drawerVal, setDrawerVal] = useState(false); 
 | 
    const [autoExce, setAutoExce] = useState(false); 
 | 
    const dict = JSON.parse(localStorage.getItem('sys_dicts'))?.filter(dict => (dict.dictTypeCode == 'sys_warehouse_type')) || []; 
 | 
  
 | 
    useEffect(() => { 
 | 
        getConfig() 
 | 
    }, []) 
 | 
  
 | 
    const getConfig = async () => { 
 | 
        const { data: { code, data, msg } } = await request.get('/config/flag/' + AUTO_RUN_CHECK_ORDERS); 
 | 
        if (code === 200) { 
 | 
            setAutoExce(JSON.parse(data?.val)) 
 | 
        } 
 | 
    } 
 | 
  
 | 
    // useEffect(() => { 
 | 
    //     const interval = setInterval(() => { 
 | 
    //         refresh(); 
 | 
    //     }, 5000); // 每5秒刷新一次 
 | 
  
 | 
    //     return () => clearInterval(interval); // 清除定时器 
 | 
    // }, [refresh]) 
 | 
  
 | 
    const filters = [ 
 | 
        <SearchInput source="condition" alwaysOn />, 
 | 
        <DateInput label='common.time.after' source="timeStart" />, 
 | 
        <DateInput label='common.time.before' source="timeEnd" />, 
 | 
        <TextInput source="taskCode" label="table.field.task.taskCode" />, 
 | 
        <NumberInput source="taskStatus" label="table.field.task.taskStatus" />, 
 | 
        <NumberInput source="taskType" label="table.field.task.taskType" />, 
 | 
        <AutocompleteInput choices={dict} optionText='label' optionValue="value" source="warehType" label="table.field.task.warehType" />, 
 | 
        <TextInput source="orgLoc" label="table.field.task.orgLoc" />, 
 | 
        <TextInput source="orgSite" label="table.field.task.orgSite" />, 
 | 
        <TextInput source="targLoc" label="table.field.task.targLoc" />, 
 | 
        <TextInput source="targSite" label="table.field.task.targSite" />, 
 | 
        <TextInput source="barcode" label="table.field.task.barcode" />, 
 | 
        <TextInput source="robotCode" label="table.field.task.robotCode" />, 
 | 
        <NumberInput source="exceStatus" label="table.field.task.exceStatus" />, 
 | 
        <TextInput source="expDesc" label="table.field.task.expDesc" />, 
 | 
        <NumberInput source="sort" label="table.field.task.sort" />, 
 | 
        <TextInput source="expCode" label="table.field.task.expCode" />, 
 | 
        <DateInput source="startTime" label="table.field.task.startTime" />, 
 | 
        <DateInput source="endTime" label="table.field.task.endTime" />, 
 | 
        <TextInput label="common.field.memo" source="memo" />, 
 | 
        <SelectInput 
 | 
            label="common.field.status" 
 | 
            source="status" 
 | 
            choices={[ 
 | 
                { id: '1', name: 'common.enums.statusTrue' }, 
 | 
                { id: '0', name: 'common.enums.statusFalse' }, 
 | 
            ]} 
 | 
            resettable 
 | 
        />, 
 | 
    ] 
 | 
  
 | 
    return ( 
 | 
        <Box display="flex"> 
 | 
            <List 
 | 
                queryOptions={{ refetchInterval: 5000 }} 
 | 
                sx={{ 
 | 
                    flexGrow: 1, 
 | 
                    transition: (theme) => 
 | 
                        theme.transitions.create(['all'], { 
 | 
                            duration: theme.transitions.duration.enteringScreen, 
 | 
                        }), 
 | 
                    marginRight: !!drawerVal ? `${PAGE_DRAWER_WIDTH}px` : 0, 
 | 
                }} 
 | 
                title={"menu.task"} 
 | 
                empty={false} 
 | 
                filters={filters} 
 | 
                sort={{ field: "sort", order: "desc" }} 
 | 
                actions={( 
 | 
                    <TopToolbar> 
 | 
                        <BulkAutoStartButton autoExce={autoExce} setAutoExce={setAutoExce}/> 
 | 
                        <BulkAutoPauseButton autoExce={autoExce} setAutoExce={setAutoExce} /> 
 | 
                        <FilterButton /> 
 | 
                        <SelectColumnsButton preferenceKey='task' /> 
 | 
                    </TopToolbar> 
 | 
                )} 
 | 
                perPage={DEFAULT_PAGE_SIZE} 
 | 
            > 
 | 
                <StyledDatagrid 
 | 
                    preferenceKey='task' 
 | 
                    bulkActionButtons={false} 
 | 
                    rowClick={false} 
 | 
                    expand={<TaskPanel />} 
 | 
                    expandSingle={true} 
 | 
                    omit={['id', 'createTime', 'createBy$', 'memo', 'robotCode', 'exceStatus', 'expDesc', 'expCode', 'status','warehType$']} 
 | 
                > 
 | 
                    <NumberField source="id" /> 
 | 
                    <TextField source="taskCode" label="table.field.task.taskCode" /> 
 | 
                    <NumberField source="taskStatus$" label="table.field.task.taskStatus" /> 
 | 
                    <NumberField source="taskType$" label="table.field.task.taskType" /> 
 | 
                    <NumberField source="warehType$" label="table.field.task.warehType" /> 
 | 
                    <TextField source="orgLoc" label="table.field.task.orgLoc" /> 
 | 
                    <TextField source="orgSite" label="table.field.task.orgSite" /> 
 | 
                    <TextField source="targLoc" label="table.field.task.targLoc" /> 
 | 
                    <TextField source="targSite" label="table.field.task.targSite" /> 
 | 
                    <TextField source="barcode" label="table.field.task.barcode" /> 
 | 
                    <TextField source="robotCode" label="table.field.task.robotCode" /> 
 | 
                    <NumberField source="exceStatus" label="table.field.task.exceStatus" /> 
 | 
                    <TextField source="expDesc" label="table.field.task.expDesc" /> 
 | 
                    <NumberField source="sort" label="table.field.task.sort" /> 
 | 
                    <TextField source="expCode" label="table.field.task.expCode" /> 
 | 
                    <TextField source="updateBy$" label="common.field.updateBy" /> 
 | 
                    <TextField source="createBy$" label="common.field.createBy" /> 
 | 
                    <DateField source="updateTime" label="common.field.updateTime" showTime /> 
 | 
                    <DateField source="createTime" label="common.field.createTime" showTime /> 
 | 
                    <TextField source="memo" label="common.field.memo" sortable={false} /> 
 | 
                    <WrapperField cellClassName="opt" label="common.field.opt" onClick={(e) => e.stopPropagation()} > 
 | 
                        <EditButton label="toolbar.detail" /> 
 | 
                        <DoneButton sx={{ padding: '1px', fontSize: '.75rem' }} ></DoneButton> 
 | 
                        <CancelButton sx={{ padding: '1px', fontSize: '.75rem' }} mutationMode={OPERATE_MODE} /> 
 | 
                        <CheckButton /> 
 | 
                        <PickButton /> 
 | 
                        <SetTopButton /> 
 | 
                    </WrapperField> 
 | 
                </StyledDatagrid> 
 | 
            </List> 
 | 
            <PageDrawer 
 | 
                title='Task Detail' 
 | 
                drawerVal={drawerVal} 
 | 
                setDrawerVal={setDrawerVal} 
 | 
            > 
 | 
            </PageDrawer> 
 | 
        </Box> 
 | 
    ) 
 | 
} 
 | 
  
 | 
export default TaskList; 
 | 
  
 | 
/** 
 | 
 * 盘点 
 | 
 * @returns te 
 | 
 */ 
 | 
const CheckButton = () => { 
 | 
    const record = useRecordContext(); 
 | 
    const notify = useNotify(); 
 | 
    const refresh = useRefresh(); 
 | 
  
 | 
    const checkClick = async () => { 
 | 
        const { data: { code, data, msg } } = await request.post(`/task/check/` + record.id); 
 | 
        if (code === 200) { 
 | 
            notify(msg); 
 | 
            refresh(); 
 | 
        } else { 
 | 
            notify(msg); 
 | 
        } 
 | 
    } 
 | 
  
 | 
    return (record?.taskStatus == 199 && record?.taskType == 107 ? <ConfirmButton label={"toolbar.check"} startIcon={<GradingOutlinedIcon />} onConfirm={checkClick} /> : <></>) 
 | 
} 
 | 
  
 | 
/**自动下发任务**/ 
 | 
const BulkAutoStartButton = ({ autoExce, setAutoExce }) => { 
 | 
    const notify = useNotify(); 
 | 
    const startClick = async () => { 
 | 
        setAutoExce(true) 
 | 
        const { data: { code, data, msg } } = await request.post('/config/byFlag', { val: true, flag: 'AUTO_RUN_CHECK_ORDERS' }); 
 | 
        if (code === 200) { 
 | 
            notify(msg); 
 | 
        } else { 
 | 
            notify(msg); 
 | 
        } 
 | 
    } 
 | 
    return ( 
 | 
        !autoExce ? <Button label="toolbar.autoStartLocs" onClick={startClick} startIcon={<PlayArrowOutlinedIcon />} /> : <></> 
 | 
    ) 
 | 
} 
 | 
  
 | 
const BulkAutoPauseButton = ({ autoExce, setAutoExce }) => { 
 | 
    const notify = useNotify(); 
 | 
    const pauseClick = async () => { 
 | 
        const { data: { code, data, msg } } = await request.post('/config/byFlag', { val: false, flag: 'AUTO_RUN_CHECK_ORDERS' }); 
 | 
        if (code === 200) { 
 | 
            notify(msg); 
 | 
            setAutoExce(false) 
 | 
        } else { 
 | 
            notify(msg); 
 | 
        } 
 | 
    } 
 | 
    return ( 
 | 
        autoExce ? <Button label="toolbar.pause" onClick={pauseClick} startIcon={<PauseIcon />} /> : <></> 
 | 
    ) 
 | 
} 
 | 
  
 | 
  
 | 
/** 
 | 
 * 拣料出库 
 | 
 * @returns  
 | 
 */ 
 | 
const PickButton = () => { 
 | 
    const record = useRecordContext(); 
 | 
    const notify = useNotify(); 
 | 
    const refresh = useRefresh(); 
 | 
  
 | 
    const pickClick = async () => { 
 | 
        const { data: { code, data, msg } } = await request.post(`/task/pick/` + record.id); 
 | 
        if (code === 200) { 
 | 
            notify(msg); 
 | 
            refresh(); 
 | 
        } else { 
 | 
            notify(msg); 
 | 
        } 
 | 
    } 
 | 
  
 | 
    return ( 
 | 
        record?.taskStatus == 199 && record?.taskType == 103 ? <ConfirmButton label={"toolbar.pick"} startIcon={<ColorizeOutlinedIcon />} onConfirm={pickClick} /> : <></> 
 | 
    ) 
 | 
} 
 | 
  
 | 
  
 | 
/** 
 | 
 * 完成操作 
 | 
 * @returns  
 | 
 */ 
 | 
const DoneButton = (props) => { 
 | 
    const record = useRecordContext(); 
 | 
    const notify = useNotify(); 
 | 
    const refresh = useRefresh(); 
 | 
    const clickComplete = () => { 
 | 
        completeTask(record) 
 | 
    }; 
 | 
    //完成任务 
 | 
    const completeTask = async (row) => { 
 | 
        const { data: { code, data, msg } } = await request.post(`task/complete/` + row.id); 
 | 
        if (code === 200) { 
 | 
            notify(msg); 
 | 
            refresh(); 
 | 
        } else { 
 | 
            notify(msg); 
 | 
        } 
 | 
    } 
 | 
    return ( 
 | 
        ((record?.taskStatus < 98) || (record?.taskType >= 101 && record?.taskStatus < 198)) || (record?.taskType == 11 && record?.taskStatus == 101) ? (<ConfirmButton label={"toolbar.complete"} color="secondary" startIcon={<TaskAltIcon />} onConfirm={clickComplete} />) : (<></>) 
 | 
    ) 
 | 
} 
 | 
  
 | 
/** 
 | 
 * 取消按钮 
 | 
 * @returns  
 | 
 */ 
 | 
const CancelButton = () => { 
 | 
    const record = useRecordContext(); 
 | 
    const notify = useNotify(); 
 | 
    const refresh = useRefresh(); 
 | 
    const clickCancel = () => { 
 | 
        cancleTask(record) 
 | 
    }; 
 | 
    //取消任务 
 | 
    const cancleTask = async (row) => { 
 | 
        const { data: { code, data, msg } } = await request.post(`/task/remove/` + row.id); 
 | 
        if (code === 200) { 
 | 
            notify(msg); 
 | 
            refresh(); 
 | 
        } else { 
 | 
            notify(msg); 
 | 
        } 
 | 
    } 
 | 
    return ( 
 | 
        (record.taskStatus == 1 || record.taskStatus == 101) && (record.taskType == 1 || record.taskType == 101 || record.taskType == 10 || record.taskType == 107 || record.taskType == 103 || record.taskType == 11) ? 
 | 
            <ConfirmButton 
 | 
                onConfirm={clickCancel} 
 | 
                startIcon={<CancelIcon />} 
 | 
                label={"toolbar.cancel"}> 
 | 
            </ConfirmButton> 
 | 
            : 
 | 
            <></> 
 | 
    ) 
 | 
} 
 | 
/** 
 | 
 *  置顶操作 
 | 
 * @returns  
 | 
 */ 
 | 
const SetTopButton = () => { 
 | 
    const record = useRecordContext(); 
 | 
    const notify = useNotify(); 
 | 
    const refresh = useRefresh(); 
 | 
    const clickTop = (event) => { 
 | 
        event.stopPropagation(); 
 | 
        topTask(record) 
 | 
    }; 
 | 
    //置顶任务 
 | 
    const topTask = async (row) => { 
 | 
        const { data: { code, data, msg } } = await request.post(`/task/top/` + row.id); 
 | 
        if (code === 200) { 
 | 
            notify(msg); 
 | 
            refresh(); 
 | 
        } else { 
 | 
            notify(msg); 
 | 
        } 
 | 
    } 
 | 
    return ( 
 | 
        (record.taskStatus == 1 || record.taskStatus == 101) && (record.taskType == 1 || record.taskType == 101 || record.taskType == 10 || record.taskType == 103 || record.taskType == 11) ? 
 | 
            <Button 
 | 
                onClick={clickTop} 
 | 
                label="toolbar.top"> 
 | 
                <AlignVerticalTopIcon /> 
 | 
            </Button> 
 | 
            : 
 | 
            <></> 
 | 
    ) 
 | 
} 
 | 
  
 | 
/** 
 | 
 * 批量取消 
 | 
 * @returns  
 | 
 */ 
 | 
const BulkCancelButton = () => { 
 | 
    const record = useRecordContext(); 
 | 
    const clickCancel = () => { 
 | 
        cancleTask([record]) 
 | 
    }; 
 | 
    //取消任务 
 | 
    const cancleTask = async (row) => { } 
 | 
    return ( 
 | 
        <Button 
 | 
            onClick={clickCancel} 
 | 
            label="toolbar.cancel"> 
 | 
            <CancelIcon /> 
 | 
        </Button> 
 | 
    ) 
 | 
} 
 | 
  
 | 
/** 
 | 
 * 批量排序 
 | 
 * @returns  
 | 
 */ 
 | 
const BulkResortButton = () => { 
 | 
    const record = useRecordContext(); 
 | 
    const bulkResort = () => { 
 | 
        resortTask([record]) 
 | 
    }; 
 | 
    //批量排序 
 | 
    const resortTask = async (row) => { } 
 | 
    return ( 
 | 
        <Button 
 | 
            onClick={bulkResort} 
 | 
            label="toolbar.resort"> 
 | 
            <SwapVertIcon /> 
 | 
        </Button> 
 | 
    ) 
 | 
} 
 |