@extends('layouts.admin', ['heading' => 'Tree Management', 'subheading' => 'Placement and sponsor tree view'])

@section('content')
    <form method="GET" class="mb-4 flex flex-wrap gap-2">
        <input name="q" value="{{ $search }}" placeholder="Search member code / mobile / email / ID" class="w-full rounded border p-2" />
        <select name="mode" class="rounded border p-2">
            <option value="placement" @selected($mode === 'placement')>Placement Tree</option>
            <option value="sponsor" @selected($mode === 'sponsor')>Sponsor Tree</option>
        </select>
        <button class="rounded bg-slate-900 px-4 py-2 text-white">Search</button>
        @if($member)
            <a href="{{ route('admin.tree.export', ['q' => $member->member_code, 'mode' => $mode]) }}" class="rounded border border-slate-200 px-4 py-2 text-slate-700">Export PDF</a>
        @endif
    </form>

    @if($member)
        @php
            $weakLeg = ($legTotals['left'] ?? 0) <= ($legTotals['right'] ?? 0) ? 'left' : 'right';
        @endphp
        <div class="mb-4 admin-card p-4">
            <div class="text-sm text-slate-600">Member</div>
            <div class="text-lg font-semibold">{{ $member->member_code }} - {{ $member->name }}</div>
            <div class="mt-2 text-xs text-slate-500">Left BV: {{ number_format($legTotals['left'], 2) }} | Right BV: {{ number_format($legTotals['right'], 2) }}</div>
            <div class="mt-1 text-xs text-slate-500">Carry: L {{ number_format((float) ($carry->repurchase_left ?? 0), 2) }} / R {{ number_format((float) ($carry->repurchase_right ?? 0), 2) }}</div>
            <div class="mt-1 text-xs text-slate-500">Weak Leg: {{ ucfirst($weakLeg) }}</div>
        </div>

        <div class="grid grid-cols-1 gap-4 lg:grid-cols-2">
            <div class="admin-card p-4">
                <div class="mb-3 text-sm font-semibold text-slate-700">Direct Nodes</div>
                <div class="overflow-x-auto">
                    <table class="min-w-full text-sm">
            <thead class="bg-slate-100 text-left">
                <tr>
                    <th class="p-3">Leg</th>
                    <th class="p-3">Member</th>
                    <th class="p-3">Status</th>
                </tr>
            </thead>
            <tbody>
                @forelse($directs as $child)
                    <tr class="border-t">
                        <td class="p-3">{{ $child->path_leg }}</td>
                        <td class="p-3">{{ $child->member_code }} - {{ $child->name }}</td>
                        <td class="p-3">{{ $child->status }}</td>
                    </tr>
                @empty
                    <tr><td colspan="3" class="p-4 text-center text-slate-500">No direct members found.</td></tr>
                @endforelse
            </tbody>
                    </table>
                </div>
            </div>

            <div class="admin-card p-4">
                <div class="mb-3 text-sm font-semibold text-slate-700">Quick Visual</div>
                <div class="grid grid-cols-1 gap-4 text-center md:grid-cols-3">
                    <div class="rounded-2xl border border-slate-200 p-4">
                        <div class="text-xs text-slate-500">Root</div>
                        <div class="mt-2 font-semibold">{{ $member->member_code }}</div>
                    </div>
                    <div class="rounded-2xl border border-slate-200 p-4">
                        <div class="text-xs text-slate-500">Left</div>
                        <div class="mt-2 font-semibold">{{ optional($directs->firstWhere('path_leg', 'left'))->member_code ?? 'Empty' }}</div>
                    </div>
                    <div class="rounded-2xl border border-slate-200 p-4">
                        <div class="text-xs text-slate-500">Right</div>
                        <div class="mt-2 font-semibold">{{ optional($directs->firstWhere('path_leg', 'right'))->member_code ?? 'Empty' }}</div>
                    </div>
                </div>
                <div class="mt-3 text-xs text-slate-500">Use search to move deeper into the tree.</div>
            </div>
        </div>
    @else
        <div class="admin-card p-4 text-slate-600">Search a member to view direct left/right.</div>
    @endif
@endsection
