<?php
declare(strict_types=1);

$tr = static function (string $en, string $ar): string {
    return current_lang() === 'ar' ? $ar : $en;
};
$isAr = current_lang() === 'ar';
$case = is_array($caseData['case'] ?? null) ? $caseData['case'] : [];
$source = is_array($caseData['source'] ?? null) ? $caseData['source'] : [];
$identity = is_array($caseData['frozen_identity'] ?? null) ? $caseData['frozen_identity'] : [];
$stats = is_array($caseData['statistical_evidence'] ?? null) ? $caseData['statistical_evidence'] : [];
$transactions = is_array($caseData['source_transactions'] ?? null) ? $caseData['source_transactions'] : [];
$related = is_array($caseData['related_cases'] ?? null) ? $caseData['related_cases'] : [];
$users = is_array($caseData['user_associations'] ?? null) ? $caseData['user_associations'] : [];
$history = is_array($caseData['history'] ?? null) ? $caseData['history'] : [];
$notes = is_array($caseData['notes'] ?? null) ? $caseData['notes'] : [];
$contract = is_array($caseData['frozen_contract'] ?? null) ? $caseData['frozen_contract'] : [];
$investigationCaseCode = trim((string)($caseData['investigation_case_code'] ?? $case['case_code'] ?? ''));
$caseCode = (string)($case['case_code'] ?? $identity['case_code'] ?? '');
$name = $isAr
    ? (string)($case['item_name_ar'] ?? $source['item_name_ar'] ?? '')
    : (string)($case['item_name_en'] ?? $source['item_name_en'] ?? '');
$fmt = static function ($value, int $decimals = 2): string {
    return $value === null || $value === '' ? '—' : number_format((float)$value, $decimals);
};
$currency = $isAr ? 'ر.س' : 'SAR';
$defaultBack = !empty($identity['month_snapshot_id']) ? url_to('diar-cases') : url_to('diar-investigations');
$backUrl = $returnUrl !== '' ? $returnUrl : $defaultBack;
$identityParams = [
    'hospital' => (string)($identity['hospital_code'] ?? ''),
    'source_db' => (string)($identity['source_db'] ?? ''),
    'snapshot_id' => (string)($identity['month_snapshot_id'] ?? ''),
    'month_case_id' => (string)($identity['month_case_id'] ?? ''),
    'return' => $backUrl,
];
$monthlyIdentityReady = $identityParams['hospital'] !== ''
    && $identityParams['source_db'] !== ''
    && $identityParams['snapshot_id'] !== ''
    && $identityParams['month_case_id'] !== '';
