@extends('layouts.main')

@section('content')
<style>
@media print {
    @page { size: landscape; margin: 10mm; }
    body { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
    .btn, .card-header form, .no-print, .summary-badges { display: none !important; }
    .card { border: none !important; box-shadow: none !important; }
    .card-body { padding: 0 !important; }
    .table { font-size: 10px; }
    .table th, .table td { padding: 4px !important; }
    .print-header { display: block !important; margin-bottom: 15px; }
    .print-footer { display: flex !important; }
    .cashier-cards { page-break-inside: avoid; }
}
.print-header, .print-footer { display: none; }
.pagination { font-size: 0.85rem; }
.pagination .page-link { padding: 0.25rem 0.5rem; }
.cashier-card {
    border-radius: 12px;
    transition: transform 0.2s, box-shadow 0.2s;
}
.cashier-card:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 15px rgba(0,0,0,0.1);
}
.cashier-avatar {
    width: 50px;
    height: 50px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.2rem;
    font-weight: bold;
    color: white;
}
.top-product-item {
    transition: background-color 0.2s;
}
.top-product-item:hover {
    background-color: #f8f9fa;
}
</style>

<div class="d-flex justify-content-between align-items-center mb-4 no-print">
    <div>
        <h4><i class="bi bi-people-fill me-2"></i>Cashier Sales Report</h4>
        <p class="text-muted mb-0 small">Detailed breakdown of items sold by each cashier</p>
    </div>
</div>

<div class="d-flex justify-content-end mb-4 no-print">
    <a href="{{ route('reports.cashier-sales.export', ['date_from' => $dateFrom, 'date_to' => $dateTo, 'user_id' => $userId ?? '', 'category_id' => $categoryId ?? '']) }}" class="btn btn-success me-2">
        <i class="bi bi-download"></i> Export CSV
    </a>
    <button onclick="window.print()" class="btn btn-danger">
        <i class="bi bi-file-earmark-pdf"></i> Print PDF
    </button>
</div>

<div class="card mb-4">
    <div class="card-header no-print">
        <form method="GET" action="{{ route('reports.cashier-sales') }}" class="row g-3">
            <div class="col-md-2">
                <label class="form-label">Date From</label>
                <input type="date" name="date_from" class="form-control" value="{{ $dateFrom }}">
            </div>
            <div class="col-md-2">
                <label class="form-label">Date To</label>
                <input type="date" name="date_to" class="form-control" value="{{ $dateTo }}">
            </div>
            <div class="col-md-2">
                <label class="form-label">Cashier</label>
                <select name="user_id" class="form-select">
                    <option value="">All Cashiers</option>
                    @foreach($users as $user)
                        <option value="{{ $user->id }}" {{ $userId == $user->id ? 'selected' : '' }}>
                            {{ $user->name }}
                        </option>
                    @endforeach
                </select>
            </div>
            <div class="col-md-2">
                <label class="form-label">Category</label>
                <select name="category_id" class="form-select">
                    <option value="">All Categories</option>
                    @foreach($categories as $category)
                        <option value="{{ $category->id }}" {{ $categoryId == $category->id ? 'selected' : '' }}>
                            {{ $category->name }}
                        </option>
                    @endforeach
                </select>
            </div>
            <div class="col-md-4 d-flex align-items-end">
                <div class="d-flex gap-2 w-100">
                    <button type="submit" class="btn btn-primary flex-grow-1">
                        <i class="bi bi-search me-1"></i> Generate Report
                    </button>
                    <a href="{{ route('reports.cashier-sales') }}" class="btn btn-secondary">
                        <i class="bi bi-arrow-clockwise"></i>
                    </a>
                </div>
            </div>
        </form>
    </div>
</div>

<div class="print-header mb-4">
    <div class="row">
        <div class="col-12 text-center">
            <h3 class="mb-1">Cashier Sales Report</h3>
            <p class="mb-0 text-muted">
                Report Period: {{ \Carbon\Carbon::parse($dateFrom)->format('M d, Y') }} - {{ \Carbon\Carbon::parse($dateTo)->format('M d, Y') }}
                @if($selectedUser)
                    | Cashier: {{ $selectedUser->name }}
                @endif
            </p>
        </div>
    </div>
