import React, { useState } from 'react'; import { __, sprintf } from '@wordpress/i18n'; import '../styles/OrderStatus.scss'; import { Truck, FileText, HelpCircle, MessageSquare, Calendar, Download } from 'lucide-react'; const getDateLocale = () => { if ( typeof window !== 'undefined' && window.AiskData?.locale ) { return window.AiskData.locale.replace( '_', '-' ); } if ( typeof document !== 'undefined' && document.documentElement.lang ) { return document.documentElement.lang; } return undefined; }; const OrderStatus = ({ order, onAction, conversationId }) => { if (!order || typeof order !== 'object') { return null; } const [noteText, setNoteText] = useState(''); const [noteError, setNoteError] = useState(''); const [isSubmitting, setIsSubmitting] = useState(false); const [showNotesForm, setShowNotesForm] = useState(false); const [successMessage, setSuccessMessage] = useState(''); const { order_number = '', status = 'unknown', date_created, total, shipping_method, shipping_address, tracking_number, items = [] } = order; const formatDate = (dateString) => { if (!dateString) return ''; return new Date(dateString).toLocaleDateString(getDateLocale(), { year: 'numeric', month: 'short', day: 'numeric' }); }; const getStatusEmoji = (status) => { const emojis = { 'pending': '⏳', 'processing': '🏭', 'on-hold': '⏸️', 'completed': 'βœ…', 'cancelled': '❌', 'refunded': 'πŸ’°', 'failed': '⚠️', 'shipping': '🚚' }; return emojis[status?.toLowerCase()] || 'πŸ“¦'; }; const getAvailableActions = () => { const actions = []; actions.push({ id: 'download_invoice', label: __( 'Download Invoice', 'aisk-ai-chat' ), icon: Download, description: __( 'Get a copy of your order invoice', 'aisk-ai-chat' ) }); actions.push({ id: 'track_order', label: __( 'Track Order', 'aisk-ai-chat' ), icon: Truck, description: __( 'View shipping status and location', 'aisk-ai-chat' ) }); return actions; }; const handleAction = (actionId) => { if (onAction) { onAction({ type: actionId, order_number: order_number }); } }; const handleNoteSubmit = async () => { if (!noteText.trim()) { setNoteError(__( 'Please enter your question or issue', 'aisk-ai-chat' )); setSuccessMessage(''); return; } setIsSubmitting(true); try { const response = await fetch(`${AiskData.apiUrl}/submit-inquiry`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-WP-Nonce': AiskData.nonce }, body: JSON.stringify({ order_number: order_number, conversation_id: conversationId, note: noteText }) }); if (response.ok) { setNoteText(''); setNoteError(''); setSuccessMessage(__( 'βœ… Inquiry submitted successfully!', 'aisk-ai-chat' )); setTimeout(() => { setShowNotesForm(false); setSuccessMessage(''); }, 3000); onAction({ type: 'inquiry_submitted' }); } else { throw new Error('Failed to submit inquiry'); } } catch (error) { setNoteError(__( 'Failed to submit inquiry. Please try again.', 'aisk-ai-chat' )); setSuccessMessage(''); } finally { setIsSubmitting(false); } }; const actions = getAvailableActions(); return (

{sprintf( __( 'Order #%s', 'aisk-ai-chat' ), order_number )}

{getStatusEmoji(status)} {status.toString().toUpperCase()}
πŸ“… {__( 'Date:', 'aisk-ai-chat' )} {formatDate(date_created)}
πŸ’° {__( 'Total:', 'aisk-ai-chat' )} {total}
{shipping_method && (
🚚 {__( 'Shipping:', 'aisk-ai-chat' )} {shipping_method}
)} {shipping_address && (
πŸ“ {__( 'Address:', 'aisk-ai-chat' )} {shipping_address}
)} {tracking_number && (
πŸ“¦ {__( 'Tracking:', 'aisk-ai-chat' )} {tracking_number}
)}
{Array.isArray(items) && items.length > 0 && (

πŸ›οΈ {__( 'Order Items', 'aisk-ai-chat' )}

{items.map((item, index) => (
{item.image && (
{item.name}
)}
{item.name || __( 'Unknown Item', 'aisk-ai-chat' )}
x{item.quantity || 1} {item.total || ''}
))}
)}
{!showNotesForm ? (
) : (

{__( 'How can we help you?', 'aisk-ai-chat' )}