<div class="card shadow-sm">
    <div class="card-body">
        <h4 class="fw-bold mb-3 d-flex justify-content-between align-items-center">
            Contacts
            <button class="btn btn-primary btn-sm" wire:click="showAdd">
                <i class="mdi mdi-plus"></i>
            </button>
        </h4>

        @if($institution->contacts()->count())
            @foreach($institution->contacts as $contact)
                <div class="p-3 rounded bg-light border position-relative mb-2">
                    <div class="pe-5"> <!-- Right padding to avoid overlap with icons -->
                        <h5 class="mb-1 text-primary fw-bold">{{ $contact->name }}</h5>
                        <p class="mb-2 text-muted small fw-bold text-uppercase">{{ $contact->job_title }}</p>

                        <div class="d-flex flex-wrap gap-3 text-secondary small">
                            @if($contact->email)
                                <span><i class="mdi mdi-email-outline me-1"></i> {{ $contact->email }}</span>
                            @endif
                            @if($contact->mobile)
                                <span><i class="mdi mdi-cellphone me-1"></i> {{ $contact->mobile }}</span>
                            @endif
                            @if($contact->telephone)
                                <span><i class="mdi mdi-phone-outline me-1"></i> {{ $contact->telephone }}</span>
                            @endif
                        </div>
                    </div>

                    <div class="position-absolute top-0 end-0 p-2 d-flex gap-2">
                        <i class="mdi mdi-star action-icon me-2 {{ $contact->is_primary ? 'text-warning' : 'text-muted opacity-25' }}"
                           wire:click="makePrimary({{ $contact->id }})"
                           title="{{ $contact->is_primary ? 'Primary Contact' : 'Set as Primary' }}"
                           style="cursor: pointer;"></i>
                        <i class="mdi mdi-pencil-outline action-icon me-2" wire:click="loadContact({{ $contact->id }})" style="cursor: pointer;"></i>
                        <i class="mdi mdi-delete-outline action-icon text-danger" wire:click="deleteContact({{ $contact->id }})" style="cursor: pointer;"></i>
                    </div>
                </div>
            @endforeach
        @else
            <div class="text-center p-5 text-muted">
                <i class="mdi mdi-account-details" style="font-size: 48px;"></i>
                <p class="mt-3 fw-bold">No contacts have been added.</p>
            </div>
        @endif

    </div>

    @if($show)
        <div>
            <div class="offcanvas offcanvas-end show"  tabindex="-1" style="--bs-offcanvas-width: 600px;" role="dialog">
                <div class="offcanvas-header">
                    <h5 id="offcanvasRightLabel">{{ $editing ? 'Edit' : 'Add' }} Contact</h5>
                    <button type="button" class="btn-close text-dark" wire:click.prevent="hideAdd"></button>
                </div>

                <div class="offcanvas-body  bg-light">
                    @if($editing)
                        <form method="post" wire:submit="editContact({{ $editing_id }})">
                            @else
                                <form method="post" wire:submit="addContact">
                                    @endif

                                    @csrf

                                    <x-ui.input name="name" label="Full Name" />
                                    <x-ui.input name="job_title" label="Job Title"/>

                                    <hr class="my-4">

                                    <x-ui.input name="email" label="Email Address" />
                                    <x-ui.input name="mobile" label="Mobile Number" />
                                    <x-ui.input name="telephone" label="Telephone (Landline)" />

                                    <button type="submit" class="btn btn-primary w-100 mt-4">{{ $editing ? 'Save Changes' : 'Add Contact' }}</button>
                                </form>

                </div>
            </div>
            <!-- Backdrop for offcanvas -->
            <div class="offcanvas-backdrop fade show" wire:click="hideAdd"></div>
        </div>
    @endif
</div>