<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>
<?php if (!empty($isChainView)): ?>
    سلسلة ملكية رقم العضوية: <?= e($membershipNumber ?? '') ?>
<?php else: ?>
    لقطة أرشيفية #<?= (int) ($snapshot['id'] ?? 0) ?>
<?php endif; ?>
<?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<a href="/archive" class="btn btn-outline">← العودة للأرشيف</a>
<?php $__template->endSection(); ?>

<?php $__template->section('content'); ?>

<?php if (!empty($isChainView)): ?>
<!-- Number Chain View -->
<div class="card" style="margin-bottom:20px;">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;">
        <h3 style="margin:0;color:#0D7377;">سلسلة ملكية رقم العضوية: <?= e($membershipNumber ?? '') ?></h3>
    </div>
    <div style="padding:20px;">
        <?php if (empty($numberChain)): ?>
            <div style="text-align:center;padding:30px;color:#6B7280;">لا توجد سجلات في سلسلة الملكية</div>
        <?php else: ?>
            <?php foreach ($numberChain as $i => $link): ?>
            <div style="display:flex;gap:15px;padding:15px 0;<?= $i < count($numberChain) - 1 ? 'border-bottom:1px solid #F3F4F6;' : '' ?>">
                <div style="flex-shrink:0;width:40px;height:40px;border-radius:50%;background:<?= $link['held_until'] === null ? '#059669' : '#E5E7EB' ?>;display:flex;align-items:center;justify-content:center;color:<?= $link['held_until'] === null ? '#fff' : '#6B7280' ?>;font-weight:700;font-size:14px;">
                    <?= $i + 1 ?>
                </div>
                <div style="flex:1;">
                    <div style="display:flex;justify-content:space-between;margin-bottom:5px;">
                        <strong style="color:#1A1A2E;"><?= e($link['holder_type']) ?></strong>
                        <?php if ($link['held_until'] === null): ?>
                            <span style="background:#F0FDF4;color:#059669;padding:2px 8px;border-radius:4px;font-size:12px;">الحالي</span>
                        <?php endif; ?>
                    </div>
                    <div style="font-size:13px;color:#6B7280;">
                        الكيان: <code><?= e($link['holder_entity_type']) ?></code> #<?= (int) $link['holder_entity_id'] ?>
                    </div>
                    <div style="font-size:12px;color:#9CA3AF;margin-top:4px;">
                        من: <?= e($link['held_from']) ?>
                        <?php if ($link['held_until']): ?>
                            — إلى: <?= e($link['held_until']) ?>
                        <?php else: ?>
                            — حتى الآن
                        <?php endif; ?>
                    </div>
                    <?php if ($link['previous_holder_id']): ?>
                        <div style="font-size:12px;color:#9CA3AF;">المالك السابق: سلسلة #<?= (int) $link['previous_holder_id'] ?></div>
                    <?php endif; ?>
                </div>
            </div>
            <?php endforeach; ?>
        <?php endif; ?>
    </div>
</div>

<?php if (!empty($otherSnapshots)): ?>
<div class="card">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;">
        <h3 style="margin:0;color:#0D7377;">اللقطات الأرشيفية المرتبطة</h3>
    </div>
    <div class="table-responsive">
        <table class="data-table">
            <thead>
                <tr><th>#</th><th>التاريخ</th><th>السبب</th><th>ملاحظات</th><th>الإجراءات</th></tr>
            </thead>
            <tbody>
                <?php foreach ($otherSnapshots as $s): ?>
                <tr>
                    <td><?= (int) $s['id'] ?></td>
                    <td style="font-size:12px;"><?= e($s['snapshot_taken_at']) ?></td>
                    <td style="font-weight:600;"><?= e($s['snapshot_reason']) ?></td>
                    <td style="font-size:12px;color:#6B7280;"><?= e(mb_substr($s['notes'] ?? '', 0, 100)) ?: '—' ?></td>
                    <td><a href="/archive/<?= (int) $s['id'] ?>" class="btn btn-sm btn-outline">عرض</a></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
<?php endif; ?>

