<x-app-layout>
    <x-module-hero
        title="Leadership History Details"
        description="View leader's term and achievements"
        icon="fa-crown">

        <x-slot name="backButton">
            <a href="{{ route('organization.leadership-history.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-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">
        {{-- Leader Profile Card --}}
        <div class="lg:col-span-1">
            <div class="bg-white rounded-lg shadow-sm border border-gray-200 overflow-hidden">
                <div class="bg-indigo-600 p-6">
                    <div class="flex items-center gap-4">
                        <div class="w-16 h-16 bg-white/20 rounded-full flex items-center justify-center shrink-0">
                            <i class="fas fa-crown text-white text-2xl"></i>
                        </div>
                        <div>
                            @if($leadershipHistory->user)
                                <h2 class="text-lg font-bold text-white">{{ $leadershipHistory->user->name }}</h2>
                                <p class="text-indigo-200 text-sm">{{ $leadershipHistory->user->email }}</p>
                            @else
                                <h2 class="text-lg font-bold text-white">{{ $leadershipHistory->leader_name ?? 'Leader Record' }}</h2>
                                <p class="text-indigo-200 text-sm">No user account linked</p>
                            @endif
                        </div>
                    </div>
                </div>
                <div class="p-6 space-y-3">
                    <div class="flex items-center gap-3 text-sm text-gray-700">
                        <i class="fas fa-building w-5 text-green-600"></i>
                        <span>{{ $leadershipHistory->organization->name }}</span>
                    </div>
                    @if($leadershipHistory->leadershipPosition)
                    <div class="flex items-center gap-3 text-sm text-gray-700">
                        <i class="fas fa-briefcase w-5 text-green-600"></i>
                        <span class="px-2 py-0.5 bg-indigo-100 text-indigo-800 rounded text-xs font-medium">
                            {{ $leadershipHistory->leadershipPosition->title }}
                        </span>
                    </div>
                    @endif
                    <div class="flex items-center gap-3 text-sm text-gray-700">
                        <i class="fas fa-calendar w-5 text-green-600"></i>
                        <span>{{ $leadershipHistory->term_start_date->format('M d, Y') }}</span>
                    </div>
                    @if($leadershipHistory->term_end_date)
                    <div class="flex items-center gap-3 text-sm text-gray-700">
                        <i class="fas fa-calendar-times w-5 text-gray-400"></i>
                        <span>{{ $leadershipHistory->term_end_date->format('M d, Y') }}</span>
                    </div>
                    @else
                    <div class="flex items-center gap-3 text-sm text-green-600">
                        <i class="fas fa-check-circle w-5"></i>
                        <span>Currently Serving</span>
                    </div>
                    @endif
                    <div class="flex items-center gap-3 text-sm text-gray-700">
                        <i class="fas fa-clock w-5 text-green-600"></i>
                        <span>{{ $leadershipHistory->term_duration }}</span>
                    </div>
                    @if($leadershipHistory->notes)
                    <div class="pt-2 border-t border-gray-100">
                        <p class="text-xs text-gray-500 font-medium mb-1">Notes</p>
                        <p class="text-sm text-gray-700">{{ $leadershipHistory->notes }}</p>
                    </div>
                    @endif
                </div>
            </div>
        </div>

        {{-- Achievements Section --}}
        <div class="lg:col-span-2">
            <div class="bg-white rounded-lg shadow-sm border border-gray-200 overflow-hidden">
                <div class="flex items-center gap-3 p-5 border-b border-gray-200">
                    <i class="fas fa-trophy text-2xl text-yellow-500"></i>
                    <div>
                        <h3 class="text-lg font-bold text-gray-800">Achievements</h3>
                        <p class="text-sm text-gray-500">Notable achievements during this leader's term</p>
                    </div>
                </div>

                @if($leadershipHistory->achievements->count() > 0)
                <div class="p-5 space-y-3">
                    @foreach($leadershipHistory->achievements->sortByDesc('achievement_date') as $achievement)
                    <div class="flex items-start gap-3 p-4 bg-yellow-50 rounded-lg">
                        <div class="w-9 h-9 bg-yellow-100 rounded-full flex items-center justify-center shrink-0">
                            <i class="fas fa-award text-yellow-600 text-sm"></i>
                        </div>
                        <div class="flex-1">
                            <p class="text-xs font-semibold text-gray-700">{{ $achievement->achievement_date->format('M d, Y') }}</p>
                            <p class="text-sm text-gray-700 mt-0.5">{{ $achievement->achievement_description }}</p>
                        </div>
                        @can('add leadership achievements')
                        <form action="{{ route('organization.leadership-history.achievements.destroy', $achievement) }}"
                              method="POST" class="shrink-0"
                              onsubmit="return confirm('Delete this achievement?');">
                            @csrf @method('DELETE')
                            <button type="submit" class="text-red-500 hover:text-red-700 text-sm">
                                <i class="fas fa-trash"></i>
                            </button>
                        </form>
                        @endcan
                    </div>
                    @endforeach
                </div>
                @else
                <div class="p-8 text-center">
                    <i class="fas fa-trophy text-5xl text-gray-200 mb-3"></i>
                    <h4 class="text-base font-semibold text-gray-600 mb-1">No Achievements Yet</h4>
                    <p class="text-sm text-gray-500">Add achievements to recognize this leader's contributions.</p>
                </div>
                @endif

                {{-- Add Achievement Form --}}
                @can('add leadership achievements')
                <div class="p-5 border-t border-gray-200">
                    <h4 class="text-sm font-semibold text-gray-800 mb-4">Add New Achievement</h4>
                    <form action="{{ route('organization.leadership-history.achievements.store', $leadershipHistory) }}" method="POST">
                        @csrf
                        <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                            <div>
                                <label class="block text-sm font-medium text-gray-700 mb-1" for="achievement_description">
                                    Description <span class="text-red-500">*</span>
                                </label>
                                <textarea id="achievement_description" name="achievement_description" rows="3"
                                    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></textarea>
                                @error('achievement_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="achievement_date">
                                    Date <span class="text-red-500">*</span>
                                </label>
                                <input type="date" id="achievement_date" name="achievement_date"
                                    max="{{ date('Y-m-d') }}"
                                    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('achievement_date')
                                    <p class="mt-1 text-xs text-red-600">{{ $message }}</p>
                                @enderror
                            </div>
                        </div>
                        <div class="flex justify-end mt-4">
                            <button type="submit" class="px-4 py-2 bg-green-700 text-white text-sm font-medium rounded-lg hover:bg-green-800 transition">
                                <i class="fas fa-plus mr-1"></i>Add Achievement
                            </button>
                        </div>
                    </form>
                </div>
                @endcan
            </div>
        </div>
    </div>
</x-app-layout>
