@extends('layouts.admin')

@section('title', $customer->display_name)

@section('content')
<div class="fundi-page-header">
    <div>
        <h1>{{ $customer->display_name }}</h1>
        <p class="fundi-page-header__desc">{{ $customer->customer_no }} · {{ ucfirst($customer->type) }}</p>
    </div>
    <div style="display:flex;gap:0.5rem;">
        <a href="{{ route('admin.customers.edit', $customer) }}" class="fundi-btn fundi-btn--primary">Edit</a>
        <a href="{{ route('admin.customers.index') }}" class="fundi-btn fundi-btn--secondary">Back</a>
    </div>
</div>

<div class="fundi-detail-grid">
    <div class="fundi-detail-item"><label>Status</label><span><x-status-badge :status="$customer->status" /></span></div>
    <div class="fundi-detail-item"><label>KYC</label><span><x-status-badge :status="$customer->kyc_status" /></span></div>
    <div class="fundi-detail-item"><label>Phone</label><span>{{ $customer->phone }}</span></div>
    <div class="fundi-detail-item"><label>Email</label><span>{{ $customer->email ?? '—' }}</span></div>
    <div class="fundi-detail-item"><label>Branch</label><span>{{ $customer->branch->name ?? '—' }}</span></div>
    <div class="fundi-detail-item"><label>Officer</label><span>{{ $customer->relationshipOfficer->name ?? '—' }}</span></div>
    <div class="fundi-detail-item"><label>BVN</label><span>{{ $customer->bvn ?? '—' }}</span></div>
    <div class="fundi-detail-item"><label>Risk</label><span>{{ ucfirst($customer->risk_rating ?? '—') }}</span></div>
    <div class="fundi-detail-item"><label>Monthly Income</label><span>{{ $customer->monthly_income ? money($customer->monthly_income) : '—' }}</span></div>
    <div class="fundi-detail-item"><label>Portal</label><span>{{ $customer->portal_enabled ? 'Enabled' : 'Disabled' }}</span></div>
</div>

@if($customer->address)
    <p class="text-muted" style="margin-bottom:1.5rem;">{{ $customer->address }}{{ $customer->city ? ', '.$customer->city : '' }}{{ $customer->state ? ', '.$customer->state : '' }}</p>
@endif

<div class="fundi-grid-2">
    <div class="fundi-card">
        <div class="fundi-card__header"><h3>Accounts ({{ $customer->accounts->count() }})</h3></div>
        <div class="fundi-card__body">
            <div class="fundi-table-wrap">
                <table class="fundi-table">
                    <thead><tr><th>Account No.</th><th>Type</th><th class="num">Balance</th><th>Status</th></tr></thead>
                    <tbody>
                        @forelse($customer->accounts as $account)
                            <tr>
                                <td><a href="{{ route('admin.accounts.show', $account) }}">{{ $account->account_no }}</a></td>
                                <td>{{ ucfirst(str_replace('_',' ',$account->account_type)) }}</td>
                                <td class="num">{{ money($account->balance) }}</td>
                                <td><x-status-badge :status="$account->status" /></td>
                            </tr>
                        @empty
                            <tr><td colspan="4" class="fundi-empty">No accounts.</td></tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <div class="fundi-card">
        <div class="fundi-card__header"><h3>Loans ({{ $customer->loans->count() }})</h3></div>
        <div class="fundi-card__body">
            <div class="fundi-table-wrap">
                <table class="fundi-table">
                    <thead><tr><th>Loan No.</th><th>Product</th><th class="num">Outstanding</th><th>Status</th></tr></thead>
                    <tbody>
                        @forelse($customer->loans as $loan)
                            <tr>
                                <td><a href="{{ route('admin.loans.show', $loan) }}">{{ $loan->loan_no }}</a></td>
                                <td>{{ $loan->loanProduct->name ?? '—' }}</td>
                                <td class="num">{{ money($loan->outstanding_principal) }}</td>
                                <td><x-status-badge :status="$loan->status" /></td>
                            </tr>
                        @empty
                            <tr><td colspan="4" class="fundi-empty">No loans.</td></tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

@if($customer->investments->isNotEmpty())
<div class="fundi-card">
    <div class="fundi-card__header"><h3>Investments</h3></div>
    <div class="fundi-card__body">
        <div class="fundi-table-wrap">
            <table class="fundi-table">
                <thead><tr><th>Investment No.</th><th class="num">Principal</th><th>Maturity</th><th>Status</th></tr></thead>
                <tbody>
                    @foreach($customer->investments as $inv)
                        <tr>
                            <td><a href="{{ route('admin.investments.show', $inv) }}">{{ $inv->investment_no }}</a></td>
                            <td class="num">{{ money($inv->principal) }}</td>
                            <td>{{ $inv->maturity_date?->format('d M Y') ?? '—' }}</td>
                            <td><x-status-badge :status="$inv->status" /></td>
                        </tr>
                    @endforeach
                </tbody>
            </table>
        </div>
    </div>
</div>
@endif

@if($customer->notes)
    <div style="margin-top:1rem;padding:1rem;background:var(--fundi-white);border:1px solid var(--fundi-border);border-radius:6px;">
        <strong>Notes:</strong> {{ $customer->notes }}
    </div>
@endif
@endsection