</div>

<div class="row mb-4">
    <div class="col-md-3">
        <div class="card bg-primary text-white cashier-card">
            <div class="card-body py-3">
                <div class="d-flex align-items-center">
                    <div class="me-3">
                        <i class="bi bi-cart-check-fill fs-1 opacity-75"></i>
                    </div>
                    <div>
                        <h6 class="mb-1 opacity-75">Total Orders</h6>
                        <h3 class="mb-0">{{ number_format($totalOrdersCount) }}</h3>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="card bg-success text-white cashier-card">
            <div class="card-body py-3">
                <div class="d-flex align-items-center">
                    <div class="me-3">
                        <i class="bi bi-currency-dollar fs-1 opacity-75"></i>
                    </div>
                    <div>
                        <h6 class="mb-1 opacity-75">Total Revenue</h6>
                        <h3 class="mb-0">{{ number_format($totalSummary['total_revenue'], 2) }}</h3>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="card bg-info text-white cashier-card">
            <div class="card-body py-3">
                <div class="d-flex align-items-center">
                    <div class="me-3">
                        <i class="bi bi-box-seam fs-1 opacity-75"></i>
                    </div>
                    <div>
                        <h6 class="mb-1 opacity-75">Items Sold</h6>
                        <h3 class="mb-0">{{ number_format($totalSummary['total_items']) }}</h3>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="card" style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white;">
            <div class="card-body py-3">
                <div class="d-flex align-items-center">
                    <div class="me-3">
                        <i class="bi bi-graph-up-arrow fs-1 opacity-75"></i>
                    </div>
                    <div>
                        <h6 class="mb-1 opacity-75">Total Profit</h6>
                        <h3 class="mb-0">{{ number_format($totalSummary['total_profit'], 2) }}</h3>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="row mb-4">
    <div class="col-md-3">
        <div class="card" style="background: linear-gradient(135deg, #ff6b6b 0%, #ee5a24 100%); color: white;">
            <div class="card-body py-3">
                <div class="d-flex align-items-center">
                    <div class="me-3">
                        <i class="bi bi-credit-card-fill fs-1 opacity-75"></i>
                    </div>
                    <div>
                        <h6 class="mb-1 opacity-75">Credit Sales</h6>
                        <h3 class="mb-0">{{ number_format($creditSummary['total_credit_sales'], 2) }}</h3>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="card bg-success text-white cashier-card">
            <div class="card-body py-3">
                <div class="d-flex align-items-center">
                    <div class="me-3">
                        <i class="bi bi-check-circle-fill fs-1 opacity-75"></i>
                    </div>
                    <div>
                        <h6 class="mb-1 opacity-75">Credit Paid</h6>
                        <h3 class="mb-0">{{ number_format($creditSummary['total_credit_paid'], 2) }}</h3>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="card bg-warning text-dark cashier-card">
            <div class="card-body py-3">
                <div class="d-flex align-items-center">
                    <div class="me-3">
                        <i class="bi bi-hourglass-split fs-1 opacity-75"></i>
                    </div>
                    <div>
                        <h6 class="mb-1 opacity-75">Credit Pending</h6>
                        <h3 class="mb-0">{{ number_format($creditSummary['total_credit_pending'], 2) }}</h3>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="card bg-danger text-white cashier-card">
            <div class="card-body py-3">
                <div class="d-flex align-items-center">
                    <div class="me-3">
                        <i class="bi bi-exclamation-triangle-fill fs-1 opacity-75"></i>
                    </div>
                    <div>
                        <h6 class="mb-1 opacity-75">Credit Overdue</h6>
                        <h3 class="mb-0">{{ number_format($creditSummary['total_credit_overdue'], 2) }}</h3>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

