<x-app-layout>
    <x-module-hero
        title="Edit Voting Procedure"
        description="{{ $votingProcedure->name }} (v{{ $votingProcedure->version }})"
        icon="fa-vote-yea">

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

        <x-slot name="tabs">
            @can('view organizations')
            <a href="{{ route('organization.details.index') }}"
               class="module-hero-tab {{ request()->routeIs('organization.details.*') ? '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-building text-sm"></i> Organization Details
            </a>
            @endcan
            @can('view leadership positions')
            <a href="{{ route('organization.leadership-positions.index') }}"
               class="module-hero-tab {{ request()->routeIs('organization.leadership-positions.*') ? '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-sitemap text-sm"></i> Leadership Positions
            </a>
            @endcan
            @can('view leadership history')
            <a href="{{ route('organization.leadership-history.index') }}"
               class="module-hero-tab {{ request()->routeIs('organization.leadership-history.*') ? '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-history text-sm"></i> Leadership History
            </a>
            @endcan
            @can('view staff history')
            <a href="{{ route('organization.staff-history.index') }}"
               class="module-hero-tab {{ request()->routeIs('organization.staff-history.*') ? '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-user-tie text-sm"></i> Staff History
            </a>
            @endcan
            @can('view election cycles')
            <a href="{{ route('organization.election-cycles.index') }}"
               class="module-hero-tab {{ request()->routeIs('organization.election-cycles.*') ? '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-calendar-alt text-sm"></i> Election Cycles
            </a>
            @endcan
            @can('view voting procedures')
            <a href="{{ route('organization.voting-procedures.index') }}"
               class="module-hero-tab {{ request()->routeIs('organization.voting-procedures.*') ? '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-vote-yea text-sm"></i> Voting Procedures
            </a>
            @endcan
            @can('view organogram')
            <a href="{{ route('organization.organogram.show') }}"
               class="module-hero-tab {{ request()->routeIs('organization.organogram.*') ? '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-project-diagram text-sm"></i> Organogram
            </a>
            @endcan
        </x-slot>
    </x-module-hero>

    <div class="p-6">
        <div class="mb-4 p-4 bg-blue-50 border border-blue-200 text-blue-800 rounded-lg text-sm">
            <i class="fas fa-info-circle mr-2"></i>
            Updating this procedure will increment the version to <strong>v{{ $votingProcedure->version + 1 }}</strong>. Version history is maintained for audit purposes.
        </div>

        <form action="{{ route('organization.voting-procedures.update', $votingProcedure) }}" method="POST">
            @csrf @method('PUT')

            {{-- Basic Info --}}
            <h3 class="text-lg font-semibold text-gray-800 border-b pb-2 mb-4">Basic Information</h3>
            <div class="grid grid-cols-1 md:grid-cols-2 gap-6 mb-6">
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1" for="organization_id">
                        Organization <span class="text-red-500">*</span>
                    </label>
                    <select id="organization_id" name="organization_id"
                        class="w-full px-4 py-2 border border-gray-300 rounded-lg bg-white text-gray-800 focus:ring-2 focus:ring-green-500 focus:border-transparent"
                        required>
                        <option value="">Select Organization</option>
                        @foreach($organizations as $org)
                            <option value="{{ $org->id }}" {{ old('organization_id', $votingProcedure->organization_id) == $org->id ? 'selected' : '' }}>{{ $org->name }}</option>
                        @endforeach
                    </select>
                    @error('organization_id')<p class="mt-1 text-xs text-red-600">{{ $message }}</p>@enderror
                </div>
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1" for="name">
                        Procedure Name <span class="text-red-500">*</span>
                    </label>
                    <input type="text" id="name" name="name"
                        value="{{ old('name', $votingProcedure->name) }}"
                        placeholder="e.g., Board Director Election Procedure"
                        class="w-full px-4 py-2 border border-gray-300 rounded-lg bg-white text-gray-800 focus:ring-2 focus:ring-green-500 focus:border-transparent"
                        required>
                    @error('name')<p class="mt-1 text-xs text-red-600">{{ $message }}</p>@enderror
                </div>
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1" for="voting_method">
                        Voting Method <span class="text-red-500">*</span>
                    </label>
                    <select id="voting_method" name="voting_method"
                        class="w-full px-4 py-2 border border-gray-300 rounded-lg bg-white text-gray-800 focus:ring-2 focus:ring-green-500 focus:border-transparent"
                        required>
                        <option value="">Select Method</option>
                        <option value="majority" {{ old('voting_method', $votingProcedure->voting_method) == 'majority' ? 'selected' : '' }}>Simple Majority (&gt;50%)</option>
                        <option value="plurality" {{ old('voting_method', $votingProcedure->voting_method) == 'plurality' ? 'selected' : '' }}>Plurality (Most Votes)</option>
                        <option value="supermajority_two_thirds" {{ old('voting_method', $votingProcedure->voting_method) == 'supermajority_two_thirds' ? 'selected' : '' }}>Supermajority (2/3)</option>
                        <option value="supermajority_three_fourths" {{ old('voting_method', $votingProcedure->voting_method) == 'supermajority_three_fourths' ? 'selected' : '' }}>Supermajority (3/4)</option>
                        <option value="unanimous" {{ old('voting_method', $votingProcedure->voting_method) == 'unanimous' ? 'selected' : '' }}>Unanimous (100%)</option>
                        <option value="ranked_choice" {{ old('voting_method', $votingProcedure->voting_method) == 'ranked_choice' ? 'selected' : '' }}>Ranked Choice Voting</option>
                        <option value="approval" {{ old('voting_method', $votingProcedure->voting_method) == 'approval' ? 'selected' : '' }}>Approval Voting</option>
                        <option value="borda_count" {{ old('voting_method', $votingProcedure->voting_method) == 'borda_count' ? 'selected' : '' }}>Borda Count</option>
                    </select>
                    @error('voting_method')<p class="mt-1 text-xs text-red-600">{{ $message }}</p>@enderror
                </div>
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">Current Version</label>
                    <div class="px-4 py-2 bg-gray-50 border border-gray-200 rounded-lg text-sm text-gray-700">
                        <span class="text-green-700 font-semibold">Version {{ $votingProcedure->version }}</span>
                        <span class="text-gray-400 ml-2">({{ $votingProcedure->updated_at->format('M d, Y') }})</span>
                    </div>
                </div>
            </div>

            {{-- Details --}}
            <h3 class="text-lg font-semibold text-gray-800 border-b pb-2 mb-4">Details</h3>
            <div class="grid grid-cols-1 md:grid-cols-2 gap-6 mb-6">
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1" for="description">
                        Description <span class="text-red-500">*</span>
                    </label>
                    <textarea id="description" name="description" rows="4"
                        class="w-full px-4 py-2 border border-gray-300 rounded-lg bg-white text-gray-800 focus:ring-2 focus:ring-green-500 focus:border-transparent"
                        required>{{ old('description', $votingProcedure->description) }}</textarea>
                    @error('description')<p class="mt-1 text-xs text-red-600">{{ $message }}</p>@enderror
                </div>
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1" for="rules">
                        Rules &amp; Guidelines <span class="text-red-500">*</span>
                    </label>
                    <textarea id="rules" name="rules" rows="4"
                        class="w-full px-4 py-2 border border-gray-300 rounded-lg bg-white text-gray-800 focus:ring-2 focus:ring-green-500 focus:border-transparent"
                        required>{{ old('rules', $votingProcedure->rules) }}</textarea>
                    @error('rules')<p class="mt-1 text-xs text-red-600">{{ $message }}</p>@enderror
                </div>
            </div>

            {{-- Election Cycles --}}
            <h3 class="text-lg font-semibold text-gray-800 border-b pb-2 mb-4">Associated Election Cycles <span class="text-gray-400 text-sm font-normal">(optional)</span></h3>
            <div class="grid grid-cols-1 md:grid-cols-2 gap-3 mb-6">
                @if($electionCycles && $electionCycles->count() > 0)
                    @foreach($electionCycles as $cycle)
                    <label class="flex items-center gap-3 p-3 border border-gray-200 rounded-lg hover:bg-gray-50 cursor-pointer">
                        <input type="checkbox" name="election_cycle_ids[]" value="{{ $cycle->id }}"
                            {{ in_array($cycle->id, old('election_cycle_ids', $selectedCycles ?? [])) ? 'checked' : '' }}
                            class="rounded text-green-600 focus:ring-green-500">
                        <div>
                            <div class="text-sm font-medium text-gray-800">{{ $cycle->name }}</div>
                            <div>
                                <span class="px-2 py-0.5 rounded text-xs
                                    {{ $cycle->status === 'active' ? 'bg-green-100 text-green-800' : 'bg-gray-100 text-gray-700' }}">
                                    {{ ucfirst($cycle->status) }}
                                </span>
                            </div>
                        </div>
                    </label>
                    @endforeach
                @else
                    <p class="text-sm text-gray-400 italic col-span-full">No election cycles available for this organization.</p>
                @endif
            </div>

            <div class="flex justify-end gap-3 pt-4 border-t border-gray-200">
                <a href="{{ route('organization.voting-procedures.show', $votingProcedure) }}"
                   class="px-5 py-2 bg-gray-200 text-gray-700 text-sm font-medium rounded-lg hover:bg-gray-300 transition">
                    <i class="fas fa-times mr-1"></i>Cancel
                </a>
                <button type="submit" class="theme-btn-primary px-5 py-2 rounded-lg text-sm font-medium">
                    <i class="fas fa-save mr-1"></i>Update Procedure (v{{ $votingProcedure->version + 1 }})
                </button>
            </div>
        </form>
    </div>
</x-app-layout>
