{{--
    $deletable is opt-in: it renders a button wired to a deleteDsar($id) action on
    the parent Livewire component. Only pass it from a parent that defines one.
--}}
@props(['dsars', 'selectable' => false, 'deletable' => false])

@if($dsars->count())
<table class="table table-hover align-middle border">
    <thead class="table-light">
    <tr>
        @if($selectable)
            <th style="width:36px;">
                {{-- select-all handled by parent --}}
            </th>
        @endif
        <th style="width: 40%">Care Setting / Facility</th>
        <th>Sent Date</th>
        <th>Due Date</th>
        <th>Our Ref</th>
        <th class="text-center">Status</th>
        <th></th>
    </tr>
    </thead>
    <tbody>
    @foreach($dsars as $dsar)
        @php
            $status = $dsar->status ?? 'pending';
            $badgeClass = \App\Enum\Dsar\DsarStatus::badgeFor($status);
            $isPostable = $selectable && $status === 'needs posting';
        @endphp
        <tr class="{{ $isPostable ? '' : '' }}" wire:key="dsar-row-{{ $dsar->id }}">
            @if($selectable)
                <td>
                    @if($isPostable)
                        <input type="checkbox"
                               class="form-check-input"
                               value="{{ $dsar->id }}"
                               wire:model.live="postIds">
                    @endif
                </td>
            @endif
            <td>
                <div class="fw-bold">{{ $dsar->institution->display_name ?? 'Unknown' }}</div>
                <span class="badge border text-dark fw-normal ms-1">{{ $dsar->institution->dsarType->display_name ?? 'N/A' }}</span>
            </td>
            <td>
                {{ $dsar->created_at->format('d M Y') }}
                @if($dsar->posted_at)
                    <br><small class="text-muted">Posted {{ $dsar->posted_at->format('d M Y') }}</small>
                @endif
            </td>
            <td>
                @if($dsar->expected_at)
                    <span class="{{ $dsar->expected_at->isPast() && !in_array($dsar->status, ['completed', 'sent']) ? 'text-danger fw-semibold' : '' }}">
                        {{ $dsar->expected_at->format('d M Y') }}
                    </span>
                @else
                    <span class="text-muted">—</span>
                @endif
            </td>
            <td>
                @if($dsar->reference)
                    <span class="badge border text-secondary font-monospace fw-normal">{{ $dsar->reference }}</span>
                @else
                    <span class="text-muted small">—</span>
                @endif
            </td>
            <td class="text-center">
                <span class="badge {{ $badgeClass }} w-75">
                    {{ \App\Enum\Dsar\DsarStatus::label($status) }}
                </span>
            </td>
            <td class="text-nowrap">
                @can('dsars.view')
                <a wire:navigate href="{{ route('dsars.view', $dsar) }}" class="btn btn-md">
                    <span class="mdi mdi-search-web text-primary"></span>
                </a>
                @endcan
                @if($deletable)
                    @can('delete', $dsar)
                        <button type="button"
                                class="btn btn-md"
                                wire:click="deleteDsar({{ $dsar->id }})"
                                wire:confirm="Permanently delete the DSAR to &quot;{{ $dsar->institution->display_name ?? 'Unknown' }}&quot;? This cannot be undone."
                                title="Delete DSAR">
                            <span class="mdi mdi-delete text-danger"></span>
                        </button>
                    @endcan
                @endif
            </td>
        </tr>
    @endforeach
    </tbody>
</table>
@else
    <div class="text-center p-5 text-muted">
        <i class="mdi mdi-file-account" style="font-size: 48px;"></i>
        <p class="mt-3 fw-bold">No DSARs have been recorded.</p>
    </div>
@endif
