@extends('layout.admin')

@section('content')

<div class="flex justify-between mb-4">
    <h1 class="text-xl font-semibold flex items-center gap-2">
        <i class="fas fa-check-circle text-blue-600"></i> Checked Gmail Sales
    </h1>
</div>

@include('admin.gmail_sales._date_filter', [
    'filterRoute' => 'admin.gmail.sales.checked',
    'selectedFromDate' => $selectedFromDate ?? null,
    'selectedToDate' => $selectedToDate ?? null,
    'exportStatus' => 'checked',
])

@if($gmailSales->count())

    <form method="POST" action="{{ route('admin.gmail.bulkAction') }}">
        @csrf
        @method('PUT') {{-- ✅ Laravel এর জন্য PUT রিকোয়েস্ট তৈরি --}}

        <!-- Bulk Action Buttons -->
        <div class="flex justify-end mb-4 gap-2">
            <button type="submit" name="action" value="rejected"
                class="bg-red-600 hover:bg-red-700 text-white px-4 py-2 rounded text-sm flex items-center gap-1">
                <i class="fas fa-times-circle"></i> Mark as Rejected
            </button>
            <button type="submit" name="action" value="completed"
                class="bg-yellow-600 hover:bg-yellow-700 text-white px-4 py-2 rounded text-sm flex items-center gap-1">
                <i class="fas fa-check-double"></i> Mark as Completed
            </button>
        </div>

        <!-- Table -->
        <div class="overflow-x-auto">
            <table class="min-w-full border border-gray-200 text-sm rounded-md overflow-hidden">
                <thead class="bg-blue-100">
                    <tr>
                        <th class="px-4 py-3 border-b">
                            <input type="checkbox" id="checkAll" class="form-checkbox text-blue-600">
                        </th>
                        <th class="px-4 py-3 border-b text-left text-blue-700 font-medium">User</th>
                        <th class="px-4 py-3 border-b text-left text-blue-700 font-medium">Gmail</th>
                        <th class="px-4 py-3 border-b text-left text-blue-700 font-medium">Password</th>
                        <th class="px-4 py-3 border-b text-left text-blue-700 font-medium">Recovery Email</th>
                        <th class="px-4 py-3 border-b text-left text-blue-700 font-medium">Rate</th>
                        <th class="px-4 py-3 border-b text-left text-blue-700 font-medium">Price</th>
                        <th class="px-4 py-3 border-b text-left text-blue-700 font-medium">Status</th>
                        <th class="px-4 py-3 border-b text-left text-blue-700 font-medium">Submitted</th>
                        <th class="px-4 py-3 border-b text-left text-blue-700 font-medium">Action</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-blue-100">
                    @foreach($gmailSales as $sale)
                        <tr class="hover:bg-blue-50">
                            <td class="px-4 py-2">
                                <input type="checkbox" name="ids[]" value="{{ $sale->id }}" class="form-checkbox text-blue-600">
                            </td>
                            <td class="px-4 py-2 text-blue-900">{{ $sale->user->name ?? 'N/A' }}</td>
                            <td class="px-4 py-2 text-blue-900">{{ $sale->gmail }}</td>
                            <td class="px-4 py-2 text-blue-900">{{ $sale->gmail_password }}</td>
                            <td class="px-4 py-2 text-blue-900">{{ $sale->recovery_email ?? 'N/A' }}</td>
                            <td class="px-4 py-2 text-blue-900">{{ $sale->rate_label }}</td>
                            <td class="px-4 py-2 text-blue-900">{{ $sale->price_label }}</td>
                            <td class="px-4 py-2">
                                <span class="px-2 py-1 rounded-full text-xs font-semibold
                                    @if($sale->status === 'pending') bg-yellow-100 text-yellow-700
                                    @elseif($sale->status === 'checked') bg-blue-100 text-blue-700
                                    @elseif($sale->status === 'rejected') bg-red-100 text-red-700
                                    @elseif($sale->status === 'completed') bg-green-100 text-green-700
                                    @endif
                                ">
                                    {{ ucfirst($sale->status) }}
                                </span>
                            </td>
                            <td class="px-4 py-2 text-blue-900">{{ $sale->created_at->diffForHumans() }}</td>
                            <td class="px-4 py-2">
                                <form action="{{ route('admin.gmail.sales.status', $sale->id) }}" method="POST" class="flex items-center gap-2">
                                    @csrf
                                    @method('PUT')
                                    <select name="status" class="text-sm border border-gray-300 rounded px-2 py-1">
                                        <option value="pending" @selected($sale->status === 'pending')>Pending</option>
                                        <option value="checked" @selected($sale->status === 'checked')>Checked</option>
                                        <option value="rejected" @selected($sale->status === 'rejected')>Rejected</option>
                                        <option value="completed" @selected($sale->status === 'completed')>Completed</option>
                                    </select>
                                    <button type="submit"
                                        class="bg-blue-600 hover:bg-blue-700 text-white px-3 py-1 rounded text-sm flex items-center gap-1">
                                        <i class="fas fa-edit"></i> Update
                                    </button>
                                </form>
                            </td>
                        </tr>
                    @endforeach
                </tbody>
            </table>
        </div>
    </form>

@else
    <div class="bg-blue-100 text-blue-800 px-4 py-3 rounded mt-4 flex items-center gap-2">
        <i class="fas fa-info-circle"></i> No Checked Gmail Sales found.
    </div>
@endif

<!-- Select All Checkbox Script -->
<script>
    const checkAll = document.getElementById('checkAll');
    if (checkAll) {
        checkAll.addEventListener('click', function () {
            document.querySelectorAll('input[name="ids[]"]').forEach(cb => cb.checked = this.checked);
        });
    }
</script>
@endsection