@if(!$selectedUser)
<div class="card mb-4">
    <div class="card-header bg-light">
        <h5 class="mb-0"><i class="bi bi-person-badge me-2"></i>Cashier Performance Summary</h5>
    </div>
    <div class="card-body">
        <div class="row g-3 cashier-cards">
            @php
                $cashierColors = [
                    'super_admin' => 'linear-gradient(135deg, #667eea 0%, #764ba2 100%)',
                    'admin' => 'linear-gradient(135deg, #f093fb 0%, #f5576c 100%)',
                    'cashier' => 'linear-gradient(135deg, #4facfe 0%, #00f2fe 100%)',
                ];
                $cashierIcons = [
                    'super_admin' => 'bi-shield-check',
                    'admin' => 'bi-person-check',
                    'cashier' => 'bi-person',
                ];
            @endphp
            @foreach($cashierSummary as $summary)
                @if($summary['order_count'] > 0)
                    <div class="col-md-6 col-lg-4 col-xl-3">
                        <div class="card border-0 shadow-sm h-100">
                            <div class="card-body">
                                <div class="d-flex align-items-center mb-3">
                                    <div class="cashier-avatar me-3" style="background: {{ $cashierColors[$summary['role']] ?? $cashierColors['cashier'] }}">
                                        {{ substr($summary['name'], 0, 1) }}
                                    </div>
                                    <div>
                                        <h6 class="mb-0">{{ $summary['name'] }}</h6>
                                        <small class="text-muted text-capitalize">{{ str_replace('_', ' ', $summary['role']) }}</small>
                                    </div>
                                </div>
                                <div class="row g-2">
                                    <div class="col-6">
                                        <div class="p-2 bg-light rounded text-center">
                                            <small class="text-muted d-block">Orders</small>
                                            <strong>{{ $summary['order_count'] }}</strong>
                                        </div>
                                    </div>
                                    <div class="col-6">
                                        <div class="p-2 bg-light rounded text-center">
                                            <small class="text-muted d-block">Items</small>
                                            <strong>{{ number_format($summary['items_sold']) }}</strong>
                                        </div>
                                    </div>
                                    <div class="col-6">
                                        <div class="p-2 bg-light rounded text-center">
                                            <small class="text-muted d-block">Revenue</small>
                                            <strong class="text-success">{{ number_format($summary['total_revenue'], 0) }}</strong>
                                        </div>
                                    </div>
                                    <div class="col-6">
                                        <div class="p-2 bg-light rounded text-center">
                                            <small class="text-muted d-block">Profit</small>
                                            <strong class="text-primary">{{ number_format($summary['total_profit'], 0) }}</strong>
                                        </div>
                                    </div>
                                    @if(($summary['credit_count'] ?? 0) > 0)
                                    <div class="col-6">
                                        <div class="p-2 bg-warning bg-opacity-25 rounded text-center">
                                            <small class="text-muted d-block">Credit Sales</small>
                                            <strong class="text-warning">{{ number_format($summary['credit_amount'] ?? 0, 0) }}</strong>
                                        </div>
                                    </div>
                                    <div class="col-6">
                                        <div class="p-2 bg-danger bg-opacity-25 rounded text-center">
                                            <small class="text-muted d-block">Pending</small>
                                            <strong class="text-danger">{{ number_format($summary['credit_pending'] ?? 0, 0) }}</strong>
                                        </div>
                                    </div>
                                    @endif
                                </div>
                            </div>
                            <div class="card-footer bg-transparent border-0 pt-0">
                                <a href="{{ route('reports.cashier-sales', ['date_from' => $dateFrom, 'date_to' => $dateTo, 'user_id' => $summary['id'], 'category_id' => $categoryId ?? '']) }}" class="btn btn-sm btn-outline-primary w-100">
                                    <i class="bi bi-eye me-1"></i> View Details
                                </a>
                            </div>
                        </div>
                    </div>
                @endif
            @endforeach
        </div>
        @if(count(array_filter($cashierSummary, fn($s) => $s['order_count'] > 0)) === 0)
            <div class="text-center text-muted py-4">
                <i class="bi bi-inbox fs-1 d-block mb-2"></i>
                No sales data found for the selected period.
            </div>
        @endif
    </div>
