<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Profit Report</title>
    <style>
        body { font-family: Arial, sans-serif; font-size: 11px; }
        .header { text-align: center; margin-bottom: 20px; }
        .header h2 { margin: 0; }
        .header p { margin: 5px 0; color: #666; }
        .summary { display: flex; justify-content: space-between; margin-bottom: 20px; }
        .summary-card { 
            border: 1px solid #ddd; 
            padding: 10px; 
            width: 23%; 
            text-align: center;
            border-radius: 4px;
        }
        .summary-card h4 { margin: 0 0 5px 0; font-size: 12px; }
        .summary-card .value { font-size: 16px; font-weight: bold; }
        .summary-card.revenue { background: #d4edda; }
        .summary-card.cost { background: #fff3cd; }
        .summary-card.gross { background: #cce5ff; }
        .summary-card.net { background: #d1ecf1; }
        table { width: 100%; border-collapse: collapse; margin-bottom: 15px; }
        th, td { border: 1px solid #ddd; padding: 6px; text-align: left; font-size: 10px; }
        th { background: #f5f5f5; font-weight: bold; }
        .text-end { text-align: right; }
        .text-center { text-align: center; }
        .section { margin-bottom: 25px; }
        .section h3 { 
            background: #f5f5f5; 
            padding: 8px; 
            margin: 0 0 10px 0; 
            font-size: 14px;
            border-left: 3px solid #0d6efd;
        }
        .two-col { display: flex; gap: 15px; }
        .two-col > div { flex: 1; }
        .badge { 
            padding: 2px 6px; 
            border-radius: 3px; 
            font-size: 9px;
        }
        .badge-success { background: #d4edda; color: #155724; }
        .badge-warning { background: #fff3cd; color: #856404; }
        .badge-danger { background: #f8d7da; color: #721c24; }
    </style>
</head>
<body>
    <div class="header">
        <h2>{{ $report_header ?? 'Profit Report' }}</h2>
        <p>{{ $report_company ?? 'POS SYSTEM' }}</p>
        <p>Period: {{ $dateFrom }} to {{ $dateTo }}</p>
        <p>Generated: {{ date('Y-m-d H:i:s') }}</p>
    </div>

    <div class="summary">
        <div class="summary-card revenue">
            <h4>Total Revenue</h4>
            <div class="value">{{ number_format($totalRevenue, 2) }}</div>
            <small>{{ $totalOrders }} orders | Avg: {{ number_format($averageOrderValue, 2) }}</small>
        </div>
        <div class="summary-card cost">
            <h4>Total Cost</h4>
            <div class="value">{{ number_format($totalCost, 2) }}</div>
        </div>
        <div class="summary-card gross">
            <h4>Gross Profit</h4>
            <div class="value">{{ number_format($grossProfit, 2) }}</div>
        </div>
        <div class="summary-card net">
            <h4>Net Profit</h4>
            <div class="value">{{ number_format($netProfit, 2) }}</div>
            <small>After expenses: {{ number_format($expenses, 2) }}</small>
        </div>
    </div>

    <div class="two-col">
        <div class="section">
            <h3>Payment Methods</h3>
            <table>
                <tr><th>Method</th><th class="text-end">Amount</th><th class="text-end">%</th></tr>
                <tr><td>Cash</td><td class="text-end">{{ number_format($cashPayments, 2) }}</td><td class="text-end">{{ $totalRevenue > 0 ? number_format(($cashPayments / $totalRevenue) * 100, 1) : 0 }}%</td></tr>
                <tr><td>M-Pesa</td><td class="text-end">{{ number_format($mpesaPayments, 2) }}</td><td class="text-end">{{ $totalRevenue > 0 ? number_format(($mpesaPayments / $totalRevenue) * 100, 1) : 0 }}%</td></tr>
                <tr><td>Card</td><td class="text-end">{{ number_format($cardPayments, 2) }}</td><td class="text-end">{{ $totalRevenue > 0 ? number_format(($cardPayments / $totalRevenue) * 100, 1) : 0 }}%</td></tr>
                <tr><td>Credit</td><td class="text-end">{{ number_format($creditPayments, 2) }}</td><td class="text-end">{{ $totalRevenue > 0 ? number_format(($creditPayments / $totalRevenue) * 100, 1) : 0 }}%</td></tr>
                <tr style="font-weight:bold;background:#f5f5f5"><td>Total</td><td class="text-end">{{ number_format($totalRevenue, 2) }}</td><td class="text-end">100%</td></tr>
            </table>
        </div>
        <div class="section">
            <h3>Category Profits</h3>
            <table>
                <tr><th>Category</th><th class="text-end">Revenue</th><th class="text-end">Cost</th><th class="text-end">Profit</th></tr>
                @forelse($categoryProfits as $cat)
                <tr>
                    <td>{{ $cat->name }}</td>
                    <td class="text-end">{{ number_format($cat->total_revenue, 2) }}</td>
                    <td class="text-end">{{ number_format($cat->total_cost, 2) }}</td>
                    <td class="text-end">{{ number_format($cat->total_revenue - $cat->total_cost, 2) }}</td>
                </tr>
                @empty
                <tr><td colspan="4" class="text-center">No data</td></tr>
                @endforelse
            </table>
        </div>
    </div>

    <div class="section">
        <h3>Daily Sales</h3>
        <table>
            <tr><th>Date</th><th class="text-end">Orders</th><th class="text-end">Revenue</th></tr>
            @forelse($dailySales as $day)
            <tr>
                <td>{{ \Carbon\Carbon::parse($day->date)->format('d M Y') }}</td>
                <td class="text-end">{{ $day->orders_count }}</td>
                <td class="text-end">{{ number_format($day->daily_revenue, 2) }}</td>
            </tr>
            @empty
            <tr><td colspan="3" class="text-center">No data</td></tr>
            @endforelse
        </table>
    </div>

    <div class="section">
        <h3>Profit per Item</h3>
        <table>
            <tr>
                <th>Product</th>
                <th class="text-end">SKU</th>
                <th class="text-end">Cost</th>
                <th class="text-end">Qty</th>
                <th class="text-end">Revenue</th>
                <th class="text-end">Cost</th>
                <th class="text-end">Profit</th>
                <th class="text-end">Margin</th>
            </tr>
            @forelse($itemProfits as $item)
            @php $margin = $item->total_revenue > 0 ? ($item->total_profit / $item->total_revenue) * 100 : 0; @endphp
            <tr>
                <td>{{ $item->name }}</td>
                <td class="text-end">{{ $item->sku ?? '-' }}</td>
                <td class="text-end">{{ number_format($item->cost_price, 2) }}</td>
                <td class="text-end">{{ $item->total_quantity }}</td>
                <td class="text-end">{{ number_format($item->total_revenue, 2) }}</td>
                <td class="text-end">{{ number_format($item->total_cost, 2) }}</td>
                <td class="text-end">{{ number_format($item->total_profit, 2) }}</td>
                <td class="text-end">
                    <span class="badge {{ $margin >= 30 ? 'badge-success' : ($margin >= 15 ? 'badge-warning' : 'badge-danger') }}">
                        {{ number_format($margin, 1) }}%
                    </span>
                </td>
            </tr>
            @empty
            <tr><td colspan="8" class="text-center">No data</td></tr>
            @endforelse
        </table>
    </div>

    <div style="text-align: center; margin-top: 20px; font-size: 10px; color: #666; border-top: 1px solid #ddd; padding-top: 10px;">
        <p>{{ $report_footer ?? 'Generated by POS System' }}</p>
        @if($report_dev_ad)
        <p style="font-size: 9px; font-style: italic; color: #888; margin-top: 5px;">{{ $report_dev_ad }}</p>
        @endif
    </div>
</body>
</html>
