{{--
    Consents issued for an appointment. Rendered inside the appointment's
    Consents tab and re-fetched after a consent is issued or a signed copy
    is uploaded. Receives: $appointment, $consents
--}}
@php
    $catLabels = \Modules\VetClinic\Entities\VcConsentTemplate::categories();
    $langLabels = ['en' => 'English', 'ar' => 'العربية', 'both' => 'EN + AR'];
    // Pre-load the active signature ids so the row can show a "signed" badge cheaply.
    $signedIds = \Modules\VetClinic\Entities\VcConsentSignature::query()
        ->whereIn('consent_id', $consents->pluck('id'))
        ->whereNull('superseded_at')
        ->pluck('consent_id')
        ->all();
@endphp

@if($consents->isEmpty())
    <p class="text-muted" style="padding:14px;text-align:center;">
        <i class="fa fa-file-signature"></i> No consent forms have been issued for this appointment yet.
    </p>
@else
<table class="table table-bordered table-condensed" style="margin-bottom:0;">
    <thead>
        <tr>
            <th>Consent Form</th>
            <th style="width:80px;">Language</th>
            <th style="width:140px;">Issued</th>
            <th style="width:180px;">Signed Copy</th>
            <th style="width:120px;">Actions</th>
        </tr>
    </thead>
    <tbody>
        @foreach($consents as $c)
        @php $isDigitallySigned = in_array($c->id, $signedIds, true); @endphp
        <tr @if(!empty($c->archived_at)) style="opacity:.55;" @endif>
            <td>
                <strong>{{ $c->title }}</strong>
                <span class="label label-{{ \Modules\VetClinic\Entities\VcConsentTemplate::categoryColor($c->category) }}">
                    {{ $catLabels[$c->category] ?? $c->category }}
                </span>
                @if($isDigitallySigned)
                    <span class="label label-success" title="Digitally signed on device"><i class="fa fa-signature"></i> Signed</span>
                @endif
                @if(!empty($c->archived_at))
                    <span class="label label-default" title="Archived"><i class="fa fa-archive"></i> Archived</span>
                @endif
                @if($c->signatory_name)
                <div class="text-muted" style="font-size:11px;margin-top:2px;">
                    <i class="fa fa-user"></i> {{ $c->signatory_name }}
                    <span style="color:#94a3b8;">({{ $c->signatory_relationship }})</span>
                    @if($c->witness_name) &nbsp;·&nbsp; Witness: {{ $c->witness_name }} @endif
                </div>
                @endif
            </td>
            <td><span class="label label-default">{{ $langLabels[$c->language] ?? $c->language }}</span></td>
            <td style="font-size:11px;">
                {{ optional($c->issued_at)->format('d M Y · H:i') }}
                <br><span class="text-muted">by {{ optional($c->issuedBy)->first_name }} {{ optional($c->issuedBy)->last_name }}</span>
            </td>
            <td>
                @if($c->has_signed_copy)
                    <a href="{{ $c->signed_url }}" target="_blank" class="btn btn-xs btn-success">
                        <i class="fa fa-check-circle"></i> View signed
                    </a>
                    @can('vet_clinic.manage_appointments')
                    <button class="btn btn-xs btn-default btn-upload-signed" data-id="{{ $c->id }}" title="Replace">
                        <i class="fa fa-sync"></i>
                    </button>
                    <button class="btn btn-xs btn-default btn-remove-signed" data-id="{{ $c->id }}" title="Remove">
                        <i class="fa fa-times"></i>
                    </button>
                    @endcan
                    <div class="text-muted" style="font-size:10px;margin-top:2px;">
                        {{ optional($c->signed_uploaded_at)->format('d M Y') }}
                    </div>
                @else
                    @can('vet_clinic.manage_appointments')
                    <button class="btn btn-xs btn-warning btn-upload-signed" data-id="{{ $c->id }}">
                        <i class="fa fa-upload"></i> Upload signed
                    </button>
                    @else
                    <span class="text-muted" style="font-size:11px;">Not uploaded</span>
                    @endcan
                @endif
            </td>
            <td style="white-space:nowrap;">
                @can('vet_clinic.manage_appointments')
                    @if(empty($c->archived_at))
                        <a href="{{ route('vc.consents.sign', $c->id) }}" target="_blank" class="btn btn-xs btn-success" title="Capture digital signature on tablet">
                            <i class="fa fa-signature"></i> {{ $isDigitallySigned ? 'Re-sign' : 'Sign' }}
                        </a>
                    @endif
                @endcan
                <a href="{{ route('vc.consents.print', $c->id) }}" target="_blank" class="btn btn-xs btn-primary">
                    <i class="fa fa-print"></i> Print
                </a>
                @can('vet_clinic.manage_appointments')
                    @if($isDigitallySigned && empty($c->archived_at))
                        <button class="btn btn-xs btn-default btn-archive-consent" data-id="{{ $c->id }}" title="Archive (signed consents cannot be deleted)">
                            <i class="fa fa-archive"></i>
                        </button>
                    @elseif(empty($c->archived_at))
                        <button class="btn btn-xs btn-danger btn-del-consent" data-id="{{ $c->id }}" title="Delete">
                            <i class="fa fa-trash"></i>
                        </button>
                    @endif
                @endcan
            </td>
        </tr>
        @endforeach
    </tbody>
</table>
@endif
