<?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/<?= (int) $member->id ?>" class="btn btn-outline">&#8592; العودة للعضو</a>
<?php $__template->endSection(); ?>

<?php $__template->section('content'); ?>
<div style="max-width:1000px;">

    <!-- Header -->
    <div class="card" style="padding:20px;margin-bottom:20px;background:linear-gradient(135deg, #EFF6FF, #DBEAFE);border:1px solid #93C5FD;">
        <div style="display:flex;align-items:center;justify-content:space-between;">
            <div>
                <h2 style="margin:0 0 5px;color:#1E40AF;">السجل التأميني</h2>
                <p style="margin:0;color:#6B7280;font-size:14px;"><?= e($member->full_name_ar) ?> — رقم العضوية: <?= e($member->membership_number ?? 'مؤرشف') ?></p>
                <?php if ($member->is_archived): ?>
                <span style="display:inline-block;margin-top:5px;background:#FEE2E2;color:#991B1B;padding:2px 8px;border-radius:4px;font-size:12px;font-weight:600;">عضوية مؤرشفة (<?= e($member->status) ?>)</span>
                <?php endif; ?>
            </div>
            <div style="font-size:48px;">&#x1f4cb;</div>
        </div>
    </div>

<?php
$hasAny = !empty($deathCases) || !empty($waiverRequests) || !empty($transferRequests);
if (!$hasAny):
?>
    <div class="card" style="padding:40px;text-align:center;">
        <div style="font-size:48px;margin-bottom:10px;">&#x1f4cb;</div>
        <p style="color:#6B7280;">لا يوجد سجل تأميني لهذا العضو</p>
    </div>