<?php else: ?>
<!-- Single Snapshot View -->
<div style="display:grid;grid-template-columns:1fr 300px;gap:20px;">
    <div>
        <!-- Snapshot Info -->
        <div class="card" style="margin-bottom:20px;">
            <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;display:flex;justify-content:space-between;align-items:center;">
                <h3 style="margin:0;color:#0D7377;">معلومات اللقطة</h3>
                <span style="background:#F3F4F6;padding:4px 12px;border-radius:4px;font-size:13px;font-weight:600;"><?= e($snapshot['snapshot_reason']) ?></span>
            </div>
            <div style="padding:20px;">
                <table style="width:100%;font-size:14px;">
                    <tr><td style="padding:6px 0;color:#6B7280;width:35%;">نوع الكيان</td><td style="padding:6px 0;"><code><?= e($snapshot['entity_type']) ?></code></td></tr>
                    <tr><td style="padding:6px 0;color:#6B7280;">رقم الكيان</td><td style="padding:6px 0;"><?= (int) $snapshot['entity_id'] ?></td></tr>
                    <?php if ($snapshot['membership_number']): ?>
                    <tr><td style="padding:6px 0;color:#6B7280;">رقم العضوية</td><td style="padding:6px 0;"><a href="/archive/number-chain/<?= urlencode($snapshot['membership_number']) ?>" style="color:#0D7377;font-weight:600;"><?= e($snapshot['membership_number']) ?></a></td></tr>
                    <?php endif; ?>
                    <tr><td style="padding:6px 0;color:#6B7280;">تاريخ اللقطة</td><td style="padding:6px 0;"><?= e($snapshot['snapshot_taken_at']) ?></td></tr>
                    <tr><td style="padding:6px 0;color:#6B7280;">بواسطة</td><td style="padding:6px 0;"><?= $snapshot['snapshot_taken_by'] ? '#' . (int) $snapshot['snapshot_taken_by'] : 'النظام' ?></td></tr>
                    <?php if ($snapshot['notes']): ?>
                    <tr><td style="padding:6px 0;color:#6B7280;">ملاحظات</td><td style="padding:6px 0;"><?= e($snapshot['notes']) ?></td></tr>
                    <?php endif; ?>
                </table>
            </div>
        </div>

        <!-- Main Entity Data -->
        <div class="card" style="margin-bottom:20px;">
            <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;">
                <h3 style="margin:0;color:#0D7377;">بيانات الكيان الرئيسية</h3>
            </div>
            <div style="padding:20px;">
                <table style="width:100%;font-size:13px;">
                    <?php foreach ($snapshot['full_data'] as $key => $value): ?>
                    <tr style="border-bottom:1px solid #F9FAFB;">
                        <td style="padding:6px 0;color:#6B7280;width:35%;font-weight:500;"><?= e($key) ?></td>
                        <td style="padding:6px 0;">
                            <?php if (is_array($value)): ?>
                                <code style="font-size:11px;background:#F3F4F6;padding:2px 6px;border-radius:3px;"><?= e(json_encode($value, JSON_UNESCAPED_UNICODE)) ?></code>
                            <?php elseif ($value === null): ?>
                                <span style="color:#D1D5DB;">NULL</span>
                            <?php elseif (mb_strlen((string) $value) > 100): ?>
                                <details>
                                    <summary style="cursor:pointer;color:#0D7377;font-size:12px;">عرض (<?= mb_strlen((string) $value) ?> حرف)</summary>
                                    <div style="margin-top:5px;padding:8px;background:#F9FAFB;border-radius:4px;font-size:12px;word-break:break-all;"><?= e((string) $value) ?></div>
                                </details>
                            <?php else: ?>
                                <?= e((string) $value) ?>
                            <?php endif; ?>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                </table>
            </div>
        </div>

        <!-- Related Data -->
        <?php if (!empty($snapshot['related_data'])): ?>
        <?php foreach ($snapshot['related_data'] as $section => $records): ?>
        <div class="card" style="margin-bottom:20px;">
            <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;">
                <h3 style="margin:0;color:#0D7377;"><?= e($section) ?> <span style="color:#9CA3AF;font-size:14px;">(<?= count($records) ?>)</span></h3>
            </div>
            <div style="padding:20px;">
                <?php if (empty($records)): ?>
                    <div style="text-align:center;color:#6B7280;padding:15px;">لا توجد سجلات</div>
                <?php else: ?>
                    <?php foreach ($records as $idx => $record): ?>
                    <details style="margin-bottom:10px;border:1px solid #E5E7EB;border-radius:6px;" <?= $idx === 0 ? 'open' : '' ?>>
                        <summary style="padding:10px 15px;cursor:pointer;background:#F9FAFB;border-radius:6px;font-weight:600;font-size:13px;">
                            سجل #<?= $record['id'] ?? ($idx + 1) ?>
                            <?php if (isset($record['full_name_ar'])): ?>
                                — <?= e($record['full_name_ar']) ?>
                            <?php endif; ?>
                        </summary>
                        <div style="padding:10px 15px;">
                            <table style="width:100%;font-size:12px;">
                                <?php foreach ($record as $rk => $rv): ?>
                                <tr>
                                    <td style="padding:3px 0;color:#6B7280;width:35%;"><?= e($rk) ?></td>
                                    <td style="padding:3px 0;">
                                        <?php if ($rv === null): ?>
                                            <span style="color:#D1D5DB;">NULL</span>
                                        <?php elseif (is_array($rv)): ?>
                                            <code style="font-size:10px;"><?= e(json_encode($rv, JSON_UNESCAPED_UNICODE)) ?></code>
                                        <?php else: ?>
                                            <?= e((string) $rv) ?>
                                        <?php endif; ?>
                                    </td>
                                </tr>
                                <?php endforeach; ?>
                            </table>
                        </div>
                    </details>
                    <?php endforeach; ?>
                <?php endif; ?>
            </div>
        </div>
        <?php endforeach; ?>
        <?php endif; ?>
    </div>

    <!-- Sidebar: Other Snapshots + Compare -->
    <div>
        <div class="card" style="margin-bottom:20px;">
            <div style="padding:12px 15px;border-bottom:1px solid #E5E7EB;">
                <h4 style="margin:0;color:#1A1A2E;font-size:14px;">لقطات أخرى لنفس الكيان</h4>
            </div>
            <div style="padding:10px 15px;">
                <?php if (empty($otherSnapshots) || count($otherSnapshots) <= 1): ?>
                    <div style="color:#6B7280;font-size:13px;text-align:center;padding:10px;">لا توجد لقطات أخرى</div>
                <?php else: ?>
                    <?php foreach ($otherSnapshots as $os): ?>
                    <div style="padding:8px 0;border-bottom:1px solid #F3F4F6;font-size:13px;<?= (int) $os['id'] === (int) $snapshot['id'] ? 'background:#EFF6FF;margin:0 -15px;padding:8px 15px;' : '' ?>">
                        <div style="display:flex;justify-content:space-between;align-items:center;">
                            <div>
                                <strong>#<?= (int) $os['id'] ?></strong>
                                <span style="color:#6B7280;font-size:11px;display:block;"><?= e($os['snapshot_reason']) ?></span>
                            </div>
                            <div style="text-align:left;">
                                <?php if ((int) $os['id'] !== (int) $snapshot['id']): ?>
                                    <a href="/archive/<?= (int) $os['id'] ?>" style="color:#0D7377;font-size:12px;">عرض</a>
                                    <br>
                                    <a href="/archive/compare/<?= (int) $snapshot['id'] ?>/<?= (int) $os['id'] ?>" style="color:#D97706;font-size:11px;">مقارنة</a>
                                <?php else: ?>
                                    <span style="color:#059669;font-size:11px;font-weight:600;">الحالي</span>
                                <?php endif; ?>
                            </div>
                        </div>
                        <div style="font-size:11px;color:#9CA3AF;margin-top:2px;"><?= e($os['snapshot_taken_at']) ?></div>
                    </div>
                    <?php endforeach; ?>
                <?php endif; ?>
            </div>
        </div>

        <!-- Audit link -->
        <div class="card">
            <div style="padding:12px 15px;">
                <a href="/audit/entity/<?= urlencode($snapshot['entity_type']) ?>/<?= (int) $snapshot['entity_id'] ?>" class="btn btn-outline" style="width:100%;text-align:center;">
                    عرض سجل المراجعة للكيان
                </a>
            </div>
        </div>
    </div>
</div>
<?php endif; ?>

<?php $__template->endSection(); ?>