import React, { useEffect, useState } from 'react'
import {
Box,
Button,
Grid,
Paper,
Table,
TableBody,
TableCell,
TableContainer,
TableHead,
TableRow,
Typography,
} from '@mui/material'
import axios from 'axios' // Make sure you have axios installed
import { useNavigate, useParams } from 'react-router-dom'
import Axios from '../../axios'
import { isAutheticated } from '../../auth'
const OrderDetails = () => {
const [order, setOrder] = useState(null)
const [loading, setLoading] = useState(true)
const [error, setError] = useState(null)
const token = isAutheticated()
const navigate = useNavigate()
const { id } = useParams() // Assuming order ID is passed as a route parameter
const [ownerDetails, setOwnerDetails] = useState()
const getData = async () => {
let res = await Axios.get(`/api/v1/user/details`, {
headers: {
Authorization: `Bearer ${token}`,
},
})
if (res.data.success) {
setOwnerDetails({ ...res.data.user })
}
}
useEffect(() => {
const fetchOrderDetails = async () => {
try {
const response = await Axios.get(`/api/get-single-placed-order-pd/${id}`, {
headers: {
Authorization: `Bearer ${token}`,
},
}) // Adjust API endpoint as needed
setOrder(response.data.singleOrder)
setLoading(false)
} catch (err) {
setError(err.return_msg)
setLoading(false)
}
}
getData()
fetchOrderDetails()
}, [id])
if (loading) {
return Loading...
}
if (error) {
return Error: {error}
}
if (!order) {
return No data found
}
const {
uniqueId,
billTo,
shipTo,
paymentMode,
orderItem,
subtotal,
gstTotal,
grandTotal,
statusHistory, // Assume this is an array of status objects
} = order
return (
{/* Order Details Section */}
Order ID: {uniqueId}
Bill Address
{billTo}
Ship Address
{shipTo}
Payment Mode: {paymentMode}
SBU: {ownerDetails?.SBU}
{/* Order Items Table */}
Product
Category
Brand
SKU
Quantity
Price
GST
Subtotal
{order.orderItem.map((row, index) => {
const gstAmount = (row.price * row.GST) / 100
return (
{row.name}
{row.categoryName}
{row.brandName}
{row.SKU}
{row.quantity}
₹{row.price}
₹{gstAmount}
₹{row.price * row.quantity}
)
})}
Subtotal: ₹ {subtotal}
Total GST: ₹ {gstTotal}
Grand Total: ₹ {grandTotal}
{/* Status History Table */}
{statusHistory.length > 0 && (
Status History
Status
Timestamp
{statusHistory.map((status, index) => (
{status.status}
{new Date(status.timestamp).toLocaleString()}
))}
)}
)
}
export default OrderDetails