<x-app-layout>

<x-module-hero
    title="Edit Partner: {{ $partner->organization_name }}"
    description="{{ $partner->organization_name }}"
    icon="fa-edit">

    <x-slot name="backButton">
        <a href="{{ route('partners.show', $partner) }}"
           class="inline-flex items-center gap-1 text-sm text-[#475569] hover:text-[#1e293b] transition-colors">
            <i class="fas fa-arrow-left text-xs"></i> Back to Partner
        </a>
    </x-slot>

    <x-slot name="tabs">
        @can('view partners')
        <a href="{{ route('partners.index') }}"
           class="module-hero-tab {{ request()->routeIs('partners.index') && !request()->routeIs('partners.forum.*') ? 'border-b-2 border-[#1e293b]' : 'opacity-70 hover:opacity-100 transition-opacity' }}
                  inline-flex items-center gap-2 px-4 py-3 text-[#1e293b] text-sm font-medium
                  md:w-auto w-full border-b border-[#cbd5e1] md:border-b-0">
            <i class="fas fa-list text-sm"></i> All Partners
        </a>
        @endcan
        @can('create partners')
        <a href="{{ route('partners.create') }}"
           class="module-hero-tab {{ request()->routeIs('partners.create') ? 'border-b-2 border-[#1e293b]' : 'opacity-70 hover:opacity-100 transition-opacity' }}
                  inline-flex items-center gap-2 px-4 py-3 text-[#1e293b] text-sm font-medium
                  md:w-auto w-full border-b border-[#cbd5e1] md:border-b-0">
            <i class="fas fa-plus text-sm"></i> Add Partner
        </a>
        @endcan
        @can('view forum')
        <a href="{{ route('partners.forum.index') }}"
           class="module-hero-tab {{ request()->routeIs('partners.forum.*') ? 'border-b-2 border-[#1e293b]' : 'opacity-70 hover:opacity-100 transition-opacity' }}
                  inline-flex items-center gap-2 px-4 py-3 text-[#1e293b] text-sm font-medium
                  md:w-auto w-full border-b border-[#cbd5e1] md:border-b-0">
            <i class="fas fa-comments text-sm"></i> Forum
        </a>
        @endcan
    </x-slot>

</x-module-hero>

<div class="p-6">
    @if(session('error'))
        <div class="mb-4 p-4 bg-red-100 border border-red-400 text-red-700 rounded-lg flex items-center gap-2">
            <i class="fas fa-exclamation-circle"></i> {{ session('error') }}
        </div>
    @endif

    <form action="{{ route('partners.update', $partner) }}" method="POST" enctype="multipart/form-data">
        @csrf
        @method('PUT')

        <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-6 mb-6">
            <h3 class="text-base font-semibold text-gray-800 border-b border-gray-200 pb-3 mb-5 flex items-center gap-2">
                <i class="fas fa-building text-blue-600"></i> Organization Information
            </h3>
            <div class="grid grid-cols-1 md:grid-cols-2 gap-5">
                <div class="md:col-span-2">
                    <label for="organization_name" class="block text-sm font-medium text-gray-700 mb-1">
                        Organization Name <span class="text-red-500">*</span>
                    </label>
                    <input type="text" name="organization_name" id="organization_name"
                           value="{{ old('organization_name', $partner->organization_name) }}" required
                           class="w-full px-4 py-2.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent @error('organization_name') border-red-500 @enderror">
                    @error('organization_name')<p class="mt-1 text-xs text-red-600">{{ $message }}</p>@enderror
                </div>
                <div class="md:col-span-2">
                    <label for="legal_name" class="block text-sm font-medium text-gray-700 mb-1">Legal Name (if different)</label>
                    <input type="text" name="legal_name" id="legal_name"
                           value="{{ old('legal_name', $partner->legal_name) }}"
                           class="w-full px-4 py-2.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent">
                </div>
                <div>
                    <label for="organization_type" class="block text-sm font-medium text-gray-700 mb-1">
                        Organization Type <span class="text-red-500">*</span>
                    </label>
                    <select name="organization_type" id="organization_type" required
                            class="w-full px-4 py-2.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent @error('organization_type') border-red-500 @enderror">
                        <option value="">Select type...</option>
                        @foreach(['government'=>'Government Agency','ngo'=>'Non-Governmental Organization','private_sector'=>'Private Sector','academic'=>'Academic Institution','healthcare'=>'Healthcare Organization','international'=>'International Organization','community_based'=>'Community-Based Organization','faith_based'=>'Faith-Based Organization','other'=>'Other'] as $val => $label)
                            <option value="{{ $val }}" {{ old('organization_type', $partner->organization_type) == $val ? 'selected' : '' }}>{{ $label }}</option>
                        @endforeach
                    </select>
                    @error('organization_type')<p class="mt-1 text-xs text-red-600">{{ $message }}</p>@enderror
                </div>
                <div>
                    <label for="category" class="block text-sm font-medium text-gray-700 mb-1">Category</label>
                    <select name="category" id="category"
                            class="w-full px-4 py-2.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent">
                        <option value="">Select category...</option>
                        @foreach(['health'=>'Health','education'=>'Education','research'=>'Research','development'=>'Development','advocacy'=>'Advocacy','technology'=>'Technology','other'=>'Other'] as $val => $label)
                            <option value="{{ $val }}" {{ old('category', $partner->category) == $val ? 'selected' : '' }}>{{ $label }}</option>
                        @endforeach
                    </select>
                </div>
                <div class="md:col-span-2">
                    <label for="description" class="block text-sm font-medium text-gray-700 mb-1">
                        Description <span class="text-red-500">*</span>
                    </label>
                    <textarea name="description" id="description" rows="4" required
                              class="w-full px-4 py-2.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent @error('description') border-red-500 @enderror">{{ old('description', $partner->description) }}</textarea>
                    @error('description')<p class="mt-1 text-xs text-red-600">{{ $message }}</p>@enderror
                </div>
                <div class="md:col-span-2">
                    <label for="mission_statement" class="block text-sm font-medium text-gray-700 mb-1">Mission Statement</label>
                    <textarea name="mission_statement" id="mission_statement" rows="2"
                              class="w-full px-4 py-2.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent">{{ old('mission_statement', $partner->mission_statement) }}</textarea>
                </div>
                <div class="md:col-span-2">
                    <label for="services_offered" class="block text-sm font-medium text-gray-700 mb-1">
                        Services Offered <span class="text-gray-400 font-normal text-xs">(comma-separated)</span>
                    </label>
                    <input type="text" name="services_offered" id="services_offered"
                           value="{{ old('services_offered', is_array($partner->services_offered) ? implode(', ', $partner->services_offered) : '') }}"
                           placeholder="Training, Consulting, Research..."
                           class="w-full px-4 py-2.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent">
                </div>
            </div>
        </div>

        <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-6 mb-6">
            <h3 class="text-base font-semibold text-gray-800 border-b border-gray-200 pb-3 mb-5 flex items-center gap-2">
                <i class="fas fa-address-card text-blue-600"></i> Contact Information
            </h3>
            <div class="grid grid-cols-1 md:grid-cols-2 gap-5">
                <div>
                    <label for="email" class="block text-sm font-medium text-gray-700 mb-1">
                        Email <span class="text-red-500">*</span>
                    </label>
                    <input type="email" name="email" id="email"
                           value="{{ old('email', $partner->email) }}" required
                           class="w-full px-4 py-2.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent @error('email') border-red-500 @enderror">
                    @error('email')<p class="mt-1 text-xs text-red-600">{{ $message }}</p>@enderror
                </div>
                <div>
                    <label for="phone" class="block text-sm font-medium text-gray-700 mb-1">Phone Number</label>
                    <input type="tel" name="phone" id="phone" value="{{ old('phone', $partner->phone) }}"
                           class="w-full px-4 py-2.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent">
                </div>
                <div>
                    <label for="website" class="block text-sm font-medium text-gray-700 mb-1">Website</label>
                    <input type="url" name="website" id="website"
                           value="{{ old('website', $partner->website) }}" placeholder="https://"
                           class="w-full px-4 py-2.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent">
                </div>
                <div>
                    <label for="country" class="block text-sm font-medium text-gray-700 mb-1">Country</label>
                    <input type="text" name="country" id="country" value="{{ old('country', $partner->country) }}"
                           class="w-full px-4 py-2.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent">
                </div>
                <div>
                    <label for="address_line_1" class="block text-sm font-medium text-gray-700 mb-1">Address Line 1</label>
                    <input type="text" name="address_line_1" id="address_line_1"
                           value="{{ old('address_line_1', $partner->address_line_1) }}"
                           class="w-full px-4 py-2.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent">
                </div>
                <div>
                    <label for="city" class="block text-sm font-medium text-gray-700 mb-1">City</label>
                    <input type="text" name="city" id="city" value="{{ old('city', $partner->city) }}"
                           class="w-full px-4 py-2.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent">
                </div>
            </div>
        </div>

        <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-6 mb-6">
            <h3 class="text-base font-semibold text-gray-800 border-b border-gray-200 pb-3 mb-5 flex items-center gap-2">
                <i class="fas fa-file-contract text-blue-600"></i> Partnership Details
            </h3>
            <div class="grid grid-cols-1 md:grid-cols-2 gap-5">
                <div>
                    <label for="partnership_status" class="block text-sm font-medium text-gray-700 mb-1">Partnership Status</label>
                    <select name="partnership_status" id="partnership_status"
                            class="w-full px-4 py-2.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent">
                        @foreach(['pending'=>'Pending','active'=>'Active','inactive'=>'Inactive','suspended'=>'Suspended'] as $val => $label)
                            <option value="{{ $val }}" {{ old('partnership_status', $partner->partnership_status) == $val ? 'selected' : '' }}>{{ $label }}</option>
                        @endforeach
                    </select>
                </div>
                <div>
                    <label for="partnership_level" class="block text-sm font-medium text-gray-700 mb-1">Partnership Level</label>
                    <select name="partnership_level" id="partnership_level"
                            class="w-full px-4 py-2.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent">
                        @foreach(['bronze'=>'Bronze','silver'=>'Silver','gold'=>'Gold','platinum'=>'Platinum'] as $val => $label)
                            <option value="{{ $val }}" {{ old('partnership_level', $partner->partnership_level) == $val ? 'selected' : '' }}>{{ $label }}</option>
                        @endforeach
                    </select>
                </div>
                <div>
                    <label for="partnership_start_date" class="block text-sm font-medium text-gray-700 mb-1">Start Date</label>
                    <input type="date" name="partnership_start_date" id="partnership_start_date"
                           value="{{ old('partnership_start_date', $partner->partnership_start_date?->format('Y-m-d')) }}"
                           class="w-full px-4 py-2.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent">
                </div>
                <div>
                    <label for="partnership_end_date" class="block text-sm font-medium text-gray-700 mb-1">End Date</label>
                    <input type="date" name="partnership_end_date" id="partnership_end_date"
                           value="{{ old('partnership_end_date', $partner->partnership_end_date?->format('Y-m-d')) }}"
                           class="w-full px-4 py-2.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent">
                </div>
                <div class="md:col-span-2 flex flex-wrap gap-6">
                    <label class="flex items-center gap-2 cursor-pointer">
                        <input type="checkbox" name="show_in_directory" value="1"
                               {{ old('show_in_directory', $partner->show_in_directory) ? 'checked' : '' }}
                               class="w-4 h-4 text-blue-600 border-gray-300 rounded focus:ring-blue-500">
                        <span class="text-sm text-gray-700">Show in public directory</span>
                    </label>
                    <label class="flex items-center gap-2 cursor-pointer">
                        <input type="checkbox" name="is_featured" value="1"
                               {{ old('is_featured', $partner->is_featured) ? 'checked' : '' }}
                               class="w-4 h-4 text-blue-600 border-gray-300 rounded focus:ring-blue-500">
                        <span class="text-sm text-gray-700">Feature this partner</span>
                    </label>
                </div>
                <div class="md:col-span-2">
                    <label for="internal_notes" class="block text-sm font-medium text-gray-700 mb-1">
                        Internal Notes <span class="text-gray-400 font-normal text-xs">(not visible to public)</span>
                    </label>
                    <textarea name="internal_notes" id="internal_notes" rows="3"
                              class="w-full px-4 py-2.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent">{{ old('internal_notes', $partner->internal_notes) }}</textarea>
                </div>
            </div>
        </div>

        <div class="flex items-center justify-between">
            <a href="{{ route('partners.show', $partner) }}"
               class="inline-flex items-center gap-2 px-5 py-2.5 bg-gray-100 text-gray-700 rounded-lg hover:bg-gray-200 transition text-sm font-medium">
                <i class="fas fa-arrow-left"></i> Back
            </a>
            <button type="submit"
                    class="inline-flex items-center gap-2 px-5 py-2.5 bg-blue-600 text-white rounded-lg hover:bg-blue-700 transition text-sm font-medium shadow-sm">
                <i class="fas fa-save mr-2"></i> Update Partner
            </button>
        </div>
    </form>
</div>

</x-app-layout>
