<div>
    <x-title :breadcrumbs="[
        ['link' => route('wills-cases.index'), 'title' => 'Wills &amp; Trusts'],
        ['link' => route('wills-cases.view', $willsCase), 'title' => $willsCase->case_number ?? $willsCase->displayId],
    ]">
        {{ $willsCase->case_number ?? $willsCase->displayId }}
    </x-title>

    <div class="row">

        {{-- LEFT: CASE CARD --}}
        <div class="col-12 col-md-3">
            <div class="card shadow-sm mb-3">
                <div class="card-body text-center">
                    <div class="mb-2">
                        @if($willsCase->is_open)
                            <span class="badge bg-success fs-6">Open</span>
                        @else
                            <span class="badge bg-secondary fs-6">Closed</span>
                        @endif
                        @if($willsCase->ifa_referred)
                            <span class="badge bg-info ms-1">IFA Referred</span>
                        @endif
                    </div>

                    <h5 class="fw-bold mb-1">{{ $willsCase->case_number ?? $willsCase->displayId }}</h5>

                    <hr>

                    <div class="text-start small">
                        <div class="mb-1">
                            <span class="text-muted">Client:</span>
                            <a href="{{ route('clients.show', $willsCase->client) }}">
                                {{ $willsCase->client->name_first }} {{ $willsCase->client->name_last }}
                            </a>
                        </div>
                        @if($willsCase->rep)
                            <div class="mb-1">
                                <span class="text-muted">REP:</span> {{ $willsCase->rep }}
                            </div>
                        @endif
                        @if($willsCase->case_owner)
                            <div class="mb-1">
                                <span class="text-muted">Case Owner:</span> {{ $willsCase->case_owner }}
                            </div>
                        @endif
                        @if($willsCase->opened_at)
                            <div class="mb-1">
                                <span class="text-muted">Opened:</span> {{ $willsCase->opened_at->format('d/m/Y') }}
                            </div>
                        @endif
                        @if($willsCase->closed_at)
                            <div class="mb-1">
                                <span class="text-muted">Closed:</span> {{ $willsCase->closed_at->format('d/m/Y') }}
                            </div>
                        @endif
                        @if($willsCase->occupation)
                            <div class="mb-1">
                                <span class="text-muted">Occupation:</span> {{ $willsCase->occupation }}
                            </div>
                        @endif
                        @if($willsCase->national_insurance)
                            <div class="mb-1">
                                <span class="text-muted">NI:</span> {{ $willsCase->national_insurance }}
                            </div>
                        @endif

                        <hr class="my-2">

                        <div class="mb-1" title="Mobile">
                            <i class="mdi mdi-cellphone text-muted me-1"></i>
                            @if($willsCase->client->phone_number)
                                <a href="tel:{{ preg_replace('/\s+/', '', $willsCase->client->phone_number) }}">{{ $willsCase->client->phone_number }}</a>
                            @else
                                —
                            @endif
                        </div>
                        <div class="mb-1" title="Landline">
                            <i class="mdi mdi-phone-classic text-muted me-1"></i>
                            @if($willsCase->client->landline)
                                <a href="tel:{{ preg_replace('/\s+/', '', $willsCase->client->landline) }}">{{ $willsCase->client->landline }}</a>
                            @else
                                —
                            @endif
                        </div>
                        <div class="mb-1" title="Email">
                            <i class="mdi mdi-email-outline text-muted me-1"></i>
                            @if($willsCase->client->email_address)
                                <a href="mailto:{{ $willsCase->client->email_address }}">{{ $willsCase->client->email_address }}</a>
                            @else
                                —
                            @endif
                        </div>
                        <div class="mb-1" title="Address">
                            <i class="mdi mdi-home text-muted me-1"></i>
                            @if($willsCase->client->currentAddress()->exists())
                                {!! $willsCase->client->currentAddress->asHtml() !!}
                            @else
                                —
                            @endif
                        </div>
                    </div>
                </div>
            </div>

            {{-- STATUS --}}
            <livewire:wills-case.edit-status :wills-case="$willsCase"/>

            {{-- QUICK ACTIONS --}}
            <div class="card shadow-sm mb-3">
                <div class="card-body">
                    <h6 class="fw-bold mb-3">Actions</h6>
                    @can('edit', $willsCase)
                        <a href="{{ route('wills-cases.edit', $willsCase) }}"
                           class="btn btn-sm btn-outline-primary w-100 mb-2">
                            <i class="mdi mdi-pencil me-1"></i> Edit Case
                        </a>
                    @endcan
                    <a href="{{ route('clients.show', $willsCase->client) }}"
                       class="btn btn-sm btn-outline-secondary w-100">
                        <i class="mdi mdi-account me-1"></i> View Client
                    </a>
                </div>
            </div>

            {{-- Quick Texter --}}
            <livewire:sms.quick-texter :owner="$willsCase" :default-phone="$willsCase->client->phone_number ?? ''" />

        </div>

        {{-- CENTER: TABS --}}
        <div class="col-12 col-md-9">
            <div class="card shadow-sm">
                <div class="card-body">

                    {{-- TAB NAV --}}
                    <ul class="nav nav-tabs mb-4 flex-wrap">
                        @foreach([
                            'overview'     => 'Overview',
                            'key-details'  => 'Key Details',
                            'dates'        => 'Dates',
                            'workflow'     => 'Workflow',
                            'registration' => 'Registration',
                            'notes'        => 'Notes',
                            'tasks'        => 'Tasks',
                            'documents'    => 'Documents',
                            'history'      => 'History',
                            'addresses'    => 'Addresses',
                            'senders'      => 'Senders',
                        ] as $tab => $label)
                            <li class="nav-item">
                                <a class="nav-link {{ $activeTab === $tab ? 'active' : '' }}"
                                   style="{{ $activeTab === $tab ? 'border-top: 3px solid #0d6efd; background: white;' : '' }}"
                                   wire:click.prevent="setTab('{{ $tab }}')">
                                    {{ $label }}
                                </a>
                            </li>
                        @endforeach
                    </ul>

                    <div wire:loading wire:target="setTab" class="p-3">
                        @include('components.skeleton.tab-content')
                    </div>

                    <div wire:loading.remove wire:target="setTab">

                        {{-- OVERVIEW --}}
                        @if($activeTab === 'overview')
                            <table class="table table-sm">
                                <tbody>
                                <tr><th class="text-muted fw-normal w-40">Case Number</th><td>{{ $willsCase->case_number ?? '—' }}</td></tr>
                                <tr><th class="text-muted fw-normal">REP</th><td>{{ $willsCase->rep ?? '—' }}</td></tr>
                                <tr><th class="text-muted fw-normal">Case Owner</th><td>{{ $willsCase->case_owner ?? '—' }}</td></tr>
                                <tr><th class="text-muted fw-normal">Drafting Contact</th><td>{{ $willsCase->drafting_contact ?? '—' }}</td></tr>
                                <tr>
                                    <th class="text-muted fw-normal">Products</th>
                                    <td>
                                        @if($willsCase->products)
                                            {{ implode(', ', $willsCase->products) }}
                                        @else
                                            —
                                        @endif
                                    </td>
                                </tr>
                                <tr><th class="text-muted fw-normal">Our Fee</th><td>{{ $willsCase->our_fee ?? '—' }}</td></tr>
                                <tr><th class="text-muted fw-normal">Our Fee Status</th><td>{{ $willsCase->our_fee_status ?? '—' }}</td></tr>
                                <tr><th class="text-muted fw-normal">AW&amp;T Invoice Issued</th><td>{{ $willsCase->awt_invoice_issued_at?->format('d/m/Y') ?? '—' }}</td></tr>
                                <tr><th class="text-muted fw-normal">External Draft Fee</th><td>{{ $willsCase->external_draft_fee ?? '—' }}</td></tr>
                                <tr><th class="text-muted fw-normal">Fee Structure</th><td>{{ $willsCase->fee_structure ?? '—' }}</td></tr>
                                <tr><th class="text-muted fw-normal">Occupation</th><td>{{ $willsCase->occupation ?? '—' }}</td></tr>
                                <tr><th class="text-muted fw-normal">National Insurance</th><td>{{ $willsCase->national_insurance ?? '—' }}</td></tr>
                                <tr>
                                    <th class="text-muted fw-normal">IFA Referred</th>
                                    <td>
                                        @if($willsCase->ifa_referred)
                                            <span class="badge bg-info">Yes</span>
                                        @else
                                            <span class="badge bg-light text-dark">No</span>
                                        @endif
                                    </td>
                                </tr>
                                </tbody>
                            </table>

                        {{-- KEY DETAILS --}}
                        @elseif($activeTab === 'key-details')
                            <table class="table table-sm">
                                <tbody>
                                <tr>
                                    <th class="text-muted fw-normal w-40">Date of Birth</th>
                                    <td>
                                        @if($willsCase->client->date_of_birth)
                                            {{ $willsCase->client->date_of_birth->format('d/m/Y') }}
                                            <span class="text-muted">({{ $willsCase->client->date_of_birth->age }})</span>
                                        @else
                                            —
                                        @endif
                                    </td>
                                </tr>
                                <tr><th class="text-muted fw-normal">NI Number</th><td>{{ $willsCase->national_insurance ?? '—' }}</td></tr>
                                <tr><th class="text-muted fw-normal">Case Number</th><td>{{ $willsCase->case_number ?? '—' }}</td></tr>
                                <tr><th class="text-muted fw-normal">Occupation</th><td>{{ $willsCase->occupation ?? '—' }}</td></tr>
                                <tr><th class="text-muted fw-normal">Fee Structure</th><td>{{ $willsCase->fee_structure ?? '—' }}</td></tr>
                                <tr>
                                    <th class="text-muted fw-normal">IFA Referred</th>
                                    <td>
                                        @if($willsCase->ifa_referred)
                                            <span class="badge bg-info">Yes</span>
                                        @else
                                            <span class="badge bg-light text-dark">No</span>
                                        @endif
                                    </td>
                                </tr>
                                </tbody>
                            </table>

                        {{-- DATES --}}
                        @elseif($activeTab === 'dates')
                            <table class="table table-sm">
                                <tbody>
                                <tr><th class="text-muted fw-normal w-40">Date/Time Opened</th><td>{{ $willsCase->opened_at ? $willsCase->opened_at->format('d/m/Y H:i') : '—' }}</td></tr>
                                <tr><th class="text-muted fw-normal">Date/Time Closed</th><td>{{ $willsCase->closed_at ? $willsCase->closed_at->format('d/m/Y H:i') : '—' }}</td></tr>
                                <tr><th class="text-muted fw-normal">IFA Referred Date</th><td>{{ $willsCase->ifa_referred_date ? $willsCase->ifa_referred_date->format('d/m/Y') : '—' }}</td></tr>
                                <tr><th class="text-muted fw-normal">AW&amp;T Invoice Issued</th><td>{{ $willsCase->awt_invoice_issued_at?->format('d/m/Y') ?? '—' }}</td></tr>
                                <tr><th class="text-muted fw-normal">Welcome Letter Sent</th><td>{{ $willsCase->welcome_letter_sent_at?->format('d/m/Y') ?? '—' }}</td></tr>
                                <tr><th class="text-muted fw-normal">Introduction Call</th><td>{{ $willsCase->introduction_call_at?->format('d/m/Y') ?? '—' }}</td></tr>
                                <tr><th class="text-muted fw-normal">Trust Docs Instructed</th><td>{{ $willsCase->trust_docs_instructed_at?->format('d/m/Y') ?? '—' }}</td></tr>
                                <tr><th class="text-muted fw-normal">In House Instructed</th><td>{{ $willsCase->in_house_instructed_at?->format('d/m/Y') ?? '—' }}</td></tr>
                                <tr><th class="text-muted fw-normal">Drafts Received</th><td>{{ $willsCase->drafts_received_at?->format('d/m/Y') ?? '—' }}</td></tr>
                                <tr><th class="text-muted fw-normal">Drafts Issued to Client</th><td>{{ $willsCase->drafts_issued_to_client_at?->format('d/m/Y') ?? '—' }}</td></tr>
                                <tr><th class="text-muted fw-normal">Master Copies Signed</th><td>{{ $willsCase->master_copies_signed_at?->format('d/m/Y') ?? '—' }}</td></tr>
                                <tr><th class="text-muted fw-normal">Master Copies Received</th><td>{{ $willsCase->master_copies_received_at?->format('d/m/Y') ?? '—' }}</td></tr>
                                <tr><th class="text-muted fw-normal">Date on Document Signed</th><td>{{ $willsCase->date_on_document_signed?->format('d/m/Y') ?? '—' }}</td></tr>
                                <tr><th class="text-muted fw-normal">Client Letters from HMRC</th><td>{{ $willsCase->client_letters_from_hmrc_at?->format('d/m/Y') ?? '—' }}</td></tr>
                                <tr><th class="text-muted fw-normal">Annual Minutes Task Set</th><td>{{ $willsCase->annual_minutes_task_set_at?->format('d/m/Y') ?? '—' }}</td></tr>
                                </tbody>
                            </table>

                        {{-- WORKFLOW --}}
                        @elseif($activeTab === 'workflow')
                            <table class="table table-sm">
                                <tbody>
                                <tr>
                                    <th class="text-muted fw-normal w-40">Intro Call By</th>
                                    <td>{{ $willsCase->intro_call_by ?? '—' }}</td>
                                </tr>
                                <tr>
                                    <th class="text-muted fw-normal">Docs Outstanding from Reps</th>
                                    <td>{{ $willsCase->docs_outstanding_from_reps ?? '—' }}</td>
                                </tr>
                                </tbody>
                            </table>

                        {{-- REGISTRATION --}}
                        @elseif($activeTab === 'registration')
                            <table class="table table-sm mb-4">
                                <tbody>
                                <tr>
                                    <th class="text-muted fw-normal w-40">Documents Requiring Registration</th>
                                    <td>
                                        @if(is_null($willsCase->documents_requiring_registration))
                                            —
                                        @elseif($willsCase->documents_requiring_registration)
                                            <span class="badge bg-warning text-dark">Yes</span>
                                        @else
                                            <span class="badge bg-light text-dark">No</span>
                                        @endif
                                    </td>
                                </tr>
                                </tbody>
                            </table>

                            <div class="row g-4">
                                <div class="col-12 col-md-6">
                                    <h6 class="fw-semibold">Registered by Aiker</h6>
                                    @if($willsCase->documents_to_register_aiker)
                                        <ul class="list-group list-group-flush">
                                            @foreach($willsCase->documents_to_register_aiker as $doc)
                                                <li class="list-group-item px-0 d-flex justify-content-between align-items-center">
                                                    <span class="small">{{ $doc }}</span>
                                                    <span class="small text-muted">
                                                        {{ isset($willsCase->registration_completed_aiker[$doc])
                                                            ? \Carbon\Carbon::parse($willsCase->registration_completed_aiker[$doc])->format('d/m/Y')
                                                            : '—' }}
                                                    </span>
                                                </li>
                                            @endforeach
                                        </ul>
                                    @else
                                        <p class="text-muted small">None selected.</p>
                                    @endif
                                </div>
                                <div class="col-12 col-md-6">
                                    <h6 class="fw-semibold">Registered by External</h6>
                                    @if($willsCase->documents_to_register_external)
                                        <ul class="list-group list-group-flush">
                                            @foreach($willsCase->documents_to_register_external as $doc)
                                                <li class="list-group-item px-0 d-flex justify-content-between align-items-center">
                                                    <span class="small">{{ $doc }}</span>
                                                    <span class="small text-muted">
                                                        {{ isset($willsCase->registration_completed_external[$doc])
                                                            ? \Carbon\Carbon::parse($willsCase->registration_completed_external[$doc])->format('d/m/Y')
                                                            : '—' }}
                                                    </span>
                                                </li>
                                            @endforeach
                                        </ul>
                                    @else
                                        <p class="text-muted small">None selected.</p>
                                    @endif
                                </div>
                            </div>

                        {{-- NOTES --}}
                        @elseif($activeTab === 'notes')
                            <livewire:note-manager :owner="$willsCase"/>

                        {{-- TASKS --}}
                        @elseif($activeTab === 'tasks')
                            <livewire:task.task-assigner :owner="$willsCase"/>

                        {{-- DOCUMENTS --}}
                        @elseif($activeTab === 'documents')
                            <livewire:document-manager :owner="$willsCase"/>

                        {{-- HISTORY --}}
                        @elseif($activeTab === 'history')
                            <livewire:history :owner="$willsCase"/>

                        {{-- ADDRESSES --}}
                        @elseif($activeTab === 'addresses')
                            <livewire:address-book :owner="$willsCase->client"/>

                        {{-- SENDERS --}}
                        @elseif($activeTab === 'senders')
                            <div class="row g-4">
                                <div class="col-12 col-xl-6">
                                    <livewire:settings.email-senders />
                                </div>
                                <div class="col-12 col-xl-6">
                                    <livewire:settings.phone-numbers />
                                </div>
                            </div>

                        @endif
                    </div>

                </div>
            </div>
        </div>

    </div>
</div>
