<?php
/** @var array<string,mixed> $caseData */
$d=is_array($caseData??null)?$caseData:[];
$case=is_array($d['case']??null)?$d['case']:[];
$source=is_array($d['source']??null)?$d['source']:[];
$members=is_array($d['event_members']??null)?$d['event_members']:[];
$transactions=is_array($d['source_transactions']??null)?$d['source_transactions']:[];
$signals=is_array($d['signal_explanations']??null)?$d['signal_explanations']:[];
$notes=is_array($d['notes']??null)?$d['notes']:[];
$evidence=is_array($d['evidence']??null)?$d['evidence']:[];
$assignments=is_array($d['assignments']??null)?$d['assignments']:[];
$requests=is_array($d['information_requests']??null)?$d['information_requests']:[];
$findings=is_array($d['findings']??null)?$d['findings']:[];
$approvals=is_array($d['approvals']??null)?$d['approvals']:[];
$corrective=is_array($d['corrective_actions']??null)?$d['corrective_actions']:[];
$history=is_array($d['history']??null)?$d['history']:[];
$investigators=is_array($d['investigators']??null)?$d['investigators']:[];
$caps=is_array($capabilities??null)?$capabilities:[];
$returnUrl=is_string($returnUrl??null)?$returnUrl:'';
$isAr=current_lang()==='ar';
$tr=static function(string $en,string $ar)use($isAr):string{return $isAr?$ar:$en;};
$stageLabel=[
 'NEW'=>$tr('New','جديدة'),'ASSIGNED'=>$tr('Assigned','معيّنة'),'UNDER_REVIEW'=>$tr('Under review','قيد المراجعة'),
 'INFORMATION_REQUIRED'=>$tr('Information required','معلومات مطلوبة'),'FINDING_PROPOSED'=>$tr('Finding proposed','نتيجة مقترحة'),
 'SUPERVISOR_REVIEW'=>$tr('Supervisor review','مراجعة المشرف'),'MANAGEMENT_APPROVAL'=>$tr('Management approval','اعتماد الإدارة'),
 'CORRECTIVE_ACTION'=>$tr('Corrective action','إجراء تصحيحي'),'CLOSED'=>$tr('Closed','مغلقة')];
$outcomeLabel=[
 'AUTHORIZED_ACTIVITY'=>$tr('Authorized activity','نشاط مصرح به'),'OPERATIONAL_ERROR'=>$tr('Operational error','خطأ تشغيلي'),
 'DOCUMENTATION_GAP'=>$tr('Documentation gap','فجوة توثيق'),'SYSTEM_DATA_ISSUE'=>$tr('System or data issue','مشكلة نظام أو بيانات'),
 'POLICY_VIOLATION'=>$tr('Policy violation','مخالفة سياسة'),'POTENTIAL_MISUSE'=>$tr('Potential misuse','استخدام محتمل غير سليم'),
 'INSUFFICIENT_EVIDENCE'=>$tr('Insufficient evidence','أدلة غير كافية')];
