<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>أرشيف — <?= e($member['full_name_ar'] ?? '') ?><?php $__template->endSection(); ?>
<?php $__template->section('page_actions'); ?>
<a href="/members/archive" class="btn btn-outline">← رجوع للأرشيف</a>
<?php $__template->endSection(); ?>
<?php $__template->section('content'); ?>

<?php
$statusLabels = ['deceased' => 'وفاة', 'transferred' => 'تحويل', 'waived' => 'تنازل', 'divorced' => 'طلاق', 'cancelled' => 'إلغاء'];
$statusColors = ['deceased' => '#6B7280', 'transferred' => '#0284C7', 'waived' => '#7C3AED', 'divorced' => '#DC2626', 'cancelled' => '#D97706'];
$color = $statusColors[$member['status']] ?? '#6B7280';
$data = $primarySnapshot['full_data'] ?? [];
$related = $primarySnapshot['related_data'] ?? [];
?>

<!-- Status Banner -->
<div style="padding:15px 20px;background:<?= $color ?>15;border:2px solid <?= $color ?>;border-radius:8px;margin-bottom:20px;display:flex;justify-content:space-between;align-items:center;">
    <div style="display:flex;align-items:center;gap:12px;">
        <span style="width:12px;height:12px;border-radius:50%;background:<?= $color ?>;"></span>
        <span style="font-weight:700;font-size:16px;color:<?= $color ?>;">مؤرشف — <?= e($statusLabels[$member['status']] ?? $member['status']) ?></span>
    </div>
    <div style="text-align:left;font-size:13px;color:#6B7280;">
        <div>تاريخ الأرشفة: <strong><?= $member['archived_at'] ? arabic_date($member['archived_at']) : '—' ?></strong></div>
        <?php if ($operator): ?>
            <div>بواسطة: <strong><?= e($operator['full_name_ar']) ?></strong></div>
        <?php endif; ?>
    </div>
</div>

<div style="display:grid;grid-template-columns:2fr 1fr;gap:20px;">
<div>

<!-- Member 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:14px;">
            <tr><td style="padding:6px 0;color:#6B7280;width:30%;">رقم العضوية</td><td style="padding:6px 0;font-weight:700;font-size:16px;"><?= e($membershipNumber ?? ($data['membership_number'] ?? '—')) ?></td></tr>
            <tr><td style="padding:6px 0;color:#6B7280;">الاسم بالكامل</td><td style="padding:6px 0;font-weight:600;"><?= e($member['full_name_ar'] ?? '') ?></td></tr>
            <?php if (!empty($member['full_name_en'])): ?><tr><td style="padding:6px 0;color:#6B7280;">الاسم بالإنجليزية</td><td style="padding:6px 0;"><?= e($member['full_name_en']) ?></td></tr><?php endif; ?>
            <tr><td style="padding:6px 0;color:#6B7280;">الرقم القومي</td><td style="padding:6px 0;font-family:monospace;"><?= e($member['national_id'] ?? '—') ?></td></tr>
            <tr><td style="padding:6px 0;color:#6B7280;">تاريخ الميلاد</td><td style="padding:6px 0;"><?= e($member['birth_date'] ?? '—') ?></td></tr>
            <tr><td style="padding:6px 0;color:#6B7280;">النوع</td><td style="padding:6px 0;"><?= ($member['gender'] ?? '') === 'male' ? 'ذكر' : (($member['gender'] ?? '') === 'female' ? 'أنثى' : '—') ?></td></tr>
            <tr><td style="padding:6px 0;color:#6B7280;">نوع العضوية</td><td style="padding:6px 0;"><?= e($member['membership_type'] ?? '—') ?></td></tr>
            <tr><td style="padding:6px 0;color:#6B7280;">الهاتف</td><td style="padding:6px 0;"><?= e($member['phone_mobile'] ?? '—') ?></td></tr>
            <?php if (!empty($member['phone_home'])): ?><tr><td style="padding:6px 0;color:#6B7280;">هاتف المنزل</td><td style="padding:6px 0;"><?= e($member['phone_home']) ?></td></tr><?php endif; ?>
            <?php if (!empty($member['email'])): ?><tr><td style="padding:6px 0;color:#6B7280;">البريد</td><td style="padding:6px 0;"><?= e($member['email']) ?></td></tr><?php endif; ?>
            <?php if (!empty($member['address'])): ?><tr><td style="padding:6px 0;color:#6B7280;">العنوان</td><td style="padding:6px 0;"><?= e($member['address']) ?></td></tr><?php endif; ?>
            <?php if (!empty($member['occupation'])): ?><tr><td style="padding:6px 0;color:#6B7280;">الوظيفة</td><td style="padding:6px 0;"><?= e($member['occupation']) ?></td></tr><?php endif; ?>
            <tr><td style="padding:6px 0;color:#6B7280;">تاريخ الإنشاء</td><td style="padding:6px 0;"><?= e($member['created_at'] ?? '—') ?></td></tr>
            <tr><td style="padding:6px 0;color:#6B7280;">آخر تعديل</td><td style="padding:6px 0;"><?= e($member['updated_at'] ?? '—') ?></td></tr>
        </table>
    </div>
