<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <title>Ingresos Efectuados a Empleados</title>
    <style>
        @page {
            margin: 20mm 15mm 22mm 15mm;
        }
        * {
            box-sizing: border-box;
        }
        body {
            font-family: 'DejaVu Sans', Arial, sans-serif;
            color: #1a1a1a;
            font-size: 10pt;
            line-height: 1.35;
            margin: 0;
            padding: 0;
        }
        .header {
            text-align: center;
            margin-bottom: 14px;
            border-bottom: 1px solid #888;
            padding-bottom: 10px;
        }
        .header .company-name {
            font-size: 14pt;
            font-weight: bold;
            letter-spacing: 0.5px;
            text-transform: uppercase;
            margin: 0 0 4px 0;
        }
        .header .subtitle {
            font-size: 11.5pt;
            font-weight: 600;
            margin: 2px 0;
        }
        .header .date-range {
            font-size: 10pt;
            color: #555;
            margin: 2px 0;
        }
        .grupo {
            margin-bottom: 18px;
            page-break-inside: avoid;
        }
        .grupo h3 {
            font-size: 11pt;
            font-weight: bold;
            background: #D9E1F2;
            color: #1f2d4a;
            padding: 6px 10px;
            margin: 0 0 4px 0;
            text-transform: uppercase;
            border-left: 4px solid #2e3a59;
        }
        table.report-table {
            width: 100%;
            border-collapse: collapse;
        }
        table.report-table thead th {
            background: #2e3a59;
            color: #fff;
            font-size: 9.5pt;
            font-weight: bold;
            padding: 6px 6px;
            text-align: left;
            border: 1px solid #2e3a59;
        }
        table.report-table thead th.right {
            text-align: right;
        }
        table.report-table tbody td {
            font-size: 9.5pt;
            padding: 4px 6px;
            border: 1px solid #d0d0d0;
            vertical-align: top;
        }
        table.report-table tbody tr:nth-child(even) td {
            background: #f7f8fa;
        }
        table.report-table td.right,
        table.report-table th.right {
            text-align: right;
            font-variant-numeric: tabular-nums;
            white-space: nowrap;
        }
        table.report-table tfoot td {
            background: #FCE4D6;
            font-weight: bold;
            font-size: 10pt;
            padding: 6px 6px;
            border: 1px solid #b0b0b0;
        }
        .bold { font-weight: bold; }
        .total-general {
            margin-top: 20px;
            background: #DAEEF3;
            border: 2px solid #2e3a59;
            padding: 10px 14px;
            font-size: 12pt;
            font-weight: bold;
            text-align: right;
        }
        .footer-generated {
            margin-top: 16px;
            font-size: 8.5pt;
            color: #777;
            font-style: italic;
        }
        .col-code   { width: 12%; }
        .col-name   { width: 63%; }
        .col-amount { width: 25%; }
    </style>
</head>
<body>

    @php
        // Resolver logo: settings.company_logo_frontend → uploads.img_path
        $logoPath = null;
        try {
            $companyId = optional($company)->id ?? auth()->user()->company_id ?? null;
            if ($companyId) {
                $logoSetting = \DB::table('settings')
                    ->where('company_id', $companyId)
                    ->where('name', 'company_logo_frontend')
                    ->value('value');
                if ($logoSetting && is_numeric($logoSetting)) {
                    $imgPath = \DB::table('uploads')->where('id', $logoSetting)->value('img_path');
                    if ($imgPath) {
                        $abs = public_path($imgPath);
                        if (file_exists($abs)) $logoPath = $abs;
                    }
                }
            }
        } catch (\Throwable $e) { /* sin logo si falla */ }

        $razonSocial = optional($company)->company_name ?: optional($company)->name ?: 'EMPRESA';
    @endphp

    <div class="header" style="display:flex;align-items:center;justify-content:space-between;gap:16px;">
        <div style="flex:0 0 auto;">
            @if ($logoPath)
                <img src="{{ $logoPath }}" alt="logo" style="max-height:60px;max-width:180px;">
            @endif
        </div>
        <div style="flex:1 1 auto;text-align:center;">
            <div class="company-name">{{ strtoupper($razonSocial) }}</div>
            <div class="subtitle">Ingresos Efectuados a Empleados</div>
            <div class="date-range">{{ $dateRange['label'] ?? '' }}</div>
        </div>
        <div style="flex:0 0 auto;width:180px;"></div>
    </div>

    @foreach ($groups as $group)
        <div class="grupo">
            <h3>{{ $group['name'] }}</h3>
            <table class="report-table">
                <thead>
                    <tr>
                        <th class="col-code">Código</th>
                        <th class="col-name">Nombre del Empleado</th>
                        <th class="col-amount right">Valor del Ingreso</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach ($group['rows'] as $r)
                        <tr>
                            <td>{{ $r['code'] }}</td>
                            <td>{{ $r['employee'] }}</td>
                            <td class="right">${{ number_format((float) $r['amount'], 2, '.', ',') }}</td>
                        </tr>
                    @endforeach
                </tbody>
                <tfoot>
                    <tr>
                        <td colspan="2" class="right bold">TOTAL {{ $group['name'] }}:</td>
                        <td class="right bold">${{ number_format((float) $group['subtotal'], 2, '.', ',') }}</td>
                    </tr>
                </tfoot>
            </table>
        </div>
    @endforeach

    <div class="total-general">
        TOTAL INGRESOS: ${{ number_format((float) $grandTotal, 2, '.', ',') }}
    </div>

    <div class="footer-generated">
        Generado: {{ now()->format('d/m/Y H:i:s') }}
    </div>

    <script type="text/php">
        if (isset($pdf)) {
            $pdf->page_text(
                $pdf->get_width() / 2 - 30,
                $pdf->get_height() - 30,
                "Pág. {PAGE_NUM} de {PAGE_COUNT}",
                null,
                9,
                array(0.4, 0.4, 0.4)
            );
        }
    </script>
</body>
</html>
