<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Shareholder Report</title>
    <style>
        body { font-family: DejaVu Sans, sans-serif; font-size: 11px; color: #222; margin: 24px; }
        h1 { font-size: 18px; margin-bottom: 2px; }
        .subtitle { font-size: 11px; color: #666; margin-bottom: 16px; }
        table { width: 100%; border-collapse: collapse; margin-bottom: 20px; }
        th { background: #1a56db; color: #fff; padding: 7px 8px; text-align: left; font-size: 10px; }
        td { padding: 6px 8px; border-bottom: 1px solid #e0e0e0; vertical-align: top; }
        .text-right { text-align: right; }
        .total-row td { font-weight: bold; background: #f0f4ff; border-top: 2px solid #1a56db; }
        .section-header { background: #e8f0fe; font-weight: bold; }
        .sh-name { font-weight: bold; font-size: 12px; color: #1a56db; }
        .badge-active { background: #d1fae5; color: #065f46; padding: 2px 6px; border-radius: 4px; font-size: 9px; }
        .badge-inactive { background: #e5e7eb; color: #374151; padding: 2px 6px; border-radius: 4px; font-size: 9px; }
        .tx-table { margin-top: 6px; margin-bottom: 0; }
        .tx-table th { background: #6b7280; font-size: 9px; padding: 4px 6px; }
        .tx-table td { font-size: 9px; padding: 3px 6px; border-bottom: 1px solid #f0f0f0; }
        .page-break { page-break-after: always; }
        .summary-table td { font-size: 11px; }
    </style>
</head>
<body>

    <h1>{{ __('Shareholder Report — All Shareholders') }}</h1>
    <div class="subtitle">{{ __('Generated') }}: {{ now()->format('Y-m-d H:i') }}</div>

    {{-- Summary table --}}
    <table class="summary-table">
        <thead>
            <tr>
                <th>{{ __('Shareholder') }}</th>
                <th class="text-right">{{ __('Investment') }}</th>
                <th class="text-right">{{ __('Withdrawn') }}</th>
                <th class="text-right">{{ __('Net Investment') }}</th>
                <th class="text-right">{{ __('Profit') }}</th>
                <th class="text-right">{{ __('Profit Withdrawn') }}</th>
                <th class="text-right">{{ __('Net Profit') }}</th>
            </tr>
        </thead>
        <tbody>
            @foreach($shareholders as $sh)
            <tr>
                <td>
                    <span class="sh-name">{{ $sh->name }}</span>
                    <span class="{{ $sh->is_active ? 'badge-active' : 'badge-inactive' }}">{{ $sh->is_active ? __('Active') : __('Inactive') }}</span>
                </td>
                <td class="text-right">{{ number_format($sh->total_investment, 2) }}</td>
                <td class="text-right">{{ number_format($sh->total_withdrawal, 2) }}</td>
                <td class="text-right">{{ number_format($sh->net_investment, 2) }}</td>
                <td class="text-right">{{ number_format($sh->total_profit, 2) }}</td>
                <td class="text-right">{{ number_format($sh->total_profit_withdrawal, 2) }}</td>
                <td class="text-right">{{ number_format($sh->net_profit, 2) }}</td>
            </tr>
            @endforeach
        </tbody>
        <tfoot>
            <tr class="total-row">
                <td>{{ __('TOTALS') }}</td>
                <td class="text-right">{{ number_format($totals['investment'], 2) }}</td>
                <td class="text-right">{{ number_format($totals['withdrawal'], 2) }}</td>
                <td class="text-right">{{ number_format($totals['net_investment'], 2) }}</td>
                <td class="text-right">{{ number_format($totals['profit'], 2) }}</td>
                <td class="text-right">{{ number_format($totals['profit_withdrawal'], 2) }}</td>
                <td class="text-right">{{ number_format($totals['net_profit'], 2) }}</td>
            </tr>
        </tfoot>
    </table>

    {{-- Per-shareholder transaction detail --}}
    @foreach($shareholders as $sh)
    <div style="margin-bottom: 18px;">
        <div class="section-header" style="padding: 6px 8px; margin-bottom: 4px;">
            {{ $sh->name }}
            &nbsp;<span class="{{ $sh->is_active ? 'badge-active' : 'badge-inactive' }}">{{ $sh->is_active ? __('Active') : __('Inactive') }}</span>
            &nbsp;|&nbsp;{{ __('Investment') }}: {{ number_format($sh->net_investment, 2) }}
            &nbsp;|&nbsp;{{ __('Net Profit') }}: {{ number_format($sh->net_profit, 2) }}
        </div>

        @if($sh->transactions->isNotEmpty())
        <table class="tx-table">
            <thead>
                <tr>
                    <th>{{ __('Date') }}</th>
                    <th>{{ __('Type') }}</th>
                    <th class="text-right">{{ __('Amount') }}</th>
                    <th>{{ __('Notes') }}</th>
                </tr>
            </thead>
            <tbody>
                @foreach($sh->transactions as $tx)
                <tr>
                    <td>{{ $tx->transaction_date->format('Y-m-d') }}</td>
                    <td>{{ $tx->type_label }}</td>
                    <td class="text-right">{{ number_format($tx->amount, 2) }}</td>
                    <td>{{ $tx->notes }}</td>
                </tr>
                @endforeach
            </tbody>
        </table>
        @else
            <div style="color:#999; font-size:9px; padding-left:8px;">{{ __('No transactions recorded.') }}</div>
        @endif
    </div>
    @endforeach

</body>
</html>
