@extends('layout.admin')

@section('content')
<div class="max-w-7xl mx-auto px-1 py-4">

    {{-- Filter Buttons --}}
    @php
        $statuses = [
            'pending' => 'Pending',
            'checked' => 'Checked',
            'sold' => 'Sold',
            'backed' => 'Backed',
        ];
        $currentStatus = request('status', 'pending');
    @endphp

    <div class="grid grid-cols-2 sm:grid-cols-4 gap-2 mb-4">
        @foreach ($statuses as $key => $label)
            <a href="{{ route('admin.in.accounts', ['status' => $key]) }}"
               class="text-center text-xs sm:text-sm font-semibold py-2 px-1 rounded 
                    {{ $currentStatus === $key
                        ? 'bg-indigo-600 text-white shadow'
                        : 'bg-gray-200 text-gray-800 hover:bg-indigo-500 hover:text-white transition' }}">
                {{ $label }}
            </a>
        @endforeach
    </div>
    
    
    {{-- Search Bar --}}
<form method="GET" action="{{ route('admin.in.accounts') }}" class="mb-4">
    <div class="flex flex-col sm:flex-row gap-2">
        <input type="text" name="query" value="{{ request('query') }}"
               placeholder="Search by Username or Password"
               class="w-full sm:w-1/2 px-3 py-2 border rounded shadow-sm focus:outline-none focus:ring-2 focus:ring-indigo-500 text-sm">

        <input type="hidden" name="status" value="{{ $currentStatus }}">

        <button type="submit"
                class="bg-indigo-600 hover:bg-indigo-700 text-white text-sm px-4 py-2 rounded shadow">
            Search
        </button>
    </div>
</form>
    
    

    {{-- Success Message --}}
    @if(session('success'))
        <div class="bg-green-100 text-green-800 text-center text-sm py-2 rounded mb-4">
            {{ session('success') }}
        </div>
    @endif
    
    
    
    

    {{-- Table & Action Buttons --}}
    <div class="overflow-auto rounded border shadow">
        <form action="{{ route('admin.in.accounts.update') }}" method="POST" id="accountsForm">
            @csrf

            {{-- Action Buttons --}}
            <div class="grid grid-cols-2 sm:grid-cols-4 gap-2 p-4 bg-gray-50 border-b">
                <a href="{{ route('admin.in.accounts.export', ['status' => $currentStatus]) }}"
                   class="text-center text-xs sm:text-sm font-semibold py-2 px-1 bg-indigo-600 hover:bg-indigo-700 text-white rounded shadow">
                    EXPORT TO EXCEL
                </a>

                <button type="submit" name="action" value="sold"
                        class="w-full text-xs sm:text-sm py-2 px-1 bg-green-600 hover:bg-green-700 text-white rounded font-semibold transition">
                    Mark as Sold
                </button>

                <button type="submit" name="action" value="backed"
                        class="w-full text-xs sm:text-sm py-2 px-1 bg-yellow-500 hover:bg-yellow-600 text-white rounded font-semibold transition">
                    Mark as Backed
                </button>

                <button type="submit" name="action" value="checked"
                        class="w-full text-xs sm:text-sm py-2 px-1 bg-blue-600 hover:bg-blue-700 text-white rounded font-semibold transition">
                    Mark as Checked
                </button>
            </div>

            {{-- Table --}}
            <table class="min-w-full text-sm text-left">
                <thead class="bg-gray-100">
                    <tr>
                        <th class="p-2 w-10"><input type="checkbox" id="selectAll"></th>
                        <th class="p-2">User</th>
                        <th class="p-2">Referral</th>
                        <th class="p-2">IG Username</th>
                        <th class="p-2">Password</th>
                        <th class="p-2">Temp Mail</th>
                        <th class="p-2">2FA</th>
                        <th class="p-2">Status</th>
                        <th class="p-2">Created</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($accounts as $acc)
                    <tr class="border-t hover:bg-gray-50">
                        <td class="p-2 text-center">
                            <input type="checkbox" name="ids[]" value="{{ $acc->id }}">
                        </td>
                        <td class="p-2">{{ $acc->user->name ?? 'N/A' }}</td>
                        <td class="p-2">{{ $acc->user->referral_code ?? 'N/A' }}</td>
                        <td class="p-2">{{ $acc->username }}</td>
                        <td class="p-2">{{ $acc->password }}</td>
                        <td class="p-2">{{ $acc->temp_email }}</td>
                        <td class="p-2">{{ $acc->two_fa_code }}</td>
                        <td class="p-2">
                            @php
                                $color = match($acc->status) {
                                    'sold' => 'bg-green-600',
                                    'checked' => 'bg-blue-500',
                                    'backed' => 'bg-yellow-500',
                                    default => 'bg-gray-500'
                                };
                            @endphp
                            <span class="text-white px-2 py-1 rounded {{ $color }}">{{ ucfirst($acc->status ?? 'pending') }}</span>
                        </td>
                        <td class="p-2 whitespace-nowrap">{{ $acc->created_at->diffForHumans() }}</td>
                    </tr>
                    @empty
                        <tr><td colspan="9" class="text-center py-4">❌ কোনো ডাটা পাওয়া যায়নি</td></tr>
                    @endforelse
                </tbody>
            </table>
        </form>
    </div>
</div>

<script>
document.getElementById('selectAll').addEventListener('click', function () {
    const checkboxes = document.querySelectorAll('input[name="ids[]"]');
    checkboxes.forEach(cb => cb.checked = this.checked);
});
</script>
@endsection