@php
    // Expects: $staff, $type, $label, $col, $canManage
    $current = $staff->$col;
    $history = $staff->renewals->where('type', $type);
    $chipCls = !$current ? 'secondary' : ((int) now()->startOfDay()->diffInDays(\Illuminate\Support\Carbon::parse($current)->startOfDay(), false) < 30 ? 'danger' : 'success');
@endphp
<div class="row">
    <div class="col-md-5 mb-3">
        <div class="border rounded p-3 h-100">
            <span class="text-xs text-uppercase text-muted">Current {{ $label }} Expiry</span>
            <div class="h4 mb-1 mt-1">{{ optional($current)->format('d M Y') ?? 'Not set' }}</div>
            @if($current)
                <span class="badge bg-{{ $chipCls }}">
                    @php $d = (int) now()->startOfDay()->diffInDays($current->startOfDay(), false); @endphp
                    {{ $d < 0 ? 'Expired '.abs($d).'d ago' : 'in '.$d.' days' }}
                </span>
            @endif
        </div>
    </div>

    @if($canManage)
    <div class="col-md-7 mb-3">
        <div class="border rounded p-3">
            <h6 class="text-sm mb-2">Record {{ $label }} Renewal</h6>
            <form action="{{ route('renewals.store', $staff) }}" method="POST" enctype="multipart/form-data">
                @csrf<input type="hidden" name="type" value="{{ $type }}">
                <div class="row g-2">
                    <div class="col-6"><label class="form-label text-xs">Previous Expiry</label><input type="date" name="previous_expiry" value="{{ optional($current)->format('Y-m-d') }}" class="form-control form-control-sm"></div>
                    <div class="col-6"><label class="form-label text-xs">New Expiry *</label><input type="date" name="new_expiry" class="form-control form-control-sm" required></div>
                    <div class="col-6"><label class="form-label text-xs">Renewed On</label><input type="date" name="renewed_on" value="{{ now()->format('Y-m-d') }}" class="form-control form-control-sm"></div>
                    <div class="col-6"><label class="form-label text-xs">Cost (RM)</label><input type="number" step="0.01" name="cost" class="form-control form-control-sm"></div>
                    <div class="col-6"><label class="form-label text-xs">Reference No</label><input name="reference_no" class="form-control form-control-sm"></div>
                    <div class="col-6"><label class="form-label text-xs">Document (scan)</label><input type="file" name="document" class="form-control form-control-sm"></div>
                    <div class="col-12"><label class="form-label text-xs">Remarks</label><textarea name="remarks" rows="1" class="form-control form-control-sm"></textarea></div>
                    <div class="col-12 text-end"><button class="btn btn-primary btn-sm mb-0">Save {{ $label }} Renewal</button></div>
                </div>
            </form>
        </div>
    </div>
    @endif

    <div class="col-12">
        <h6 class="text-xs text-uppercase text-muted">{{ $label }} History</h6>
        <div class="table-responsive"><table class="table table-sm align-items-center mb-0">
            <thead><tr class="text-xxs text-uppercase text-secondary"><th>Renewed On</th><th>Previous</th><th>New Expiry</th><th>Cost</th><th>Ref</th><th>By</th><th>Doc</th></tr></thead>
            <tbody>
            @forelse($history as $r)
                <tr>
                    <td class="text-sm">{{ optional($r->renewed_on)->format('d/m/Y') ?? '—' }}</td>
                    <td class="text-sm">{{ optional($r->previous_expiry)->format('d/m/Y') ?? '—' }}</td>
                    <td class="text-sm font-weight-bold">{{ optional($r->new_expiry)->format('d/m/Y') }}</td>
                    <td class="text-sm">{{ $r->cost ? 'RM '.number_format($r->cost,2) : '—' }}</td>
                    <td class="text-sm">{{ $r->reference_no ?? '—' }}</td>
                    <td class="text-sm">{{ $r->creator?->name ?? '—' }}</td>
                    <td>@if($r->document_path)<a href="{{ route('renewals.download', $r) }}" class="text-secondary"><i class="bi bi-paperclip"></i></a>@else—@endif</td>
                </tr>
            @empty
                <tr><td colspan="7" class="text-center text-muted py-3">No {{ strtolower($label) }} renewals recorded yet.</td></tr>
            @endforelse
            </tbody>
        </table></div>
    </div>
</div>