$signalTitleMap = [
    'QTY_MAGNITUDE' => $tr('Unusual quantity magnitude', 'حجم كمية غير معتاد'),
    'VALUE_MAGNITUDE' => $tr('High associated inventory value', 'ارتفاع القيمة المرتبطة بالمخزون'),
    'TXN_FREQUENCY' => $tr('Unusual transaction frequency', 'تكرار غير معتاد للمعاملات'),
    'LINE_SIZE' => $tr('Unusual movement-line size', 'حجم غير معتاد لسطر الحركة'),
    'DORMANCY_REACTIVATION' => $tr('Activity resumed after inactivity', 'عودة النشاط بعد فترة توقف'),
];
$signalExplanations = is_array($caseData['signal_explanations'] ?? null) ? $caseData['signal_explanations'] : [];
$rawReason = trim((string)($case['review_reason'] ?? $source['trigger_codes'] ?? ''));
$triggerCodes = array_values(array_unique(array_filter(array_map(static function (string $code): string {
    return strtoupper(trim($code));
}, preg_split('/[|,]+/', $rawReason) ?: []))));
$triggerTitles = [];
foreach ($triggerCodes as $triggerCode) {
    $triggerTitles[] = $signalTitleMap[$triggerCode] ?? str_replace('_', ' ', $triggerCode);
}
if ($triggerTitles === []) {
    foreach ($signalExplanations as $signalExplanation) {
        $titleKey = 'title_' . current_lang();
        $title = trim((string)($signalExplanation[$titleKey] ?? $signalExplanation['title_en'] ?? ''));
        if ($title !== '' && !in_array($title, $triggerTitles, true)) $triggerTitles[] = $title;
    }
}
$relationshipLabels = [
    'SAME_STORE_AND_ITEM' => $tr('Same store and item', 'نفس المخزن والصنف'),
    'SAME_ITEM' => $tr('Same item', 'نفس الصنف'),
    'SAME_STORE' => $tr('Same store', 'نفس المخزن'),
    'NEARBY_CASE' => $tr('Nearby case date', 'حالة في تاريخ قريب'),
];
?>
<section class="frozen-evidence-page" data-frozen-evidence data-evidence-identity="<?= e($monthlyIdentityReady ? 'MONTHLY' : 'INVESTIGATION') ?>">
    <nav class="evidence-breadcrumb" aria-label="<?= e($tr('Evidence breadcrumb', 'مسار صفحة الأدلة')) ?>">
        <a href="<?= e(url_to('diar-portal')) ?>"><?= e($tr('DIAR Platform', 'منصة DIAR')) ?></a>
        <span>›</span>
        <a href="<?= e($backUrl) ?>"><?= e($monthlyIdentityReady ? $tr('Suspicious Cases', 'الحالات المشتبه بها') : $tr('Investigations', 'التحقيقات')) ?></a>
        <span>›</span>
        <strong><?= e($caseCode !== '' ? $caseCode : $tr('Case evidence', 'أدلة الحالة')) ?></strong>
    </nav>

    <?php if ($loadError !== ''): ?>
        <div class="diar-alert error evidence-load-error">
            <strong><?= e($tr('Evidence could not be opened', 'تعذر فتح الأدلة')) ?></strong>
            <span><?= e($loadError) ?></span>
            <a href="<?= e($backUrl) ?>"><?= e($tr('Return to the case list', 'العودة إلى قائمة الحالات')) ?></a>
        </div>
    <?php endif; ?>

    <?php if ($case !== []): ?>
        <header class="evidence-hero">
            <div class="evidence-hero-copy">
                <span class="eyebrow"><?= e($tr('IMMUTABLE CASE EVIDENCE', 'أدلة الحالة المجمدة')) ?></span>
                <h1><?= e($name !== '' ? $name : $caseCode) ?></h1>
                <p><?= e($tr(
                    'A read-only evidence workspace bound to the hospital, source database, monthly snapshot and exact case identity.',
                    'مساحة أدلة للقراءة فقط مرتبطة بالمستشفى وقاعدة المصدر واللقطة الشهرية وهوية الحالة الدقيقة.'
                )) ?></p>
                <div class="hero-tags">
                    <span><?= e((string)($case['severity_code'] ?? '')) ?></span>
                    <span><?= e((string)($case['status_code'] ?? $tr('Frozen', 'مجمدة'))) ?></span>
                    <span><?= e($caseCode) ?></span>
                    <?php if (!empty($identity['month_snapshot_id'])): ?>
                        <span>#<?= e((string)$identity['month_snapshot_id']) ?> · MC-<?= e((string)$identity['month_case_id']) ?></span>
                    <?php endif; ?>
                </div>
            </div>
            <aside class="evidence-risk-card">
                <span><?= e($tr('Anomaly score', 'درجة الشذوذ')) ?></span>
                <strong><?= $fmt($case['risk_score'] ?? null, 0) ?></strong>
                <small><?= e($tr('Snapshot-bound evidence', 'أدلة مرتبطة باللقطة')) ?></small>
            </aside>
        </header>

        <nav class="evidence-tabs" aria-label="<?= e($tr('Evidence sections', 'أقسام الأدلة')) ?>">
            <?php
            $tabs = [
                ['overview', $tr('Overview', 'نظرة عامة')],
                ['movement', $tr('Movement Evidence', 'أدلة الحركات')],
                ['statistics', $tr('Statistical Evidence', 'الأدلة الإحصائية')],
                ['related', $tr('Related Cases', 'الحالات المرتبطة')],
                ['users', $tr('User Associations', 'ارتباطات المستخدمين')],
                ['methodology', $tr('Snapshot & Methodology', 'اللقطة والمنهجية')],
                ['history', $tr('Investigation History', 'سجل التحقيق')],
            ];
            foreach ($tabs as $index => $tab):
            ?>
                <button type="button" data-evidence-tab="<?= e($tab[0]) ?>" class="<?= $index === 0 ? 'is-active' : '' ?>" aria-selected="<?= $index === 0 ? 'true' : 'false' ?>">
                    <?= e($tab[1]) ?>
                </button>
            <?php endforeach; ?>
        </nav>

        <div class="evidence-panel is-active" data-evidence-panel="overview">
            <section class="evidence-card">
                <div class="section-heading">
                    <div>
                        <h2><?= e($tr('Case Overview', 'نظرة عامة على الحالة')) ?></h2>
                        <p><?= e($tr('The exact grain and frozen source identity used to detect this case.', 'الحبة الدقيقة وهوية المصدر المجمدة المستخدمة لاكتشاف هذه الحالة.')) ?></p>
                    </div>
                    <div class="evidence-heading-actions">
                        <a class="secondary-button" href="<?= e($backUrl) ?>"><?= e($tr('Back to cases', 'العودة إلى الحالات')) ?></a>
                        <?php if ($canOpenInvestigation && $investigationCaseCode !== '' && strpos($investigationCaseCode, 'MC-') !== 0): ?>
                            <a class="primary-button" href="<?= e(url_to('diar-investigation', ['case' => $investigationCaseCode, 'return' => $backUrl])) ?>"><?= e($tr('Open investigation', 'فتح التحقيق')) ?></a>
                        <?php elseif ($canOpenInvestigation): ?>
                            <a class="primary-button" href="<?= e(url_to('diar-investigations')) ?>"><?= e($tr('Open investigation queue', 'فتح قائمة التحقيقات')) ?></a>
                        <?php endif; ?>
                    </div>
                </div>

                <div class="identity-grid">
                    <?php
                    $identityRows = [
                        [$tr('Hospital', 'المستشفى'), $identity['hospital_code'] ?? $case['site_code'] ?? ''],
                        [$tr('Source database', 'قاعدة المصدر'), $identity['source_db'] ?? $case['source_db'] ?? ''],
                        [$tr('Snapshot ID', 'معرف اللقطة'), $identity['month_snapshot_id'] ?? ''],
                        [$tr('Monthly case ID', 'معرف الحالة الشهرية'), $identity['month_case_id'] ?? ''],
                        [$tr('Branch', 'الفرع'), $source['branch_id'] ?? $case['branch_id'] ?? ''],
                        [$tr('Store', 'المخزن'), $case['store_code'] ?? ''],
                        [$tr('Item', 'الصنف'), $case['item_code'] ?? ''],
                        [$tr('Raw unit', 'الوحدة الخام'), $source['raw_unit_code'] ?? $case['raw_unit_code'] ?? ''],
                        ['STORETRANSCODE', $source['storetranscode'] ?? $case['storetranscode'] ?? ''],
                        ['STORETRANSEFFECT', $source['storetranseffect'] ?? $case['storetranseffect'] ?? ''],
                        [$tr('Case day', 'تاريخ الحالة'), $case['calendar_day'] ?? ''],
                        [$tr('Snapshot status', 'حالة اللقطة'), $source['snapshot_status'] ?? $contract['snapshot_status'] ?? ''],
                    ];
                    foreach ($identityRows as $row):
                    ?>
                        <article><span><?= e((string)$row[0]) ?></span><strong><?= e((string)$row[1] !== '' ? (string)$row[1] : '—') ?></strong></article>
                    <?php endforeach; ?>
                </div>
            </section>

            <section class="evidence-card">
                <h2><?= e($tr('Why this case was raised', 'سبب إنشاء الحالة')) ?></h2>
                <div class="evidence-reason-summary">
                    <span><?= e($tr('Triggered review signals', 'إشارات المراجعة التي أدت إلى إنشاء الحالة')) ?></span>
                    <div class="evidence-reason-chips">
                        <?php foreach ($triggerTitles as $triggerTitle): ?>
                            <strong><?= e($triggerTitle) ?></strong>
                        <?php endforeach; ?>
                        <?php if ($triggerTitles === []): ?>
                            <strong><?= e($tr('Frozen statistical anomaly', 'شذوذ إحصائي مجمد')) ?></strong>
                        <?php endif; ?>
                    </div>
                </div>
                <?php if ($rawReason !== ''): ?>
                    <details class="evidence-raw-codes">
                        <summary><?= e($tr('Show technical signal codes', 'عرض أكواد الإشارات التقنية')) ?></summary>
                        <code dir="ltr"><?= e($rawReason) ?></code>
                    </details>
                <?php endif; ?>
                <div class="signal-list">
                    <?php foreach ($signalExplanations as $ex): ?>
                        <article>
                            <header>
                                <strong><?= e((string)($ex['title_' . current_lang()] ?? $ex['title_en'] ?? $ex['code'] ?? '')) ?></strong>
                                <code dir="ltr"><?= e((string)($ex['code'] ?? '')) ?></code>
                            </header>
                            <p dir="auto"><?= e((string)($ex['explanation_' . current_lang()] ?? $ex['explanation_en'] ?? '')) ?></p>
                        </article>
                    <?php endforeach; ?>
                </div>
            </section>
        </div>

        <div class="evidence-panel" data-evidence-panel="movement">
            <section class="evidence-card">
                <div class="section-heading">
                    <div>
                        <h2><?= e($tr('Exact Movement References', 'مراجع الحركات الدقيقة')) ?></h2>
                        <p><?= e($tr('Read directly from the selected hospital using the exact date, branch, store, item, raw unit, transaction code and effect.', 'تُقرأ مباشرة من المستشفى المحدد باستخدام التاريخ والفرع والمخزن والصنف والوحدة الخام وكود الحركة وتأثيرها بدقة.')) ?></p>
                    </div>
                    <strong><?= number_format((int)($caseData['source_transaction_total'] ?? count($transactions))) ?></strong>
                </div>
                <?php if (!empty($caseData['source_transaction_error'])): ?>
                    <div class="evidence-inline-warning"><?= e($tr('Exact source movements are temporarily unavailable.', 'مراجع الحركات المصدرية غير متاحة مؤقتًا.')) ?></div>
                <?php endif; ?>
                <div class="evidence-table-wrap">
                    <table>
                        <thead><tr>
                            <?php foreach ([
                                $tr('Date & time', 'التاريخ والوقت'),
                                $tr('Transaction', 'الحركة'),
                                $tr('Movement type', 'نوع الحركة'),
                                $tr('User', 'المستخدم'),
                                $tr('Store', 'المخزن'),
                                $tr('Item', 'الصنف'),
                                $tr('Unit', 'الوحدة'),
                                $tr('Quantity', 'الكمية'),
                                $tr('Value', 'القيمة'),
                                $tr('Effect', 'التأثير'),
                                $tr('Reference', 'المرجع'),
                            ] as $heading): ?><th><?= e($heading) ?></th><?php endforeach; ?>
                        </tr></thead>
                        <tbody>
                            <?php foreach ($transactions as $row): ?>
                                <tr>
                                    <td><?= e((string)($row['transaction_at'] ?? $row['trans_datetime'] ?? '')) ?></td>
                                    <td><?= e((string)($row['transaction_no'] ?? $row['trans_serial'] ?? '')) ?></td>
                                    <td><?= e($isAr ? (string)($row['movement_name_ar'] ?? $row['movement_name_en'] ?? $row['movement_family'] ?? '') : (string)($row['movement_name_en'] ?? $row['movement_name_ar'] ?? $row['movement_family'] ?? '')) ?></td>
                                    <td><?= e((string)($row['user_name'] ?? $row['effective_user_id'] ?? '')) ?></td>
                                    <td><?= e((string)($row['store_code'] ?? $case['store_code'] ?? '')) ?></td>
                                    <td><?= e((string)($row['item_code'] ?? $case['item_code'] ?? '')) ?></td>
                                    <td><?= e((string)($row['raw_unit_code'] ?? $source['raw_unit_code'] ?? '')) ?></td>
                                    <td><?= $fmt($row['raw_qty'] ?? $row['quantity'] ?? null) ?></td>
                                    <td><?= $fmt($row['raw_value'] ?? $row['value'] ?? null) ?> <?= e($currency) ?></td>
                                    <td><?= e((string)($row['effect_direction'] ?? $row['storetranseffect'] ?? '')) ?></td>
                                    <td><?= e((string)($row['reference_no'] ?? $row['reference_key'] ?? '')) ?></td>
                                </tr>
                            <?php endforeach; ?>
                            <?php if ($transactions === []): ?>
                                <tr><td colspan="11" class="empty-state"><?= e($tr('No exact movement references were returned for this case.', 'لم تُرجع مراجع حركات دقيقة لهذه الحالة.')) ?></td></tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </section>
        </div>

        <div class="evidence-panel" data-evidence-panel="statistics">
            <section class="evidence-card">
                <h2><?= e($tr('Statistical Comparison', 'المقارنة الإحصائية')) ?></h2>
                <div class="metric-grid">
                    <?php foreach ([
                        [$tr('Observed value', 'القيمة المرصودة'), $stats['observed_value'] ?? null],
                        [$tr('Historical average', 'المتوسط التاريخي'), $stats['historical_average'] ?? null],
                        [$tr('Median', 'الوسيط'), $stats['median'] ?? null],
                        [$tr('95th percentile (P95)', 'المئين 95 (P95)'), $stats['p95'] ?? null],
                        [$tr('99th percentile (P99)', 'المئين 99 (P99)'), $stats['p99'] ?? null],
                        [$tr('Historical maximum', 'الحد التاريخي الأقصى'), $stats['historical_maximum'] ?? null],
                        [$tr('Median absolute deviation (MAD)', 'الانحراف المطلق الوسيط (MAD)'), $stats['mad'] ?? null],
                        [$tr('Robust Z score', 'درجة Z المتينة'), $stats['z_score'] ?? null],
                        [$tr('Transactions', 'المعاملات'), $stats['frequency'] ?? null],
                        [$tr('Days since previous activity', 'الأيام منذ النشاط السابق'), $stats['days_since_previous'] ?? null],
                        [$tr('Baseline coverage', 'تغطية خط الأساس'), $stats['baseline_days'] ?? null],
                        [$tr('Profile readiness', 'جاهزية الملف'), $stats['profile_status'] ?? null],
                    ] as $metric): ?>
                        <article><span><?= e((string)$metric[0]) ?></span><strong><bdi dir="ltr"><?= is_numeric($metric[1]) ? $fmt($metric[1]) : e((string)($metric[1] ?? '—')) ?></bdi></strong></article>
                    <?php endforeach; ?>
                </div>
            </section>
        </div>

        <div class="evidence-panel" data-evidence-panel="related">
            <section class="evidence-card">
                <h2><?= e($tr('Related Cases and Event Members', 'الحالات المرتبطة وأعضاء الحدث')) ?></h2>
                <div class="related-grid">
                    <?php foreach ($related as $row): ?>
                        <?php
                        $relatedUrl = '#';
                        if ($monthlyIdentityReady && !empty($row['month_case_id'])) {
                            $params = $identityParams;
                            $params['month_case_id'] = (string)$row['month_case_id'];
                            $relatedUrl = url_to('diar-case-evidence', $params);
                        } elseif (!empty($row['case_code'])) {
                            $relatedUrl = url_to('diar-case-evidence', ['case' => (string)$row['case_code'], 'return' => $backUrl]);
                        }
                        ?>
                        <a href="<?= e($relatedUrl) ?>" aria-label="<?= e($tr('Open related case evidence', 'فتح أدلة الحالة المرتبطة')) ?>">
                            <div><strong><?= e((string)($row['case_code'] ?? '')) ?></strong><span><?= e((string)($row['item_code'] ?? '')) ?></span></div>
                            <p><?= e($isAr ? (string)($row['item_name_ar'] ?? '') : (string)($row['item_name_en'] ?? '')) ?></p>
                            <footer><span><?= e((string)($row['severity_code'] ?? '')) ?></span><span><?= e((string)($relationshipLabels[(string)($row['relationship'] ?? '')] ?? ($row['relationship'] ?? ''))) ?></span><b><?= e($tr('Open evidence', 'فتح الأدلة')) ?> ›</b></footer>
                        </a>
                    <?php endforeach; ?>
                    <?php if ($related === []): ?><div class="empty-state"><?= e($tr('No related cases are attached to this evidence record.', 'لا توجد حالات مرتبطة مرفقة بسجل الأدلة.')) ?></div><?php endif; ?>
                </div>
            </section>
        </div>

        <div class="evidence-panel" data-evidence-panel="users">
            <section class="evidence-card">
                <h2><?= e($tr('Users Associated with Source Movements', 'المستخدمون المرتبطون بحركات المصدر')) ?></h2>
                <div class="evidence-table-wrap">
                    <table>
                        <thead><tr><th><?= e($tr('User', 'المستخدم')) ?></th><th><?= e($tr('Transactions', 'المعاملات')) ?></th><th><?= e($tr('Absolute quantity', 'الكمية المطلقة')) ?></th><th><?= e($tr('First activity', 'أول نشاط')) ?></th><th><?= e($tr('Last activity', 'آخر نشاط')) ?></th></tr></thead>
                        <tbody>
                            <?php foreach ($users as $user): ?>
                                <tr><td><strong><?= e((string)$user['user_name']) ?></strong></td><td><?= e((string)$user['transaction_count']) ?></td><td><?= $fmt($user['absolute_quantity']) ?></td><td><?= e((string)($user['first_at'] ?? '—')) ?></td><td><?= e((string)($user['last_at'] ?? '—')) ?></td></tr>
                            <?php endforeach; ?>
                            <?php if ($users === []): ?><tr><td colspan="5" class="empty-state"><?= e($tr('No user identity was available in the returned movement references.', 'لا توجد هوية مستخدم متاحة في مراجع الحركات المسترجعة.')) ?></td></tr><?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </section>
        </div>

        <div class="evidence-panel" data-evidence-panel="methodology">
            <section class="evidence-card">
                <h2><?= e($tr('Snapshot and Evidence Controls', 'ضوابط اللقطة والأدلة')) ?></h2>
                <div class="control-grid">
                    <article><strong><?= !empty($contract['read_only']) ? 'PASS' : 'CHECK' ?></strong><span><?= e($tr('Read-only evidence', 'أدلة للقراءة فقط')) ?></span></article>
                    <article><strong><?= !empty($contract['case_bound']) ? 'PASS' : 'CHECK' ?></strong><span><?= e($tr('Bound to case identity', 'مرتبطة بهوية الحالة')) ?></span></article>
                    <article><strong><?= !empty($contract['snapshot_bound']) ? 'PASS' : 'CHECK' ?></strong><span><?= e($tr('Bound to monthly snapshot', 'مرتبطة باللقطة الشهرية')) ?></span></article>
                    <article><strong><?= e((string)($contract['snapshot_status'] ?? '—')) ?></strong><span><?= e($tr('Snapshot status', 'حالة اللقطة')) ?></span></article>
                    <article><strong>NONE</strong><span><?= e($tr('Operational table writes', 'الكتابة في الجداول التشغيلية')) ?></span></article>
                    <article><strong>NONE</strong><span><?= e($tr('Database links', 'روابط قواعد البيانات')) ?></span></article>
                </div>
                <details class="technical-contract">
                    <summary><?= e($tr('Technical identity and statistical grain', 'الهوية التقنية والحبة الإحصائية')) ?></summary>
                    <pre><?= e(json_encode([
                        'identity' => $identity,
                        'grain' => ['hospital','source_db','branch','store','item','raw_unit','storetranscode','storetranseffect'],
                        'snapshot_hash' => $contract['snapshot_hash'] ?? null,
                    ], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) ?: '{}') ?></pre>
                </details>
            </section>
        </div>

        <div class="evidence-panel" data-evidence-panel="history">
            <section class="evidence-card">
                <h2><?= e($tr('Investigation History', 'سجل التحقيق')) ?></h2>
                <div class="history-list">
                    <?php foreach ($history as $row): ?>
                        <article><span></span><div><strong><?= e((string)($row['action_code'] ?? '')) ?></strong><small><?= e((string)($row['performed_by'] ?? '')) ?> · <?= e((string)($row['performed_at'] ?? '')) ?></small><p><?= e((string)($row['old_status_code'] ?? '')) ?><?= !empty($row['new_status_code']) ? ' → ' . e((string)$row['new_status_code']) : '' ?></p><?php if (!empty($row['note_text'])): ?><p><?= e((string)$row['note_text']) ?></p><?php endif; ?></div></article>
                    <?php endforeach; ?>
                    <?php foreach ($notes as $row): ?>
                        <article><span></span><div><strong><?= e((string)($row['note_type'] ?? $tr('Note', 'ملاحظة'))) ?></strong><small><?= e((string)($row['created_by'] ?? '')) ?> · <?= e((string)($row['created_at'] ?? '')) ?></small><p><?= e((string)($row['note_text'] ?? '')) ?></p></div></article>
                    <?php endforeach; ?>
                    <?php if ($history === [] && $notes === []): ?><div class="empty-state"><?= e($tr('No investigation history has been recorded for this frozen case.', 'لم يتم تسجيل سجل تحقيق لهذه الحالة المجمدة بعد.')) ?></div><?php endif; ?>
                </div>
            </section>
        </div>
    <?php endif; ?>
</section>
