@extends('layouts.admin')

@section('content')
    <div class="container mx-auto px-4 py-6">
        <div class="flex justify-between items-center mb-6">
            <h1 class="text-2xl font-bold">Delivery Information</h1>
            <a href="{{ route('admin.delivery-info.create') }}" class="bg-blue-600 text-white px-4 py-2 rounded-lg hover:bg-blue-700">
                Add New
            </a>
        </div>

        @if(session('success'))
            <div class="bg-green-100 border border-green-400 text-green-700 px-4 py-3 rounded mb-4">
                {{ session('success') }}
            </div>
        @endif

        @forelse($deliveryInfos as $info)
            <!-- Mobile Card View -->
            <div class="lg:hidden bg-white rounded-lg shadow mb-4 p-4">
                <div class="flex items-start justify-between mb-3">
                    <div class="flex items-center space-x-3">
                        <span class="text-3xl">{{ $info->icon }}</span>
                        <div>
                            <h3 class="font-semibold text-gray-900">{{ $info->title }}</h3>
                            <div class="flex items-center space-x-2 mt-1">
                                @if($info->is_global)
                                    <span class="px-2 py-0.5 text-xs rounded-full bg-blue-100 text-blue-800">Global</span>
                                @else
                                    <span class="px-2 py-0.5 text-xs rounded-full bg-purple-100 text-purple-800">Product</span>
                                @endif
                                <span class="text-xs text-gray-500">Order: {{ $info->order }}</span>
                            </div>
                        </div>
                    </div>
                    <form action="{{ route('admin.delivery-info.toggle-status', $info) }}" method="POST">
                        @csrf
                        @method('PATCH')
                        <button type="submit" class="px-2 py-1 text-xs rounded-full {{ $info->is_active ? 'bg-green-100 text-green-800' : 'bg-red-100 text-red-800' }}">
                            {{ $info->is_active ? 'Active' : 'Inactive' }}
                        </button>
                    </form>
                </div>

                <div class="mb-3">
                    <p class="text-sm text-gray-600 mb-1">
                        <span class="font-medium">Product:</span> {{ $info->product ? $info->product->name : 'All Products' }}
                    </p>
                    <div class="text-sm text-gray-600">
                        <span class="font-medium">Items:</span>
                        <ul class="list-disc list-inside mt-1 space-y-1">
                            @foreach($info->items as $item)
                                <li>{{ $item }}</li>
                            @endforeach
                        </ul>
                    </div>
                </div>

                <div class="flex justify-end space-x-3 pt-3 border-t">
                    <a href="{{ route('admin.delivery-info.edit', $info) }}" class="text-blue-600 hover:text-blue-900 text-sm font-medium">Edit</a>
                    <form action="{{ route('admin.delivery-info.destroy', $info) }}" method="POST" class="inline">
                        @csrf
                        @method('DELETE')
                        <button type="submit" class="text-red-600 hover:text-red-900 text-sm font-medium" onclick="return confirm('Are you sure?')">Delete</button>
                    </form>
                </div>
            </div>
        @empty
            <div class="lg:hidden bg-white rounded-lg shadow p-6 text-center text-gray-500">
                No delivery information found. <a href="{{ route('admin.delivery-info.create') }}" class="text-blue-600 hover:underline">Add one now</a>
            </div>
        @endforelse

        <!-- Desktop Table View -->
        <div class="hidden lg:block bg-white rounded-lg shadow overflow-hidden">
            <div class="overflow-x-auto">
                <table class="min-w-full divide-y divide-gray-200">
                    <thead class="bg-gray-50">
                        <tr>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Icon</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Title</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Type</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Product</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Items</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Status</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Order</th>
                            <th class="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase">Actions</th>
                        </tr>
                    </thead>
                    <tbody class="bg-white divide-y divide-gray-200">
                        @forelse($deliveryInfos as $info)
                            <tr>
                                <td class="px-6 py-4 whitespace-nowrap text-2xl">
                                    {{ $info->icon }}
                                </td>
                                <td class="px-6 py-4 whitespace-nowrap">
                                    <div class="text-sm font-medium text-gray-900">{{ $info->title }}</div>
                                </td>
                                <td class="px-6 py-4 whitespace-nowrap">
                                    @if($info->is_global)
                                        <span class="px-2 py-1 text-xs rounded-full bg-blue-100 text-blue-800">Global</span>
                                    @else
                                        <span class="px-2 py-1 text-xs rounded-full bg-purple-100 text-purple-800">Product</span>
                                    @endif
                                </td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                                    {{ $info->product ? $info->product->name : 'All Products' }}
                                </td>
                                <td class="px-6 py-4 text-sm text-gray-500">
                                    <div class="max-w-xs">
                                        <ul class="list-disc list-inside">
                                            @foreach($info->items as $item)
                                                <li class="truncate">{{ $item }}</li>
                                            @endforeach
                                        </ul>
                                    </div>
                                </td>
                                <td class="px-6 py-4 whitespace-nowrap">
                                    <form action="{{ route('admin.delivery-info.toggle-status', $info) }}" method="POST">
                                        @csrf
                                        @method('PATCH')
                                        <button type="submit" class="px-2 py-1 text-xs rounded-full {{ $info->is_active ? 'bg-green-100 text-green-800' : 'bg-red-100 text-red-800' }}">
                                            {{ $info->is_active ? 'Active' : 'Inactive' }}
                                        </button>
                                    </form>
                                </td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                                    {{ $info->order }}
                                </td>
                                <td class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium">
                                    <a href="{{ route('admin.delivery-info.edit', $info) }}" class="text-blue-600 hover:text-blue-900 mr-3">Edit</a>
                                    <form action="{{ route('admin.delivery-info.destroy', $info) }}" method="POST" class="inline">
                                        @csrf
                                        @method('DELETE')
                                        <button type="submit" class="text-red-600 hover:text-red-900" onclick="return confirm('Are you sure?')">Delete</button>
                                    </form>
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="8" class="px-6 py-4 text-center text-gray-500">
                                    No delivery information found. <a href="{{ route('admin.delivery-info.create') }}" class="text-blue-600 hover:underline">Add one now</a>
                                </td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>

        <div class="mt-6">
            {{ $deliveryInfos->links() }}
        </div>
    </div>
@endsection
