import React, { useEffect, useState, useCallback } from 'react' import { Descriptions, Space, Flex, Alert, Typography, Spin, message, Button, Collapse, Segmented, Card } from 'antd' import { LoadingOutlined, CaretLeftOutlined } from '@ant-design/icons' import { Line } from '@ant-design/charts' import axios from 'axios' import PrinterIcon from '../../Icons/PrinterIcon' import JobIcon from '../../Icons/JobIcon' import ReloadIcon from '../../Icons/ReloadIcon' import useCollapseState from '../hooks/useCollapseState' import config from '../../../config' const { Title, Text } = Typography const ProductionOverview = () => { const [messageApi, contextHolder] = message.useMessage() const [error, setError] = useState(null) const [fetchPrinterStatsLoading, setFetchPrinterStatsLoading] = useState(true) const [chartData, setChartData] = useState([]) const [collapseState, updateCollapseState] = useCollapseState( 'ProductionOverview', { overview: true, printerStats: true, jobStats: true } ) const [timeRanges, setTimeRanges] = useState({ overview: '24h', printerStats: '24h', jobStats: '24h' }) const [stats, setStats] = useState({ totalPrinters: 0, activePrinters: 0, totalJobs: 0, activeJobs: 0, completedJobs: 0, printerStatus: { idle: 0, printing: 0, error: 0, offline: 0 } }) const fetchAllStats = useCallback(async () => { await fetchPrinterStats() await fetchJobstats() await fetchChartData() }, []) const fetchPrinterStats = async () => { try { setFetchPrinterStatsLoading(true) const response = await axios.get(`${config.backendUrl}/printers/stats`, { headers: { Accept: 'application/json' }, withCredentials: true }) const printStats = response.data setStats((prev) => ({ ...prev, printers: printStats })) setError(null) } catch (err) { setError('Failed to fetch printer details') messageApi.error('Failed to fetch printer details') } finally { setFetchPrinterStatsLoading(false) } } const fetchJobstats = async () => { try { setFetchPrinterStatsLoading(true) const response = await axios.get(`${config.backendUrl}/jobs/stats`, { headers: { Accept: 'application/json' }, withCredentials: true }) const jobstats = response.data setStats((prev) => ({ ...prev, jobs: jobstats })) setError(null) } catch (err) { setError('Failed to fetch printer details') messageApi.error('Failed to fetch printer details') } finally { setFetchPrinterStatsLoading(false) } } const fetchChartData = async () => { try { const response = await axios.get(`${config.backendUrl}/stats/history`, { headers: { Accept: 'application/json' }, withCredentials: true }) setChartData(response.data) } catch (err) { console.error('Failed to fetch chart data:', err) } } useEffect(() => { fetchAllStats() }, [fetchAllStats]) if (fetchPrinterStatsLoading || fetchPrinterStatsLoading) { return (
} />
) } if (error || !stats) { return (

{error || 'Printer not found'}

) } return (
{contextHolder} updateCollapseState('overview', keys.length > 0)} expandIcon={({ isActive }) => ( )} className='no-h-padding-collapse no-t-padding-collapse' > Status Overview } key='1' > Ready {(stats.printers.standby || 0) + (stats.printers.complete || 0)} } /> Printing {stats.printers.printing || 0} } /> Queued {stats.jobs.queued || 0} } /> Printing {stats.jobs.printing || 0} } /> Failed {(stats.jobs.failed || 0) + (stats.jobs.cancelled || 0)} } /> Complete {stats.jobs.complete || 0} } /> updateCollapseState('printerStats', keys.length > 0) } expandIcon={({ isActive }) => ( )} className='no-h-padding-collapse' > Production Statistics setTimeRanges((prev) => ({ ...prev, printerStats: value })) } size='small' /> } key='2' > {stats.totalPrinters} {stats.activePrinters} {stats.activePrinters} updateCollapseState('jobStats', keys.length > 0) } expandIcon={({ isActive }) => ( )} className='no-h-padding-collapse' > Job Statistics setTimeRanges((prev) => ({ ...prev, jobStats: value })) } size='small' /> } key='3' > {stats.totalJobs} {stats.activeJobs} {stats.completedJobs}
) } export default ProductionOverview