</div>

<!-- Dependents -->
<div class="card" style="margin-bottom:20px;">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;">
        <h3 style="margin:0;color:#0D7377;">التابعون (<?= count($dependents['spouses'] ?? []) + count($dependents['children'] ?? []) + count($dependents['temporary'] ?? []) ?>)</h3>
    </div>
    <div style="padding:20px;">
        <?php if (!empty($dependents['spouses'])): ?>
        <h4 style="margin:0 0 10px;font-size:14px;color:#374151;">الزوجات (<?= count($dependents['spouses']) ?>)</h4>
        <table class="data-table" style="margin-bottom:15px;font-size:13px;">
            <thead><tr><th>الاسم</th><th>الرقم القومي</th><th>الحالة</th></tr></thead>
            <tbody>
            <?php foreach ($dependents['spouses'] as $s): ?>
                <tr>
                    <td><?= e($s['full_name_ar'] ?? '') ?></td>
                    <td style="font-family:monospace;"><?= e($s['national_id'] ?? '—') ?></td>
                    <td><?= ($s['is_archived'] ?? 0) ? '<span style="color:#DC2626;">مؤرشف</span>' : '<span style="color:#059669;">فعال</span>' ?></td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
        <?php endif; ?>

        <?php if (!empty($dependents['children'])): ?>
        <h4 style="margin:0 0 10px;font-size:14px;color:#374151;">الأبناء (<?= count($dependents['children']) ?>)</h4>
        <table class="data-table" style="margin-bottom:15px;font-size:13px;">
            <thead><tr><th>الاسم</th><th>تاريخ الميلاد</th><th>النوع</th><th>الحالة</th></tr></thead>
            <tbody>
            <?php foreach ($dependents['children'] as $c): ?>
                <tr>
                    <td><?= e($c['full_name_ar'] ?? '') ?></td>
                    <td><?= e($c['birth_date'] ?? '—') ?></td>
                    <td><?= ($c['gender'] ?? '') === 'male' ? 'ذكر' : 'أنثى' ?></td>
                    <td><?= ($c['is_archived'] ?? 0) ? '<span style="color:#DC2626;">مؤرشف</span>' : '<span style="color:#059669;">فعال</span>' ?></td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
        <?php endif; ?>

        <?php if (!empty($dependents['temporary'])): ?>
        <h4 style="margin:0 0 10px;font-size:14px;color:#374151;">الأعضاء المؤقتون (<?= count($dependents['temporary']) ?>)</h4>
        <table class="data-table" style="font-size:13px;">
            <thead><tr><th>الاسم</th><th>الرقم القومي</th><th>الحالة</th></tr></thead>
            <tbody>
            <?php foreach ($dependents['temporary'] as $t): ?>
                <tr>
                    <td><?= e($t['full_name_ar'] ?? '') ?></td>
                    <td style="font-family:monospace;"><?= e($t['national_id'] ?? '—') ?></td>
                    <td><?= ($t['is_archived'] ?? 0) ? '<span style="color:#DC2626;">مؤرشف</span>' : '<span style="color:#059669;">فعال</span>' ?></td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
        <?php endif; ?>

        <?php if (empty($dependents['spouses']) && empty($dependents['children']) && empty($dependents['temporary'])): ?>
            <div style="text-align:center;padding:20px;color:#6B7280;">لا يوجد تابعون مسجلون</div>
        <?php endif; ?>
    </div>
</div>

