<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="utf-8">
    <title>Rentabilidad {{ $report['from'] ?? '' }} {{ $report['to'] ?? '' }}</title>
    <style>
        body { font-family: DejaVu Sans, sans-serif; font-size: 11px; color: #1b4332; }
        h1 { font-size: 17px; margin-bottom: 4px; }
        h2 { font-size: 13px; margin-top: 18px; }
        .muted { color: #555; font-size: 10px; }
        table { width: 100%; border-collapse: collapse; margin-top: 8px; }
        th, td { border: 1px solid #ccc; padding: 4px 6px; }
        th { background: #e8f5e9; text-align: left; }
        .num { text-align: right; }
        .positive { color: #2d6a4f; }
        .negative { color: #9b2226; }
    </style>
</head>
<body>
    <h1>Rentabilidad por animal</h1>
    <p class="muted">
        @if($report['from'] && $report['to'])
            Ventas del {{ $report['from'] }} al {{ $report['to'] }} · Estatus: {{ $report['status_filter'] }}
        @else
            Estatus: {{ $report['status_filter'] }} · Costos acumulados del ciclo
        @endif
    </p>
    @if(!empty($empresa))
        <p class="muted">{{ $empresa }}</p>
    @endif

    <table>
        <tr><td>Total ingresos</td><td class="num">${{ number_format($report['totals']['income'], 2) }}</td></tr>
        <tr><td>Total costos</td><td class="num">${{ number_format($report['totals']['total_cost'], 2) }}</td></tr>
        <tr>
            <td>Utilidad neta</td>
            <td class="num {{ $report['totals']['net_profit'] >= 0 ? 'positive' : 'negative' }}">
                ${{ number_format($report['totals']['net_profit'], 2) }}
            </td>
        </tr>
        <tr><td>Costo / kg ganado (global)</td><td class="num">{{ $report['totals']['cost_per_kg_gained'] !== null ? '$'.number_format($report['totals']['cost_per_kg_gained'], 2) : '—' }}</td></tr>
        <tr><td>Utilidad / kg ganado (global)</td><td class="num">{{ $report['totals']['profit_per_kg_gained'] !== null ? '$'.number_format($report['totals']['profit_per_kg_gained'], 2) : '—' }}</td></tr>
    </table>

    @if(!empty($report['animals']))
        <h2>Por animal</h2>
        <table>
            <thead>
                <tr>
                    <th>Arete</th>
                    <th>Venta</th>
                    <th class="num">Kg +</th>
                    <th class="num">Ingreso</th>
                    <th class="num">Costos</th>
                    <th class="num">Utilidad</th>
                    <th class="num">$/kg</th>
                </tr>
            </thead>
            <tbody>
                @foreach($report['animals'] as $row)
                    <tr>
                        <td>{{ $row['ear_tag'] }}</td>
                        <td>{{ $row['sale_date'] ?? '—' }}</td>
                        <td class="num">{{ $row['weight_gain_kg'] ?? '—' }}</td>
                        <td class="num">${{ number_format($row['income'], 2) }}</td>
                        <td class="num">${{ number_format($row['total_cost'], 2) }}</td>
                        <td class="num {{ $row['net_profit'] >= 0 ? 'positive' : 'negative' }}">${{ number_format($row['net_profit'], 2) }}</td>
                        <td class="num">{{ isset($row['profit_per_kg_gained']) ? '$'.number_format($row['profit_per_kg_gained'], 2) : '—' }}</td>
                    </tr>
                @endforeach
            </tbody>
        </table>
    @endif
</body>
</html>
