import React, { useEffect, useState, useContext } from 'react' import { useLocation, useNavigate } from 'react-router-dom' import axios from 'axios' import { Card, Descriptions, Button, Space, message, Typography, Table, Tag } from 'antd' import { ArrowLeftOutlined, LoadingOutlined, ClockCircleOutlined } from '@ant-design/icons' import { AuthContext } from '../../context/AuthContext' import IdText from '../../common/IdText' import TimeDisplay from '../../common/TimeDisplay' import config from '../../../../config' import XMarkCircleIcon from '../../../Icons/XMarkCircleIcon' import CheckCircleIcon from '../../../Icons/CheckCircleIcon' const { Text, Title } = Typography const StockAuditInfo = () => { const [messageApi, contextHolder] = message.useMessage() const location = useLocation() const navigate = useNavigate() const { authenticated } = useContext(AuthContext) const [stockAudit, setStockAudit] = useState(null) const [loading, setLoading] = useState(true) const stockAuditId = new URLSearchParams(location.search).get('stockAuditId') useEffect(() => { const fetchStockAudit = async () => { if (!stockAuditId) { messageApi.error('No stock audit ID provided') navigate('/dashboard/inventory/stockaudits') return } try { const response = await axios.get( `${config.backendUrl}/stockaudits/${stockAuditId}`, { headers: { Accept: 'application/json' }, withCredentials: true } ) setStockAudit(response.data) setLoading(false) } catch (err) { messageApi.error('Failed to fetch stock audit details') navigate('/dashboard/inventory/stockaudits') } } if (authenticated) { fetchStockAudit() } }, [authenticated, stockAuditId, messageApi, navigate]) const getStatusTag = (status) => { switch (status?.toLowerCase()) { case 'completed': return ( } color='success'> Completed ) case 'in_progress': return ( } color='processing'> In Progress ) case 'failed': return ( } color='error'> Failed ) default: return ( } color='default'> Unknown ) } } const auditItemsColumns = [ { title: 'Item ID', dataIndex: '_id', key: 'id', width: 165, render: (text) => ( ) }, { title: 'Item Type', dataIndex: 'itemType', key: 'itemType', width: 120 }, { title: 'Expected Weight', dataIndex: 'expectedWeight', key: 'expectedWeight', width: 120, render: (weight) => `${weight.toFixed(2)}g` }, { title: 'Actual Weight', dataIndex: 'actualWeight', key: 'actualWeight', width: 120, render: (weight) => `${weight.toFixed(2)}g` }, { title: 'Difference', key: 'difference', width: 120, render: (_, record) => { const diff = record.actualWeight - record.expectedWeight return ( {diff.toFixed(2)}g ) } }, { title: 'Status', dataIndex: 'status', key: 'status', width: 120, render: (status) => getStatusTag(status) } ] if (loading) { return (
Loading stock audit details...
) } if (!stockAudit) { return null } return ( <> {contextHolder} Stock Audit Details {getStatusTag(stockAudit.status)} ) } export default StockAuditInfo