@extends('layouts.app')

@section('content')
<div class="max-w-7xl mx-auto px-6 py-8">
    <div class="mb-6 flex justify-between items-center">
        <div>
            <h1 class="text-3xl font-extrabold text-gray-900">Requests by Submitter</h1>
            <p class="mt-2 text-gray-500">Filter export requests by the user who created them.</p>
        </div>
        <a href="{{ route('export_reports.index') }}" class="text-indigo-600 hover:text-indigo-900 font-medium">
            &larr; Back to Reports
        </a>
    </div>

    {{-- Filter Form --}}
    <div class="bg-white rounded-lg shadow p-6 mb-8">
        <form action="{{ route('export_reports.by_submitter') }}" method="GET" class="flex flex-col md:flex-row items-end gap-4">
            <div class="w-full md:w-1/2">
                <label for="user_id" class="block text-sm font-medium text-gray-700 mb-1">Select User</label>
                <select name="user_id" id="user_id" class="block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 sm:text-sm p-2 border">
                    <option value="">-- Select User --</option>
                    @foreach($users as $user)
                        <option value="{{ $user->id }}" {{ request('user_id') == $user->id ? 'selected' : '' }}>
                            {{ $user->name }} ({{ $user->email }})
                        </option>
                    @endforeach
                </select>
            </div>
            <button type="submit" class="px-4 py-2 bg-indigo-600 text-white rounded hover:bg-indigo-700 transition shadow-sm h-10">
                Generate Report
            </button>
        </form>
    </div>

    {{-- Results Table --}}
    @if(request('user_id'))
        <div class="bg-white rounded shadow p-4">
            <h2 class="text-lg font-semibold text-gray-800 mb-4">Results ({{ $exportRequests->count() }})</h2>
            
            @if($exportRequests->count() > 0)
                <div class="overflow-x-auto">
                    <table class="min-w-full border whitespace-nowrap">
                        <thead>
                            <tr class="bg-gray-100 border-b">
                                <th class="px-6 py-4 text-left">ID</th>
                                <th class="px-6 py-4 text-left">Date</th>
                                <th class="px-6 py-4 text-left">Location</th>
                                <th class="px-6 py-4 text-left">Pickup Date</th>
                                <th class="px-6 py-4 text-left">Contact Name</th>
                                <th class="px-6 py-4 text-left">Company</th>
                                <th class="px-6 py-4 text-left">Courier</th>
                                <th class="px-6 py-4 text-left">AWB</th>
                                <th class="px-6 py-4 text-left">AWB PDF</th>
                                <th class="px-6 py-4 text-left">Status/Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach($exportRequests as $request)
                            <tr class="border-b hover:bg-gray-50">
                                <td class="px-6 py-4">{{ $request->id }}</td>
                                <td class="px-6 py-4">{{ $request->created_at->format('Y-m-d / H:i') }}</td>
                                <td class="px-6 py-4">{{ $request->pickup_location ?? '-' }}</td>
                                <td class="px-6 py-4">{{ $request->pickup_date_time ? $request->pickup_date_time->format('Y-m-d H:i') : '-' }}</td>
                                <td class="px-6 py-4">{{ $request->shipToAddress->contact_name ?? 'N/A' }}</td>
                                <td class="px-6 py-4">{{ $request->shipToAddress->company_name ?? 'N/A' }}</td>
                                <td class="px-6 py-4">{{ $request->courier_service ?? '-' }}</td>
                                <td class="px-6 py-4">{{ $request->awb_no ?? 'N/A' }}</td>
                                <td class="px-6 py-4">
                                    @if($request->other_attachment)
                                        <a href="{{ route('export_requests.download_other_attachment', $request->id) }}" class="text-indigo-600 hover:text-indigo-900 underline" target="_blank">Download PDF</a>
                                    @else
                                        <span class="text-gray-400">-</span>
                                    @endif
                                </td>
                                <td class="px-6 py-4">
                                    <a href="{{ route('export_requests.show', $request->id) }}" class="text-indigo-600 hover:text-indigo-900 underline" target="_blank">View</a>
                                </td>
                            </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>
            @else
                <div class="text-center py-8 text-gray-500 bg-gray-50 rounded-lg border border-dashed border-gray-300">
                    <p>No export requests found for this user.</p>
                </div>
            @endif
        </div>
    @endif

</div>
@endsection
@section('scripts')
<script>
    $(document).ready(function() {
        $('#user_id').select2({
            placeholder: "-- Select User --",
            allowClear: true,
            width: '100%'
        });
    });
</script>
@endsection
