@extends('layouts.app')

@section('content')
<div class="max-w-7xl mx-auto px-6 py-8">
    <div class="flex justify-between items-center mb-6">
        <h2 class="text-2xl font-bold text-gray-800">Export Requests</h2>
        <a href="{{ route('export_requests.create') }}" class="bg-blue-600 text-white px-4 py-2 rounded hover:bg-blue-700 shadow-md transition">
            Create New Request
        </a>
    </div>

    @if(session('success'))
        <div class="bg-green-100 border border-green-400 text-green-700 px-4 py-3 rounded relative mb-4" role="alert">
            <span class="block sm:inline">{{ session('success') }}</span>
        </div>
    @endif
    @if(session('error'))
        <div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded relative mb-4" role="alert">
            <span class="block sm:inline">{{ session('error') }}</span>
        </div>
    @endif
    @if(session('warning'))
        <div class="bg-yellow-100 border border-yellow-400 text-yellow-700 px-4 py-3 rounded relative mb-4" role="alert">
            <span class="block sm:inline">{{ session('warning') }}</span>
        </div>
    @endif

    <div class="bg-white rounded shadow p-4">
        <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">Actions</th>
                        <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">Created By</th>
                        <th class="px-6 py-4 text-left">Contact Name</th>
                        <th class="px-6 py-4 text-left">Company Name</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">Courier</th>
                        <th class="px-6 py-4 text-left">AWB</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach($exportRequests as $request)
                    <tr class="border-b">
                        <td class="px-6 py-4">
                            <div x-data="{ 
                                    open: false, 
                                    position: { top: 0, left: 0 },
                                    toggle() {
                                        if (this.open) { this.open = false; return; }
                                        let rect = $refs.btn.getBoundingClientRect();
                                        const spaceBelow = window.innerHeight - rect.bottom;
                                        const menuHeight = 250; // Approximating menu height
                                        
                                        if (spaceBelow < menuHeight) {
                                            // Open upwards
                                            this.position = {
                                                top: (rect.top + window.scrollY - menuHeight) + 'px',
                                                left: (rect.left + window.scrollX) + 'px' 
                                            };
                                        } else {
                                            // Open downwards
                                            this.position = {
                                                top: (rect.bottom + window.scrollY) + 'px',
                                                left: (rect.left + window.scrollX) + 'px'
                                            };
                                        }
                                        this.open = true;
                                    }
                                }" 
                                @scroll.window="open = false" 
                                class="relative">
                                <button x-ref="btn" @click="toggle()" @click.away="open = false" class="text-gray-500 hover:text-gray-700 focus:outline-none">
                                    <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 5v.01M12 12v.01M12 19v.01M12 6a1 1 0 110-2 1 1 0 010 2zm0 7a1 1 0 110-2 1 1 0 010 2zm0 7a1 1 0 110-2 1 1 0 010 2z"></path>
                                    </svg>
                                </button>
                                <template x-teleport="body">
                                    <div x-show="open" 
                                         :style="`top: ${position.top}; left: ${position.left};`"
                                         class="fixed w-48 bg-white rounded-md shadow-lg z-50 border border-gray-200" 
                                         style="display: none;" 
                                         @click.outside="open = false">
                                        <a href="{{ route('export_requests.show', $request->id) }}" class="flex items-center px-4 py-2 text-sm text-gray-700 hover:bg-gray-100">
                                            <svg class="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"></path><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z"></path></svg>
                                            View
                                        </a>
                                        <a href="{{ route('export_requests.edit', $request->id) }}" class="flex items-center px-4 py-2 text-sm text-gray-700 hover:bg-gray-100">
                                            <svg class="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15.232 5.232l3.536 3.536m-2.036-5.036a2.5 2.5 0 113.536 3.536L6.5 21.036H3v-3.572L16.732 3.732z"></path></svg>
                                            Edit
                                        </a>
                                        <a href="{{ route('export_requests.clone', $request->id) }}" class="flex items-center px-4 py-2 text-sm text-gray-700 hover:bg-gray-100">
                                            <svg class="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7v8a2 2 0 002 2h6M8 7V5a2 2 0 012-2h4.586a1 1 0 01.707.293l4.414 4.414a1 1 0 01.293.707V15a2 2 0 01-2 2h-2M8 7H6a2 2 0 00-2 2v10a2 2 0 002 2h8a2 2 0 002-2v-2"></path></svg>
                                            Clone
                                        </a>
                                        @hasanyrole('ImPex|Admin')
                                        <a href="{{ route('export_request_details.edit', $request->id) }}" class="flex items-center px-4 py-2 text-sm text-gray-700 hover:bg-gray-100">
                                            <svg class="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-3 7h3m-3 4h3m-6-4h.01M9 16h.01"></path></svg>
                                            Other Details
                                        </a>
                                        @endhasanyrole
                                        @if($request->other_attachment)
                                        <a href="{{ route('export_requests.download_other_attachment', $request->id) }}" class="flex items-center px-4 py-2 text-sm text-gray-700 hover:bg-gray-100">
                                            <svg class="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M7 21h10a2 2 0 002-2V9.414a1 1 0 00-.293-.707l-5.414-5.414A1 1 0 0012.586 3H7a2 2 0 00-2 2v14a2 2 0 002 2z"></path></svg>
                                            AWB PDF
                                        </a>
                                        @endif
                                            @hasanyrole('ImPex|Admin')
                                            <a href="{{ route('export_requests.dhl_csv', $request->id) }}" class="flex items-center px-4 py-2 text-sm text-gray-700 hover:bg-gray-100">
                                                <svg class="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 10v6m0 0l-3-3m3 3l3-3m2 8H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"></path></svg>
                                                DHL CSV
                                            </a>
                                            @if($request->courier_service === 'FedEx')
                                            <a href="{{ route('export_requests.fedex_excel', $request->id) }}" class="flex items-center px-4 py-2 text-sm text-gray-700 hover:bg-gray-100">
                                                <svg class="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-4l-4 4m0 0l-4-4m4 4V4"></path></svg>
                                                FedEx Excel
                                            </a>
                                            @endif
                                            <form action="{{ route('export_requests.destroy', $request->id) }}" method="POST" class="block" onsubmit="return confirm('Are you sure you want to delete this export request?');">
                                                @csrf
                                                @method('DELETE')
                                                <button type="submit" class="w-full text-left flex items-center px-4 py-2 text-sm text-red-600 hover:bg-gray-100">
                                                    <svg class="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"></path></svg>
                                                    Delete
                                                </button>
                                            </form>
                                            @endhasanyrole
                                    </div>
                                </template>
                            </div>
                        </td>
                        <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->creator->name ?? 'N/A' }}</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->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->courier_service ?? '-' }}</td>
                        <td class="px-6 py-4">{{ $request->awb_no ?? 'N/A' }}</td>
                    </tr>
                    @endforeach
                </tbody>
            </table>
        </div>
    </div>
    <div class="mt-4">
        {{ $exportRequests->links() }}
    </div>
</div>
@endsection

@section('scripts')
<script>
    $(document).ready(function() {
        $('#exportRequestsTable').DataTable({
            paging: false, // Using Laravel pagination
            info: false
        });
    });
</script>
@endsection
