@extends('layout.admin')

@section('content')
<div class="max-w-7xl mx-auto px-2 sm:px-4 py-6">
    
    <div class="grid grid-cols-2 sm:grid-cols-4 gap-2 mb-4">
    <a href="{{ route('admin.fb.submissions.status', ['status' => 'pending']) }}" class="text-center text-xs sm:text-sm font-semibold py-2 px-1 rounded bg-gray-200 text-gray-800 hover:bg-indigo-500 hover:text-white transition">
        Pending
    </a>
    <a href="{{ route('admin.fb.submissions.status', ['status' => 'processing']) }}" class="text-center text-xs sm:text-sm font-semibold py-2 px-1 rounded bg-gray-200 text-gray-800 hover:bg-indigo-500 hover:text-white transition">
        Processing
    </a>

    <a href="{{ route('admin.fb.submissions.status', ['status' => 'rejected']) }}" class="text-center text-xs sm:text-sm font-semibold py-2 px-1 rounded  bg-indigo-600 text-white shadow">
        Rejected
    </a>
    <a href="{{ route('admin.fb.submissions.status', ['status' => 'approved']) }}" class="text-center text-xs sm:text-sm font-semibold py-2 px-1 rounded bg-gray-200 text-gray-800 hover:bg-indigo-500 hover:text-white transition">
        Approved
    </a>
