@if($msg['role'] === 'assistant' && is_array($msg['content']))
@if(isset($msg['content']['answer']))
{{ $msg['content']['answer'] }}
@endif
@if(isset($msg['content']['widgets']) && !empty($msg['content']['widgets']))
@php
// First, check if AI provided highlight_card widgets
$highlightCards = collect($msg['content']['widgets'] ?? [])->where('type', 'highlight_card');
// If no highlight cards, try to calculate from table data
$salesSummary = null;
if ($highlightCards->isEmpty()) {
$tableWidget = collect($msg['content']['widgets'] ?? [])->firstWhere('type', 'table');
if ($tableWidget && !empty($tableWidget['data'])) {
$tableData = $tableWidget['data'];
// Check if this is sales_by_day data (has gross_sales, net_sales, orders_count)
if (!empty($tableData) && isset($tableData[0]['gross_sales'])) {
$totalGross = 0;
$totalNet = 0;
$totalOrders = 0;
foreach ($tableData as $row) {
$totalGross += (float) ($row['gross_sales'] ?? 0);
$totalNet += (float) ($row['net_sales'] ?? 0);
$totalOrders += (int) ($row['orders_count'] ?? 0);
}
// Create highlight cards from sales data
$highlightCards = collect([
[
'type' => 'highlight_card',
'title' => 'Total Gross Sales',
'data' => ['value' => '$' . number_format($totalGross, 2)]
],
[
'type' => 'highlight_card',
'title' => 'Total Net Sales',
'data' => ['value' => '$' . number_format($totalNet, 2)]
],
[
'type' => 'highlight_card',
'title' => 'Total Orders',
'data' => ['value' => number_format($totalOrders)]
]
]);
}
// Check if this is orders data (has total, order_no, or any numeric field that could be total)
elseif (!empty($tableData)) {
$firstRow = $tableData[0] ?? [];
// Try different possible field names for total/amount
$totalField = null;
$possibleFields = ['total', 'order_total', 'amount', 'TOTAL', 'order_amount', 'subtotal', 'grand_total'];
foreach ($possibleFields as $field) {
if (isset($firstRow[$field]) && (is_numeric($firstRow[$field]) || is_string($firstRow[$field]))) {
$totalField = $field;
break;
}
}
if ($totalField) {
$totalSales = 0;
foreach ($tableData as $row) {
$value = $row[$totalField] ?? 0;
// Handle string values like "$1,234.56" or "1234.56"
if (is_string($value)) {
$value = preg_replace('/[^0-9.]/', '', $value);
}
$totalSales += (float) $value;
}
$orderCount = count($tableData);
// Always create cards (even if totals are 0, user should see the summary)
// Create highlight cards from orders data
$highlightCards = collect([
[
'type' => 'highlight_card',
'title' => 'Total Sales',
'data' => ['value' => '$' . number_format($totalSales, 2)]
],
[
'type' => 'highlight_card',
'title' => 'Total Orders',
'data' => ['value' => number_format($orderCount)]
]
]);
}
}
}
}
@endphp
@if($highlightCards->isNotEmpty())
@foreach($highlightCards->take(3) as $card)
@php
$cardValue = $card['data']['value'] ?? $card['data']['total'] ?? '0';
$cardTitle = $card['title'] ?? 'Metric';
// Determine card color based on title
$cardColors = [
'Total Gross Sales' => 'from-green-500 to-green-600',
'Total Net Sales' => 'from-blue-500 to-blue-600',
'Total Sales' => 'from-green-500 to-green-600',
'Total Orders' => 'from-purple-500 to-purple-600',
'Total Revenue' => 'from-green-500 to-green-600',
];
$bgColor = $cardColors[$cardTitle] ?? 'from-skin-base to-skin-base/[.9]';
@endphp
{{ $cardTitle }}
{{ $cardValue }}
@endforeach
@endif
@foreach($msg['content']['widgets'] as $widget)
@if($widget['type'] === 'highlight_card')
{{-- Highlight cards are displayed above, skip here --}}
@elseif($widget['type'] === 'ranked_metric')
{{ $widget['title'] ?? '' }}
@foreach(array_slice($widget['data'] ?? [], 0, 10) as $itemIndex => $item)
@php
// Debug: Log item data structure (remove in production)
// \Log::info('Ranked metric item data', ['item' => $item, 'index' => $itemIndex]);
@endphp
{{ $item['name'] ?? $item['item_name'] ?? '' }}
@if(isset($item['change']) || isset($item['percentage']))
@php
$change = floatval($item['change'] ?? $item['percentage'] ?? 0);
$isPositive = $change >= 0;
@endphp
{{ $isPositive ? '+' : '' }}{{ number_format($change, 1) }}%
@if($isPositive)
@else
@endif
@endif
@php
$displayValue = '';
// Helper function to parse currency strings
$parseCurrency = function($val) {
if (is_string($val)) {
// Remove currency symbols and commas, then parse
$cleaned = preg_replace('/[^0-9.]/', '', $val);
return (float)$cleaned;
}
return (float)$val;
};
// Find the best numeric field to display (prioritize revenue-related fields)
$revenueFields = ['revenue', 'total_revenue', 'amount', 'total_amount', 'sales', 'total_sales'];
$otherNumericFields = ['value', 'qty_sold', 'quantity', 'count'];
$foundValue = null;
$foundValueName = null;
// First, try revenue-related fields
foreach ($revenueFields as $field) {
if (isset($item[$field])) {
$parsed = $parseCurrency($item[$field]);
if ($parsed > 0) {
$foundValue = $parsed;
$foundValueName = $field;
break;
}
}
}
// If no revenue field found, try other numeric fields
if ($foundValue === null) {
foreach ($otherNumericFields as $field) {
if (isset($item[$field])) {
$parsed = $parseCurrency($item[$field]);
if ($parsed > 0) {
$foundValue = $parsed;
$foundValueName = $field;
break;
}
}
}
}
// If still nothing, check all fields for numeric values
if ($foundValue === null) {
foreach ($item as $key => $val) {
// Skip non-numeric fields
if (in_array($key, ['name', 'item_name', 'title', 'id', 'item_id'])) {
continue;
}
$parsed = $parseCurrency($val);
if ($parsed > 0 && $parsed > ($foundValue ?? 0)) {
$foundValue = $parsed;
$foundValueName = $key;
}
}
}
// Format the display value
if ($foundValue !== null && $foundValue > 0) {
// If it's a revenue-related field or a large number (> 10), format as currency
if (in_array($foundValueName, $revenueFields) || $foundValue >= 10) {
$displayValue = '$' . number_format($foundValue, 2);
} else {
// For small numbers, might be quantity
$displayValue = number_format((int)$foundValue);
}
} elseif (isset($item['revenue'])) {
// Revenue exists but is 0, still show formatted
$revenue = $parseCurrency($item['revenue']);
$displayValue = '$' . number_format($revenue, 2);
} elseif (isset($item['qty_sold'])) {
// Show quantity as fallback
$displayValue = number_format((int)$item['qty_sold']);
}
@endphp
{{ $displayValue ?: '$0.00' }}
@endforeach
@elseif($widget['type'] === 'table')
@php
// Get consistent column order from first row
$firstRow = $widget['data'][0] ?? [];
$columnOrder = array_keys($firstRow);
// Define preferred column order for orders table
$preferredOrder = ['order_no', 'order_number', 'date', 'date_time', 'table', 'customer', 'total', 'status'];
$orderedColumns = [];
$remainingColumns = [];
// Sort columns by preferred order
foreach ($preferredOrder as $col) {
foreach ($columnOrder as $key) {
if (strtolower($key) === strtolower($col) && !in_array($key, $orderedColumns)) {
$orderedColumns[] = $key;
break;
}
}
}
// Add remaining columns
foreach ($columnOrder as $key) {
if (!in_array($key, $orderedColumns)) {
$orderedColumns[] = $key;
}
}
// Use ordered columns or fallback to original order
$displayColumns = !empty($orderedColumns) ? $orderedColumns : $columnOrder;
@endphp
@foreach($displayColumns as $header)
|
{{ ucfirst(str_replace('_', ' ', $header)) }}
|
@endforeach
@foreach(array_slice($widget['data'] ?? [], 0, 10) as $index => $row)
@foreach($displayColumns as $key)
|
@php
$cell = $row[$key] ?? null;
// Format based on column type
$keyLower = strtolower($key);
// Handle order number fields - should be displayed as integer
if (in_array($keyLower, ['order_no', 'order_number', 'order_num'])) {
// Order number should be integer, remove decimals if present
if (is_numeric($cell)) {
$formatted = (string)(int)(float)$cell;
} else {
$formatted = $cell ?? '';
}
} elseif (in_array($keyLower, ['gross_sales', 'net_sales', 'revenue', 'total', 'tax_total', 'discount_total', 'amount', 'price'])) {
// Currency fields - ensure we have a valid numeric value
if ($cell === null || $cell === '') {
$formatted = '$0.00';
} else {
$cellValue = is_string($cell) ? preg_replace('/[^0-9.-]/', '', $cell) : $cell;
$cellValue = (float)$cellValue;
$formatted = '$' . number_format($cellValue, 2);
}
} elseif (in_array($keyLower, ['date', 'date_time', 'reservation_date_time', 'created_at', 'completed_at', 'cancel_time', 'pickup_date'])) {
// Date/DateTime fields - format with time if it's a datetime
if ($cell === null || $cell === '') {
$formatted = '';
} else {
try {
$date = \Carbon\Carbon::parse($cell);
// Check if it's a datetime (has time component) or just a date
if (in_array($keyLower, ['date_time', 'reservation_date_time', 'created_at', 'completed_at', 'cancel_time', 'pickup_date'])) {
// DateTime fields - show date and time
$formatted = $date->format('M d, Y h:i A');
} else {
// Date only fields
$formatted = $date->format('M d, Y');
}
} catch (\Exception $e) {
$formatted = $cell;
}
}
} elseif (in_array($keyLower, ['reservation_id', 'order_id', 'id'])) {
// ID fields - format as integer without decimals
$formatted = $cell !== null ? '#' . (int)$cell : '';
} elseif (in_array($keyLower, ['orders_count', 'qty_sold', 'qty_used', 'quantity', 'count', 'party_size'])) {
// Integer fields
$formatted = $cell !== null ? number_format((int)$cell) : '0';
} elseif (is_numeric($cell)) {
// Other numeric fields
$formatted = number_format((float)$cell, 2);
} else {
$formatted = $cell ?? '';
}
@endphp
{{ $formatted }}
|
@endforeach
@endforeach
@elseif($widget['type'] === 'highlight_card')
{{ $widget['title'] ?? '' }}
{{ $widget['data']['value'] ?? '' }}
@endif
@endforeach
@endif
@if(isset($msg['content']['followups']) && !empty($msg['content']['followups']))
@lang('aitools::app.core.suggestedQuestions')
@foreach($msg['content']['followups'] as $followup)
@endforeach
@endif
@else
@if(is_array($msg['content']))
@if(isset($msg['content']['text']))
{{ $msg['content']['text'] }}
@elseif(isset($msg['content']['answer']))
{{ $msg['content']['answer'] }}
@else
{{ json_encode($msg['content'], JSON_PRETTY_PRINT) }}
@endif
@else
{{ $msg['content'] }}
@endif
@endif
{{ $msg['created_at'] }}