@extends('layouts.admin')

@section('title', 'Product Links - ' . $service->name)

@section('content')
    <div class="py-6">
        <div class="max-w-5xl mx-auto sm:px-6 lg:px-8">
            <div class="bg-white overflow-hidden shadow-sm sm:rounded-lg mb-6">
                <div class="p-6">
                    <div class="flex justify-between items-center">
                        <div>
                            <h2 class="text-2xl font-bold text-gray-900">Product Links</h2>
                            <p class="text-gray-600 mt-1">Service: <span class="font-semibold">{{ $service->name }}</span></p>
                        </div>
                        <a href="{{ route('admin.services.index') }}" class="bg-gray-200 hover:bg-gray-300 text-gray-800 font-bold py-2 px-4 rounded">Back to Services</a>
                    </div>
                </div>
            </div>

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

            <div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
                <div class="lg:col-span-1">
                    <div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
                        <div class="p-6">
                            <h3 class="text-lg font-semibold text-gray-900 mb-4">Link Product</h3>
                            <form action="{{ route('admin.services.product-links.store', $service) }}" method="POST">
                                @csrf
                                <div class="space-y-4">
                                    <div>
                                        <label class="block text-sm font-medium text-gray-700">Product</label>
                                        <select name="product_id" required class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500">
                                            <option value="">Select Product</option>
                                            @foreach($products as $product)
                                                <option value="{{ $product->id }}">{{ $product->name }}</option>
                                            @endforeach
                                        </select>
                                        @error('product_id') <p class="text-red-600 text-sm mt-1">{{ $message }}</p> @enderror
                                    </div>

                                    <div>
                                        <label class="block text-sm font-medium text-gray-700">Override Price (৳)</label>
                                        <input type="number" step="0.01" name="override_price" value="{{ old('override_price') }}" placeholder="Leave empty to use category/service default" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500">
                                        <p class="text-xs text-gray-500 mt-1">Leave empty to fall back to category/service default.</p>
                                        @error('override_price') <p class="text-red-600 text-sm mt-1">{{ $message }}</p> @enderror
                                    </div>

                                    <div class="flex items-center">
                                        <input type="checkbox" name="is_enabled" value="1" {{ old('is_enabled', true) ? 'checked' : '' }} class="h-4 w-4 text-blue-600 focus:ring-blue-500 border-gray-300 rounded">
                                        <label class="ml-2 block text-sm text-gray-900">Enabled</label>
                                    </div>

                                    <button type="submit" class="w-full bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded">Save Link</button>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>

                <div class="lg:col-span-2">
                    <div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
                        <div class="p-6">
                            <h3 class="text-lg font-semibold text-gray-900 mb-4">Existing Links</h3>
                            @if($links->isEmpty())
                                <p class="text-gray-500 text-sm">No product links yet.</p>
                            @else
                                <div class="overflow-x-auto">
                                    <table class="min-w-full divide-y divide-gray-200">
                                        <thead class="bg-gray-50">
                                            <tr>
                                                <th class="px-4 py-2 text-left text-xs font-medium text-gray-500 uppercase">Product</th>
                                                <th class="px-4 py-2 text-left text-xs font-medium text-gray-500 uppercase">Category</th>
                                                <th class="px-4 py-2 text-left text-xs font-medium text-gray-500 uppercase">Override Price</th>
                                                <th class="px-4 py-2 text-left text-xs font-medium text-gray-500 uppercase">Status</th>
                                                <th class="px-4 py-2 text-right text-xs font-medium text-gray-500 uppercase">Actions</th>
                                            </tr>
                                        </thead>
                                        <tbody class="bg-white divide-y divide-gray-200">
                                            @foreach($links as $link)
                                                <tr>
                                                    <td class="px-4 py-3 text-sm text-gray-900">{{ $link->product->name ?? 'N/A' }}</td>
                                                    <td class="px-4 py-3 text-sm text-gray-500">{{ $link->product->category->name ?? 'N/A' }}</td>
                                                    <td class="px-4 py-3 text-sm text-gray-900">
                                                        {{ $link->override_price !== null ? '৳' . number_format($link->override_price, 2) : '<span class="text-gray-400">Default</span>' }}
                                                    </td>
                                                    <td class="px-4 py-3 whitespace-nowrap">
                                                        @if($link->is_enabled)
                                                            <span class="px-2 py-1 text-xs font-semibold rounded-full bg-green-100 text-green-800">Enabled</span>
                                                        @else
                                                            <span class="px-2 py-1 text-xs font-semibold rounded-full bg-red-100 text-red-800">Disabled</span>
                                                        @endif
                                                    </td>
                                                    <td class="px-4 py-3 text-right text-sm">
                                                        <form action="{{ route('admin.services.product-links.toggle', [$service, $link]) }}" method="POST" class="inline-block mr-2">
                                                            @csrf
                                                            @method('PATCH')
                                                            <button type="submit" class="text-indigo-600 hover:text-indigo-900">
                                                                {{ $link->is_enabled ? 'Disable' : 'Enable' }}
                                                            </button>
                                                        </form>
                                                        <form action="{{ route('admin.services.product-links.destroy', [$service, $link]) }}" method="POST" class="inline-block" onsubmit="return confirm('Remove this link?');">
                                                            @csrf
                                                            @method('DELETE')
                                                            <button type="submit" class="text-red-600 hover:text-red-900">Remove</button>
                                                        </form>
                                                    </td>
                                                </tr>
                                            @endforeach
                                        </tbody>
                                    </table>
                                </div>
                                <div class="mt-4">
                                    {{ $links->links() }}
                                </div>
                            @endif
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection
