@php
    /** @var \App\Models\Dealer $dealer */
@endphp

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

    <div class="p-6 space-y-6">
        <div class="flex flex-col sm:flex-row sm:items-center sm:justify-between gap-3">
            <div>
                <h1 class="text-xl font-semibold text-gray-900">
                    {{ $dealer->name }}
                </h1>
                <p class="mt-1 text-sm text-gray-600">
                    Code:
                    <span class="font-mono text-gray-800">{{ $dealer->code }}</span>
                    @if ($dealer->is_default)
                        <span class="ml-2 inline-flex items-center rounded-full bg-blue-50 px-2 py-0.5 text-xs font-medium text-blue-700">
                            Default dealer
                        </span>
                    @endif
                </p>
            </div>

            <a
                href="{{ route('settings.dealers.index') }}"
                class="inline-flex items-center rounded-md border border-gray-200 px-3 py-1.5 text-xs sm:text-sm text-gray-700 hover:bg-gray-50 whitespace-nowrap"
            >
                Back to Dealers
            </a>
        </div>

        <div class="grid gap-6 lg:grid-cols-3">
            <div class="lg:col-span-2 space-y-6">
                <div class="bg-white border border-gray-200 rounded-xl shadow-sm p-5 space-y-4">
                    <h2 class="text-sm font-semibold text-gray-900">Dealer Information</h2>

                    <dl class="grid gap-x-6 gap-y-3 sm:grid-cols-2 text-sm">
                        <div>
                            <dt class="text-gray-500">Name</dt>
                            <dd class="mt-0.5 text-gray-900">{{ $dealer->name }}</dd>
                        </div>
                        <div>
                            <dt class="text-gray-500">Code</dt>
                            <dd class="mt-0.5 text-gray-900 font-mono text-xs">{{ $dealer->code }}</dd>
                        </div>
                        <div>
                            <dt class="text-gray-500">Business Registration Number</dt>
                            <dd class="mt-0.5 text-gray-900">{{ $dealer->business_registration_number ?? '—' }}</dd>
                        </div>
                        <div>
                            <dt class="text-gray-500">VAT Number</dt>
                            <dd class="mt-0.5 text-gray-900">{{ $dealer->vat_number ?? '—' }}</dd>
                        </div>
                        <div class="sm:col-span-2">
                            <dt class="text-gray-500">Address</dt>
                            <dd class="mt-0 text-gray-900 whitespace-pre-line">
                                {{ $dealer->address ?? '—' }}
                            </dd>
                        </div>
                        <div>
                            <dt class="text-gray-500">Tel</dt>
                            <dd class="mt-0.5 text-gray-900">{{ $dealer->telephone ?? '—' }}</dd>
                        </div>
                        <div>
                            <dt class="text-gray-500">General Contact Email</dt>
                            <dd class="mt-0.5 text-gray-900">
                                @if ($dealer->contact_email)
                                    <a href="mailto:{{ $dealer->contact_email }}" class="text-blue-600 hover:underline">
                                        {{ $dealer->contact_email }}
                                    </a>
                                @else
                                    —
                                @endif
                            </dd>
                        </div>
                    </dl>
                </div>

                <div class="bg-white border border-gray-200 rounded-xl shadow-sm p-5 space-y-4">
                    <h2 class="text-sm font-semibold text-gray-900">Main Contact</h2>

                    <dl class="grid gap-x-6 gap-y-3 sm:grid-cols-3 text-sm">
                        <div>
                            <dt class="text-gray-500">Name</dt>
                            <dd class="mt-0.5 text-gray-900">{{ $dealer->main_contact_name ?? '—' }}</dd>
                        </div>
                        <div>
                            <dt class="text-gray-500">Cell</dt>
                            <dd class="mt-0.5 text-gray-900">{{ $dealer->main_contact_cell ?? '—' }}</dd>
                        </div>
                        <div>
                            <dt class="text-gray-500">Email</dt>
                            <dd class="mt-0.5 text-gray-900">
                                @if ($dealer->main_contact_email)
                                    <a href="mailto:{{ $dealer->main_contact_email }}" class="text-blue-600 hover:underline">
                                        {{ $dealer->main_contact_email }}
                                    </a>
                                @else
                                    —
                                @endif
                            </dd>
                        </div>
                    </dl>
                </div>
            </div>

            <div class="space-y-4">
                <div class="bg-white border border-gray-200 rounded-xl shadow-sm p-5 space-y-3">
                    <h2 class="text-sm font-semibold text-gray-900">Documents</h2>
                    <p class="text-xs text-gray-600">
                        Compliance documents uploaded for this dealer.
                    </p>

                    @php
                        $cipcDocs = $dealer->documents->where('document_type', 'cipc');
                        $vatDoc = $dealer->documents->firstWhere('document_type', 'vat_certificate');
                        $proofDoc = $dealer->documents->firstWhere('document_type', 'proof_of_address');
                    @endphp

                    <div class="space-y-3">
                        <div>
                            <h3 class="text-xs font-semibold text-gray-800 uppercase tracking-wide">CIPC Documents</h3>
                            @if ($cipcDocs->isEmpty())
                                <p class="mt-1 text-xs text-gray-500">No CIPC documents uploaded.</p>
                            @else
                                <ul class="mt-1 space-y-1 text-xs">
                                    @foreach ($cipcDocs as $doc)
                                        <li class="flex items-center justify-between gap-2">
                                            <div>
                                                <div class="text-gray-800">
                                                    {{ $doc->original_name }}
                                                </div>
                                                <div class="text-[11px] text-gray-500">
                                                    Uploaded {{ $doc->created_at?->format('Y-m-d H:i') ?? '—' }}
                                                </div>
                                            </div>
                                            <a
                                                href="{{ route('settings.dealers.documents.show', [$dealer, $doc]) }}"
                                                target="_blank"
                                                class="inline-flex items-center rounded-md border border-gray-200 px-2 py-1 text-[11px] text-gray-700 hover:bg-gray-50 whitespace-nowrap"
                                            >
                                                View
                                            </a>
                                        </li>
                                    @endforeach
                                </ul>
                            @endif
                        </div>

                        <div class="pt-2 border-t border-gray-100">
                            <h3 class="text-xs font-semibold text-gray-800 uppercase tracking-wide">VAT Certificate</h3>
                            @if (! $vatDoc)
                                <p class="mt-1 text-xs text-gray-500">No VAT certificate uploaded.</p>
                            @else
                                <div class="mt-1 flex items-center justify-between gap-2 text-xs">
                                    <div>
                                        <div class="text-gray-800">
                                            {{ $vatDoc->original_name }}
                                        </div>
                                        <div class="text-[11px] text-gray-500">
                                            Uploaded {{ $vatDoc->created_at?->format('Y-m-d H:i') ?? '—' }}
                                        </div>
                                    </div>
                                    <a
                                        href="{{ route('settings.dealers.documents.show', [$dealer, $vatDoc]) }}"
                                        target="_blank"
                                        class="inline-flex items-center rounded-md border border-gray-200 px-2 py-1 text-[11px] text-gray-700 hover:bg-gray-50 whitespace-nowrap"
                                    >
                                        View
                                    </a>
                                </div>
                            @endif
                        </div>

                        <div class="pt-2 border-t border-gray-100">
                            <h3 class="text-xs font-semibold text-gray-800 uppercase tracking-wide">Proof of Address</h3>
                            @if (! $proofDoc)
                                <p class="mt-1 text-xs text-gray-500">No proof of address uploaded.</p>
                            @else
                                <div class="mt-1 flex items-center justify-between gap-2 text-xs">
                                    <div>
                                        <div class="text-gray-800">
                                            {{ $proofDoc->original_name }}
                                        </div>
                                        <div class="text-[11px] text-gray-500">
                                            Uploaded {{ $proofDoc->created_at?->format('Y-m-d H:i') ?? '—' }}
                                        </div>
                                    </div>
                                    <a
                                        href="{{ route('settings.dealers.documents.show', [$dealer, $proofDoc]) }}"
                                        target="_blank"
                                        class="inline-flex items-center rounded-md border border-gray-200 px-2 py-1 text-[11px] text-gray-700 hover:bg-gray-50 whitespace-nowrap"
                                    >
                                        View
                                    </a>
                                </div>
                            @endif
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</x-app-layout>