</div>
    
    <div class="flex flex-col sm:flex-row justify-between items-start sm:items-center mb-4 gap-3">
        <h2 class="text-xl sm:text-2xl font-semibold text-red-600">Rejected Submissions</h2>
        <a href="{{ route('admin.fb.submissions.export', ['status' => 'rejected']) }}"
           class="bg-green-600 text-white px-4 py-2 text-sm rounded hover:bg-green-700">
            <i class="fas fa-file-excel"></i> Export to Excel
        </a>
    </div>

    <form method="GET" action="" class="mb-5">
        <div class="flex flex-col sm:flex-row items-stretch gap-3">
            <textarea name="uids" rows="2" placeholder="Enter one or more Facebook links (comma/space/new line separated)"
                class="w-full border border-gray-300 rounded px-3 py-2 text-sm resize-none focus:outline-none focus:ring-2 focus:ring-blue-500">{{ request('uids') }}</textarea>
            <button type="submit"
                class="bg-blue-600 text-white px-4 py-2 rounded text-sm hover:bg-blue-700">
                <i class="fas fa-search mr-1"></i> Search
            </button>
        </div>
    </form>

    <form method="POST" action="{{ route('admin.fb.submissions.bulk-action') }}">
        @csrf
        <div class="mb-4 flex flex-col sm:flex-row sm:items-center sm:justify-between gap-3">
            <div class="flex items-center gap-2">
                <input type="checkbox" id="select-all" class="form-checkbox scale-125">
                <label for="select-all" class="text-sm sm:text-base">Select all</label>
            </div>
            <div class="flex items-center gap-2 flex-wrap">
                <select name="action" class="border border-gray-300 rounded px-3 py-1 text-sm">
                    <option value="">-- Select action --</option>
                    <option value="pending">Mark as Pending</option>
                    <option value="approved">Approve</option>
                    <option value="processing">Mark as Processing</option>
                </select>
                <button type="submit"
                    class="bg-blue-600 text-white px-4 py-1 rounded text-sm hover:bg-blue-700">Apply</button>
            </div>
        </div>

        @if($submissions->count())
            <div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-4">
                @foreach ($submissions as $sub)
                    <div class="bg-white rounded-lg shadow p-4 relative hover:shadow-lg transition">
                        <input type="checkbox" name="selected_ids[]" value="{{ $sub->id }}" class="submission-checkbox absolute top-3 left-3 scale-125">
                        <span class="absolute top-3 right-3 bg-red-100 text-red-800 text-xs font-semibold px-2 py-1 rounded-full">Rejected</span>

                         {{-- Info --}}
                        <div class="space-y-1 text-sm mt-6 break-words">
                            <div>
                                <i class="fas fa-user mr-1 text-gray-600"></i>
                                <strong>User:</strong> {{ $sub->user->name ?? 'N/A' }} ({{ $sub->user->referral_code ?? 'N/A' }})
                            </div>
                            <div>
                                <i class="fas fa-id-badge mr-1 text-gray-600"></i>
                                <strong>Facebook Link:</strong>
                                @if(filter_var($sub->fb_uid, FILTER_VALIDATE_URL))
                                    <a href="{{ $sub->fb_uid }}" target="_blank" rel="noopener" class="text-blue-600 underline break-all">
                                        {{ Str::limit($sub->fb_uid, 45) }}
                                    </a>
                                @else
                                    <span class="break-all">{{ $sub->fb_uid }}</span>
                                @endif
                            </div>
                               {{-- Email --}}
                                <div class="flex items-start gap-1">
                                    <i class="fas fa-envelope mt-1 text-gray-600"></i>
                                    <div>
                                        <strong>Email:</strong>
                                        <span class="break-all">
                                            <span class="short-email text-gray-800">
                                                {{ Str::limit($sub->fb_email, 25) }}
                                                @if(strlen($sub->fb_email) > 25)
                                                    <button type="button" class="toggle-email text-blue-600 text-xs underline ml-1">more</button>
                                                @endif
                                            </span>
                                            <span class="long-email hidden text-gray-800">
                                                {{ $sub->fb_email }}
                                                <button type="button" class="toggle-email text-blue-600 text-xs underline ml-1">less</button>
                                            </span>
                                        </span>
                                    </div>
                                </div>
                            <div>
                                <i class="fas fa-key mr-1 text-gray-600"></i>
                                <strong>Password:</strong> {{ decrypt($sub->fb_password) }}
                            </div>
                            <div>
                                <i class="fas fa-lock mr-1 text-gray-600"></i>
                                <strong>2FA:</strong> {{ decrypt($sub->fb_2fa) }}
                            </div>
                            @if(!empty($sub->fb_cookie_link))
                                <div>
                                    <i class="fas fa-link mr-1 text-gray-600"></i>
                                    <strong>Cookie/Text:</strong> <span class="break-all">{{ $sub->fb_cookie_link }}</span>
                                </div>
                            @endif
                            <div class="text-gray-500 text-xs mt-2">
                                <i class="fas fa-calendar-alt mr-1"></i>
                                {{ \Carbon\Carbon::parse($sub->created_at)->format('d-m-Y h:i A') }}
                            </div>
                        </div>

                        {{-- Copy --}}
                        <button type="button"
                            onclick="copyToClipboard(`Facebook Link: {{ $sub->fb_uid }}\nEmail: {{ $sub->fb_email }}\nPassword: {{ decrypt($sub->fb_password) }}\n2FA: {{ decrypt($sub->fb_2fa) }}\nCookie/Text: {{ str_replace('`', '\\`', (string) $sub->fb_cookie_link) }}`)"
                            class="mt-3 inline-block bg-gray-100 hover:bg-gray-200 text-xs text-gray-800 px-3 py-1 rounded transition">
                            <i class="fas fa-copy mr-1"></i> Copy
                        </button>
                    </div>
                @endforeach
            </div>
        @else
            <p class="text-center text-gray-500 text-lg mt-10">No rejected submissions found.</p>
        @endif
    </form>
</div>

<script>
    function copyToClipboard(text) {
        navigator.clipboard.writeText(text).then(() => alert("Copied!")).catch(err => alert("Failed to copy: " + err));
    }
    document.getElementById('select-all').addEventListener('change', function () {
        document.querySelectorAll('.submission-checkbox').forEach(cb => cb.checked = this.checked);
    });
    
     //more / less
    document.addEventListener("DOMContentLoaded", function () {
        document.querySelectorAll(".toggle-email").forEach(btn => {
            btn.addEventListener("click", function () {
                let parent = this.closest(".break-all");
                parent.querySelector(".short-email").classList.toggle("hidden");
                parent.querySelector(".long-email").classList.toggle("hidden");
            });
        });
    });
</script>
@endsection


