@extends('layouts.app') @section('title', 'Workflow Rescue — Diagnostic Engine') @push('stylesheets') @endpush @section('contents')
@if (session('error'))
{{ session('error') }}
@endif @if (session('success'))
{{ session('success') }}
@endif
Search @if ($report) Export PDF @if ($recoveryEnabled) Safe Recovery available @else READ ONLY — no repairs @endif @endif
@if ($notFound)
PC Form #{{ $pcFormId }} was not found.
@elseif ($report)

Form #{{ $report['pc_form_id'] }}

@if (! empty($report['imported_from_pcfms'])) Imported From PCFMS @endif

{{ $report['message'] }}

Overall Health Score
{{ $score }}%
@if ($scoreLabel === 'Healthy') {{ $scoreLabel }} @elseif ($scoreLabel === 'Needs Attention') {{ $scoreLabel }} @else {{ $scoreLabel }} @endif PASS {{ $health['pass'] ?? 0 }} · WARNING {{ $health['warning'] ?? 0 }} · FAIL {{ $health['fail'] ?? 0 }}
@if ($tab === 'diagnostic')

1. General Information

PC Number{{ $s['pc_number'] ?? '—' }}
UID{{ $s['uid'] ?? '—' }}
Kind{{ $s['kind'] ?? '—' }}
Scheme{{ $s['scheme'] ?? '—' }}
ADP{{ $s['adp'] ?? '—' }}
Department{{ $s['department'] ?? '—' }}
Current / Parent / Root #{{ $s['current_revision_id'] ?? '—' }} / #{{ $s['parent_form_id'] ?? '—' }} / #{{ $s['root_form_id'] ?? '—' }}
Created / Updated {{ $s['created_at'] ?? '—' }} / {{ $s['updated_at'] ?? '—' }}
Imported From PCFMS{{ !empty($s['imported_from_pcfms']) ? 'Yes' : 'No' }}
Import Batch{{ $s['import_batch'] ?? '—' }}
Source Table / ID {{ $s['source_table'] ?? '—' }} / {{ $s['source_id'] ?? '—' }}

2. Workflow Information

