<div class="card shadow-sm" x-data="{
    show: @entangle('showOffcanvas'),
    instance: null,
    init() {
        this.instance = new bootstrap.Offcanvas($refs.canvas);
        $watch('show', value => {
            if (value) {
                this.instance.show();
            } else {
                this.instance.hide();
            }
        });
        $refs.canvas.addEventListener('hidden.bs.offcanvas', () => {
            this.show = false;
        });
    }
}">
    <div class="card-body">
        <!-- Main Header & Trigger -->
        <div class="d-flex justify-content-between align-items-center mb-3">
            <h4 class="fw-bold m-0">Family Tree</h4>
            <button class="btn btn-primary btn-sm" wire:click="create">
                <i class="mdi mdi-plus"></i>
            </button>
        </div>

        <!-- Read-Only Summary List -->
        @if($relations->isEmpty())
            <div class="text-center p-5 text-muted">
                <i class="mdi mdi-account-group-outline" style="font-size: 48px;"></i>
                <p class="mt-2 mb-0 fw-bold">No relations recorded.</p>
            </div>
        @else
            <div class="list-group">
                @foreach($relations as $relation)
                    <div class="list-group-item bg-light mb-2 rounded border-0 d-flex justify-content-between align-items-center">
                        <!-- Info Column -->
                        <div class="flex-grow-1">
                            <p class="mb-1 fw-semibold">
                                {{ $relation->first_name }} {{ $relation->last_name }}
                                <span class="badge bg-white text-primary border ms-1" style="font-size: 0.75em;">
                                    {{ $relation->type }}
                                </span>
                            </p>

                            <div class="text-muted small">
                                @if(!empty($relation->email))
                                    <span class="me-2"><i class="mdi mdi-email-outline me-1"></i>{{ $relation->email }}</span>
                                @endif
                                @if(!empty($relation->phone))
                                    <span><i class="mdi mdi-phone-outline me-1"></i>{{ $relation->phone }}</span>
                                @endif
                            </div>

                            @if(!empty($relation->notes))
                                <small class="text-muted d-block mt-1 fst-italic">
                                    <i class="mdi mdi-information-outline me-1"></i>{{ $relation->notes }}
                                </small>
                            @endif
                        </div>

                        <!-- Actions Column -->
                        <div class="d-flex gap-2 ms-3">
                            <button wire:click="edit({{ $relation->id }})" class="btn btn-md">
                                <i class="mdi mdi-pencil"></i>
                            </button>
                            <button wire:click="delete({{ $relation->id }})"
                                    class="btn btn-md"
                                    onclick="confirm('Are you sure you want to remove this family member?') || event.stopImmediatePropagation()">
                                <i class="mdi mdi-trash-can text-danger"></i>
                            </button>
                        </div>
                    </div>
                @endforeach
            </div>
        @endif
    </div>

    <!-- Offcanvas Editor -->
    <div wire:ignore.self
         x-ref="canvas"
         class="offcanvas offcanvas-end"
         tabindex="-1"
         style="--bs-offcanvas-width: 500px;"
         id="relationOffcanvas">
        <div class="offcanvas-header border-bottom">
            <h5 class="offcanvas-title fw-bold">
                {{ $form['id'] ? 'Edit Family Member' : 'Add Family Member' }}
            </h5>
            <button type="button" class="btn-close text-reset" data-bs-dismiss="offcanvas"></button>
        </div>

        <div class="offcanvas-body bg-light">
            <form wire:submit.prevent="save">
                <div class="card border-0 shadow-sm">
                    <div class="card-body">
                        <div class="row g-3">
                            <div class="col-12">
                                <label class="form-label small fw-bold text-uppercase text-muted">Type</label>
                                <select wire:model="form.type" class="form-select @error('form.type') is-invalid @enderror">
                                    <option value="Spouse">Spouse</option>
                                    <option value="Child">Child</option>
                                    <option value="Parent">Parent</option>
                                    <option value="Sibling">Sibling</option>
                                    <option value="Other">Other</option>
                                </select>
                                @error('form.type') <span class="text-danger small">{{ $message }}</span> @enderror
                            </div>

                            <div class="col-md-6">
                                <label class="form-label small fw-bold text-uppercase text-muted">First Name</label>
                                <input type="text" wire:model="form.first_name" class="form-control @error('form.first_name') is-invalid @enderror">
                                @error('form.first_name') <span class="text-danger small">{{ $message }}</span> @enderror
                            </div>

                            <div class="col-md-6">
                                <label class="form-label small fw-bold text-uppercase text-muted">Last Name</label>
                                <input type="text" wire:model="form.last_name" class="form-control @error('form.last_name') is-invalid @enderror">
                                @error('form.last_name') <span class="text-danger small">{{ $message }}</span> @enderror
                            </div>

                            <div class="col-md-12">
                                <label class="form-label small fw-bold text-uppercase text-muted">Email</label>
                                <div class="input-group">
                                    <span class="input-group-text bg-light"><i class="mdi mdi-email-outline"></i></span>
                                    <input type="email" wire:model="form.email" class="form-control">
                                </div>
                                @error('form.email') <span class="text-danger small">{{ $message }}</span> @enderror
                            </div>

                            <div class="col-md-12">
                                <label class="form-label small fw-bold text-uppercase text-muted">Phone</label>
                                <div class="input-group">
                                    <span class="input-group-text bg-light"><i class="mdi mdi-phone"></i></span>
                                    <input type="text" wire:model="form.phone" class="form-control">
                                </div>
                                @error('form.phone') <span class="text-danger small">{{ $message }}</span> @enderror
                            </div>

                            <div class="col-12">
                                <label class="form-label small fw-bold text-uppercase text-muted">Notes</label>
                                <textarea wire:model="form.notes" class="form-control" rows="3" placeholder="Optional notes..."></textarea>
                                @error('form.notes') <span class="text-danger small">{{ $message }}</span> @enderror
                            </div>
                        </div>
                    </div>
                </div>

                <div class="d-grid mt-4">
                    <button type="submit" class="btn btn-success btn-lg shadow">
                        <i class="mdi mdi-check-circle me-2"></i>
                        {{ $form['id'] ? 'Update Member' : 'Save Member' }}
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>