<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Customers Export</title>
    <style>
        @page {
            margin: 15mm;
        }
        body {
            font-family: 'DejaVu Sans', sans-serif;
            font-size: 10px;
            color: #333;
            line-height: 1.4;
        }
        .header {
            margin-bottom: 20px;
            border-bottom: 2px solid #333;
            padding-bottom: 10px;
        }
        .header h1 {
            margin: 0;
            font-size: 18px;
            font-weight: bold;
        }
        .header p {
            margin: 5px 0 0 0;
            font-size: 11px;
            color: #666;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 10px;
        }
        th {
            background-color: #f3f4f6;
            border: 1px solid #d1d5db;
            padding: 8px 6px;
            text-align: left;
            font-weight: bold;
            font-size: 9px;
        }
        td {
            border: 1px solid #d1d5db;
            padding: 6px;
            font-size: 9px;
        }
        tr:nth-child(even) {
            background-color: #f9fafb;
        }
        .text-right {
            text-align: right;
        }
        .text-center {
            text-align: center;
        }
        .status-active {
            color: #059669;
            font-weight: bold;
        }
        .status-inactive {
            color: #6b7280;
        }
        .balance-positive {
            color: #dc2626;
            font-weight: bold;
        }
        .footer {
            margin-top: 20px;
            padding-top: 10px;
            border-top: 1px solid #d1d5db;
            font-size: 8px;
            color: #666;
            text-align: center;
        }
    </style>
</head>
<body>
    <div class="header">
        <h1>Customers List</h1>
        <p>Generated on: {{ date('F d, Y h:i A') }}</p>
        @if(!empty($filters['search']) || !empty($filters['is_active']))
            <p>
                Filters: 
                @if(!empty($filters['search']))
                    Search: {{ $filters['search'] }}
                @endif
                @if(!empty($filters['is_active']))
                    Status: {{ $filters['is_active'] == '1' ? 'Active' : 'Inactive' }}
                @endif
            </p>
        @endif
    </div>

    <table>
        <thead>
            <tr>
                <th>Name</th>
                <th>Email</th>
                <th>Phone</th>
                <th>Company</th>
                <th>Address</th>
                <th>City</th>
                <th>State</th>
                <th>Status</th>
                <th class="text-center">Invoices</th>
                <th class="text-right">Balance</th>
            </tr>
        </thead>
        <tbody>
            @forelse($customers as $customer)
                @php
                    $balance = $customer->invoices ? $customer->invoices->sum('balance') : 0;
                    $invoiceCount = $customer->invoices ? $customer->invoices->count() : 0;
                @endphp
                <tr>
                    <td>{{ $customer->name }}</td>
                    <td>{{ $customer->email ?? 'N/A' }}</td>
                    <td>{{ $customer->phone ?? 'N/A' }}</td>
                    <td>{{ $customer->company ?? 'N/A' }}</td>
                    <td>{{ $customer->address ?? 'N/A' }}</td>
                    <td>{{ $customer->city ?? 'N/A' }}</td>
                    <td>{{ $customer->state ?? 'N/A' }}</td>
                    <td class="{{ $customer->is_active ? 'status-active' : 'status-inactive' }}">
                        {{ $customer->is_active ? 'Active' : 'Inactive' }}
                    </td>
                    <td class="text-center">{{ $invoiceCount }}</td>
                    <td class="text-right {{ $balance > 0 ? 'balance-positive' : '' }}">
                        ${{ number_format($balance, 2) }}
                    </td>
                </tr>
            @empty
                <tr>
                    <td colspan="10" class="text-center">No customers found</td>
                </tr>
            @endforelse
        </tbody>
    </table>

    <div class="footer">
        <p>Total Customers: {{ $customers->count() }}</p>
    </div>
</body>
</html>