State{{ $s['state'] ?? '—' }}
Workflow Stage{{ $s['workflow_stage'] ?? '—' }}
Current Actor{{ $s['current_actor_name'] ?? '—' }} @if(!empty($s['current_actor_id'])) (#{{ $s['current_actor_id'] }}) @endif
Current Owner{{ $s['current_owner'] ?? '—' }}
Created By{{ $s['created_by_name'] ?? '—' }} @if(!empty($s['created_by'])) (#{{ $s['created_by'] }}) @endif
Updated By{{ $s['updated_by_name'] ?? '—' }}
Allowed Actions{{ !empty($s['allowed_actions']) ? implode(', ', $s['allowed_actions']) : '—' }}
Uses Forward Workflow{{ !empty($s['uses_forward_workflow']) ? 'Yes' : 'No' }}
Meeting{{ $s['meeting_details'] ?? '—' }}
Forward / Review / Signature {{ $s['forward_count'] ?? 0 }} / {{ $s['review_count'] ?? 0 }} / {{ $s['signature_count'] ?? 0 }}

3. Revision Chain

@forelse ($s['revision_chain'] ?? [] as $i => $rev)
Version {{ $i + 1 }} — Form #{{ $rev['id'] }} {{ !empty($rev['status']) ? 'ACTIVE' : 'Inactive' }}
State: {{ $rev['state'] ?: '—' }} · Stage: {{ $rev['workflow_stage'] ?: '—' }} @if (!empty($rev['current_actor_id'])) · Actor #{{ $rev['current_actor_id'] }} @endif
@if (! $loop->last)
@endif @empty

No revision chain.

@endforelse
Active revision id: #{{ $s['active_revision_id'] ?? '—' }} · Active count: {{ $s['active_revision_count'] ?? 0 }}

4. Workflow Health Engine

@foreach ($health['rules'] ?? [] as $rule) @endforeach
RuleResultDetail
{{ $rule['label'] }} {{ $rule['result'] }} {{ $rule['detail'] }}

5. Permission Analyzer

@if (! empty($report['permission'])) @php $perm = $report['permission']; @endphp @if (empty($perm['user']))
User not found.
@else

Analyzing {{ $perm['user']['name'] }} (#{{ $perm['user']['id'] }}) @if (isset($perm['eligible_recipients_count'])) · eligibleRecipients() count: {{ $perm['eligible_recipients_count'] }} @endif

@foreach ($perm['checks'] as $check) @endforeach
CheckResultReason if FALSEMethod
{{ $check['label'] }} @if ($check['allowed']) TRUE @else FALSE @endif {{ $check['allowed'] ? '—' : ($check['reason'] ?? '—') }} {{ $check['method'] }}
@endif @else

Enter a user ID to run live PMS permission methods.

@endif
@endif @if ($tab === 'findings')

7. Diagnostic Findings

@foreach ($report['findings'] ?? [] as $f)
{{ $f['severity'] }} {{ $f['title'] }}
{{ $f['detail'] }}
Cause: {{ $f['cause'] }}
What should be fixed: {{ $f['fix'] }}
@endforeach
@endif @if ($tab === 'timeline')

6. Timeline

@forelse ($report['timeline'] ?? [] as $row) @empty @endforelse
DateActionFromTo StageRemarksOriginController / CLI
{{ $row['date'] ?? '—' }} {{ $row['action'] }} {{ $row['from_user'] }} {{ $row['to_user'] }} {{ $row['stage'] }} {{ $row['remarks'] }} {{ $row['origin'] ?? '—' }} {{ $row['source'] }}
No timeline events.
@endif @if ($tab === 'recommendations')

8. Recommendations (read-only)

No action buttons. Guidance only for Support.

@foreach ($report['recommendations'] ?? [] as $rec)
Recommendation: {{ $rec['recommendation'] }} {{ $rec['severity'] }}
Reason: {{ $rec['reason'] }}
@endforeach
@endif @if ($tab === 'recovery')

Safe Recovery Engine

Admin / Support only. One field at a time · validated · audited · reversible. If you applied the wrong fix, use Rollback on that action (below) — end users never see this panel.
@if (! empty($s))
Precheck (live diagnostic)
State: {{ $s['state'] ?? '—' }}
Stage: {{ $s['workflow_stage'] ?? '—' }}
Actor: {{ $s['current_owner'] ?? '—' }}
Created By: {{ $s['created_by_name'] ?? '—' }}
Health Score: {{ $health['score'] ?? 0 }}% ({{ $health['label'] ?? '—' }})
@endif @if (! empty($recoveryResult) && empty($rollbackResult) && ! empty($recoveryResult['log_id']) && can('workflow_rescue', 'recover'))
Recovery applied (log #{{ $recoveryResult['log_id'] }}). Health {{ $recoveryResult['health_before'] ?? '—' }}% → {{ $recoveryResult['health_after'] ?? '—' }}%. @if (! empty($recoveryResult['changed_fields']))
Changed: {{ implode(', ', $recoveryResult['changed_fields']) }}
@endif
Undo / Rollback this action
@elseif (! empty($recoveryResult) && empty($rollbackResult))
Postcheck Health {{ $recoveryResult['health_before'] ?? '—' }}% → {{ $recoveryResult['health_after'] ?? '—' }}%
@endif @if (! empty($rollbackResult)) @php $rbPost = $rollbackResult['postcheck'] ?? []; $rbSnap = $rbPost['snapshot'] ?? []; $rbHealth = $rbPost['health']['score'] ?? ($rollbackResult['health_after_rollback'] ?? '—'); @endphp
Rollback completed — form restored to pre-recovery fields (only those changed).
Health: {{ $rbHealth }}% · Actor: {{ $rbSnap['current_owner'] ?? ($rollbackResult['after_rollback']['current_actor_id'] ?? '—') }} · Stage: {{ $rbSnap['workflow_stage'] ?? ($rollbackResult['after_rollback']['workflow_stage'] ?? '—') }} · Created By: {{ $rbSnap['created_by_name'] ?? ($rollbackResult['after_rollback']['created_by'] ?? '—') }}
@endif @php $rollbackableByType = $rollbackableByType ?? []; $openRollbacks = array_values(array_filter($recoveryHistory ?? [], fn ($h) => ! empty($h['rollback_available']))); @endphp @if (! empty($openRollbacks) && can('workflow_rescue', 'recover'))
Admin undo — rollback a recovery
@foreach ($openRollbacks as $h) @endforeach
When Action By Fields Reason
{{ $h['timestamp'] }} {{ $h['action_type'] }} {{ $h['support_user'] }} @if (! empty($h['changed_fields'])) {{ implode(', ', $h['changed_fields']) }} @else — @endif {{ $h['reason'] ?: '—' }} Rollback
Rollback restores only the fields that recovery changed. Blocked if workflow progressed afterward.
@endif @if (! $recoveryEnabled || ! can('workflow_rescue', 'recover'))
Recovery permission required (Super Admin + workflow_rescue.recover).
@else
@foreach ($report['recovery_actions'] ?? [] as $action)
{{ $action['label'] }} @if (! empty($action['enabled'])) Available @else Unavailable @endif

{{ $action['note'] }}

@if (! empty($action['enabled']))
@csrf @if (in_array($action['key'], ['initialize_created_by', 'assign_current_actor'], true))
Defaults to this scheme’s department. Active users with roles/permissions only; eligibility is re-checked on apply.
@endif @if ($action['key'] === 'repair_meeting_link')
@endif @if ($action['key'] === 'change_status')
Only one Active PC is allowed per revision chain. Close siblings first if needed.
@endif @if ($action['key'] === 'close_as_approved')
Will set: status=Inactive, state=approved, workflow_stage=approved. Use on older revisions that still show “requested” while inactive.
@endif @if ($action['key'] === 'mark_active_revised')
Full chain: @foreach ($revisionChainOptions ?? [] as $opt) {{ $opt['label'] }} @endforeach
@endif
@endif @php $rbForAction = $rollbackableByType[$action['key']] ?? null; @endphp @if ($rbForAction && can('workflow_rescue', 'recover'))
Last apply by {{ $rbForAction['support_user'] }} at {{ $rbForAction['timestamp'] }} @if (! empty($rbForAction['changed_fields'])) · {{ implode(', ', $rbForAction['changed_fields']) }} @endif
Rollback {{ $action['label'] }}
@endif
@endforeach
@endif @if (! empty($recoveryHistory))
Full recovery / rollback history
    @foreach ($recoveryHistory as $h)
  1. @if (! empty($h['is_rollback'])) Rollback @else Recovery @endif {{ $h['action_type'] }} by {{ $h['support_user'] }} at {{ $h['timestamp'] }} @if ($h['reason'] !== '') — {{ $h['reason'] }} @endif @if (! empty($h['rollback_available']) && can('workflow_rescue', 'recover')) · Rollback @elseif (! empty($h['rollback_used'])) · rolled back {{ $h['rollback_timestamp'] }} @endif
  2. @endforeach
@endif
@endif @endif
@endsection @push('scripts') @endpush