<?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:900px;">

    <!-- 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>
            </div>
            <div style="font-size:48px;">&#x1f4cb;</div>
        </div>
    </div>

    <!-- Death Cases Summary -->
    <?php if (!empty($cases)): ?>
    <div class="card" style="margin-bottom:20px;">
        <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;">
            <h3 style="margin:0;font-size:16px;color:#1F2937;">حالات الوفاة المرتبطة</h3>
        </div>
        <div class="table-responsive">
            <table class="data-table" style="width:100%;">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>العضو المتوفى</th>
                        <th>نوع الوفاة</th>
                        <th>تاريخ الوفاة</th>
                        <th>الحالة</th>
                        <th></th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($cases as $c):
                        $typeLabel = match($c['deceased_type']) {
                            'primary_member' => 'عضو أساسي',
                            'spouse' => 'زوج/ة',
                            'child' => 'ابن/ابنة',
                            default => $c['deceased_type'],
                        };
                        $statusLabel = match($c['status']) {
                            'recorded' => 'مسجل',
                            'board_review' => 'مراجعة مجلس الإدارة',
                            'board_approved' => 'معتمد',
                            'rejected' => 'مرفوض',
                            'fee_paid' => 'تم السداد',
                            'pending_form_fill' => 'انتظار ملء الاستمارة',
                            'completed' => 'مكتمل',
                            default => $c['status'],
                        };
                        $statusColor = match($c['status']) {
                            'completed' => '#059669',
                            'rejected' => '#DC2626',
                            'board_approved', 'fee_paid' => '#3B82F6',
                            default => '#F59E0B',
                        };
                    ?>
                    <tr>
                        <td style="font-weight:600;">#<?= (int) $c['id'] ?></td>
                        <td><?= e($c['member_name']) ?></td>
                        <td><?= $typeLabel ?></td>
                        <td><?= e($c['death_date'] ?? '') ?></td>
                        <td><span style="color:<?= $statusColor ?>;font-weight:600;"><?= $statusLabel ?></span></td>
                        <td><a href="/death/<?= (int) $c['id'] ?>" class="btn btn-sm btn-outline">عرض</a></td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
    <?php endif; ?>

    <!-- Audit Timeline -->
    <?php if (!empty($auditLogs)): ?>
    <div class="card">
        <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;">
            <h3 style="margin:0;font-size:16px;color:#1F2937;">سجل العمليات التفصيلي</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 ($auditLogs 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:20px;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:12px 15px;">
                        <div style="font-weight:600;color:#1F2937;margin-bottom:4px;"><?= e($log['action_description_ar']) ?></div>
                        <div style="font-size:12px;color:#6B7280;display:flex;gap:15px;flex-wrap:wrap;">
                            <span><?= e($log['performed_at']) ?></span>
                            <?php if ($log['performer_name']): ?>
                            <span>بواسطة: <?= e($log['performer_name']) ?></span>
                            <?php endif; ?>
                            <?php if ($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 else: ?>
    <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 endif; ?>

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