<x-app-layout>
    <x-module-hero
        title="Organization Details"
        description="Manage organization information and settings"
        icon="fa-building">

        <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 organizations')
            <a href="{{ route('organization.details.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> New Organization
            </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

    {{-- Table --}}
    <div class="w-full">
        @if($organizations->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">Organization</th>
                        <th class="px-4 py-3 text-left th-col-2">Location</th>
                        <th class="px-4 py-3 text-left th-col-3">Contact</th>
                        <th class="px-4 py-3 text-center th-col-4">Stats</th>
                        <th class="px-4 py-3 text-center th-col-5">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach($organizations as $organization)
                    <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-green-100 rounded-lg flex items-center justify-center shrink-0">
                                    <i class="fas fa-building text-green-600"></i>
                                </div>
                                <div>
                                    <div class="font-semibold text-gray-800">{{ $organization->name }}</div>
                                    <div class="text-xs text-gray-500">{{ $organization->short_name }}</div>
                                </div>
                            </div>
                        </td>
                        <td class="px-4 py-3 text-sm text-gray-700">
                            <div>{{ $organization->city }}, {{ $organization->country }}</div>
                            <div class="text-xs text-gray-500">{{ $organization->postal_code }}</div>
                        </td>
                        <td class="px-4 py-3 text-sm text-gray-700">
                            <div><i class="fas fa-envelope mr-1 text-xs"></i>{{ $organization->email }}</div>
                            <div><i class="fas fa-phone mr-1 text-xs"></i>{{ $organization->hotline }}</div>
                        </td>
                        <td class="px-4 py-3 text-center">
                            <div class="flex justify-center gap-1 flex-wrap">
                                <span class="px-2 py-0.5 bg-blue-100 text-blue-800 rounded text-xs" title="Leadership Positions">
                                    <i class="fas fa-users mr-1"></i>{{ $organization->leadership_positions_count }}
                                </span>
                                <span class="px-2 py-0.5 bg-purple-100 text-purple-800 rounded text-xs" title="Election Cycles">
                                    <i class="fas fa-calendar mr-1"></i>{{ $organization->election_cycles_count }}
                                </span>
                                <span class="px-2 py-0.5 bg-orange-100 text-orange-800 rounded text-xs" title="Staff Records">
                                    <i class="fas fa-user-tie mr-1"></i>{{ $organization->staff_histories_count }}
                                </span>
                            </div>
                        </td>
                        <td class="px-4 py-3 text-center">
                            <div class="flex justify-center gap-1 flex-wrap">
                                @can('view organizations')
                                <a href="{{ route('organization.details.show', $organization) }}" class="btn-action-view">
                                    <i class="fas fa-eye"></i> View
                                </a>
                                @endcan
                                @can('edit organizations')
                                <a href="{{ route('organization.details.edit', $organization) }}" class="btn-action-edit">
                                    <i class="fas fa-edit"></i> Edit
                                </a>
                                @endcan
                                @can('delete organizations')
                                <form action="{{ route('organization.details.destroy', $organization) }}" method="POST" class="inline"
                                      onsubmit="return confirm('Delete this organization?');">
                                    @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">
            {{ $organizations->links() }}
        </div>
        @else
        <div class="p-6">
            <div class="text-center py-12">
                <i class="fas fa-building text-6xl text-gray-300 mb-4"></i>
                <h3 class="text-xl font-semibold text-gray-700 mb-2">No Organizations Found</h3>
                <p class="text-gray-500 mb-4">Get started by creating your first organization.</p>
                @can('create organizations')
                <a href="{{ route('organization.details.create') }}" class="theme-btn-primary px-6 py-2 rounded-lg">
                    <i class="fas fa-plus mr-2"></i>Create Organization
                </a>
                @endcan
            </div>
        </div>
        @endif
    </div>
</x-app-layout>
