<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Payroll Summary Report</title>
    <style>
        @page {
            margin: 16px 18px;
        }

        body {
            font-family: DejaVu Sans, sans-serif;
            font-size: 10px;
            color: #0f172a;
            margin: 0;
            padding: 0;
        }

        .wrap {
            width: 100%;
        }

        .header {
            margin-bottom: 10px;
        }

        .title {
            font-size: 14px;
            font-weight: 700;
            color: #0f172a;
            margin: 0 0 2px 0;
        }

        .sub {
            font-size: 10px;
            color: #64748b;
            margin: 0;
        }

        .meta {
            margin-top: 6px;
            font-size: 9px;
            color: #475569;
        }

        .meta span {
            margin-right: 12px;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            table-layout: fixed;
        }

        th, td {
            border: 1px solid #cbd5e1;
            padding: 5px 6px;
            vertical-align: top;
            word-wrap: break-word;
        }

        th {
            background: #f8fafc;
            font-size: 9.5px;
            font-weight: 700;
            color: #334155;
            text-align: left;
        }

        td {
            font-size: 9.5px;
            color: #0f172a;
        }

        .text-right {
            text-align: right;
        }

        .text-center {
            text-align: center;
        }

        .employees {
            line-height: 1.35;
        }

        .status {
            display: inline-block;
            border: 1px solid #cbd5e1;
            padding: 2px 4px;
            font-size: 8.5px;
            color: #334155;
        }

        .empty {
            text-align: center;
            color: #64748b;
            padding: 10px 0;
        }

        .tfoot td {
            font-weight: 700;
            background: #fcfcfd;
        }
    </style>
</head>
<body>
    <div class="wrap">
        <div class="header">
            <h1 class="title">Payroll Summary Report</h1>
            <p class="sub">Payroll month-wise gross, net, paid & due summary</p>

            <div class="meta">
                @if(request('month'))
                    <span><strong>Month:</strong> {{ sprintf('%02d', (int) request('month')) }}</span>
                @endif

                @if(request('year'))
                    <span><strong>Year:</strong> {{ request('year') }}</span>
                @endif

                @if(request('status'))
                    <span><strong>Status:</strong> {{ ucwords(str_replace('_', ' ', request('status'))) }}</span>
                @endif
            </div>
        </div>

        <table>
            <thead>
                <tr>
                    <th style="width: 10%;">Payroll Month</th>
                    <th style="width: 26%;">Employees</th>
                    <th style="width: 9%;" class="text-right">Gross</th>
                    <th style="width: 8%;" class="text-right">OT</th>
                    <th style="width: 10%;" class="text-right">Advance</th>
                    <th style="width: 9%;" class="text-right">Net</th>
                    <th style="width: 9%;" class="text-right">Paid</th>
                    <th style="width: 9%;" class="text-right">Due</th>
                    <th style="width: 10%;">Status</th>
                </tr>
            </thead>
            <tbody>
                @php
                    $totalGross   = 0;
                    $totalOt      = 0;
                    $totalAdvance = 0;
                    $totalNet     = 0;
                    $totalPaid    = 0;
                    $totalDue     = 0;
                @endphp

                @forelse($payrolls as $payroll)
                    @php
                        $stats = $payrollItemStats[$payroll->id] ?? [
                            'gross' => 0,
                            'ot' => 0,
                            'advance' => 0,
                            'net' => 0,
                            'paid' => 0,
                            'due' => 0,
                        ];

                        $employeeNames = collect($payroll->items ?? [])
                            ->map(fn($item) => $item->employee->name ?? null)
                            ->filter()
                            ->unique()
                            ->values();

                        $totalGross   += (float) $stats['gross'];
                        $totalOt      += (float) $stats['ot'];
                        $totalAdvance += (float) $stats['advance'];
                        $totalNet     += (float) $stats['net'];
                        $totalPaid    += (float) $stats['paid'];
                        $totalDue     += (float) $stats['due'];
                    @endphp

                    <tr>
                        <td>
                            {{ sprintf('%02d', $payroll->payroll_month) }}-{{ $payroll->payroll_year }}
                        </td>

                        <td class="employees">
                            @if($employeeNames->count())
                                {{ $employeeNames->implode(', ') }}
                            @else
                                N/A
                            @endif
                        </td>

                        <td class="text-right">{{ number_format((float) $stats['gross'], 2) }}</td>
                        <td class="text-right">{{ number_format((float) $stats['ot'], 2) }}</td>
                        <td class="text-right">{{ number_format((float) $stats['advance'], 2) }}</td>
                        <td class="text-right">{{ number_format((float) $stats['net'], 2) }}</td>
                        <td class="text-right">{{ number_format((float) $stats['paid'], 2) }}</td>
                        <td class="text-right">{{ number_format((float) $stats['due'], 2) }}</td>

                        <td>
                            <span class="status">
                                {{ ucwords(str_replace('_', ' ', $payroll->status)) }}
                            </span>
                        </td>
                    </tr>
                @empty
                    <tr>
                        <td colspan="9" class="empty">No payroll found.</td>
                    </tr>
                @endforelse
            </tbody>

            @if(($payrolls->count() ?? 0) > 0)
                <tfoot class="tfoot">
                    <tr>
                        <td colspan="2">Total</td>
                        <td class="text-right">{{ number_format($totalGross, 2) }}</td>
                        <td class="text-right">{{ number_format($totalOt, 2) }}</td>
                        <td class="text-right">{{ number_format($totalAdvance, 2) }}</td>
                        <td class="text-right">{{ number_format($totalNet, 2) }}</td>
                        <td class="text-right">{{ number_format($totalPaid, 2) }}</td>
                        <td class="text-right">{{ number_format($totalDue, 2) }}</td>
                        <td></td>
                    </tr>
                </tfoot>
            @endif
        </table>
    </div>
</body>
</html>