<?php else: ?>

    <!-- ═══════ DEATH CASES ═══════ -->
    <?php if (!empty($deathCases)): ?>
    <div class="card" style="margin-bottom:20px;">
        <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;background:#F9FAFB;">
            <h3 style="margin:0;font-size:16px;color:#1F2937;">&#x1f54a; حالات الوفاة (<?= count($deathCases) ?>)</h3>
        </div>
        <div style="padding:15px 20px;">
            <?php foreach ($deathCases as $dc):
                $dcTypeLabel = match($dc['deceased_type']) {
                    'primary_member' => 'عضو أساسي', 'spouse' => 'زوج/ة', 'child' => 'ابن/ابنة', default => $dc['deceased_type'],
                };
                $dcStatusLabel = match($dc['status']) {
                    'recorded' => 'مسجل', 'board_review' => 'مراجعة مجلس الإدارة', 'board_approved' => 'معتمد',
                    'rejected' => 'مرفوض', 'fee_paid' => 'تم السداد', 'pending_form_fill' => 'انتظار ملء الاستمارة',
                    'completed' => 'مكتمل', default => $dc['status'],
                };
                $dcStatusColor = match($dc['status']) {
                    'completed' => '#059669', 'rejected' => '#DC2626', 'board_approved','fee_paid' => '#3B82F6', default => '#F59E0B',
                };
                $isSource = ((int) $dc['member_id'] === (int) $member->id);
            ?>
            <div style="border:1px solid #E5E7EB;border-radius:8px;padding:15px;margin-bottom:12px;<?= $dc['status'] === 'completed' ? 'border-right:4px solid #059669;' : '' ?>">
                <div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:8px;">
                    <div>
                        <span style="font-weight:700;">حالة وفاة #<?= (int) $dc['id'] ?></span>
                        <span style="margin:0 8px;color:#9CA3AF;">|</span>
                        <span style="font-size:13px;color:#6B7280;"><?= $dcTypeLabel ?></span>
                        <span style="margin:0 8px;color:#9CA3AF;">|</span>
                        <span style="font-size:13px;background:<?= $isSource ? '#FEE2E2' : '#DCFCE7' ?>;color:<?= $isSource ? '#991B1B' : '#166534' ?>;padding:1px 6px;border-radius:3px;"><?= $isSource ? 'العضو المتوفى' : 'المستفيد (العضوية الجديدة)' ?></span>
                    </div>
                    <span style="color:<?= $dcStatusColor ?>;font-weight:600;font-size:13px;"><?= $dcStatusLabel ?></span>
                </div>
                <table style="width:100%;font-size:13px;">
                    <tr><td style="padding:3px 0;color:#6B7280;width:25%;">العضو المتوفى</td><td style="padding:3px 0;font-weight:600;"><?= e($dc['member_name']) ?></td></tr>
                    <tr><td style="padding:3px 0;color:#6B7280;">تاريخ الوفاة</td><td style="padding:3px 0;"><?= e($dc['death_date'] ?? '—') ?></td></tr>
                    <?php if (!empty($dc['target_member_name'])): ?>
                    <tr><td style="padding:3px 0;color:#6B7280;">المستفيد (العضوية الجديدة)</td><td style="padding:3px 0;"><?= e($dc['target_member_name']) ?></td></tr>
                    <?php endif; ?>
                    <?php if (!empty($dc['fee_amount']) && bccomp($dc['fee_amount'], '0', 2) > 0): ?>
                    <tr><td style="padding:3px 0;color:#6B7280;">إجمالي الرسوم</td><td style="padding:3px 0;font-weight:700;color:#059669;"><?= money($dc['fee_amount']) ?></td></tr>
                    <?php endif; ?>
                    <?php if (!empty($dc['board_fee_amount']) && bccomp($dc['board_fee_amount'], '0', 2) > 0): ?>
                    <tr><td style="padding:3px 0;color:#6B7280;">رسوم مجلس الأمناء</td><td style="padding:3px 0;"><?= money($dc['board_fee_amount']) ?> (<?= e($dc['board_fee_method'] === 'percentage' ? $dc['board_fee_value'] . '%' : 'مبلغ ثابت') ?>)</td></tr>
                    <?php endif; ?>
                    <tr><td style="padding:3px 0;color:#6B7280;">تاريخ التسجيل</td><td style="padding:3px 0;"><?= e(substr($dc['created_at'], 0, 16)) ?></td></tr>
                    <?php if ($dc['status'] === 'completed' && !empty($dc['updated_at'])): ?>
                    <tr><td style="padding:3px 0;color:#6B7280;">تاريخ الإتمام</td><td style="padding:3px 0;"><?= e(substr($dc['updated_at'], 0, 16)) ?></td></tr>
                    <?php endif; ?>
                </table>
                <div style="margin-top:8px;"><a href="/death/<?= (int) $dc['id'] ?>" class="btn btn-sm btn-outline" style="font-size:11px;">عرض التفاصيل</a></div>
            </div>
            <?php endforeach; ?>
        </div>
    </div>
    <?php endif; ?>

    <!-- ═══════ WAIVER REQUESTS ═══════ -->
    <?php if (!empty($waiverRequests)): ?>
    <div class="card" style="margin-bottom:20px;">
        <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;background:#F9FAFB;">
            <h3 style="margin:0;font-size:16px;color:#1F2937;">&#x1f4dd; طلبات التنازل (<?= count($waiverRequests) ?>)</h3>
        </div>
        <div style="padding:15px 20px;">
            <?php foreach ($waiverRequests as $wr):
                $wrStatusLabel = match($wr['status']) {
                    'requested' => 'مقدم', 'approved' => 'معتمد', 'fee_paid' => 'تم السداد',
                    'completed' => 'مكتمل', 'rejected' => 'مرفوض', 'cancelled' => 'ملغى', default => $wr['status'],
                };
                $wrStatusColor = match($wr['status']) {
                    'completed' => '#059669', 'rejected','cancelled' => '#DC2626', 'approved','fee_paid' => '#3B82F6', default => '#F59E0B',
                };
                $isSource = ((int) $wr['source_member_id'] === (int) $member->id);
            ?>
            <div style="border:1px solid #E5E7EB;border-radius:8px;padding:15px;margin-bottom:12px;<?= $wr['status'] === 'completed' ? 'border-right:4px solid #059669;' : '' ?>">
                <div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:8px;">
                    <div>
                        <span style="font-weight:700;">طلب تنازل #<?= (int) $wr['id'] ?></span>
                        <span style="margin:0 8px;color:#9CA3AF;">|</span>
                        <span style="font-size:13px;background:<?= $isSource ? '#FEE2E2' : '#DCFCE7' ?>;color:<?= $isSource ? '#991B1B' : '#166534' ?>;padding:1px 6px;border-radius:3px;"><?= $isSource ? 'المتنازل (العضوية المؤرشفة)' : 'المتنازل إليه (المستفيد)' ?></span>
                    </div>
                    <span style="color:<?= $wrStatusColor ?>;font-weight:600;font-size:13px;"><?= $wrStatusLabel ?></span>
                </div>
                <table style="width:100%;font-size:13px;">
                    <tr><td style="padding:3px 0;color:#6B7280;width:25%;">المتنازل</td><td style="padding:3px 0;font-weight:600;"><?= e($wr['source_member_name']) ?></td></tr>
                    <tr><td style="padding:3px 0;color:#6B7280;">المتنازل إليه</td><td style="padding:3px 0;font-weight:600;"><?= e($wr['target_member_name'] ?? '—') ?></td></tr>
                    <tr><td style="padding:3px 0;color:#6B7280;">رقم العضوية</td><td style="padding:3px 0;font-family:monospace;font-weight:700;"><?= e($wr['membership_number']) ?></td></tr>
                    <tr><td style="padding:3px 0;color:#6B7280;">قيمة العضوية وقت التنازل</td><td style="padding:3px 0;"><?= money($wr['membership_value_at_waiver']) ?></td></tr>
                    <tr><td style="padding:3px 0;color:#6B7280;">نسبة رسوم التنازل</td><td style="padding:3px 0;"><?= e($wr['waiver_fee_percentage']) ?>%</td></tr>
                    <tr><td style="padding:3px 0;color:#6B7280;">رسوم التنازل</td><td style="padding:3px 0;font-weight:700;color:#059669;"><?= money($wr['waiver_fee_amount']) ?></td></tr>
                    <tr><td style="padding:3px 0;color:#6B7280;">عدد التابعين الأصلي</td><td style="padding:3px 0;"><?= (int) $wr['original_dependent_count'] ?></td></tr>
                    <?php if ($wr['new_dependent_count'] !== null): ?>
                    <tr><td style="padding:3px 0;color:#6B7280;">عدد التابعين الجديد</td><td style="padding:3px 0;"><?= (int) $wr['new_dependent_count'] ?></td></tr>
                    <?php endif; ?>
                    <?php if (!empty($wr['approved_by_name'])): ?>
                    <tr><td style="padding:3px 0;color:#6B7280;">معتمد بواسطة</td><td style="padding:3px 0;"><?= e($wr['approved_by_name']) ?> (<?= e(substr($wr['approved_at'] ?? '', 0, 16)) ?>)</td></tr>
                    <?php endif; ?>
                    <tr><td style="padding:3px 0;color:#6B7280;">تاريخ الطلب</td><td style="padding:3px 0;"><?= e(substr($wr['created_at'], 0, 16)) ?></td></tr>
                    <?php if ($wr['status'] === 'completed' && !empty($wr['updated_at'])): ?>
                    <tr><td style="padding:3px 0;color:#6B7280;">تاريخ الإتمام</td><td style="padding:3px 0;"><?= e(substr($wr['updated_at'], 0, 16)) ?></td></tr>
                    <?php endif; ?>
                </table>
                <div style="margin-top:8px;"><a href="/waivers/<?= (int) $wr['id'] ?>" class="btn btn-sm btn-outline" style="font-size:11px;">عرض التفاصيل</a></div>
            </div>
            <?php endforeach; ?>
        </div>
    </div>
    <?php endif; ?>

    <!-- ═══════ TRANSFER REQUESTS ═══════ -->
    <?php if (!empty($transferRequests)): ?>
    <div class="card" style="margin-bottom:20px;">
        <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;background:#F9FAFB;">
            <h3 style="margin:0;font-size:16px;color:#1F2937;">&#x1f504; طلبات النقل والفصل (<?= count($transferRequests) ?>)</h3>
        </div>
        <div style="padding:15px 20px;">
            <?php foreach ($transferRequests as $tr):
                $trTypeLabel = match($tr['transfer_type'] ?? '') {
                    'full_transfer' => 'نقل كامل', 'child_separation' => 'فصل أبناء', 'child_mandatory_25' => 'فصل إلزامي (25 سنة)',
                    'sports_conversion' => 'تحويل رياضي', 'cross_branch' => 'نقل بين الفروع', default => $tr['transfer_type'] ?? '—',
                };
                $trStatusLabel = match($tr['status']) {
                    'requested' => 'مقدم', 'approved' => 'معتمد', 'fee_paid' => 'تم السداد',
                    'completed' => 'مكتمل', 'rejected' => 'مرفوض', 'cancelled' => 'ملغى', default => $tr['status'],
                };
                $trStatusColor = match($tr['status']) {
                    'completed' => '#059669', 'rejected','cancelled' => '#DC2626', 'approved','fee_paid' => '#3B82F6', default => '#F59E0B',
                };
                $isSource = ((int) $tr['source_member_id'] === (int) $member->id);
            ?>
            <div style="border:1px solid #E5E7EB;border-radius:8px;padding:15px;margin-bottom:12px;<?= $tr['status'] === 'completed' ? 'border-right:4px solid #059669;' : '' ?>">
                <div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:8px;">
                    <div>
                        <span style="font-weight:700;">طلب نقل #<?= (int) $tr['id'] ?></span>
                        <span style="margin:0 8px;color:#9CA3AF;">|</span>
                        <span style="font-size:13px;color:#6B7280;"><?= $trTypeLabel ?></span>
                        <span style="margin:0 8px;color:#9CA3AF;">|</span>
                        <span style="font-size:13px;background:<?= $isSource ? '#FEE2E2' : '#DCFCE7' ?>;color:<?= $isSource ? '#991B1B' : '#166534' ?>;padding:1px 6px;border-radius:3px;"><?= $isSource ? 'العضوية الأصلية' : 'العضوية الجديدة' ?></span>
                    </div>
                    <span style="color:<?= $trStatusColor ?>;font-weight:600;font-size:13px;"><?= $trStatusLabel ?></span>
                </div>
                <table style="width:100%;font-size:13px;">
                    <tr><td style="padding:3px 0;color:#6B7280;width:25%;">العضو الأصلي</td><td style="padding:3px 0;font-weight:600;"><?= e($tr['source_member_name']) ?></td></tr>
                    <?php if (!empty($tr['target_member_name'])): ?>
                    <tr><td style="padding:3px 0;color:#6B7280;">العضو الجديد</td><td style="padding:3px 0;font-weight:600;"><?= e($tr['target_member_name']) ?></td></tr>
                    <?php endif; ?>
                    <?php if (!empty($tr['source_membership_number'])): ?>
                    <tr><td style="padding:3px 0;color:#6B7280;">رقم العضوية الأصلي</td><td style="padding:3px 0;font-family:monospace;"><?= e($tr['source_membership_number']) ?></td></tr>
                    <?php endif; ?>
                    <?php if (!empty($tr['new_membership_number'])): ?>
                    <tr><td style="padding:3px 0;color:#6B7280;">رقم العضوية الجديد</td><td style="padding:3px 0;font-family:monospace;font-weight:700;"><?= e($tr['new_membership_number']) ?></td></tr>
                    <?php endif; ?>
                    <?php if (!empty($tr['total_fee']) && bccomp($tr['total_fee'], '0', 2) > 0): ?>
                    <tr><td style="padding:3px 0;color:#6B7280;">إجمالي الرسوم</td><td style="padding:3px 0;font-weight:700;color:#059669;"><?= money($tr['total_fee']) ?></td></tr>
                    <?php endif; ?>
                    <?php if (!empty($tr['separation_fee']) && bccomp($tr['separation_fee'], '0', 2) > 0): ?>
                    <tr><td style="padding:3px 0;color:#6B7280;">رسوم الفصل</td><td style="padding:3px 0;"><?= money($tr['separation_fee']) ?></td></tr>
                    <?php endif; ?>
                    <?php if (!empty($tr['approved_by_name'])): ?>
                    <tr><td style="padding:3px 0;color:#6B7280;">معتمد بواسطة</td><td style="padding:3px 0;"><?= e($tr['approved_by_name']) ?> (<?= e(substr($tr['approved_at'] ?? '', 0, 16)) ?>)</td></tr>
                    <?php endif; ?>
                    <tr><td style="padding:3px 0;color:#6B7280;">تاريخ الطلب</td><td style="padding:3px 0;"><?= e(substr($tr['created_at'], 0, 16)) ?></td></tr>
                    <?php if (!empty($tr['completed_at'])): ?>
                    <tr><td style="padding:3px 0;color:#6B7280;">تاريخ الإتمام</td><td style="padding:3px 0;"><?= e(substr($tr['completed_at'], 0, 16)) ?></td></tr>
                    <?php endif; ?>
                </table>
                <div style="margin-top:8px;"><a href="/transfers/<?= (int) $tr['id'] ?>" class="btn btn-sm btn-outline" style="font-size:11px;">عرض التفاصيل</a></div>
            </div>
            <?php endforeach; ?>
        </div>
    </div>
    <?php endif; ?>

    <!-- ═══════ PROCEDURE PAYMENTS ═══════ -->
    <?php if (!empty($procedurePayments)): ?>
    <div class="card" style="margin-bottom:20px;">
        <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;background:#F9FAFB;">
            <h3 style="margin:0;font-size:16px;color:#1F2937;">&#x1f4b0; المدفوعات المرتبطة بالإجراءات (<?= count($procedurePayments) ?>)</h3>
        </div>
        <div class="table-responsive" style="padding:10px;">
            <table class="data-table" style="width:100%;font-size:13px;">
                <thead>
                    <tr>
                        <th>رقم الإيصال</th>
                        <th>النوع</th>
                        <th>الوصف</th>
                        <th>المبلغ</th>
                        <th>تاريخ الدفع</th>
                        <th>طريقة الدفع</th>
                    </tr>
                </thead>
                <tbody>
                <?php foreach ($procedurePayments as $pp):
                    $ppTypeLabel = match($pp['payment_type'] ?? '') {
                        'death_fee' => 'رسوم وفاة', 'waiver_fee' => 'رسوم تنازل',
                        'transfer_fee' => 'رسوم نقل', 'separation_fee' => 'رسوم فصل', default => $pp['payment_type'] ?? '—',
                    };
                    $ppMethodLabel = match($pp['payment_method'] ?? '') {
                        'cash' => 'نقدي', 'card' => 'بطاقة', 'transfer' => 'تحويل', 'cheque' => 'شيك', default => $pp['payment_method'] ?? '—',
                    };
                ?>
                    <tr>
                        <td style="font-weight:600;font-family:monospace;"><?= e($pp['receipt_number'] ?? '#' . $pp['id']) ?></td>
                        <td><span style="background:#EFF6FF;color:#1E40AF;padding:2px 6px;border-radius:3px;font-size:11px;"><?= $ppTypeLabel ?></span></td>
                        <td style="font-size:12px;"><?= e($pp['description'] ?? '—') ?></td>
                        <td style="font-weight:700;color:#059669;"><?= money($pp['amount']) ?></td>
                        <td><?= e(substr($pp['payment_date'] ?? $pp['created_at'] ?? '', 0, 16)) ?></td>
                        <td><?= $ppMethodLabel ?></td>
                    </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
    <?php endif; ?>

    <!-- ═══════ DEATH AUDIT TIMELINE ═══════ -->
    <?php if (!empty($deathAuditLogs)): ?>
    <div class="card" style="margin-bottom:20px;">
        <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;background:#F9FAFB;">
            <h3 style="margin:0;font-size:16px;color:#1F2937;">&#x23f3; سجل العمليات التفصيلي — حالات الوفاة</h3>
        </div>
        <div style="padding:20px;">
            <div style="position:relative;padding-right:30px;">
                <div style="position:absolute;right:12px;top:0;bottom:0;width:2px;background:#E5E7EB;"></div>
                <?php foreach ($deathAuditLogs as $log):
                    $iconMap = [
                        'snapshot_taken' => '&#x1f4f7;', 'member_archived' => '&#x1f4e6;',
                        'new_member_created' => '&#x2728;', 'children_transferred' => '&#x1f46a;',
                        'temps_transferred' => '&#x1f465;', 'secondary_member_created' => '&#x1f4cc;',
                        'installments_transferred' => '&#x1f4b3;', 'documents_linked' => '&#x1f4c4;',
                        'subscriptions_created' => '&#x1f4c5;', 'transfer_completed' => '&#x2705;',
                        'case_completed' => '&#x2705;', 'spouse_deceased' => '&#x1f54a;', 'child_deceased' => '&#x1f54a;',
                    ];
                    $icon = $iconMap[$log['action_type']] ?? '&#x25cf;';
                ?>
                <div style="position:relative;margin-bottom:16px;padding-right:25px;">
                    <div style="position:absolute;right:-6px;top:4px;width:24px;height:24px;background:#fff;border:2px solid #3B82F6;border-radius:50%;text-align:center;line-height:20px;font-size:12px;"><?= $icon ?></div>
                    <div style="background:#F9FAFB;border:1px solid #E5E7EB;border-radius:8px;padding:10px 14px;">
                        <div style="font-weight:600;color:#1F2937;margin-bottom:3px;font-size:13px;"><?= e($log['action_description_ar']) ?></div>
                        <div style="font-size:11px;color:#6B7280;display:flex;gap:12px;flex-wrap:wrap;">
                            <span><?= e($log['performed_at']) ?></span>
                            <?php if (!empty($log['performer_name'])): ?>
                            <span>بواسطة: <?= e($log['performer_name']) ?></span>
                            <?php endif; ?>
                            <?php if (!empty($log['affected_entity_type'])): ?>
                            <span style="color:#9CA3AF;"><?= e($log['affected_entity_type']) ?> #<?= (int) $log['affected_entity_id'] ?></span>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>
                <?php endforeach; ?>
            </div>
        </div>
    </div>
    <?php endif; ?>

    <!-- ═══════ ARCHIVE SNAPSHOTS ═══════ -->
    <?php if (!empty($snapshots)): ?>
    <div class="card" style="margin-bottom:20px;">
        <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;background:#F9FAFB;">
            <h3 style="margin:0;font-size:16px;color:#1F2937;">&#x1f4e6; لقطات الأرشيف (<?= count($snapshots) ?>)</h3>
        </div>
        <div class="table-responsive" style="padding:10px;">
            <table class="data-table" style="width:100%;font-size:13px;">
                <thead><tr><th>#</th><th>السبب</th><th>رقم العضوية</th><th>تاريخ اللقطة</th></tr></thead>
                <tbody>
                <?php foreach ($snapshots as $snap): ?>
                    <tr>
                        <td style="font-weight:600;">#<?= (int) $snap['id'] ?></td>
                        <td><?= e($snap['snapshot_reason'] ?? '—') ?></td>
                        <td style="font-family:monospace;"><?= e($snap['membership_number'] ?? '—') ?></td>
                        <td><?= e(substr($snap['snapshot_taken_at'] ?? '', 0, 16)) ?></td>
                    </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
    <?php endif; ?>

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