</div>
@endif

@if($selectedUser)
<div class="alert alert-primary d-flex align-items-center no-print mb-4" role="alert">
    <i class="bi bi-person-check-fill me-2"></i>
    <div>
        Showing results for <strong>{{ $selectedUser->name }}</strong> | 
        <a href="{{ route('reports.cashier-sales', ['date_from' => $dateFrom, 'date_to' => $dateTo, 'category_id' => $categoryId ?? '']) }}" class="alert-link">View All Cashiers</a>
    </div>
</div>
@endif

<div class="row mb-4">
    <div class="col-lg-8">
        <div class="card">
            <div class="card-header bg-light d-flex justify-content-between align-items-center">
                <h5 class="mb-0"><i class="bi bi-list-ul me-2"></i>Items Sold Detail</h5>
                <span class="badge bg-primary">{{ $items->total() }} Items</span>
            </div>
            <div class="card-body table-responsive">
                <table class="table table-hover table-sm">
                    <thead class="table-light">
                        <tr>
                            <th>Date & Time</th>
                            <th>Invoice #</th>
                            <th>Cashier</th>
                            <th>Product</th>
                            <th>SKU</th>
                            <th class="text-center">Qty</th>
                            <th class="text-end">Unit Price</th>
                            <th class="text-end">Subtotal</th>
                            <th class="text-end">Cost</th>
                            <th class="text-end">Profit</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($items as $item)
                            @php
                                $orderTime = $item->order->client_time ? \Carbon\Carbon::parse($item->order->client_time) : $item->order->created_at;
                                $costPrice = $item->product->cost_price ?? 0;
                                $lineCost = $costPrice * $item->quantity;
                                $lineProfit = $item->subtotal - $lineCost;
                            @endphp
                            <tr>
                                <td>
                                    <small>{{ $orderTime->format('M d, Y') }}</small><br>
                                    <span class="text-muted">{{ $orderTime->format('H:i:s') }}</span>
                                </td>
                                <td>
                                    <a href="{{ route('orders.show', $item->order) }}" class="text-decoration-none">
                                        <span class="badge bg-secondary">{{ $item->order->invoice_number }}</span>
                                    </a>
                                </td>
                                <td>
                                    <div class="d-flex align-items-center">
                                        <div class="avatar-sm me-2" style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; width: 28px; height: 28px; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-size: 11px; font-weight: bold;">
                                            {{ substr($item->order->user->name, 0, 1) }}
                                        </div>
                                        <small>{{ $item->order->user->name }}</small>
                                    </div>
                                </td>
                                <td>
                                    <strong>{{ $item->product->name ?? $item->product_name }}</strong>
                                    @if($item->product?->category)
                                        <br><span class="badge bg-light text-dark small">{{ $item->product->category->name }}</span>
                                    @endif
                                </td>
                                <td><code>{{ $item->product->sku ?? $item->product_sku ?? 'N/A' }}</code></td>
                                <td class="text-center">
                                    <span class="badge bg-info">{{ $item->quantity }}</span>
                                </td>
                                <td class="text-end">{{ number_format($item->price, 2) }}</td>
                                <td class="text-end"><strong>{{ number_format($item->subtotal, 2) }}</strong></td>
                                <td class="text-end text-muted">{{ number_format($lineCost, 2) }}</td>
                                <td class="text-end">
                                    <span class="{{ $lineProfit >= 0 ? 'text-success' : 'text-danger' }}">
                                        <strong>{{ number_format($lineProfit, 2) }}</strong>
                                    </span>
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="10" class="text-center text-muted py-4">
                                    <i class="bi bi-inbox fs-1 d-block mb-2"></i>
                                    No items found for the selected filters.
                                </td>
                            </tr>
                        @endforelse
                    </tbody>
                    @if($items->count() > 0)
                        <tfoot class="table-light fw-bold">
                            <tr>
                                <td colspan="5" class="text-end">TOTALS:</td>
                                <td class="text-center">{{ number_format($items->sum('quantity')) }}</td>
                                <td class="text-end">-</td>
                                <td class="text-end">{{ number_format($items->sum('subtotal'), 2) }}</td>
                                <td class="text-end">{{ number_format($totalSummary['total_cost'], 2) }}</td>
                                <td class="text-end text-success">{{ number_format($totalSummary['total_profit'], 2) }}</td>
                            </tr>
                        </tfoot>
                    @endif
                </table>
                
                @if($items->hasPages())
                    <div class="no-print mt-3">
                        {{ $items->appends(request()->query())->links('pagination::bootstrap-4') }}
                    </div>
                @endif
            </div>
        </div>
    </div>
    
    <div class="col-lg-4">
        <div class="card">
            <div class="card-header bg-light">
                <h5 class="mb-0"><i class="bi bi-trophy me-2"></i>Top Selling Products</h5>
            </div>
            <div class="card-body p-0">
                @if($topProducts->count() > 0)
                    <div class="list-group list-group-flush">
                        @foreach($topProducts as $index => $product)
                            <div class="list-group-item top-product-item">
                                <div class="d-flex align-items-center">
                                    <div class="me-3">
                                        @if($index === 0)
                                            <span class="badge bg-warning text-dark rounded-circle p-2">
                                                <i class="bi bi-trophy-fill"></i>
                                            </span>
                                        @elseif($index === 1)
                                            <span class="badge bg-secondary rounded-circle p-2">
                                                <i class="bi bi-award-fill"></i>
                                            </span>
                                        @elseif($index === 2)
                                            <span class="badge rounded-circle p-2" style="background-color: #cd7f32; color: white;">
                                                <i class="bi bi-star-fill"></i>
                                            </span>
                                        @else
                                            <span class="badge bg-light text-dark rounded-circle p-2">
                                                {{ $index + 1 }}
                                            </span>
                                        @endif
                                    </div>
                                    <div class="flex-grow-1">
                                        <h6 class="mb-0">{{ $product->name }}</h6>
                                        <small class="text-muted">SKU: {{ $product->sku ?? 'N/A' }}</small>
                                    </div>
                                    <div class="text-end">
                                        <div class="fw-bold">{{ number_format($product->total_quantity) }} units</div>
                                        <small class="text-success">Kes {{ number_format($product->total_revenue, 0) }}</small>
                                    </div>
                                </div>
                            </div>
                        @endforeach
                    </div>
                @else
                    <div class="text-center text-muted py-4">
                        <i class="bi bi-bar-chart fs-1 d-block mb-2"></i>
                        No product data available.
                    </div>
                @endif
            </div>
        </div>

        @if($selectedUser && isset($cashierSummary[$selectedUser->id]))
            <div class="card mt-4">
                <div class="card-header bg-gradient text-white" style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);">
                    <h5 class="mb-0"><i class="bi bi-person-circle me-2"></i>{{ $selectedUser->name }} Summary</h5>
                </div>
                <div class="card-body">
                    @php $summary = $cashierSummary[$selectedUser->id]; @endphp
                    <table class="table table-sm mb-0">
                        <tr>
                            <td><i class="bi bi-cart-check me-2 text-muted"></i>Total Orders</td>
                            <td class="text-end fw-bold">{{ $summary['order_count'] }}</td>
                        </tr>
                        <tr>
                            <td><i class="bi bi-box-seam me-2 text-muted"></i>Items Sold</td>
                            <td class="text-end fw-bold">{{ number_format($summary['items_sold']) }}</td>
                        </tr>
                        <tr class="table-success">
                            <td><i class="bi bi-currency-dollar me-2"></i>Total Revenue</td>
                            <td class="text-end fw-bold text-success">{{ number_format($summary['total_revenue'], 2) }}</td>
                        </tr>
                        <tr>
                            <td><i class="bi bi-receipt me-2 text-muted"></i>Total Cost</td>
                            <td class="text-end fw-bold">{{ number_format($summary['total_cost'], 2) }}</td>
                        </tr>
                        <tr class="table-primary">
                            <td><i class="bi bi-graph-up me-2"></i>Gross Profit</td>
                            <td class="text-end fw-bold text-primary">{{ number_format($summary['total_profit'], 2) }}</td>
                        </tr>
                        <tr class="table-light">
                            <td><i class="bi bi-calculator me-2 text-muted"></i>Avg Order Value</td>
                            <td class="text-end fw-bold">{{ $summary['order_count'] > 0 ? number_format($summary['total_sales'] / $summary['order_count'], 2) : '0.00' }}</td>
                        </tr>
                        @if(($summary['credit_count'] ?? 0) > 0)
                        <tr class="table-warning bg-warning bg-opacity-10">
                            <td colspan="2"><i class="bi bi-credit-card me-2"></i><strong>Credit Sales</strong></td>
                        </tr>
                        <tr>
                            <td><i class="bi bi-receipt me-2 text-muted"></i>Credit Orders</td>
                            <td class="text-end fw-bold">{{ $summary['credit_count'] ?? 0 }}</td>
                        </tr>
                        <tr>
                            <td><i class="bi bi-currency-dollar me-2 text-warning"></i>Total Credit</td>
                            <td class="text-end fw-bold text-warning">{{ number_format($summary['credit_amount'] ?? 0, 2) }}</td>
                        </tr>
                        <tr>
                            <td><i class="bi bi-check-circle me-2 text-success"></i>Paid</td>
                            <td class="text-end fw-bold text-success">{{ number_format($summary['credit_paid'] ?? 0, 2) }}</td>
                        </tr>
                        <tr>
                            <td><i class="bi bi-hourglass-split me-2 text-warning"></i>Pending</td>
                            <td class="text-end fw-bold text-warning">{{ number_format($summary['credit_pending'] ?? 0, 2) }}</td>
                        </tr>
                        @if(($summary['credit_overdue'] ?? 0) > 0)
                        <tr class="table-danger">
                            <td><i class="bi bi-exclamation-triangle me-2"></i>Overdue</td>
                            <td class="text-end fw-bold text-danger">{{ number_format($summary['credit_overdue'] ?? 0, 2) }}</td>
                        </tr>
                        @endif
                        @endif
                    </table>
                </div>
            </div>
        @endif
    </div>
