<x-app-layout>
    <x-slot name="header">{{ __('Shareholders') }}</x-slot>

    <div class="d-flex justify-content-between align-items-center mb-3">
        <div></div>
        <a href="{{ route('shareholders.create') }}" class="btn btn-primary btn-sm">
            <i class="bi bi-plus-circle me-1"></i>{{ __('Add Shareholder') }}
        </a>
    </div>

    {{-- Summary Cards --}}
    @php
        $totalInvestment       = $shareholders->sum('total_investment');
        $totalWithdrawal       = $shareholders->sum('total_withdrawal');
        $totalProfit           = $shareholders->sum('total_profit');
        $totalProfitWithdrawal = $shareholders->sum('total_profit_withdrawal');
    @endphp
    <div class="row g-3 mb-4">
        <div class="col-6 col-md-3">
            <div class="card border-0 shadow-sm text-bg-success">
                <div class="card-body py-3">
                    <div class="small fw-semibold mb-1"><i class="bi bi-arrow-down-circle me-1"></i>{{ __('Total Investment') }}</div>
                    <h5 class="mb-0">{{ number_format($totalInvestment, 2) }}</h5>
                </div>
            </div>
        </div>
        <div class="col-6 col-md-3">
            <div class="card border-0 shadow-sm text-bg-danger">
                <div class="card-body py-3">
                    <div class="small fw-semibold mb-1"><i class="bi bi-arrow-up-circle me-1"></i>{{ __('Total Withdrawal') }}</div>
                    <h5 class="mb-0">{{ number_format($totalWithdrawal, 2) }}</h5>
                </div>
            </div>
        </div>
        <div class="col-6 col-md-3">
            <div class="card border-0 shadow-sm text-bg-primary">
                <div class="card-body py-3">
                    <div class="small fw-semibold mb-1"><i class="bi bi-graph-up me-1"></i>{{ __('Total Profit') }}</div>
                    <h5 class="mb-0">{{ number_format($totalProfit, 2) }}</h5>
                </div>
            </div>
        </div>
        <div class="col-6 col-md-3">
            <div class="card border-0 shadow-sm text-bg-warning">
                <div class="card-body py-3">
                    <div class="small fw-semibold mb-1"><i class="bi bi-cash-coin me-1"></i>{{ __('Profit Withdrawn') }}</div>
                    <h5 class="mb-0">{{ number_format($totalProfitWithdrawal, 2) }}</h5>
                </div>
            </div>
        </div>
    </div>

    <div class="card shadow-sm border-0">
        <div class="table-responsive">
            <table class="table table-hover align-middle mb-0">
                <thead class="table-light">
                    <tr>
                        <th>{{ __('#') }}</th>
                        <th>{{ __('Name') }}</th>
                        <th>{{ __('Phone') }}</th>
                        <th class="text-end">{{ __('Investment') }}</th>
                        <th class="text-end">{{ __('Withdrawn') }}</th>
                        <th class="text-end">{{ __('Net Investment') }}</th>
                        <th class="text-end">{{ __('Profit') }}</th>
                        <th class="text-end">{{ __('Profit W/D') }}</th>
                        <th class="text-end">{{ __('Net Profit') }}</th>
                        <th>{{ __('Status') }}</th>
                        <th>{{ __('Actions') }}</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($shareholders as $s)
                        <tr>
                            <td>{{ $loop->iteration }}</td>
                            <td class="fw-semibold">{{ $s->name }}</td>
                            <td>{{ $s->phone ?? '—' }}</td>
                            <td class="text-end text-success">{{ number_format($s->total_investment, 2) }}</td>
                            <td class="text-end text-danger">{{ number_format($s->total_withdrawal, 2) }}</td>
                            <td class="text-end fw-semibold">{{ number_format($s->net_investment, 2) }}</td>
                            <td class="text-end text-primary">{{ number_format($s->total_profit, 2) }}</td>
                            <td class="text-end text-warning">{{ number_format($s->total_profit_withdrawal, 2) }}</td>
                            <td class="text-end fw-semibold">{{ number_format($s->net_profit, 2) }}</td>
                            <td>
                                @if($s->is_active)
                                    <span class="badge bg-success">{{ __('Active') }}</span>
                                @else
                                    <span class="badge bg-secondary">{{ __('Inactive') }}</span>
                                @endif
                            </td>
                            <td>
                                <a href="{{ route('shareholders.show', $s) }}" class="btn btn-sm btn-primary me-1">
                                    <i class="bi bi-journal-text me-1"></i>{{ __('Transactions') }}
                                </a>
                                <a href="{{ route('shareholders.edit', $s) }}" class="btn btn-sm btn-outline-secondary me-1" title="Edit">
                                    <i class="bi bi-pencil"></i>
                                </a>
                                <form method="POST" action="{{ route('shareholders.destroy', $s) }}" class="d-inline" onsubmit="return confirm('{{ __('Delete this shareholder?') }}')">
                                    @csrf @method('DELETE')
                                    <button class="btn btn-sm btn-outline-danger" title="Delete"><i class="bi bi-trash"></i></button>
                                </form>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="11" class="text-center text-muted py-4">{{ __('No shareholders yet.') }}</td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>
</x-app-layout>
