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

@section('content')
    <form method="GET" class="mb-4 admin-card p-4">
        <div class="grid grid-cols-1 gap-3 md:grid-cols-2 xl:grid-cols-6">
            <input name="q" value="{{ $search }}" placeholder="Search member code / mobile / email / ID" class="rounded border p-2 xl:col-span-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>
            <select name="status" class="rounded border p-2">
                <option value="all" @selected($status === 'all')>All Status</option>
                <option value="active" @selected($status === 'active')>Active</option>
                <option value="pending" @selected($status === 'pending')>Pending</option>
                <option value="blocked" @selected($status === 'blocked')>Blocked</option>
            </select>
            <select name="leg" class="rounded border p-2">
                <option value="all" @selected($leg === 'all')>All Legs</option>
                <option value="left" @selected($leg === 'left')>Left</option>
                <option value="right" @selected($leg === 'right')>Right</option>
            </select>
            <input name="depth" value="{{ $depth }}" type="number" min="1" max="10" class="rounded border p-2" placeholder="Depth 1-10" />
            <input name="node_q" value="{{ $nodeSearch }}" placeholder="Node filter (name/code/mobile)" class="rounded border p-2 md:col-span-2 xl:col-span-3" />
        </div>
        <div class="mt-3 flex flex-wrap gap-2">
            <button class="rounded bg-slate-900 px-4 py-2 text-white">Search</button>
            <a href="{{ route('admin.tree') }}" class="rounded border border-slate-300 px-4 py-2 text-slate-700">Reset</a>
            @if($member)
                <a href="{{ route('admin.tree.export', request()->query() + ['format' => 'pdf']) }}" class="rounded border border-slate-200 px-4 py-2 text-slate-700">Export PDF</a>
                <a href="{{ route('admin.tree.export', request()->query() + ['format' => 'xlsx']) }}" class="rounded border border-slate-200 px-4 py-2 text-slate-700">Export Excel</a>
                <a href="{{ route('admin.tree.export', request()->query() + ['format' => 'csv']) }}" class="rounded border border-slate-200 px-4 py-2 text-slate-700">Export CSV</a>
            @endif
        </div>
    </form>

    @if($member)
        @php
            $weakLeg = ($legTotals['left'] ?? 0) <= ($legTotals['right'] ?? 0) ? 'left' : 'right';
        @endphp
        <div class="mb-4 grid grid-cols-1 gap-3 md:grid-cols-2 xl:grid-cols-4">
            <div class="admin-card p-4">
                <div class="text-xs text-slate-500">Member</div>
                <div class="mt-1 text-base font-semibold">{{ $member->member_code }} - {{ $member->name }}</div>
                <div class="mt-2 text-xs text-slate-500">Weak Leg: <span class="font-semibold text-rose-600">{{ ucfirst($weakLeg) }}</span></div>
            </div>
            <div class="admin-card p-4">
                <div class="text-xs text-slate-500">Network Nodes (Filtered)</div>
                <div class="mt-1 text-xl font-semibold">{{ number_format($treeStats['total_nodes']) }}</div>
                <div class="mt-2 text-xs text-slate-500">Left {{ number_format($treeStats['left_nodes']) }} / Right {{ number_format($treeStats['right_nodes']) }}</div>
            </div>
            <div class="admin-card p-4">
                <div class="text-xs text-slate-500">Active Nodes (Filtered)</div>
                <div class="mt-1 text-xl font-semibold">{{ number_format($treeStats['left_active'] + $treeStats['right_active']) }}</div>
                <div class="mt-2 text-xs text-slate-500">Left {{ number_format($treeStats['left_active']) }} / Right {{ number_format($treeStats['right_active']) }}</div>
            </div>
            <div class="admin-card p-4">
                <div class="text-xs text-slate-500">Anomaly Signals</div>
                <div class="mt-1 text-sm text-slate-700">Orphans: {{ $anomalies['orphan_nodes'] }}</div>
                <div class="text-sm text-slate-700">Self Loops: {{ $anomalies['self_loops'] }}</div>
                <div class="text-sm text-slate-700">Duplicate Legs: {{ $anomalies['duplicate_leg_groups'] }}</div>
                <div class="text-sm text-slate-700">Missing Closures: {{ $anomalies['missing_closure_entries'] }}</div>
            </div>
        </div>

        <div class="mb-4 admin-card p-4">
            <div class="text-sm text-slate-600">Business & Carry Summary</div>
            <div class="mt-2 text-xs text-slate-500">
                Left BV:
                <span class="{{ $weakLeg === 'left' ? 'font-semibold text-rose-600' : 'font-semibold text-emerald-700' }}">{{ number_format($legTotals['left'], 2) }}</span>
                |
                Right BV:
                <span class="{{ $weakLeg === 'right' ? 'font-semibold text-rose-600' : 'font-semibold text-emerald-700' }}">{{ number_format($legTotals['right'], 2) }}</span>
            </div>
            <div class="mt-1 text-xs text-slate-500">
                Carry Forward: L {{ number_format((float) ($carry->repurchase_left ?? 0), 2) }} / R {{ number_format((float) ($carry->repurchase_right ?? 0), 2) }}
            </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 (Depth 1)</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 {{ $child->path_leg === $weakLeg ? 'bg-rose-50' : '' }}">
                                    <td class="p-3">{{ ucfirst($child->path_leg) }}</td>
                                    <td class="p-3">{{ $child->member_code }} - {{ $child->name }}</td>
                                    <td class="p-3">{{ ucfirst($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">Filtered by status/leg/depth with weak leg emphasis.</div>
            </div>
        </div>

        <div class="mt-4 grid grid-cols-1 gap-4 xl:grid-cols-3">
            <div class="admin-card p-4 xl:col-span-2">
                <div class="mb-3 text-sm font-semibold text-slate-700">Network 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">Depth</th>
                                <th class="p-3">Leg</th>
                                <th class="p-3">Member</th>
                                <th class="p-3">Mobile</th>
                                <th class="p-3">Status</th>
                            </tr>
                        </thead>
                        <tbody>
                            @forelse($nodes as $node)
                                <tr class="border-t {{ $node->path_leg === $weakLeg ? 'bg-rose-50/60' : '' }}">
                                    <td class="p-3">{{ $node->depth }}</td>
                                    <td class="p-3">{{ ucfirst($node->path_leg) }}</td>
                                    <td class="p-3">{{ $node->member_code }} - {{ $node->name }}</td>
                                    <td class="p-3">{{ $node->mobile ?: '-' }}</td>
                                    <td class="p-3">{{ ucfirst($node->status) }}</td>
                                </tr>
                            @empty
                                <tr><td colspan="5" class="p-4 text-center text-slate-500">No nodes found for selected filters.</td></tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>
                <div class="mt-3">
                    {{ $nodes->links() }}
                </div>
            </div>

            <div class="admin-card p-4">
                <div class="mb-3 text-sm font-semibold text-slate-700">Level Summary</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">Depth</th>
                                <th class="p-3">Total</th>
                                <th class="p-3">Active</th>
                            </tr>
                        </thead>
                        <tbody>
                            @forelse($levelSummary as $level)
                                <tr class="border-t">
                                    <td class="p-3">{{ $level->depth }}</td>
                                    <td class="p-3">{{ $level->total_nodes }}</td>
                                    <td class="p-3">{{ $level->active_nodes }}</td>
                                </tr>
                            @empty
                                <tr><td colspan="3" class="p-4 text-center text-slate-500">No level data.</td></tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    @else
        <div class="admin-card p-4 text-slate-600">Search a member to view direct left/right.</div>
    @endif
@endsection
