<x-app-layout>
    <x-module-hero
        title="{{ $votingProcedure->name }}"
        description="Voting Procedure Details"
        icon="fa-vote-yea">

        <x-slot name="backButton">
            <a href="{{ route('organization.voting-procedures.index') }}"
               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 List
            </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-slot name="actions">
            @can('edit voting procedures')
            <a href="{{ route('organization.voting-procedures.edit', $votingProcedure) }}"
               class="inline-flex items-center gap-2 px-4 py-2 bg-[#1e293b] text-white text-sm font-semibold rounded-lg hover:bg-[#334155] transition">
                <i class="fas fa-edit"></i> Edit
            </a>
            @endcan
            @can('delete voting procedures')
            <form action="{{ route('organization.voting-procedures.destroy', $votingProcedure) }}" method="POST" class="inline"
                  onsubmit="return confirm('Delete this voting procedure?');">
                @csrf @method('DELETE')
                <button type="submit"
                    class="inline-flex items-center gap-2 px-4 py-2 bg-red-600 text-white text-sm font-semibold rounded-lg hover:bg-red-700 transition">
                    <i class="fas fa-trash"></i> Delete
                </button>
            </form>
            @endcan
        </x-slot>
    </x-module-hero>

    {{-- Flash Messages --}}
    @if(session('success'))
    <div class="px-6 pt-4">
        <div class="p-4 bg-green-100 border border-green-400 text-green-700 rounded-lg flex items-center gap-2">
            <i class="fas fa-check-circle"></i>{{ session('success') }}
        </div>
    </div>
    @endif
    @if(session('error'))
    <div class="px-6 pt-4">
        <div class="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>
    </div>
    @endif

    <div class="p-6 grid grid-cols-1 lg:grid-cols-3 gap-6">
        {{-- Main Details --}}
        <div class="lg:col-span-2 space-y-4">
            {{-- Procedure Info --}}
            <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-5">
                <h3 class="text-sm font-semibold text-gray-800 mb-4 border-b pb-2">Procedure Information</h3>
                <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                    <div>
                        <p class="text-xs text-gray-500 font-medium mb-0.5">Organization</p>
                        <p class="text-sm text-gray-800 font-medium">{{ $votingProcedure->organization->name }}</p>
                    </div>
                    <div>
                        <p class="text-xs text-gray-500 font-medium mb-0.5">Voting Method</p>
                        <p class="text-sm text-gray-800">{{ $votingProcedure->voting_method_label }}</p>
                    </div>
                    <div>
                        <p class="text-xs text-gray-500 font-medium mb-0.5">Version</p>
                        <span class="px-2 py-0.5 bg-purple-100 text-purple-800 rounded text-xs font-semibold">v{{ $votingProcedure->version }}</span>
                    </div>
                    <div>
                        <p class="text-xs text-gray-500 font-medium mb-0.5">Active Election Cycles</p>
                        <span class="px-2 py-0.5 {{ $activeElectionCycles > 0 ? 'bg-green-100 text-green-800' : 'bg-gray-100 text-gray-700' }} rounded text-xs font-semibold">
                            {{ $activeElectionCycles }}
                        </span>
                    </div>
                </div>
            </div>

            {{-- Description --}}
            <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-5">
                <h3 class="text-sm font-semibold text-gray-800 mb-3 border-b pb-2">Description</h3>
                <p class="text-sm text-gray-700 leading-relaxed whitespace-pre-line">{{ $votingProcedure->description }}</p>
            </div>

            {{-- Rules --}}
            <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-5">
                <h3 class="text-sm font-semibold text-gray-800 mb-3 border-b pb-2">Rules &amp; Guidelines</h3>
                <p class="text-sm text-gray-700 leading-relaxed whitespace-pre-line">{{ $votingProcedure->rules }}</p>
            </div>

            {{-- Associated Election Cycles --}}
            <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-5">
                <h3 class="text-sm font-semibold text-gray-800 mb-4 border-b pb-2">Associated Election Cycles</h3>
                @if($votingProcedure->electionCycles->count() > 0)
                <div class="space-y-3">
                    @foreach($votingProcedure->electionCycles as $cycle)
                    <div class="flex items-center justify-between p-3 bg-orange-50 rounded-lg">
                        <div class="flex items-center gap-3">
                            <div class="w-8 h-8 bg-orange-100 rounded-full flex items-center justify-center shrink-0">
                                <i class="fas fa-calendar text-orange-600 text-xs"></i>
                            </div>
                            <div>
                                <p class="text-sm font-medium text-gray-800">{{ $cycle->name }}</p>
                                <p class="text-xs text-gray-500">
                                    {{ $cycle->start_date->format('M d, Y') }} — {{ $cycle->end_date ? $cycle->end_date->format('M d, Y') : 'Present' }}
                                </p>
                            </div>
                        </div>
                        <div class="flex items-center gap-2">
                            <span class="px-2 py-0.5 rounded text-xs font-semibold
                                @if($cycle->isActive()) bg-green-100 text-green-800
                                @elseif($cycle->isCompleted()) bg-blue-100 text-blue-800
                                @else bg-red-100 text-red-800 @endif">
                                {{ ucfirst($cycle->status) }}
                            </span>
                            <a href="{{ route('organization.election-cycles.show', $cycle) }}" class="text-blue-600 hover:text-blue-800 text-sm">
                                <i class="fas fa-eye"></i>
                            </a>
                        </div>
                    </div>
                    @endforeach
                </div>
                @else
                <p class="text-sm text-gray-400 italic">No election cycles associated with this voting procedure.</p>
                @endif
            </div>
        </div>

        {{-- Sidebar --}}
        <div class="lg:col-span-1">
            <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-5">
                <h3 class="text-sm font-semibold text-gray-800 mb-4 border-b pb-2">Quick Stats</h3>
                <div class="space-y-3">
                    <div class="flex items-center justify-between">
                        <span class="text-sm text-gray-600">Total Cycles</span>
                        <span class="font-bold text-gray-800">{{ $votingProcedure->electionCycles->count() }}</span>
                    </div>
                    <div class="flex items-center justify-between">
                        <span class="text-sm text-gray-600">Active Cycles</span>
                        <span class="font-bold text-green-700">{{ $activeElectionCycles }}</span>
                    </div>
                    <div class="flex items-center justify-between">
                        <span class="text-sm text-gray-600">Version</span>
                        <span class="font-bold text-gray-800">{{ $votingProcedure->version }}</span>
                    </div>
                </div>
            </div>
        </div>
    </div>
</x-app-layout>