<!-- Financial Data from Snapshot -->
<?php if (!empty($related['subscriptions']) || !empty($related['fines']) || !empty($related['payments'])): ?>
<div class="card" style="margin-bottom:20px;">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;">
        <h3 style="margin:0;color:#D97706;">البيانات المالية (وقت الأرشفة)</h3>
    </div>
    <div style="padding:20px;">
        <?php if (!empty($related['subscriptions'])): ?>
        <h4 style="margin:0 0 10px;font-size:14px;color:#374151;">الاشتراكات (<?= count($related['subscriptions']) ?>)</h4>
        <table class="data-table" style="margin-bottom:15px;font-size:12px;">
            <thead><tr><th>السنة</th><th>الحالة</th><th>المبلغ</th><th>المدفوع</th></tr></thead>
            <tbody>
            <?php foreach (array_slice($related['subscriptions'], 0, 10) as $sub): ?>
                <tr>
                    <td><?= e($sub['year'] ?? $sub['subscription_year'] ?? '—') ?></td>
                    <td><?= e($sub['status'] ?? '—') ?></td>
                    <td style="text-align:left;"><?= money($sub['total_amount'] ?? '0') ?></td>
                    <td style="text-align:left;"><?= money($sub['paid_amount'] ?? '0') ?></td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
        <?php endif; ?>

        <?php if (!empty($related['fines'])): ?>
        <h4 style="margin:0 0 10px;font-size:14px;color:#374151;">الغرامات (<?= count($related['fines']) ?>)</h4>
        <table class="data-table" style="margin-bottom:15px;font-size:12px;">
            <thead><tr><th>السبب</th><th>المبلغ</th><th>الحالة</th></tr></thead>
            <tbody>
            <?php foreach (array_slice($related['fines'], 0, 10) as $fine): ?>
                <tr>
                    <td><?= e($fine['reason'] ?? '—') ?></td>
                    <td style="text-align:left;"><?= money($fine['amount'] ?? '0') ?></td>
                    <td><?= e($fine['status'] ?? '—') ?></td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
        <?php endif; ?>

        <?php if (!empty($related['payments'])): ?>
        <h4 style="margin:0 0 10px;font-size:14px;color:#374151;">المدفوعات (<?= count($related['payments']) ?>)</h4>
        <div style="font-size:12px;color:#6B7280;">عدد المدفوعات المحفوظة: <?= count($related['payments']) ?> — <a href="/archive/<?= (int) ($snapshots[0]['id'] ?? 0) ?>" style="color:#0D7377;">عرض التفاصيل في اللقطة</a></div>
        <?php endif; ?>
    </div>
</div>
<?php endif; ?>

<!-- Audit Trail -->
<?php if (!empty($auditTrail)): ?>
<div class="card" style="margin-bottom:20px;">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;">
        <h3 style="margin:0;color:#1A1A2E;">سجل التعديلات (Audit Trail)</h3>
    </div>
    <div style="padding:20px;max-height:400px;overflow-y:auto;">
        <?php foreach ($auditTrail as $log): ?>
        <div style="padding:10px 0;border-bottom:1px solid #F3F4F6;font-size:13px;">
            <div style="display:flex;justify-content:space-between;margin-bottom:4px;">
                <strong style="color:#374151;"><?= e($log['action'] ?? '') ?></strong>
                <span style="font-size:11px;color:#9CA3AF;"><?= e($log['created_at'] ?? '') ?></span>
            </div>
            <div style="color:#6B7280;font-size:12px;">
                <?php if ($log['employee_name']): ?>
                    بواسطة: <?= e($log['employee_name']) ?>
                <?php endif; ?>
                <?php if ($log['changed_fields_json']): ?>
                    <?php $changes = json_decode($log['changed_fields_json'], true) ?? []; ?>
                    <?php if (!empty($changes)): ?>
                        <div style="margin-top:4px;padding:6px 10px;background:#F9FAFB;border-radius:4px;">
                            <?php foreach (array_slice($changes, 0, 5) as $field => $vals): ?>
                                <div style="margin-bottom:2px;">
                                    <span style="color:#6B7280;"><?= e($field) ?>:</span>
                                    <span style="color:#DC2626;text-decoration:line-through;"><?= e(is_array($vals) ? ($vals['old'] ?? '') : '') ?></span>
                                    → <span style="color:#059669;"><?= e(is_array($vals) ? ($vals['new'] ?? '') : (string) $vals) ?></span>
                                </div>
                            <?php endforeach; ?>
                            <?php if (count($changes) > 5): ?>
                                <div style="color:#9CA3AF;">+ <?= count($changes) - 5 ?> تعديلات أخرى</div>
                            <?php endif; ?>
                        </div>
                    <?php endif; ?>
                <?php endif; ?>
            </div>
        </div>
        <?php endforeach; ?>
    </div>
</div>
<?php endif; ?>

</div>
<div>

<!-- Sidebar: Archive Info + Links -->
<div class="card" style="margin-bottom:20px;">
    <div style="padding:12px 15px;border-bottom:1px solid #E5E7EB;">
        <h4 style="margin:0;font-size:14px;color:#1A1A2E;">معلومات الأرشفة</h4>
    </div>
    <div style="padding:15px;">
        <table style="width:100%;font-size:13px;">
            <tr><td style="padding:4px 0;color:#6B7280;">السبب</td><td style="padding:4px 0;font-weight:600;color:<?= $color ?>;"><?= e($statusLabels[$member['status']] ?? $member['status']) ?></td></tr>
            <tr><td style="padding:4px 0;color:#6B7280;">التاريخ</td><td style="padding:4px 0;"><?= $member['archived_at'] ? arabic_date($member['archived_at']) : '—' ?></td></tr>
            <tr><td style="padding:4px 0;color:#6B7280;">المنفذ</td><td style="padding:4px 0;"><?= e($operator['full_name_ar'] ?? 'النظام') ?></td></tr>
            <tr><td style="padding:4px 0;color:#6B7280;">عدد اللقطات</td><td style="padding:4px 0;"><?= count($snapshots) ?></td></tr>
        </table>
    </div>