</div>

@if($creditOrdersList->count() > 0)
<div class="card mb-4">
    <div class="card-header bg-danger bg-opacity-10">
        <h5 class="mb-0"><i class="bi bi-credit-card-fill me-2 text-danger"></i>Credit Sales Records</h5>
    </div>
    <div class="card-body table-responsive">
        <table class="table table-hover table-sm">
            <thead class="table-light">
                <tr>
                    <th>Date</th>
                    <th>Invoice #</th>
                    <th>Cashier</th>
                    <th>Customer</th>
                    <th class="text-center">Status</th>
                    <th class="text-end">Total Amount</th>
                    <th class="text-end">Credit Amount</th>
                    <th class="text-end">Paid</th>
                    <th class="text-end">Balance</th>
                    <th>Due Date</th>
                </tr>
            </thead>
            <tbody>
                @foreach($creditOrdersList as $order)
                    @php
                        $creditPaid = $order->creditPayments->sum('amount');
                        $balance = ($order->credit_amount ?? 0) - $creditPaid;
                        $isOverdue = $order->credit_status === 'overdue' && $balance > 0;
                    @endphp
                    <tr class="{{ $isOverdue ? 'table-danger bg-danger bg-opacity-10' : '' }}">
                        <td>
                            <small>{{ $order->client_time ? \Carbon\Carbon::parse($order->client_time)->format('M d, Y') : $order->created_at->format('M d, Y') }}</small>
                        </td>
                        <td>
                            <a href="{{ route('orders.show', $order) }}" class="text-decoration-none">
                                <span class="badge bg-secondary">{{ $order->invoice_number }}</span>
                            </a>
                        </td>
                        <td>
                            <div class="d-flex align-items-center">
                                <div class="avatar-sm me-2" style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; width: 28px; height: 28px; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-size: 11px; font-weight: bold;">
                                    {{ substr($order->user->name, 0, 1) }}
                                </div>
                                <small>{{ $order->user->name }}</small>
                            </div>
                        </td>
                        <td>
                            <strong>{{ $order->customer->name ?? 'Walk-in' }}</strong>
                            @if($order->customer?->phone)
                                <br><small class="text-muted">{{ $order->customer->phone }}</small>
                            @endif
                        </td>
                        <td class="text-center">
                            @switch($order->credit_status)
                                @case('paid')
                                    <span class="badge bg-success">Paid</span>
                                    @break
                                @case('partial')
                                    <span class="badge bg-info">Partial</span>
                                    @break
                                @case('overdue')
                                    <span class="badge bg-danger">Overdue</span>
                                    @break
                                @default
                                    <span class="badge bg-warning text-dark">Pending</span>
                            @endswitch
                        </td>
                        <td class="text-end">{{ number_format($order->total, 2) }}</td>
                        <td class="text-end text-warning">{{ number_format($order->credit_amount ?? 0, 2) }}</td>
                        <td class="text-end text-success">{{ number_format($creditPaid, 2) }}</td>
                        <td class="text-end {{ $balance > 0 ? 'text-danger' : 'text-success' }}">
                            <strong>{{ number_format($balance, 2) }}</strong>
                        </td>
                        <td>
                            @if($order->due_date)
                                <small class="{{ $isOverdue ? 'text-danger fw-bold' : '' }}">
                                    {{ \Carbon\Carbon::parse($order->due_date)->format('M d, Y') }}
                                    @if($isOverdue)
                                        <br><span class="badge bg-danger mt-1">Overdue</span>
                                    @endif
                                </small>
                            @else
                                <span class="text-muted">-</span>
                            @endif
                        </td>
                    </tr>
                @endforeach
            </tbody>
            <tfoot class="table-light fw-bold">
                <tr>
                    <td colspan="5">TOTALS</td>
                    <td class="text-end">{{ number_format($creditOrdersList->sum('total'), 2) }}</td>
                    <td class="text-end text-warning">{{ number_format($creditOrdersList->sum('credit_amount'), 2) }}</td>
                    <td class="text-end text-success">{{ number_format($creditOrdersList->sum(function($o) { return $o->creditPayments->sum('amount'); }), 2) }}</td>
                    <td class="text-end text-danger">{{ number_format($creditOrdersList->sum('credit_amount') - $creditOrdersList->sum(function($o) { return $o->creditPayments->sum('amount'); }), 2) }}</td>
                    <td></td>
                </tr>
            </tfoot>
        </table>
        
        @if($creditOrdersList->hasPages())
            <div class="no-print mt-3">
                {{ $creditOrdersList->appends(request()->query())->links('pagination::bootstrap-4') }}
            </div>
        @endif
    </div>
</div>
@endif

<div class="print-footer mt-4">
    <div class="row text-center">
        <div class="col border p-2 mx-1 bg-light">
            <strong>Total Orders</strong><br>{{ $totalOrdersCount }}
        </div>
        <div class="col border p-2 mx-1 bg-light">
            <strong>Items Sold</strong><br>{{ number_format($totalSummary['total_items']) }}
        </div>
        <div class="col border p-2 mx-1 bg-success text-white">
            <strong>Revenue</strong><br>{{ number_format($totalSummary['total_revenue'], 2) }}
        </div>
        <div class="col border p-2 mx-1 bg-primary text-white">
            <strong>Profit</strong><br>{{ number_format($totalSummary['total_profit'], 2) }}
        </div>
    </div>
</div>
@endsection
