@extends('layouts.admin', ['heading' => 'Ranks', 'subheading' => 'Rank criteria and bonuses'])

@section('content')
    <div class="admin-card p-5">
        <div class="mb-4 text-sm font-semibold text-slate-700">Create Rank (Repurchase BV based)</div>
        <form method="POST" action="{{ route('admin.ranks.store') }}" class="grid grid-cols-1 gap-3 md:grid-cols-4">
            @csrf
            <input name="name" placeholder="Rank Name" class="rounded border p-2 text-sm" required>
            <input name="priority" type="number" min="1" placeholder="Priority" class="rounded border p-2 text-sm" required>
            <input name="required_active_directs" type="number" min="0" placeholder="Required Directs" class="rounded border p-2 text-sm">
            <input name="required_weak_leg_bv" type="number" step="0.01" min="0" placeholder="Weak Leg BV (fallback)" class="rounded border p-2 text-sm">
            <input name="required_left_bv" type="number" step="0.01" min="0" placeholder="Required Left BV" class="rounded border p-2 text-sm">
            <input name="required_right_bv" type="number" step="0.01" min="0" placeholder="Required Right BV" class="rounded border p-2 text-sm">
            <input name="required_team_turnover" type="number" step="0.01" min="0" placeholder="Repurchase Team Turnover" class="rounded border p-2 text-sm">
            <input name="reward_amount" type="number" step="0.01" min="0" placeholder="Rank Bonus" class="rounded border p-2 text-sm">
            <input name="daily_capping" type="number" step="0.01" min="0" placeholder="Daily Capping" class="rounded border p-2 text-sm">
            <label class="flex items-center gap-2 text-sm">
                <input type="checkbox" name="is_active" value="1" checked> Active
            </label>
            <button class="rounded bg-emerald-600 px-4 py-2 text-sm text-white md:col-span-4">Create Rank</button>
        </form>
    </div>

    <div class="mt-6 admin-card p-5">
        <div class="overflow-x-auto">
            <table class="min-w-full text-sm">
                <thead class="bg-slate-100 text-left">
                    <tr>
                        <th class="p-3">Rank</th>
                        <th class="p-3">Criteria</th>
                        <th class="p-3">Bonus</th>
                        <th class="p-3">Capping</th>
                        <th class="p-3">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($ranks as $rank)
                        <tr class="border-t align-top">
                            <td class="p-3">
                                <div class="font-semibold">{{ $rank->name }}</div>
                                <div class="text-xs text-slate-500">Priority {{ $rank->priority }}</div>
                            </td>
                            <td class="p-3 text-xs">
                                Directs: {{ $rank->required_active_directs }}<br>
                                Left BV: {{ $rank->required_left_bv }}<br>
                                Right BV: {{ $rank->required_right_bv }}<br>
                                Weak Leg BV: {{ $rank->required_weak_leg_bv }}<br>
                                Team Turnover: {{ $rank->required_team_turnover }}
                            </td>
                            <td class="p-3">{{ number_format((float) $rank->reward_amount, 2) }}</td>
                            <td class="p-3">{{ number_format((float) $rank->daily_capping, 2) }}</td>
                            <td class="p-3">
                                <form method="POST" action="{{ route('admin.ranks.update', $rank) }}" class="grid grid-cols-1 gap-2">
                                    @csrf
                                    <input name="name" value="{{ $rank->name }}" class="rounded border p-1 text-xs">
                                    <input name="priority" type="number" min="1" value="{{ $rank->priority }}" class="rounded border p-1 text-xs">
                                    <input name="required_active_directs" type="number" min="0" value="{{ $rank->required_active_directs }}" class="rounded border p-1 text-xs">
                                    <input name="required_weak_leg_bv" type="number" step="0.01" min="0" value="{{ $rank->required_weak_leg_bv }}" class="rounded border p-1 text-xs">
                                    <input name="required_left_bv" type="number" step="0.01" min="0" value="{{ $rank->required_left_bv }}" class="rounded border p-1 text-xs">
                                    <input name="required_right_bv" type="number" step="0.01" min="0" value="{{ $rank->required_right_bv }}" class="rounded border p-1 text-xs">
                                    <input name="required_team_turnover" type="number" step="0.01" min="0" value="{{ $rank->required_team_turnover }}" class="rounded border p-1 text-xs">
                                    <input name="reward_amount" type="number" step="0.01" min="0" value="{{ $rank->reward_amount }}" class="rounded border p-1 text-xs">
                                    <input name="daily_capping" type="number" step="0.01" min="0" value="{{ $rank->daily_capping }}" class="rounded border p-1 text-xs">
                                    <label class="text-xs">
                                        <input type="checkbox" name="is_active" value="1" {{ $rank->is_active ? 'checked' : '' }}> Active
                                    </label>
                                    <div class="flex gap-2">
                                        <button class="rounded bg-slate-900 px-2 py-1 text-xs text-white">Update</button>
                                        <button formaction="{{ route('admin.ranks.delete', $rank) }}" class="rounded bg-rose-600 px-2 py-1 text-xs text-white" data-confirm="Delete this rank?">Delete</button>
                                    </div>
                                </form>
                            </td>
                        </tr>
                    @empty
                        <tr><td colspan="5" class="p-4 text-center text-slate-500">No ranks found.</td></tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>
@endsection
