<x-app-layout>
    <div class="py-6">
        <div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
            <!-- Header Block -->
            <div class="flex flex-col md:flex-row md:items-center md:justify-between gap-4 mb-6">
                <div>
                    <h2 class="font-semibold text-2xl text-slate-800 leading-tight">
                        {{ __('Consignments & Projects Registry') }}
                    </h2>
                    <p class="text-slate-500 text-sm mt-1">Group transactions and monitor profitability for importing cars, machinery deals, or specific consignments.</p>
                </div>
                @if(!Auth::user()->isAccountant())
                    <a href="{{ route('consignments.create') }}" class="inline-flex items-center px-4 py-2 bg-brand-500 hover:bg-brand-600 text-white text-sm font-medium rounded-lg shadow-sm transition duration-150 ease-in-out">
                        <svg class="w-5 h-5 mr-1.5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v3m0 0v3m0-3h3m-3 0H9m12 0a9 9 0 11-18 0 9 9 0 0118 0z" />
                        </svg>
                        {{ __('Add Consignment') }}
                    </a>
                @endif
            </div>

            <!-- Search & Filters -->
            <div class="bg-white rounded-xl shadow-sm border border-slate-100 p-6 mb-6">
                <form action="{{ route('consignments.index') }}" method="GET" class="flex flex-col md:flex-row gap-4 items-center justify-between">
                    <input type="hidden" name="status" value="{{ $status }}">
                    
                    <div class="relative w-full md:w-96">
                        <div class="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none">
                            <svg class="h-5 w-5 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z" />
                            </svg>
                        </div>
                        <input type="text" name="search" value="{{ $search }}" placeholder="Search by project name..." class="pl-10 block w-full rounded-lg border-slate-300 focus:border-brand-500 focus:ring focus:ring-brand-200 focus:ring-opacity-50 text-sm">
                    </div>

                    <div class="flex items-center gap-2 w-full md:w-auto">
                        <button type="submit" class="w-full md:w-auto px-4 py-2 bg-slate-800 hover:bg-slate-700 text-white text-sm font-medium rounded-lg transition duration-150">
                            {{ __('Filter') }}
                        </button>
                        @if($search)
                            <a href="{{ route('consignments.index', ['status' => $status]) }}" class="w-full md:w-auto px-4 py-2 bg-slate-100 hover:bg-slate-200 text-slate-700 text-sm font-medium rounded-lg text-center transition duration-150">
                                {{ __('Clear') }}
                            </a>
                        @endif
                    </div>
                </form>
            </div>

            <!-- Status Filter Tabs -->
            <div class="border-b border-slate-200 mb-6 flex space-x-4">
                <a href="{{ route('consignments.index', ['status' => 'active', 'search' => $search]) }}" class="py-4 px-4 font-semibold text-sm border-b-2 transition {{ $status === 'active' ? 'border-brand-500 text-brand-600' : 'border-transparent text-slate-500 hover:text-slate-700 hover:border-slate-300' }}">
                    {{ __('Active') }}
                </a>
                <a href="{{ route('consignments.index', ['status' => 'completed', 'search' => $search]) }}" class="py-4 px-4 font-semibold text-sm border-b-2 transition {{ $status === 'completed' ? 'border-brand-500 text-brand-600' : 'border-transparent text-slate-500 hover:text-slate-700 hover:border-slate-300' }}">
                    {{ __('Completed') }}
                </a>
                <a href="{{ route('consignments.index', ['status' => 'archived', 'search' => $search]) }}" class="py-4 px-4 font-semibold text-sm border-b-2 transition {{ $status === 'archived' ? 'border-brand-500 text-brand-600' : 'border-transparent text-slate-500 hover:text-slate-700 hover:border-slate-300' }}">
                    {{ __('Archived') }}
                </a>
                <a href="{{ route('consignments.index', ['status' => 'all', 'search' => $search]) }}" class="py-4 px-4 font-semibold text-sm border-b-2 transition {{ $status === 'all' ? 'border-brand-500 text-brand-600' : 'border-transparent text-slate-500 hover:text-slate-700 hover:border-slate-300' }}">
                    {{ __('All') }}
                </a>
            </div>

            <!-- List Grid -->
            <div class="bg-white rounded-xl shadow-sm border border-slate-100 overflow-hidden">
                <div class="overflow-x-auto">
                    <table class="min-w-full divide-y divide-slate-200">
                        <thead class="bg-slate-50">
                            <tr>
                                <th class="px-6 py-3 text-left text-xs font-semibold text-slate-500 uppercase tracking-wider">{{ __('Project Name') }}</th>
                                <th class="px-6 py-3 text-left text-xs font-semibold text-slate-500 uppercase tracking-wider">{{ __('Buyer') }}</th>
                                <th class="px-6 py-3 text-left text-xs font-semibold text-slate-500 uppercase tracking-wider">{{ __('Seller') }}</th>
                                <th class="px-6 py-3 text-left text-xs font-semibold text-slate-500 uppercase tracking-wider">{{ __('Investor') }}</th>
                                <th class="px-6 py-3 text-left text-xs font-semibold text-slate-500 uppercase tracking-wider">{{ __('Agreement Share') }}</th>
                                <th class="px-6 py-3 text-left text-xs font-semibold text-slate-500 uppercase tracking-wider">{{ __('Status') }}</th>
                                <th class="px-6 py-3 text-right text-xs font-semibold text-slate-500 uppercase tracking-wider">{{ __('Actions') }}</th>
                            </tr>
                        </thead>
                        <tbody class="bg-white divide-y divide-slate-200">
                            @forelse($consignments as $project)
                                <tr class="hover:bg-slate-50 transition duration-150">
                                    <td class="px-6 py-4 whitespace-nowrap text-sm font-semibold text-slate-900">
                                        <a href="{{ route('consignments.show', $project->id) }}" class="text-brand-500 hover:text-brand-600">{{ $project->title }}</a>
                                    </td>
                                    {{-- Buyer --}}
                                    <td class="px-6 py-4 whitespace-nowrap text-sm text-slate-600">
                                        @if($project->buyerClient)
                                            <span class="inline-flex items-center px-2 py-0.5 bg-blue-50 text-blue-700 text-xs font-medium rounded-full">
                                                👤 {{ $project->buyerClient->full_name }}
                                            </span>
                                        @elseif($project->buyerCustomAccount)
                                            <span class="inline-flex items-center px-2 py-0.5 bg-blue-50 text-blue-700 text-xs font-medium rounded-full">
                                                💼 {{ $project->buyerCustomAccount->name }}
                                            </span>
                                        @else
                                            <span class="text-slate-400">—</span>
                                        @endif
                                    </td>
                                    {{-- Seller --}}
                                    <td class="px-6 py-4 whitespace-nowrap text-sm text-slate-600">
                                        @if($project->sellerClient)
                                            <span class="inline-flex items-center px-2 py-0.5 bg-amber-50 text-amber-700 text-xs font-medium rounded-full">
                                                👤 {{ $project->sellerClient->full_name }}
                                            </span>
                                        @elseif($project->sellerCustomAccount)
                                            <span class="inline-flex items-center px-2 py-0.5 bg-amber-50 text-amber-700 text-xs font-medium rounded-full">
                                                💼 {{ $project->sellerCustomAccount->name }}
                                            </span>
                                        @else
                                            <span class="text-slate-400">—</span>
                                        @endif
                                    </td>
                                    {{-- Investor --}}
                                    <td class="px-6 py-4 whitespace-nowrap text-sm text-slate-600">
                                        @if($project->investorClient)
                                            <span class="inline-flex items-center px-2 py-0.5 bg-purple-50 text-purple-700 text-xs font-medium rounded-full">
                                                👤 {{ $project->investorClient->full_name }}
                                            </span>
                                        @elseif($project->investorCustomAccount)
                                            <span class="inline-flex items-center px-2 py-0.5 bg-purple-50 text-purple-700 text-xs font-medium rounded-full">
                                                💼 {{ $project->investorCustomAccount->name }}
                                            </span>
                                        @else
                                            <span class="text-slate-400">—</span>
                                        @endif
                                    </td>
                                    <td class="px-6 py-4 whitespace-nowrap text-sm text-slate-600">
                                        @if($project->investor_share_type === 'fixed')
                                            {{ $currencySymbol }}{{ number_format($project->investor_share_value, 2) }}
                                        @else
                                            {{ number_format($project->investor_share_value, 1) }}%
                                        @endif
                                    </td>
                                    <td class="px-6 py-4 whitespace-nowrap text-sm">
                                        @if($project->status === 'active')
                                            <span class="px-2 py-1 text-xs font-semibold rounded-full bg-emerald-100 text-emerald-800">Active</span>
                                        @elseif($project->status === 'completed')
                                            <span class="px-2 py-1 text-xs font-semibold rounded-full bg-blue-100 text-blue-800">Completed</span>
                                        @else
                                            <span class="px-2 py-1 text-xs font-semibold rounded-full bg-slate-100 text-slate-800">Archived</span>
                                        @endif
                                    </td>
                                    <td class="px-6 py-4 whitespace-nowrap text-right text-sm font-semibold flex items-center justify-end gap-3.5">
                                        <a href="{{ route('consignments.show', $project->id) }}" class="text-brand-500 hover:text-brand-600" title="{{ __('View Ledger') }}">
                                            {{ __('View') }}
                                        </a>
                                        @if(!Auth::user()->isAccountant())
                                            <a href="{{ route('consignments.edit', $project->id) }}" class="text-amber-500 hover:text-amber-600" title="{{ __('Edit') }}">
                                                {{ __('Edit') }}
                                            </a>
                                            @if(Auth::user()->isAdmin())
                                                <form action="{{ route('consignments.destroy', $project->id) }}" method="POST" class="inline-block" onsubmit="return confirm('Are you sure you want to permanently delete this consignment project?');">
                                                    @csrf
                                                    @method('DELETE')
                                                    <button type="submit" class="text-rose-600 hover:text-rose-800">{{ __('Delete') }}</button>
                                                </form>
                                            @endif
                                        @endif
                                    </td>
                                </tr>
                            @empty
                                <tr>
                                    <td colspan="8" class="px-6 py-10 text-center text-slate-400 text-sm">
                                        {{ __('No consignment projects found.') }}
                                    </td>
                                </tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>
                <div class="px-6 py-4 border-t border-slate-200 bg-slate-50">
                    {{ $consignments->links() }}
                </div>
            </div>
        </div>
    </div>
</x-app-layout>
