<x-app-layout>
    <x-slot name="header">
        {{ __('Dealer Management') }}
    </x-slot>

    <div class="p-6 space-y-6">
        @if (session('status'))
            <div class="rounded-md bg-green-50 border border-green-200 px-4 py-3 text-sm text-green-800">
                {{ session('status') }}
            </div>
        @endif

        @if ($errors->any())
            <div class="rounded-md bg-red-50 border border-red-200 px-4 py-3 text-sm text-red-800">
                <ul class="list-disc pl-5 space-y-1">
                    @foreach ($errors->all() as $error)
                        <li>{{ $error }}</li>
                    @endforeach
                </ul>
            </div>
        @endif

        <div class="bg-white border border-gray-200 rounded-xl shadow-sm p-5 flex items-center justify-between">
            <div>
                <h1 class="text-xl font-semibold text-gray-900">Dealers</h1>
                <p class="mt-1 text-sm text-gray-600">
                    Platform-level dealer management. This list is not scoped by the current dealer.
                </p>
            </div>

            @can('settings.dealers.manage')
                <a
                    href="{{ route('settings.dealers.create') }}"
                    class="inline-flex items-center rounded-lg bg-red-600 px-4 py-2 text-sm font-semibold text-white shadow-sm hover:bg-red-700"
                >
                    Add Dealer
                </a>
            @endcan
        </div>

        <div class="bg-white border border-gray-200 rounded-xl shadow-sm overflow-hidden">
            <table class="min-w-full divide-y divide-gray-200 text-sm">
                <thead class="bg-gray-50">
                    <tr>
                        <th class="px-4 py-2 text-left font-semibold text-gray-700">Name</th>
                        <th class="px-4 py-2 text-left font-semibold text-gray-700">Code</th>
                        <th class="px-4 py-2 text-left font-semibold text-gray-700">Default</th>
                        <th class="px-4 py-2 text-left font-semibold text-gray-700">Status</th>
                        <th class="px-4 py-2 text-left font-semibold text-gray-700">Provisioning</th>
                        <th class="px-4 py-2 text-left font-semibold text-gray-700">Created</th>
                        <th class="px-4 py-2 text-right font-semibold text-gray-700">Actions</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-gray-100">
                    @forelse ($dealers as $dealer)
                        <tr>
                            <td class="px-4 py-2 text-gray-900">{{ $dealer->name }}</td>
                            <td class="px-4 py-2 text-gray-700 font-mono text-xs">{{ $dealer->code }}</td>
                            <td class="px-4 py-2">
                                @if ($dealer->is_default)
                                    <span class="inline-flex items-center rounded-full bg-blue-50 px-2 py-0.5 text-xs font-medium text-blue-700">
                                        Default
                                    </span>
                                @endif
                            </td>
                            <td class="px-4 py-2">
                                @if ($dealer->is_active)
                                    <span class="inline-flex items-center rounded-full bg-green-50 px-2 py-0.5 text-xs font-medium text-green-700">
                                        Active
                                    </span>
                                @else
                                    <span class="inline-flex items-center rounded-full bg-gray-100 px-2 py-0.5 text-xs font-medium text-gray-600">
                                        Inactive
                                    </span>
                                @endif
                            </td>
                            <td class="px-4 py-2">
                                @if ($dealer->is_default)
                                    <span class="inline-flex items-center rounded-full bg-green-50 px-2 py-0.5 text-xs font-medium text-green-700">
                                        Provisioned
                                    </span>
                                    <div class="mt-0.5 text-xs text-gray-500">
                                        Base dealer
                                    </div>
                                @else
                                    @if ($dealer->provisioning_status === 'provisioned')
                                        <span class="inline-flex items-center rounded-full bg-green-50 px-2 py-0.5 text-xs font-medium text-green-700">
                                            Provisioned
                                        </span>
                                    @elseif ($dealer->provisioning_status === 'failed')
                                        <span class="inline-flex items-center rounded-full bg-red-50 px-2 py-0.5 text-xs font-medium text-red-700">
                                            Failed
                                        </span>
                                    @else
                                        <span class="inline-flex items-center rounded-full bg-yellow-50 px-2 py-0.5 text-xs font-medium text-yellow-700">
                                            Pending
                                        </span>
                                    @endif
                                    @if ($dealer->provisioned_at)
                                        <div class="mt-0.5 text-xs text-gray-500">
                                            {{ $dealer->provisioned_at->format('Y-m-d H:i') }}
                                        </div>
                                    @endif
                                    @if ($dealer->provisioning_status === 'failed' && $dealer->provisioning_error)
                                        <div class="mt-0.5 text-[11px] text-red-600">
                                            {{ Str::limit($dealer->provisioning_error, 80) }}
                                        </div>
                                    @endif
                                @endif
                            </td>
                            <td class="px-4 py-2 text-gray-500">
                                {{ $dealer->created_at?->format('Y-m-d') ?? '—' }}
                            </td>
                            <td class="px-4 py-2">
                                <div class="flex items-center justify-end gap-2">
                                    @can('settings.dealers.manage')
                                        <a
                                            href="{{ route('settings.dealers.edit', $dealer) }}"
                                            class="inline-flex items-center rounded-md border border-gray-200 px-2 py-1 text-xs text-gray-700 hover:bg-gray-50"
                                        >
                                            Edit
                                        </a>

                                        @if (! $dealer->is_default)
                                            <form
                                                id="dealer-toggle-{{ $dealer->id }}"
                                                method="POST"
                                                action="{{ route('settings.dealers.toggle-active', $dealer) }}"
                                            >
                                                @csrf
                                                @method('PATCH')
                                                <button
                                                    type="button"
                                                    @click="$store.__confirm.openConfirm('Are you sure you want to change this dealer&#039;s active status?', 'dealer-toggle-{{ $dealer->id }}')"
                                                    class="inline-flex items-center rounded-md border border-gray-200 px-2 py-1 text-xs text-gray-700 hover:bg-gray-50"
                                                >
                                                    {{ $dealer->is_active ? 'Deactivate' : 'Activate' }}
                                                </button>
                                            </form>
                                        @endif
                                    @endcan
                                </div>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="6" class="px-4 py-4 text-center text-sm text-gray-500">
                                No dealers found.
                            </td>
                        </tr>
                    @endforelse
                </tbody>
            </table>

            <div class="px-4 py-3 border-t border-gray-100">
                {{ $dealers->links() }}
            </div>
        </div>

    </div>
</x-app-layout>