</div>

<!-- Linked Member (who received the number) -->
<?php if ($linkedMember): ?>
<div class="card" style="margin-bottom:20px;border:2px solid #059669;">
    <div style="padding:12px 15px;border-bottom:1px solid #E5E7EB;background:#F0FDF4;">
        <h4 style="margin:0;font-size:14px;color:#059669;">العضو الجديد (المنقول إليه)</h4>
    </div>
    <div style="padding:15px;">
        <div style="font-weight:700;font-size:15px;margin-bottom:5px;"><?= e($linkedMember['full_name_ar']) ?></div>
        <div style="font-size:12px;color:#6B7280;">رقم العضوية: <strong><?= e($linkedMember['membership_number'] ?? '—') ?></strong></div>
        <div style="font-size:12px;color:#6B7280;">الحالة: <?= e($linkedMember['status']) ?></div>
        <a href="/members/<?= (int) $linkedMember['id'] ?>" class="btn btn-sm btn-primary" style="margin-top:10px;width:100%;text-align:center;">الانتقال لسجل العضو الجديد</a>
    </div>
</div>
<?php endif; ?>

<!-- Number Chain -->
<?php if (!empty($numberChain)): ?>
<div class="card" style="margin-bottom:20px;">
    <div style="padding:12px 15px;border-bottom:1px solid #E5E7EB;">
        <h4 style="margin:0;font-size:14px;color:#0D7377;">سلسلة رقم العضوية: <?= e($membershipNumber ?? '') ?></h4>
    </div>
    <div style="padding:15px;">
        <?php foreach ($numberChain as $i => $link): ?>
        <div style="padding:8px 0;<?= $i < count($numberChain) - 1 ? 'border-bottom:1px solid #F3F4F6;' : '' ?>display:flex;gap:8px;align-items:start;">
            <div style="flex-shrink:0;width:24px;height:24px;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:11px;"><?= $i + 1 ?></div>
            <div style="flex:1;font-size:12px;">
                <div style="font-weight:600;color:#374151;"><?= e($link['holder_type']) ?></div>
                <div style="color:#6B7280;">عضو #<?= (int) $link['holder_entity_id'] ?></div>
                <div style="color:#9CA3AF;font-size:11px;">من: <?= e(mb_substr($link['held_from'], 0, 10)) ?><?= $link['held_until'] ? ' — إلى: ' . e(mb_substr($link['held_until'], 0, 10)) : ' — حتى الآن' ?></div>
            </div>
        </div>
        <?php endforeach; ?>
        <a href="/archive/number-chain/<?= urlencode($membershipNumber ?? '') ?>" class="btn btn-sm btn-outline" style="width:100%;text-align:center;margin-top:10px;">عرض السلسلة الكاملة</a>
    </div>
</div>
<?php endif; ?>

<!-- Snapshots -->
<?php if (!empty($snapshots)): ?>
<div class="card" style="margin-bottom:20px;">
    <div style="padding:12px 15px;border-bottom:1px solid #E5E7EB;">
        <h4 style="margin:0;font-size:14px;color:#1A1A2E;">اللقطات الأرشيفية (<?= count($snapshots) ?>)</h4>
    </div>
    <div style="padding:15px;">
        <?php foreach ($snapshots as $s): ?>
        <div style="padding:8px 0;border-bottom:1px solid #F3F4F6;font-size:12px;">
            <div style="display:flex;justify-content:space-between;align-items:center;">
                <div>
                    <strong style="color:#374151;"><?= e($s['snapshot_reason']) ?></strong>
                    <div style="color:#9CA3AF;margin-top:2px;"><?= e(mb_substr($s['snapshot_taken_at'], 0, 16)) ?></div>
                </div>
                <a href="/archive/<?= (int) $s['id'] ?>" class="btn btn-sm btn-outline">عرض</a>
            </div>
        </div>
        <?php endforeach; ?>
    </div>
</div>
<?php endif; ?>

<!-- Quick Links -->
<div class="card">
    <div style="padding:15px;display:flex;flex-direction:column;gap:8px;">
        <?php if (!empty($snapshots[0]['id'])): ?>
        <a href="/archive/<?= (int) $snapshots[0]['id'] ?>" class="btn btn-outline" style="text-align:center;">عرض اللقطة الكاملة (JSON)</a>
        <?php endif; ?>
        <a href="/audit/entity/members/<?= (int) $member['id'] ?>" class="btn btn-outline" style="text-align:center;">سجل المراجعة الكامل</a>
    </div>
</div>

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