<x-app-layout>
    <x-module-hero
        title="Staff History"
        description="Manage staff employment records"
        icon="fa-user-tie">

        <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('create staff history')
            <a href="{{ route('organization.staff-history.create') }}"
               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-plus"></i> Add Staff Record
            </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

    {{-- Filter Bar --}}
    <div class="px-6 pt-4 pb-2 bg-white border-b border-gray-200">
        <form method="GET" class="flex flex-wrap items-end gap-4">
            <div class="flex-1 min-w-[180px]">
                <label class="block text-sm font-medium text-gray-700 mb-1">Search</label>
                <input type="text" name="search" value="{{ request('search') }}"
                    placeholder="Name or position..."
                    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">
            </div>
            <div class="flex-1 min-w-[180px]">
                <label class="block text-sm font-medium text-gray-700 mb-1">Organization</label>
                <select 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">
                    <option value="">All Organizations</option>
                    @foreach($organizations as $org)
                        <option value="{{ $org->id }}" {{ request('organization_id') == $org->id ? 'selected' : '' }}>{{ $org->name }}</option>
                    @endforeach
                </select>
            </div>
            <div class="flex-1 min-w-[160px]">
                <label class="block text-sm font-medium text-gray-700 mb-1">Status</label>
                <select name="employment_status" 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">
                    <option value="">All Status</option>
                    <option value="current" {{ request('employment_status') === 'current' ? 'selected' : '' }}>Current</option>
                    <option value="former" {{ request('employment_status') === 'former' ? 'selected' : '' }}>Former</option>
                </select>
            </div>
            <div class="flex items-end gap-2">
                <button type="submit" class="px-4 py-2 bg-green-700 text-white rounded-lg hover:bg-green-800 transition text-sm font-medium">
                    <i class="fas fa-search mr-1"></i>Filter
                </button>
                <a href="{{ route('organization.staff-history.index') }}" class="px-4 py-2 bg-gray-200 text-gray-700 rounded-lg hover:bg-gray-300 transition text-sm font-medium">
                    <i class="fas fa-times mr-1"></i>Reset
                </a>
            </div>
        </form>
    </div>

    {{-- Table --}}
    <div class="w-full">
        @if($staffHistories->count() > 0)
        <div class="overflow-x-auto">
            <table class="w-full table-zebra">
                <thead>
                    <tr>
                        <th class="px-4 py-3 text-left th-col-1">Staff Name</th>
                        <th class="px-4 py-3 text-left th-col-2">Position</th>
                        <th class="px-4 py-3 text-left th-col-3">Organization</th>
                        <th class="px-4 py-3 text-left th-col-4">Employment Period</th>
                        <th class="px-4 py-3 text-center th-col-5">Status</th>
                        <th class="px-4 py-3 text-center th-col-6">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach($staffHistories as $staff)
                    <tr class="odd:bg-white even:bg-gray-50 hover:bg-gray-100 transition-colors border-b border-gray-100 last:border-0">
                        <td class="px-4 py-3">
                            <div class="flex items-center gap-3">
                                <div class="w-9 h-9 bg-orange-100 rounded-full flex items-center justify-center shrink-0">
                                    <i class="fas fa-user text-orange-600 text-sm"></i>
                                </div>
                                <div>
                                    <div class="font-semibold text-gray-800">{{ $staff->staff_name }}</div>
                                    @if($staff->user)
                                    <div class="text-xs text-gray-500"><i class="fas fa-link mr-1"></i>{{ $staff->user->name }}</div>
                                    @endif
                                </div>
                            </div>
                        </td>
                        <td class="px-4 py-3 text-sm text-gray-700">{{ $staff->position }}</td>
                        <td class="px-4 py-3 text-sm text-gray-700">{{ $staff->organization->name }}</td>
                        <td class="px-4 py-3 text-sm text-gray-700">
                            <div>From {{ $staff->employment_start_date->format('M d, Y') }}</div>
                            @if($staff->employment_end_date)
                                <div class="text-xs text-gray-500">to {{ $staff->employment_end_date->format('M d, Y') }}</div>
                            @else
                                <div class="text-xs text-green-600">Currently employed</div>
                            @endif
                        </td>
                        <td class="px-4 py-3 text-center">
                            <span class="px-2 py-0.5 rounded text-xs font-semibold
                                {{ $staff->employment_status === 'current' ? 'bg-green-100 text-green-800' : 'bg-gray-100 text-gray-700' }}">
                                {{ ucfirst($staff->employment_status) }}
                            </span>
                        </td>
                        <td class="px-4 py-3 text-center">
                            <div class="flex justify-center gap-1 flex-wrap">
                                @can('view staff history')
                                <a href="{{ route('organization.staff-history.show', $staff) }}" class="btn-action-view">
                                    <i class="fas fa-eye"></i> View
                                </a>
                                @endcan
                                @can('edit staff history')
                                <a href="{{ route('organization.staff-history.edit', $staff) }}" class="btn-action-edit">
                                    <i class="fas fa-edit"></i> Edit
                                </a>
                                @endcan
                                @can('delete staff history')
                                <form action="{{ route('organization.staff-history.destroy', $staff) }}" method="POST" class="inline"
                                      onsubmit="return confirm('Delete this staff record?');">
                                    @csrf @method('DELETE')
                                    <button type="submit" class="btn-action-delete">
                                        <i class="fas fa-trash"></i> Delete
                                    </button>
                                </form>
                                @endcan
                            </div>
                        </td>
                    </tr>
                    @endforeach
                </tbody>
            </table>
        </div>
        <div class="px-4 py-3 border-t border-gray-200 bg-white">
            {{ $staffHistories->links() }}
        </div>
        @else
        <div class="p-6">
            <div class="text-center py-12">
                <i class="fas fa-user-tie text-6xl text-gray-300 mb-4"></i>
                <h3 class="text-xl font-semibold text-gray-700 mb-2">No Staff Records Found</h3>
                <p class="text-gray-500 mb-4">Create your first staff record to get started.</p>
                @can('create staff history')
                <a href="{{ route('organization.staff-history.create') }}" class="theme-btn-primary px-6 py-2 rounded-lg">
                    <i class="fas fa-plus mr-2"></i>Add Staff Record
                </a>
                @endcan
            </div>
        </div>
        @endif
    </div>
</x-app-layout>