$caseCode=(string)($case['case_code']??'');
$stage=strtoupper((string)($d['workflow_stage']??$case['workflow_stage_code']??'NEW'));
$sla=strtoupper((string)($d['sla_status']??'NO_DUE_DATE'));
$schemaReady=!empty($d['workflow_schema_ready']);
$entity=(string)($case['entity_type']??'CASE');
$severity=(string)($case['severity_code']??'INFO');
$latestFinding=is_array($findings[0]??null)?$findings[0]:[];
$evidenceParams=['case'=>$caseCode,'return'=>$returnUrl];
$hospital=(string)($source['hospital_code']??$case['site_code']??'');
$sourceDb=(string)($source['source_db']??'');
$snapshot=(int)($source['month_snapshot_id']??0);
$monthCase=(int)($source['month_case_id']??0);
if($hospital!==''&&$sourceDb!==''&&$snapshot>0&&$monthCase>0){$evidenceParams+=['hospital'=>$hospital,'source_db'=>$sourceDb,'snapshot_id'=>$snapshot,'month_case_id'=>$monthCase];}
$postAction='index.php?page=diar-investigation&lang='.rawurlencode(current_lang());
$hidden=function(string $action)use($caseCode,$returnUrl):string{return '<input type="hidden" name="action" value="'.e($action).'"><input type="hidden" name="case_code" value="'.e($caseCode).'"><input type="hidden" name="return_to" value="'.e($returnUrl).'">';};
?>
<section class="diar-case-page diar-functional-page diar-workflow-v4" data-diar-route="diar-investigation" data-workflow-stage="<?= e($stage) ?>">
 <nav class="case-breadcrumb"><a href="<?= e($returnUrl!==''?$returnUrl:url_to('diar-investigations')) ?>"><?= e($tr('Back to cases & events','العودة إلى القضايا والأحداث')) ?></a><span>/</span><strong><?= e($caseCode) ?></strong></nav>
 <?php if(!empty($successMessage)):?><div class="diar-alert success"><?= e($successMessage) ?></div><?php endif;?>
 <?php if(!empty($errorMessage)):?><div class="diar-alert error"><?= e($errorMessage) ?></div><?php endif;?>
 <?php if(!empty($loadError)):?><div class="diar-alert error"><?= e($loadError) ?></div>
 <?php elseif($case!==[]):?>
 <header class="case-hero workflow-case-hero"><div><div class="case-hero-tags"><span class="entity-tag"><?= e($entity==='EVENT'?$tr('CORRELATED EVENT','حدث مترابط'):$tr('INVESTIGATION CASE','قضية تحقيق')) ?></span><span class="severity-badge severity-<?= e(strtolower($severity)) ?>"><?= e($severity) ?></span><span class="workflow-stage-badge stage-<?= e(strtolower($stage)) ?>"><?= e($stageLabel[$stage]??$stage) ?></span></div><h1><?= e($caseCode) ?></h1><p><?= e((string)($case['review_reason']??$tr('Controlled evidence-based investigation.','تحقيق محكوم قائم على الأدلة.'))) ?></p></div><div class="risk-panel"><span><?= e($tr('Priority score','درجة الأولوية')) ?></span><strong><?= number_format((float)($case['risk_score']??0),0) ?></strong><small class="sla-badge sla-<?= e(strtolower($sla)) ?>"><?= e($sla) ?> · <?= e((string)($case['due_at']??$tr('No due date','دون موعد'))) ?></small></div></header>

 <?php if(!$schemaReady):?><div class="diar-alert error"><?= e($tr('Workflow controls are disabled until the DIAR-6B.4 Oracle migration is applied.','عناصر التحكم في دورة التحقيق معطلة حتى تطبيق ترقية Oracle الخاصة بـ DIAR-6B.4.')) ?></div><?php endif;?>

 <ol class="workflow-stage-strip case-stage-strip"><?php $i=0;foreach($stageLabel as $code=>$label):$i++;$state=$code===$stage?'current':($code==='CLOSED'&&$stage==='CLOSED'?'complete':'');?><li class="<?= e($state) ?>"><b><?= $i ?></b><span><?= e($label) ?></span></li><?php endforeach;?></ol>
 <nav class="case-workspace-tabs" data-workflow-tabs><a href="#overview"><?= e($tr('Overview','نظرة عامة')) ?></a><a href="#evidence"><?= e($tr('Evidence','الأدلة')) ?></a><a href="#information"><?= e($tr('Information requests','طلبات المعلومات')) ?></a><a href="#finding"><?= e($tr('Finding','النتيجة')) ?></a><a href="#approvals"><?= e($tr('Approvals','الاعتمادات')) ?></a><a href="#corrective"><?= e($tr('Corrective actions','الإجراءات التصحيحية')) ?></a><a href="#history"><?= e($tr('Audit history','سجل التدقيق')) ?></a></nav>

 <div class="case-layout workflow-case-layout"><main class="case-main">
  <section class="case-card workflow-panel" id="overview"><div class="card-title-row"><div><h2><?= e($tr('Case overview','نظرة عامة على القضية')) ?></h2><p><?= e($tr('Frozen source identity and current controlled workflow state.','هوية المصدر المجمدة والحالة الحالية لدورة التحقيق.')) ?></p></div><a class="secondary-button" href="<?= e(url_to('diar-case-evidence',$evidenceParams)) ?>"><?= e($tr('Open frozen evidence','فتح الأدلة المجمدة')) ?></a></div>
   <div class="case-facts-grid workflow-facts"><article><span><?= e($tr('Hospital','المستشفى')) ?></span><strong><?= e($case['site_code']??'') ?></strong></article><article><span><?= e($tr('Store','المخزن')) ?></span><strong><?= e($isAr?($case['store_name_ar']??$case['store_name_en']??''):($case['store_name_en']??$case['store_name_ar']??'')) ?></strong><small><?= e($case['store_code']??'') ?></small></article><article><span><?= e($tr('Item','الصنف')) ?></span><strong><?= e($case['item_code']??$tr('Event scope','نطاق حدث')) ?></strong></article><article><span><?= e($tr('Assigned investigator','المحقق المعيّن')) ?></span><strong><?= e($case['assigned_to']??$tr('Unassigned','غير معيّنة')) ?></strong></article><article><span><?= e($tr('Supervisor','المشرف')) ?></span><strong><?= e($case['supervisor_username']??'—') ?></strong></article><article><span><?= e($tr('Management approver','معتمد الإدارة')) ?></span><strong><?= e($case['management_approver']??'—') ?></strong></article><article><span><?= e($tr('Reopened','مرات إعادة الفتح')) ?></span><strong><?= number_format((int)($case['reopen_count']??0)) ?></strong></article><article><span><?= e($tr('Last action','آخر إجراء')) ?></span><strong><?= e((string)($case['last_action_at']??$case['updated_at']??'')) ?></strong></article></div>
  </section>

  <section class="case-card workflow-panel" id="evidence"><div class="card-title-row"><div><h2><?= e($tr('Evidence review','مراجعة الأدلة')) ?></h2><p><?= e($tr('Evidence remains read-only at source; investigation notes and references are stored separately.','تظل أدلة المصدر للقراءة فقط، بينما تحفظ ملاحظات التحقيق ومراجعه بصورة منفصلة.')) ?></p></div><strong><?= number_format(count($transactions)) ?></strong></div>
   <?php if($entity==='EVENT'&&$members!==[]):?><div class="member-summary"><span><?= e($tr('Event members','أعضاء الحدث')) ?> <strong><?= count($members) ?></strong></span><span><?= e($tr('Suppressed duplicates','مكررات مخفّضة')) ?> <strong><?= number_format((int)($case['suppressed_member_count']??0)) ?></strong></span></div><?php endif;?>
   <?php if($signals!==[]):?><div class="signal-grid"><?php foreach($signals as $signal):?><article><strong><?= e($isAr?($signal['label_ar']??$signal['code']??''):($signal['label_en']??$signal['code']??'')) ?></strong><p><?= e($isAr?($signal['explanation_ar']??''):($signal['explanation_en']??'')) ?></p></article><?php endforeach;?></div><?php endif;?>
   <div class="workflow-table-wrap"><table><thead><tr><th><?= e($tr('Date','التاريخ')) ?></th><th><?= e($tr('Transaction','الحركة')) ?></th><th><?= e($tr('User','المستخدم')) ?></th><th><?= e($tr('Quantity','الكمية')) ?></th><th><?= e($tr('Value','القيمة')) ?></th></tr></thead><tbody><?php foreach(array_slice($transactions,0,50) as $tx):?><tr><td><?= e($tx['trans_datetime']??'') ?></td><td><?= e(($tx['store_trans_code']??'').' · '.($isAr?($tx['movement_name_ar']??''):($tx['movement_name_en']??''))) ?></td><td><?= e($tx['effective_user_id']??'') ?></td><td><?= e(number_format((float)($tx['raw_qty']??0),2)) ?></td><td><?= e(number_format((float)($tx['raw_value']??0),2)) ?></td></tr><?php endforeach;?></tbody></table></div>
   <h3><?= e($tr('Investigation notes','ملاحظات التحقيق')) ?></h3><div class="note-list"><?php foreach($notes as $note):?><article><strong><?= e($note['note_type']??'') ?></strong><p><?= e($note['note_text']??'') ?></p><small><?= e(($note['created_by']??'').' · '.($note['created_at']??'')) ?></small></article><?php endforeach;?></div>
   <?php if(!empty($caps['review'])):?><form class="case-form compact-form" method="post" action="<?= e($postAction) ?>"><?= \App\Core\Csrf::field() ?><?= $hidden('note') ?><label><?= e($tr('Add note','إضافة ملاحظة')) ?></label><select name="note_type"><option value="REVIEW"><?= e($tr('Review','مراجعة')) ?></option><option value="ACTION"><?= e($tr('Action','إجراء')) ?></option><option value="GENERAL"><?= e($tr('General','عامة')) ?></option></select><textarea name="note_text" required maxlength="4000"></textarea><button class="primary-button" type="submit"><?= e($tr('Save note','حفظ الملاحظة')) ?></button></form><?php endif;?>
   <h3><?= e($tr('Evidence references','مراجع الأدلة')) ?></h3><div class="evidence-list"><?php foreach($evidence as $item):?><article><div><strong><?= e($item['evidence_type']??'') ?></strong><span><?= e($item['reference_no']??'') ?></span></div><p><?= e($item['description_text']??'') ?></p><small><?= e(($item['added_by']??'').' · '.($item['added_at']??'')) ?></small></article><?php endforeach;?></div>
   <?php if(!empty($caps['evidence'])):?><form class="case-form" method="post" action="<?= e($postAction) ?>"><?= \App\Core\Csrf::field() ?><?= $hidden('evidence') ?><div class="form-grid"><div><label><?= e($tr('Evidence type','نوع الدليل')) ?></label><select name="evidence_type"><option value="TRANSACTION_REPORT"><?= e($tr('Transaction report','تقرير معاملات')) ?></option><option value="APPROVAL"><?= e($tr('Approval','اعتماد')) ?></option><option value="INVENTORY_SHEET"><?= e($tr('Inventory sheet','كشف جرد')) ?></option><option value="USER_STATEMENT"><?= e($tr('User statement','إفادة مستخدم')) ?></option><option value="SYSTEM_LOG"><?= e($tr('System log','سجل نظام')) ?></option><option value="POLICY_DOCUMENT"><?= e($tr('Policy document','وثيقة سياسة')) ?></option><option value="OTHER"><?= e($tr('Other','أخرى')) ?></option></select></div><div><label><?= e($tr('Reference','المرجع')) ?></label><input name="reference_no"></div><div class="wide"><label><?= e($tr('Description','الوصف')) ?></label><textarea name="description" required></textarea></div><div><label><?= e($tr('File name','اسم الملف')) ?></label><input name="file_name"></div><div><label><?= e($tr('Controlled path','مسار محكوم')) ?></label><input name="storage_path"></div><div class="wide"><label>SHA-256</label><input name="file_sha256" maxlength="64"></div></div><button class="primary-button" type="submit"><?= e($tr('Record evidence','تسجيل الدليل')) ?></button></form><?php endif;?>
  </section>

  <section class="case-card workflow-panel" id="information"><div class="card-title-row"><div><h2><?= e($tr('Information requests','طلبات المعلومات')) ?></h2><p><?= e($tr('Requests and responses remain case-bound and auditable.','تظل الطلبات والردود مرتبطة بالقضية وقابلة للتدقيق.')) ?></p></div><strong><?= count($requests) ?></strong></div>
   <div class="workflow-record-list"><?php foreach($requests as $req):?><article><div><strong><?= e($req['requested_from']??'') ?></strong><span class="status-badge"><?= e($req['status_code']??'') ?></span></div><p><?= e($req['request_text']??'') ?></p><small><?= e(($req['requested_by']??'').' · '.($req['requested_at']??'').' · '.($req['due_at']??'')) ?></small><?php if(!empty($req['response_text'])):?><blockquote><?= e($req['response_text']) ?></blockquote><?php elseif(!empty($caps['request_information'])):?><form class="inline-workflow-form" method="post" action="<?= e($postAction) ?>"><?= \App\Core\Csrf::field() ?><?= $hidden('respond_information') ?><input type="hidden" name="request_id" value="<?= (int)($req['request_id']??0) ?>"><textarea name="response_text" required placeholder="<?= e($tr('Record response','تسجيل الرد')) ?>"></textarea><button type="submit"><?= e($tr('Submit response','حفظ الرد')) ?></button></form><?php endif;?></article><?php endforeach;?></div>
   <?php if(!empty($caps['request_information'])&&$stage==='UNDER_REVIEW'):?><form class="case-form" method="post" action="<?= e($postAction) ?>"><?= \App\Core\Csrf::field() ?><?= $hidden('request_information') ?><div class="form-grid"><div><label><?= e($tr('Requested from','مطلوب من')) ?></label><input name="requested_from" required></div><div><label><?= e($tr('Due date','تاريخ الاستحقاق')) ?></label><input type="datetime-local" name="due_date"></div><div class="wide"><label><?= e($tr('Information required','المعلومات المطلوبة')) ?></label><textarea name="request_text" required></textarea></div></div><button class="primary-button" type="submit"><?= e($tr('Create information request','إنشاء طلب معلومات')) ?></button></form><?php endif;?>
  </section>

  <section class="case-card workflow-panel" id="finding"><div class="card-title-row"><div><h2><?= e($tr('Proposed findings','النتائج المقترحة')) ?></h2><p><?= e($tr('The investigator proposes; approval is performed by separate roles.','يقترح المحقق، بينما يتم الاعتماد بواسطة أدوار منفصلة.')) ?></p></div><strong><?= count($findings) ?></strong></div>
   <div class="workflow-record-list"><?php foreach($findings as $finding):?><article><div><strong><?= e($outcomeLabel[$finding['outcome_code']??'']??($finding['outcome_code']??'')) ?></strong><span class="status-badge"><?= e($finding['status_code']??'') ?></span></div><p><?= e($finding['summary_text']??'') ?></p><dl><dt><?= e($tr('Root cause','السبب الجذري')) ?></dt><dd><?= e($finding['root_cause_text']??'') ?></dd><dt><?= e($tr('Recommended action','الإجراء الموصى به')) ?></dt><dd><?= e($finding['recommended_action_text']??'—') ?></dd></dl><small><?= e(($finding['proposed_by']??'').' · '.($finding['proposed_at']??'')) ?></small></article><?php endforeach;?></div>
   <?php if(!empty($caps['propose_finding'])&&$stage==='UNDER_REVIEW'):?><form class="case-form" method="post" action="<?= e($postAction) ?>"><?= \App\Core\Csrf::field() ?><?= $hidden('propose_finding') ?><div class="form-grid"><div><label><?= e($tr('Outcome','النتيجة')) ?></label><select name="investigation_outcome" required><?php foreach($outcomeLabel as $code=>$label):?><option value="<?= e($code) ?>"><?= e($label) ?></option><?php endforeach;?></select></div><div><label><input type="checkbox" name="requires_corrective_action" value="1"> <?= e($tr('Corrective action required','يلزم إجراء تصحيحي')) ?></label></div><div class="wide"><label><?= e($tr('Finding summary','ملخص النتيجة')) ?></label><textarea name="finding_summary" required></textarea></div><div class="wide"><label><?= e($tr('Root cause','السبب الجذري')) ?></label><textarea name="root_cause" required></textarea></div><div class="wide"><label><?= e($tr('Recommended action','الإجراء الموصى به')) ?></label><textarea name="recommended_action"></textarea></div></div><button class="primary-button" type="submit"><?= e($tr('Submit finding','تقديم النتيجة')) ?></button></form><?php endif;?>
  </section>

  <section class="case-card workflow-panel" id="approvals"><div class="card-title-row"><div><h2><?= e($tr('Supervisor and management approvals','اعتمادات المشرف والإدارة')) ?></h2><p><?= e($tr('Each decision is immutable in the approval history.','كل قرار محفوظ بصورة غير قابلة للتغيير في سجل الاعتماد.')) ?></p></div><strong><?= count($approvals) ?></strong></div><div class="workflow-record-list approvals-list"><?php foreach($approvals as $approval):?><article><div><strong><?= e($approval['approval_level']??'') ?></strong><span class="decision-<?= e(strtolower((string)($approval['decision_code']??''))) ?>"><?= e($approval['decision_code']??'') ?></span></div><p><?= e($approval['decision_comment']??'') ?></p><small><?= e(($approval['decided_by']??'').' · '.($approval['decided_at']??'')) ?></small></article><?php endforeach;?></div></section>

  <section class="case-card workflow-panel" id="corrective"><div class="card-title-row"><div><h2><?= e($tr('Corrective actions','الإجراءات التصحيحية')) ?></h2><p><?= e($tr('The case cannot close while an action remains open or in progress.','لا يمكن إغلاق القضية بينما يوجد إجراء مفتوح أو قيد التنفيذ.')) ?></p></div><strong><?= count($corrective) ?></strong></div><div class="corrective-grid"><?php foreach($corrective as $action):?><article><div><strong><?= e($action['action_title']??'') ?></strong><span class="status-badge"><?= e($action['status_code']??'') ?></span></div><p><?= e($action['action_description']??'') ?></p><small><?= e(($action['action_owner']??'').' · '.($action['due_at']??'')) ?></small><?php if(!empty($caps['corrective_action'])&&in_array((string)($action['status_code']??''),['OPEN','IN_PROGRESS'],true)):?><form class="inline-workflow-form" method="post" action="<?= e($postAction) ?>"><?= \App\Core\Csrf::field() ?><?= $hidden('corrective_update') ?><input type="hidden" name="corrective_action_id" value="<?= (int)($action['action_id']??0) ?>"><select name="corrective_status"><option value="IN_PROGRESS"><?= e($tr('In progress','قيد التنفيذ')) ?></option><option value="COMPLETED"><?= e($tr('Completed','مكتمل')) ?></option><option value="CANCELLED"><?= e($tr('Cancelled','ملغى')) ?></option></select><textarea name="completion_note" placeholder="<?= e($tr('Completion note','ملاحظة الإكمال')) ?>"></textarea><button type="submit"><?= e($tr('Update','تحديث')) ?></button></form><?php endif;?></article><?php endforeach;?></div></section>

  <section class="case-card workflow-panel" id="history"><h2><?= e($tr('Immutable audit history','سجل التدقيق غير القابل للتغيير')) ?></h2><div class="history-timeline"><?php foreach($history as $entry):?><article><span class="history-dot"></span><div><strong><?= e($entry['action_code']??'') ?></strong><small><?= e(($entry['performed_by']??'').' · '.($entry['performed_at']??'')) ?></small><p><?= e(($entry['old_status_code']??'').' → '.($entry['new_status_code']??'')) ?></p><?php if(!empty($entry['note_text'])):?><p><?= e($entry['note_text']) ?></p><?php endif;?></div></article><?php endforeach;?></div></section>
 </main>

 <aside class="case-sidebar workflow-sidebar">
  <?php if(!empty($caps['assign'])):?><section class="case-card sticky-card"><h2><?= e($tr('Assignment','التعيين')) ?></h2><form class="case-form" method="post" action="<?= e($postAction) ?>"><?= \App\Core\Csrf::field() ?><?= $hidden('assign') ?><label><?= e($tr('Investigator','المحقق')) ?></label><select name="assigned_to" required><option value=""><?= e($tr('Select a user','اختر مستخدمًا')) ?></option><?php foreach($investigators as $investigator):$username=(string)($investigator['username']??'');?><option value="<?= e($username) ?>" <?= strcasecmp((string)($case['assigned_to']??''),$username)===0?'selected':'' ?>><?= e($username) ?></option><?php endforeach;?></select><label><?= e($tr('Reason','السبب')) ?></label><textarea name="assignment_reason"></textarea><button class="primary-button" type="submit"><?= e($tr('Assign / reassign','تعيين / إعادة تعيين')) ?></button></form></section><?php endif;?>
  <section class="case-card stage-action-card"><h2><?= e($tr('Available workflow action','إجراء الدورة المتاح')) ?></h2>
   <?php if(!$schemaReady):?><p><?= e($tr('Oracle workflow migration required.','يلزم تطبيق ترقية Oracle.')) ?></p>
   <?php elseif(in_array($stage,['ASSIGNED','INFORMATION_REQUIRED'],true)&&!empty($caps['review'])):?><form method="post" action="<?= e($postAction) ?>"><?= \App\Core\Csrf::field() ?><?= $hidden('start_review') ?><button class="primary-button" type="submit"><?= e($tr('Start / resume review','بدء / استئناف المراجعة')) ?></button></form>
   <?php elseif($stage==='FINDING_PROPOSED'&&!empty($caps['supervisor_review'])):?><form method="post" action="<?= e($postAction) ?>"><?= \App\Core\Csrf::field() ?><?= $hidden('start_supervisor_review') ?><button class="primary-button" type="submit"><?= e($tr('Start supervisor review','بدء مراجعة المشرف')) ?></button></form>
   <?php elseif($stage==='SUPERVISOR_REVIEW'&&!empty($caps['supervisor_review'])):?><form class="case-form" method="post" action="<?= e($postAction) ?>"><?= \App\Core\Csrf::field() ?><?= $hidden('supervisor_decision') ?><label><?= e($tr('Decision','القرار')) ?></label><select name="decision"><option value="APPROVED"><?= e($tr('Approve','اعتماد')) ?></option><option value="RETURNED"><?= e($tr('Return for revision','إعادة للمراجعة')) ?></option><option value="REJECTED"><?= e($tr('Reject','رفض')) ?></option></select><label><?= e($tr('Comment','التعليق')) ?></label><textarea name="decision_comment" required></textarea><button class="primary-button" type="submit"><?= e($tr('Record supervisor decision','تسجيل قرار المشرف')) ?></button></form>
   <?php elseif($stage==='MANAGEMENT_APPROVAL'&&!empty($caps['management_approve'])):?><form class="case-form" method="post" action="<?= e($postAction) ?>"><?= \App\Core\Csrf::field() ?><?= $hidden('management_decision') ?><label><?= e($tr('Decision','القرار')) ?></label><select name="decision"><option value="APPROVED"><?= e($tr('Approve','اعتماد')) ?></option><option value="RETURNED"><?= e($tr('Return for revision','إعادة للمراجعة')) ?></option><option value="REJECTED"><?= e($tr('Reject','رفض')) ?></option></select><label><?= e($tr('Comment','التعليق')) ?></label><textarea name="decision_comment" required></textarea><button class="primary-button" type="submit"><?= e($tr('Record management decision','تسجيل قرار الإدارة')) ?></button></form>
   <?php elseif($stage==='CORRECTIVE_ACTION'&&!empty($caps['corrective_action'])):?><form class="case-form" method="post" action="<?= e($postAction) ?>"><?= \App\Core\Csrf::field() ?><?= $hidden('corrective_create') ?><label><?= e($tr('Action title','عنوان الإجراء')) ?></label><input name="action_title" required><label><?= e($tr('Owner','المالك')) ?></label><input name="action_owner" required><label><?= e($tr('Due date','تاريخ الاستحقاق')) ?></label><input type="datetime-local" name="action_due_date"><label><?= e($tr('Description','الوصف')) ?></label><textarea name="action_description"></textarea><button class="primary-button" type="submit"><?= e($tr('Create corrective action','إنشاء إجراء تصحيحي')) ?></button></form>
   <?php elseif($stage==='CLOSED'&&!empty($caps['reopen'])):?><form class="case-form" method="post" action="<?= e($postAction) ?>"><?= \App\Core\Csrf::field() ?><?= $hidden('reopen_case') ?><label><?= e($tr('Reopen reason','سبب إعادة الفتح')) ?></label><textarea name="reopen_reason" required></textarea><button class="primary-button" type="submit"><?= e($tr('Reopen case','إعادة فتح القضية')) ?></button></form>
   <?php else:?><p><?= e($tr('No action is available for your role at the current stage.','لا يوجد إجراء متاح لدورك في المرحلة الحالية.')) ?></p><?php endif;?>
  </section>
  <?php if(in_array($stage,['CORRECTIVE_ACTION','MANAGEMENT_APPROVAL'],true)&&!empty($caps['close'])):?><section class="case-card"><h2><?= e($tr('Close case','إغلاق القضية')) ?></h2><form class="case-form" method="post" action="<?= e($postAction) ?>"><?= \App\Core\Csrf::field() ?><?= $hidden('close_case') ?><textarea name="closure_summary" required placeholder="<?= e($tr('Closure summary','ملخص الإغلاق')) ?>"></textarea><button class="primary-button" type="submit"><?= e($tr('Close after controls pass','الإغلاق بعد اكتمال الضوابط')) ?></button></form></section><?php endif;?>
  <section class="case-card"><h2><?= e($tr('Assignment history','سجل التعيين')) ?></h2><div class="compact-list"><?php foreach($assignments as $assignment):?><div><strong><?= e($assignment['assigned_to']??'') ?></strong><span><?= e(($assignment['status_code']??'').' · '.($assignment['assigned_at']??'')) ?></span></div><?php endforeach;?></div></section>
 </aside></div>
 <?php endif;?>
</section>
