<x-app-layout>
    <x-slot:heading>Edit Pond</x-slot:heading>

    <div class="mb-4 flex items-center gap-2 text-sm text-gray-500">
        <a href="{{ route('ponds.index') }}" class="hover:text-gray-700">Ponds</a>
        <svg class="h-4 w-4 text-gray-300" fill="none" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" d="M8.25 4.5l7.5 7.5-7.5 7.5"/></svg>
        <a href="{{ route('ponds.show', $pond) }}" class="hover:text-gray-700">{{ $pond->name }}</a>
        <svg class="h-4 w-4 text-gray-300" fill="none" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" d="M8.25 4.5l7.5 7.5-7.5 7.5"/></svg>
        <span class="text-gray-900">Edit</span>
    </div>

    <div class="max-w-2xl">
        <div class="rounded-xl border border-gray-100 bg-white p-6 shadow-sm">
            <form method="POST" action="{{ route('ponds.update', $pond) }}" class="space-y-5">
                @csrf
                @method('PATCH')

                <div class="grid grid-cols-1 gap-5 sm:grid-cols-2">
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1">Pond Name <span class="text-red-500">*</span></label>
                        <input type="text" name="name" value="{{ old('name', $pond->name) }}"
                               class="w-full rounded-lg border-gray-200 text-sm focus:border-primary-500 focus:ring-primary-500 @error('name') border-red-300 @enderror">
                        @error('name') <p class="mt-1 text-xs text-red-500">{{ $message }}</p> @enderror
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1">Pond Code <span class="text-red-500">*</span></label>
                        <input type="text" name="pond_code" value="{{ old('pond_code', $pond->pond_code) }}"
                               class="w-full rounded-lg border-gray-200 text-sm focus:border-primary-500 focus:ring-primary-500 @error('pond_code') border-red-300 @enderror">
                        @error('pond_code') <p class="mt-1 text-xs text-red-500">{{ $message }}</p> @enderror
                    </div>
                </div>

                <div class="grid grid-cols-1 gap-5 sm:grid-cols-2">
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1">Type</label>
                        <select name="type" class="w-full rounded-lg border-gray-200 text-sm focus:border-primary-500 focus:ring-primary-500">
                            @foreach($pondTypes as $opt)
                                <option value="{{ $opt->value }}" @selected(old('type', $pond->type) === $opt->value)>{{ $opt->label }}</option>
                            @endforeach
                        </select>
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1">Status</label>
                        <select name="status" class="w-full rounded-lg border-gray-200 text-sm focus:border-primary-500 focus:ring-primary-500">
                            @foreach(['active','fallow','treatment','harvest_ready','decommissioned'] as $s)
                                <option value="{{ $s }}" @selected(old('status', $pond->status) === $s)>{{ ucwords(str_replace('_',' ',$s)) }}</option>
                            @endforeach
                        </select>
                    </div>
                </div>

                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-2">Dimensions (metres)</label>
                    <div class="grid grid-cols-3 gap-3">
                        <div>
                            <label class="text-xs text-gray-500">Length</label>
                            <input type="number" name="length_m" value="{{ old('length_m', $pond->length_m) }}" step="0.01" min="0.1"
                                   class="mt-1 w-full rounded-lg border-gray-200 text-sm focus:border-primary-500 focus:ring-primary-500">
                        </div>
                        <div>
                            <label class="text-xs text-gray-500">Width</label>
                            <input type="number" name="width_m" value="{{ old('width_m', $pond->width_m) }}" step="0.01" min="0.1"
                                   class="mt-1 w-full rounded-lg border-gray-200 text-sm focus:border-primary-500 focus:ring-primary-500">
                        </div>
                        <div>
                            <label class="text-xs text-gray-500">Depth</label>
                            <input type="number" name="depth_m" value="{{ old('depth_m', $pond->depth_m) }}" step="0.01" min="0.1"
                                   class="mt-1 w-full rounded-lg border-gray-200 text-sm focus:border-primary-500 focus:ring-primary-500">
                        </div>
                    </div>
                </div>

                <div class="grid grid-cols-1 gap-5 sm:grid-cols-2">
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1">Water Source</label>
                        <select name="water_source" class="w-full rounded-lg border-gray-200 text-sm focus:border-primary-500 focus:ring-primary-500">
                            @foreach($waterSources as $opt)
                                <option value="{{ $opt->value }}" @selected(old('water_source', $pond->water_source) === $opt->value)>{{ $opt->label }}</option>
                            @endforeach
                        </select>
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1">Aeration Type</label>
                        <input type="text" name="aeration_type" value="{{ old('aeration_type', $pond->aeration_type) }}"
                               class="w-full rounded-lg border-gray-200 text-sm focus:border-primary-500 focus:ring-primary-500">
                    </div>
                </div>

                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">Location / Block</label>
                    <input type="text" name="location_block" value="{{ old('location_block', $pond->location_block) }}"
                           class="w-full rounded-lg border-gray-200 text-sm focus:border-primary-500 focus:ring-primary-500">
                </div>

                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">Notes</label>
                    <textarea name="notes" rows="3"
                              class="w-full rounded-lg border-gray-200 text-sm focus:border-primary-500 focus:ring-primary-500">{{ old('notes', $pond->notes) }}</textarea>
                </div>

                <div class="flex items-center justify-between border-t border-gray-100 pt-5">
                    <div class="flex items-center gap-3">
                        <button type="submit"
                                class="inline-flex items-center gap-2 rounded-lg bg-primary-600 px-5 py-2 text-sm font-semibold text-white hover:bg-primary-700">
                            Save Changes
                        </button>
                        <a href="{{ route('ponds.show', $pond) }}" class="text-sm text-gray-500 hover:text-gray-700">Cancel</a>
                    </div>
                    @if(! $pond->activeCycle)
                    <form method="POST" action="{{ route('ponds.destroy', $pond) }}"
                          onsubmit="return confirm('Archive this pond? This cannot be undone.')">
                        @csrf @method('DELETE')
                        <button type="submit" class="text-sm text-red-500 hover:text-red-700">Archive Pond</button>
                    </form>
                    @endif
                </div>
            </form>
        </div>
    </div>
</x-app-layout>
