@extends('layouts.app')

@section('title', 'Delivery Rates')

@section('content')
<div class="bg-gray-50 min-h-screen py-8">
    <div class="container mx-auto px-4">
        <div class="mb-8">
            <h1 class="text-3xl font-bold text-gray-900">Delivery Rates</h1>
            <p class="text-gray-600">Customize your delivery rates for each zone</p>
        </div>

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

        <div class="bg-white rounded-xl shadow-sm p-6 mb-6">
            <div class="flex items-start gap-4 mb-6">
                <div class="w-12 h-12 bg-blue-100 rounded-full flex items-center justify-center text-2xl flex-shrink-0">🚚</div>
                <div>
                    <h3 class="font-semibold text-gray-900">How Delivery Rates Work</h3>
                    <p class="text-gray-600 text-sm mt-1">
                        You can set custom delivery rates for each zone. If you don't set a custom rate, the platform's default rates will be used.
                        Setting a "Free Delivery Threshold" means customers get free delivery when their order exceeds that amount.
                    </p>
                </div>
            </div>
        </div>

        <form action="{{ route('supplier.delivery-rates.update') }}" method="POST">
            @csrf
            <div class="bg-white rounded-xl shadow-sm overflow-hidden">
                <div class="overflow-x-auto">
                    <table class="w-full">
                        <thead class="bg-gray-50">
                            <tr>
                                <th class="px-6 py-4 text-left text-sm font-semibold text-gray-900">Zone</th>
                                <th class="px-6 py-4 text-center text-sm font-semibold text-gray-900">Distance</th>
                                <th class="px-6 py-4 text-center text-sm font-semibold text-gray-900">Default Rate</th>
                                <th class="px-6 py-4 text-center text-sm font-semibold text-gray-900">Your Base Rate</th>
                                <th class="px-6 py-4 text-center text-sm font-semibold text-gray-900">Your Rate/km</th>
                                <th class="px-6 py-4 text-center text-sm font-semibold text-gray-900">Your Rate/kg</th>
                                <th class="px-6 py-4 text-center text-sm font-semibold text-gray-900">Free Delivery Above</th>
                                <th class="px-6 py-4 text-center text-sm font-semibold text-gray-900">Active</th>
                            </tr>
                        </thead>
                        <tbody class="divide-y divide-gray-200">
                            @foreach($zones as $zone)
                                @php
                                    $rate = $supplierRates[$zone->id] ?? null;
                                @endphp
                                <tr class="hover:bg-gray-50">
                                    <td class="px-6 py-4">
                                        <input type="hidden" name="rates[{{ $loop->index }}][delivery_zone_id]" value="{{ $zone->id }}">
                                        <p class="font-medium">{{ $zone->name }}</p>
                                    </td>
                                    <td class="px-6 py-4 text-center text-gray-600">
                                        {{ $zone->min_distance_km }}-{{ $zone->max_distance_km }} km
                                    </td>
                                    <td class="px-6 py-4 text-center text-gray-500 text-sm">
                                        R{{ number_format($zone->base_rate, 2) }} + R{{ number_format($zone->rate_per_km, 2) }}/km + R{{ number_format($zone->rate_per_kg, 2) }}/kg
                                    </td>
                                    <td class="px-6 py-4">
                                        <input type="number" name="rates[{{ $loop->index }}][base_rate]" value="{{ $rate?->base_rate }}" step="0.01" min="0"
                                            class="w-24 px-2 py-1 border border-gray-300 rounded text-center text-sm focus:ring-2 focus:ring-blue-500"
                                            placeholder="Default">
                                    </td>
                                    <td class="px-6 py-4">
                                        <input type="number" name="rates[{{ $loop->index }}][rate_per_km]" value="{{ $rate?->rate_per_km }}" step="0.01" min="0"
                                            class="w-24 px-2 py-1 border border-gray-300 rounded text-center text-sm focus:ring-2 focus:ring-blue-500"
                                            placeholder="Default">
                                    </td>
                                    <td class="px-6 py-4">
                                        <input type="number" name="rates[{{ $loop->index }}][rate_per_kg]" value="{{ $rate?->rate_per_kg }}" step="0.01" min="0"
                                            class="w-24 px-2 py-1 border border-gray-300 rounded text-center text-sm focus:ring-2 focus:ring-blue-500"
                                            placeholder="Default">
                                    </td>
                                    <td class="px-6 py-4">
                                        <input type="number" name="rates[{{ $loop->index }}][free_delivery_threshold]" value="{{ $rate?->free_delivery_threshold }}" step="0.01" min="0"
                                            class="w-24 px-2 py-1 border border-gray-300 rounded text-center text-sm focus:ring-2 focus:ring-blue-500"
                                            placeholder="None">
                                    </td>
                                    <td class="px-6 py-4 text-center">
                                        <input type="checkbox" name="rates[{{ $loop->index }}][is_active]" value="1" {{ ($rate?->is_active ?? true) ? 'checked' : '' }}
                                            class="w-4 h-4 text-blue-600 border-gray-300 rounded focus:ring-blue-500">
                                    </td>
                                </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>
                <div class="px-6 py-4 border-t bg-gray-50 flex justify-end">
                    <button type="submit" class="px-6 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 transition">
                        Save Delivery Rates
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
@endsection
