<x-app-layout>
    <x-slot name="header">
        <div class="d-flex justify-content-between align-items-center">
            <span>{{ __('app.shareholder_report') }}</span>
            <a href="{{ route('reports.shareholders.pdf') }}" class="btn btn-danger btn-sm" target="_blank">
                <i class="bi bi-file-earmark-pdf me-1"></i>{{ __('Download All PDF') }}
            </a>
        </div>
    </x-slot>

    {{-- Summary Cards --}}
    <div class="row g-3 mb-4">
        <div class="col-sm-6 col-lg-3">
            <div class="card border-0 shadow-sm text-bg-success">
                <div class="card-body text-center py-3">
                    <div class="small fw-semibold mb-1">{{ __('Total Investment') }}</div>
                    <div class="fs-5 fw-bold">{{ number_format($totals['investment'], 2) }}</div>
                    <div class="small opacity-75">{{ __('Net') }}: {{ number_format($totals['net_investment'], 2) }}</div>
                </div>
            </div>
        </div>
        <div class="col-sm-6 col-lg-3">
            <div class="card border-0 shadow-sm text-bg-danger">
                <div class="card-body text-center py-3">
                    <div class="small fw-semibold mb-1">{{ __('Total Withdrawal') }}</div>
                    <div class="fs-5 fw-bold">{{ number_format($totals['withdrawal'], 2) }}</div>
                </div>
            </div>
        </div>
        <div class="col-sm-6 col-lg-3">
            <div class="card border-0 shadow-sm text-bg-primary">
                <div class="card-body text-center py-3">
                    <div class="small fw-semibold mb-1">{{ __('Total Profit') }}</div>
                    <div class="fs-5 fw-bold">{{ number_format($totals['profit'], 2) }}</div>
                    <div class="small opacity-75">{{ __('Net') }}: {{ number_format($totals['net_profit'], 2) }}</div>
                </div>
            </div>
        </div>
        <div class="col-sm-6 col-lg-3">
            <div class="card border-0 shadow-sm text-bg-warning">
                <div class="card-body text-center py-3">
                    <div class="small fw-semibold mb-1">{{ __('Profit Withdrawn') }}</div>
                    <div class="fs-5 fw-bold">{{ number_format($totals['profit_withdrawal'], 2) }}</div>
                </div>
            </div>
        </div>
    </div>

    {{-- Per-Shareholder breakdown --}}
    @foreach($shareholders as $sh)
    <div class="card shadow-sm mb-4">
        <div class="card-header d-flex justify-content-between align-items-center">
            <span class="fw-bold"><i class="bi bi-person-workspace me-2 text-primary"></i>{{ $sh->name }}</span>
            <div class="d-flex align-items-center gap-2">
                <span class="badge {{ $sh->is_active ? 'bg-success' : 'bg-secondary' }}">
                    {{ $sh->is_active ? __('Active') : __('Inactive') }}
                </span>
                <a href="{{ route('reports.shareholders.one-pdf', $sh) }}" class="btn btn-outline-danger btn-sm" target="_blank">
                    <i class="bi bi-file-earmark-pdf"></i> PDF
                </a>
            </div>
        </div>
        <div class="card-body">
            <div class="row g-3 mb-3">
                <div class="col-6 col-md-3">
                    <div class="text-muted small">{{ __('Investment') }}</div>
                    <div class="fw-semibold text-success">{{ number_format($sh->total_investment, 2) }}</div>
                </div>
                <div class="col-6 col-md-3">
                    <div class="text-muted small">{{ __('Withdrawal') }}</div>
                    <div class="fw-semibold text-danger">{{ number_format($sh->total_withdrawal, 2) }}</div>
                </div>
                <div class="col-6 col-md-3">
                    <div class="text-muted small">{{ __('Net Investment') }}</div>
                    <div class="fw-semibold">{{ number_format($sh->net_investment, 2) }}</div>
                </div>
                <div class="col-6 col-md-3">
                    <div class="text-muted small">{{ __('Net Profit') }}</div>
                    <div class="fw-semibold text-primary">{{ number_format($sh->net_profit, 2) }}</div>
                </div>
            </div>

            @if($sh->transactions->isNotEmpty())
            <div class="table-responsive">
                <table class="table table-sm table-bordered mb-0 align-middle">
                    <thead class="table-light">
                        <tr>
                            <th>{{ __('Date') }}</th>
                            <th>{{ __('Type') }}</th>
                            <th class="text-end">{{ __('Amount') }}</th>
                            <th>{{ __('Notes') }}</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($sh->transactions as $tx)
                        <tr>
                            <td>{{ $tx->transaction_date->format('Y-m-d') }}</td>
                            <td><span class="badge text-bg-{{ $tx->type_badge }}">{{ $tx->type_label }}</span></td>
                            <td class="text-end">{{ number_format($tx->amount, 2) }}</td>
                            <td>{{ $tx->notes }}</td>
                        </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
            @else
                <p class="text-muted small mb-0">{{ __('No transactions recorded.') }}</p>
            @endif
        </div>
    </div>
    @endforeach

    @if($shareholders->isEmpty())
        <div class="alert alert-info">{{ __('No shareholders found.') }}</div>
    @endif
</x-app-layout>
