<div class="tab-pane fade p-4" id="invitedMeetingTab" role="tabpanel">
    <h5 class="fw-bold mb-4">Invited Meetings</h5>
    @forelse ($invitedMeetings as $meeting)
        <div class="border rounded p-3 mb-2">
            <strong>{{ $meeting->title ?: $meeting->subject ?: 'Meeting' }}</strong>
            <span class="badge bg-soft-primary text-primary ms-2">{{ $meeting->meeting_type ?: 'Meeting' }}</span>
            <div class="text-muted">{{ $meeting->start_date }} {{ $meeting->start_time }} · {{ $meeting->location ?: 'No location' }}</div>
            @if ($meeting->agenda)<div class="mt-1">{{ $meeting->agenda }}</div>@endif
        </div>
    @empty
        <p class="text-muted">No invited meetings are linked to this Contact.</p>
    @endforelse
</div>

<div class="tab-pane fade p-4" id="productTab" role="tabpanel">
    <h5 class="fw-bold mb-4">Products</h5>
    <div class="table-responsive"><table class="table">
        <thead><tr><th>Product</th><th>Code</th><th>Price</th><th>Status</th></tr></thead>
        <tbody>
        @forelse ($products as $product)
            <tr><td>{{ $product->product_name }}</td><td>{{ $product->product_code }}</td>
                <td>{{ number_format((float) $product->price, 2) }}</td><td>{{ $product->status }}</td></tr>
        @empty
            <tr><td colspan="4" class="text-muted">No products are linked directly or through Deals.</td></tr>
        @endforelse
        </tbody>
    </table></div>
</div>

<div class="tab-pane fade p-4" id="accountDetailsTab" role="tabpanel">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h5 class="fw-bold mb-0">Accounts</h5>
        <span class="badge bg-soft-success text-success">Total Deal Value: {{ number_format($totalDealValue, 2) }}</span>
    </div>
    <div class="table-responsive"><table class="table">
        <thead><tr><th>Account</th><th>Number</th><th>Type</th><th>Relationship</th></tr></thead>
        <tbody>
        @forelse ($contact->accounts as $account)
            <tr><td>{{ $account->account_name }}</td><td>{{ $account->account_number ?: '—' }}</td>
                <td>{{ $account->account_type ?: '—' }}</td>
                <td>{{ $account->pivot->is_primary ? 'Primary' : ($account->pivot->role ?: 'Related') }}</td></tr>
        @empty
            <tr><td colspan="4" class="text-muted">No Accounts are linked.</td></tr>
        @endforelse
        </tbody>
    </table></div>
</div>

<div class="tab-pane fade p-4" id="tradeDetailsTab" role="tabpanel">
    <h5 class="fw-bold mb-4">Trade Details</h5>
    <div class="table-responsive"><table class="table">
        <thead><tr><th>Document</th><th>Type</th><th>Deal</th><th>Status</th><th>Total</th><th>Payment</th></tr></thead>
        <tbody>
        @forelse ($tradeDocuments as $document)
            <tr><td>{{ $document->document_number }}</td><td>{{ ucfirst($document->type) }}</td>
                <td>{{ $document->deal?->name ?: '#'.$document->deal_id }}</td><td>{{ $document->status }}</td>
                <td>{{ $document->currency }} {{ number_format((float) $document->total, 2) }}</td>
                <td>{{ ucfirst($document->payment_status) }}</td></tr>
        @empty
            <tr><td colspan="6" class="text-muted">No quotations, proposals, or invoices are linked through this Contact’s Deals.</td></tr>
        @endforelse
        </tbody>
    </table></div>
</div>

<div class="tab-pane fade p-4" id="projectsTab" role="tabpanel">
    <h5 class="fw-bold mb-4">Projects</h5>
    @forelse ($projects as $project)
        <div class="border rounded p-3 mb-2 d-flex justify-content-between">
            <div><strong>{{ $project->name }}</strong><div class="text-muted">{{ $project->description }}</div></div>
            <div class="text-end"><span class="badge bg-soft-primary text-primary">{{ $project->status }}</span>
                <div class="small mt-1">{{ $project->progress }}% complete</div></div>
        </div>
    @empty
        <p class="text-muted">No projects are linked directly or through Deals.</p>
    @endforelse
</div>

<div class="tab-pane fade p-4" id="filesTab" role="tabpanel">
    <x-crm-file-manager module="contacts" :record="$contact" />
</div>

<div class="tab-pane fade p-4" id="caseTab" role="tabpanel">
    <div class="d-flex justify-content-between mb-4"><h5 class="fw-bold mb-0">Contact Cases</h5><a class="btn btn-sm btn-primary" href="{{ route('cases.index',['contact_id'=>$contact->id]) }}">Create Case</a></div>
    @forelse ($cases as $case)
        <div class="border rounded p-3 mb-2"><strong>{{ $case->case_number }} · {{ $case->subject }}</strong>
            <span class="badge bg-soft-warning text-warning ms-2">{{ $case->priority }}</span>
            <span class="badge bg-soft-primary text-primary ms-1">{{ $case->status }}</span>
            <div class="text-muted mt-1">{{ $case->description }}</div><a href="{{ route('cases.show',$case) }}">Open Case</a></div>
    @empty
        <p class="text-muted">No Cases are assigned to this Contact.</p>
    @endforelse
</div>

<div class="tab-pane fade p-4" id="surveyTab" role="tabpanel">
    <h5 class="fw-bold mb-4">Contact Surveys</h5>
    @forelse ($surveys as $survey)
        @php($surveyRecipient = $survey->recipients()->where('respondent_type', $contact::class)->where('respondent_id', $contact->id)->first())
        <div class="border rounded p-3 mb-2"><strong>{{ $survey->name }}</strong>
            <span class="badge bg-soft-primary text-primary ms-2">{{ $survey->type }}</span>
            <span class="badge bg-soft-success text-success ms-1">{{ $survey->status }}</span>
            <div class="text-muted mt-1">{{ $survey->description }}</div><a href="{{ route('surveys.show',$survey) }}">Analytics</a>@if($surveyRecipient) · <a href="{{ route('surveys.respond',$surveyRecipient->token) }}">{{ ucfirst($surveyRecipient->status) }}</a>@endif</div>
    @empty
        <p class="text-muted">No Surveys target this Contact.</p>
    @endforelse
</div>

<div class="tab-pane fade p-4" id="movementTab" role="tabpanel">
    <h5 class="fw-bold mb-4">Movement History</h5>
    @forelse ($moveHistories as $history)
        <div class="border-start border-3 border-primary ps-3 pb-4">
            <strong>Contact moved to {{ ucfirst($history->destination_type) }} #{{ $history->destination_id }}</strong>
            <div class="small text-muted">{{ $history->created_at->format('M d, Y H:i') }} by {{ $history->actor?->name ?? 'System' }}</div>
            @if ($history->reason)<div>{{ $history->reason }}</div>@endif
            <div class="small text-muted mt-1">
                Preserved {{ count($history->relationship_snapshot['account_ids'] ?? []) }} Accounts,
                {{ count($history->relationship_snapshot['activity_ids'] ?? []) }} Activities,
                {{ count($history->relationship_snapshot['file_ids'] ?? []) }} Files.
            </div>
        </div>
    @empty
        <p class="text-muted">No Contact movement has been recorded.</p>
    @endforelse
    <x-profile-notification-history entity-type="Contact" :entity-id="$contact->id" />
</div>
