<?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 ?>/edit" class="btn btn-outline">✏️ تعديل</a>
<a href="/audit/entity/members/<?= (int) $member->id ?>" class="btn btn-outline">📜 سجل المراجعة</a>
<a href="/members" class="btn btn-outline">← العودة</a>
<?php $__template->endSection(); ?>

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

<?php
$statusLabel = $member->getStatusLabel();
$statusColor = $member->getStatusColor();
$isInitialPhase = in_array($member->status, ['potential', 'under_review', 'interview_scheduled', 'accepted', 'payment_pending']);
$isActive = ($member->status === 'active');
?>

<!-- ═══════════════════════════════════════ -->
<!-- STEP 1: Form Fee Not Paid -->
<!-- ═══════════════════════════════════════ -->
<?php if (!$bill['form_fee_paid'] && $member->status === 'potential'): ?>
<div class="card" style="margin-bottom:20px;padding:30px;background:linear-gradient(135deg, #FEF2F2, #FEE2E2);border:2px solid #DC2626;">
    <div style="text-align:center;margin-bottom:20px;">
        <div style="font-size:48px;">🔒</div>
        <h2 style="color:#DC2626;margin:10px 0 5px;">يجب دفع رسوم الاستمارة أولاً</h2>
    </div>
    <div style="max-width:500px;margin:0 auto;background:#fff;border-radius:12px;padding:25px;box-shadow:0 4px 12px rgba(0,0,0,0.1);">
        <table style="width:100%;font-size:15px;margin-bottom:20px;">
            <tr><td style="padding:8px 0;color:#6B7280;">رقم الاستمارة</td><td style="padding:8px 0;text-align:left;font-weight:700;font-size:20px;color:#0D7377;"><?= e($member->form_number ?? '—') ?></td></tr>
            <tr><td style="padding:8px 0;color:#6B7280;">رسوم الاستمارة</td><td style="padding:8px 0;text-align:left;font-weight:700;font-size:24px;color:#DC2626;"><?= money($formFee) ?></td></tr>
        </table>
        <form method="POST" action="/members/<?= (int) $member->id ?>/pay-form-fee">
            <?= csrf_field() ?>
            <div class="form-group" style="margin-bottom:15px;">
                <label class="form-label">طريقة الدفع</label>
                <select name="payment_method" class="form-select"><option value="cash">نقدي 💵</option><option value="visa">فيزا 💳</option><option value="bank_transfer">تحويل بنكي 🏦</option></select>
            </div>
            <button type="submit" class="btn btn-primary" style="width:100%;padding:15px;font-size:18px;background:#059669;border-color:#059669;" onclick="return confirm('تأكيد دفع <?= money($formFee) ?>؟')">💰 دفع رسوم الاستمارة</button>
        </form>
    </div>
</div>

<!-- ═══════════════════════════════════════ -->
<!-- STEP 2: Fee Paid, Form Not Filled -->
<!-- ═══════════════════════════════════════ -->
<?php elseif ($bill['form_fee_paid'] && !$formFilled && $member->status === 'potential'): ?>
<div class="card" style="margin-bottom:20px;padding:25px;background:#F0FDF4;border:2px solid #059669;">
    <div style="display:flex;justify-content:space-between;align-items:center;">
        <div>
            <h3 style="color:#059669;margin:0 0 8px;">✅ تم دفع رسوم الاستمارة — الآن قم بملء البيانات</h3>
            <p style="color:#6B7280;margin:0;font-size:14px;">استمارة رقم <strong><?= e($member->form_number) ?></strong></p>
        </div>
        <a href="/members/<?= (int) $member->id ?>/fill-form" class="btn btn-primary" style="padding:15px 30px;font-size:18px;">📝 ملء الاستمارة</a>
    </div>
</div>
<?php endif; ?>

<!-- Member Header -->
<div class="card" style="margin-bottom:20px;padding:25px;">
    <div style="display:flex;justify-content:space-between;align-items:start;">
        <div>
            <h2 style="margin:0 0 5px;color:#1A1A2E;font-size:22px;"><?= e($member->full_name_ar) ?></h2>
            <?php if ($member->full_name_en): ?><div style="color:#6B7280;font-size:14px;margin-bottom:8px;"><?= e($member->full_name_en) ?></div><?php endif; ?>
            <div style="display:flex;gap:15px;flex-wrap:wrap;font-size:13px;color:#6B7280;margin-top:10px;">
                <?php if ($member->membership_number): ?><span>🪪 عضوية: <strong style="color:#0D7377;font-size:16px;"><?= e($member->membership_number) ?></strong></span><?php endif; ?>
                <span>📋 استمارة: <strong style="color:#D97706;"><?= e($member->form_number ?? '—') ?></strong></span>
                <span>🏢 <?= e($branchName) ?></span>
                <span><?= $member->gender === 'male' ? '👨' : '👩' ?> <?= $member->getGenderLabel() ?></span>
                <?php if ($member->age_years): ?><span>🎂 <?= (int) $member->age_years ?> سنة</span><?php endif; ?>
            </div>
        </div>
        <div style="text-align:left;">
            <span style="display:inline-block;padding:6px 16px;border-radius:20px;font-weight:700;font-size:14px;color:#fff;background:<?= $statusColor ?>;"><?= e($statusLabel) ?></span>
            <div style="margin-top:10px;font-size:12px;color:#9CA3AF;">تاريخ: <?= e(substr($member->created_at, 0, 10)) ?></div>
        </div>
    </div>
</div>

<!-- ═══════════════════════════════════════════════ -->
<!-- BILL / INVOICE — visible during initial phase -->
<!-- ═══════════════════════════════════════════════ -->
<?php if ($isInitialPhase && $formFilled): ?>
<div class="card" style="margin-bottom:20px;border:2px solid #0D7377;">
    <div style="padding:15px 20px;background:#0D7377;color:#fff;display:flex;justify-content:space-between;align-items:center;">
        <h3 style="margin:0;">🧾 فاتورة العضوية</h3>
        <div style="text-align:left;">
            <div style="font-size:12px;opacity:0.8;">الإجمالي المطلوب</div>
            <div style="font-size:28px;font-weight:700;"><?= money($bill['total_pending']) ?></div>
        </div>
    </div>

    <div style="padding:20px;">
        <table style="width:100%;font-size:14px;border-collapse:collapse;">
            <thead>
                <tr style="border-bottom:2px solid #E5E7EB;">
                    <th style="padding:10px;text-align:right;color:#6B7280;">البند</th>
                    <th style="padding:10px;text-align:left;color:#6B7280;width:140px;">المبلغ</th>
                    <th style="padding:10px;text-align:center;color:#6B7280;width:100px;">الحالة</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($bill['items'] as $item): ?>
                <tr style="border-bottom:1px solid #F3F4F6;">
                    <td style="padding:10px;">
                        <?= e($item['label']) ?>
                        <?php if (!empty($item['included_note'])): ?>
                            <small style="display:block;color:#059669;font-size:11px;"><?= e($item['included_note']) ?></small>
                        <?php endif; ?>
                    </td>
                    <td style="padding:10px;text-align:left;font-weight:600;direction:ltr;<?= $item['included'] ? 'color:#9CA3AF;text-decoration:line-through;' : '' ?>">
                        <?= money($item['amount']) ?>
                    </td>
                    <td style="padding:10px;text-align:center;">
                        <?php if ($item['paid']): ?>
                            <span style="color:#059669;font-weight:700;">✅ مدفوع</span>
                        <?php elseif ($item['included']): ?>
                            <span style="color:#0284C7;font-size:12px;">مشمول</span>
                        <?php else: ?>
                            <span style="color:#DC2626;font-weight:600;">⏳ معلق</span>
                        <?php endif; ?>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
            <tfoot>
                <?php if (bccomp($bill['total_included'], '0', 2) > 0): ?>
                <tr style="background:#EFF6FF;">
                    <td style="padding:10px;font-weight:600;color:#0284C7;">بنود مشمولة في قيمة العضوية</td>
                    <td style="padding:10px;text-align:left;color:#0284C7;"><?= money($bill['total_included']) ?></td>
                    <td></td>
                </tr>
                <?php endif; ?>
                <tr style="background:#F0FDF4;">
                    <td style="padding:10px;font-weight:600;color:#059669;">إجمالي المدفوع</td>
                    <td style="padding:10px;text-align:left;font-weight:700;color:#059669;"><?= money($bill['total_paid']) ?></td>
                    <td></td>
                </tr>
                <tr style="background:#FEF2F2;">
                    <td style="padding:12px;font-weight:700;font-size:16px;color:#DC2626;">المتبقي للسداد</td>
                    <td style="padding:12px;text-align:left;font-weight:700;font-size:20px;color:#DC2626;"><?= money($bill['total_pending']) ?></td>
                    <td></td>
                </tr>
            </tfoot>
        </table>
    </div>

    <!-- Add Family Before Paying -->
    <div style="padding:0 20px 15px;display:flex;gap:10px;flex-wrap:wrap;border-top:1px solid #E5E7EB;padding-top:15px;">
        <span style="color:#6B7280;font-size:13px;padding:8px 0;">أضف أفراد الأسرة قبل الدفع:</span>
        <a href="/members/<?= (int) $member->id ?>/spouses/create" class="btn btn-sm btn-outline">💍 إضافة زوجة</a>
        <a href="/members/<?= (int) $member->id ?>/children/create" class="btn btn-sm btn-outline">👶 إضافة ابن/ابنة</a>
    </div>

    <!-- Payment Section -->
    <?php if (bccomp($bill['total_pending'], '0', 2) > 0 && in_array($member->status, ['accepted', 'payment_pending'])): ?>
    <div style="padding:20px;background:#FFF7ED;border-top:2px solid #F59E0B;">
        <h4 style="margin:0 0 15px;color:#D97706;">💰 اختر طريقة السداد</h4>
        <div style="display:grid;grid-template-columns:1fr 1fr;gap:20px;">
            <!-- Cash Full -->
            <div style="background:#fff;border:2px solid #059669;border-radius:12px;padding:20px;">
                <h5 style="margin:0 0 10px;color:#059669;">💵 كاش كامل</h5>
                <p style="font-size:13px;color:#6B7280;margin:0 0 10px;">ادفع المبلغ كاملاً — بدون فوائد</p>
                <div style="font-size:24px;font-weight:700;color:#059669;margin-bottom:15px;"><?= money($bill['total_pending']) ?></div>
                <form method="POST" action="/members/<?= (int) $member->id ?>/pay-membership">
                    <?= csrf_field() ?>
                    <input type="hidden" name="payment_type" value="membership_fee">
                    <input type="hidden" name="amount" value="<?= e($bill['total_pending']) ?>">
                    <select name="payment_method" class="form-select" style="margin-bottom:10px;">
                        <option value="cash">نقدي</option><option value="visa">فيزا</option><option value="bank_transfer">تحويل بنكي</option>
                    </select>
                    <button type="submit" class="btn btn-primary" style="width:100%;background:#059669;border-color:#059669;" onclick="return confirm('تأكيد دفع <?= money($bill['total_pending']) ?>؟')">ادفع الآن</button>
                </form>
            </div>
            <!-- Installment -->
            <div style="background:#fff;border:2px solid #0284C7;border-radius:12px;padding:20px;">
                <h5 style="margin:0 0 10px;color:#0284C7;">📅 تقسيط</h5>
                <p style="font-size:13px;color:#6B7280;margin:0 0 5px;">مقدم 25% على الأقل + باقي على أقساط</p>
                <p style="font-size:12px;color:#D97706;margin:0 0 10px;">فائدة 22% سنوياً — حتى 30 شهر</p>
                <?php
                $minDown = bcdiv(bcmul($bill['total_pending'], '25', 2), '100', 2);
                $remaining = bcsub($bill['total_pending'], $minDown, 2);
                $interest = bcdiv(bcmul($remaining, '22', 2), '100', 2);
                $totalWithInterest = bcadd($remaining, $interest, 2);
                $monthlyEst = bcdiv($totalWithInterest, '30', 2);
                ?>
                <table style="width:100%;font-size:13px;margin-bottom:10px;">
                    <tr><td style="color:#6B7280;">المقدم (25%)</td><td style="text-align:left;font-weight:600;"><?= money($minDown) ?></td></tr>
                    <tr><td style="color:#6B7280;">الفائدة (22%)</td><td style="text-align:left;color:#D97706;"><?= money($interest) ?></td></tr>
                    <tr><td style="color:#6B7280;">القسط (~30 شهر)</td><td style="text-align:left;font-weight:600;"><?= money($monthlyEst) ?></td></tr>
                </table>
                <form method="POST" action="/members/<?= (int) $member->id ?>/pay-membership">
                    <?= csrf_field() ?>
                    <input type="hidden" name="payment_type" value="down_payment">
                    <div class="form-group" style="margin-bottom:10px;">
                        <label class="form-label" style="font-size:12px;">المقدم (≥ <?= money($minDown) ?>)</label>
                        <input type="number" name="amount" value="<?= e($minDown) ?>" min="<?= e($minDown) ?>" step="0.01" class="form-input" style="direction:ltr;text-align:left;" required>
                    </div>
                    <div class="form-group" style="margin-bottom:10px;">
                        <label class="form-label" style="font-size:12px;">عدد الأشهر (حتى 30)</label>
                        <input type="number" name="installment_months" value="30" min="1" max="30" class="form-input" style="direction:ltr;text-align:left;">
                    </div>
                    <select name="payment_method" class="form-select" style="margin-bottom:10px;">
                        <option value="cash">نقدي</option><option value="visa">فيزا</option><option value="bank_transfer">تحويل</option>
                    </select>
                    <button type="submit" class="btn btn-primary" style="width:100%;background:#0284C7;border-color:#0284C7;" onclick="return confirm('تأكيد دفع المقدم؟')">ادفع المقدم</button>
                </form>
            </div>
        </div>
        <div style="margin-top:15px;padding:10px;background:#FEF2F2;border-radius:8px;font-size:12px;color:#DC2626;">
            ⚠️ مهلة السداد: 15 يوم من تاريخ القبول — بعدها تنتهي الاستمارة
        </div>
    </div>
    <?php elseif ($formFilled && in_array($member->status, ['under_review', 'interview_scheduled'])): ?>
    <div style="padding:20px;background:#EFF6FF;border-top:2px solid #0284C7;">
        <p style="margin:0;color:#0284C7;font-size:14px;">📋 الفاتورة جاهزة — في انتظار قرار مجلس الأمناء قبل السداد</p>
        <p style="margin:5px 0 0;color:#6B7280;font-size:13px;">يمكنك إضافة أفراد الأسرة الآن وستُضاف رسومهم تلقائياً</p>
    </div>
    <?php endif; ?>
</div>
<?php endif; ?>

<!-- Info Grid -->
<div style="display:grid;grid-template-columns:1fr 1fr;gap:20px;margin-bottom:20px;">
    <div class="card">
        <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;"><h3 style="margin:0;color:#0D7377;font-size:15px;">👤 البيانات الشخصية</h3></div>
        <div style="padding:15px 20px;">
            <table style="width:100%;font-size:13px;">
                <tr><td style="padding:5px 0;color:#6B7280;width:40%;">الرقم القومي</td><td style="padding:5px 0;direction:ltr;text-align:right;font-weight:600;"><?= e($member->national_id ?? '—') ?></td></tr>
                <tr><td style="padding:5px 0;color:#6B7280;">تاريخ الميلاد</td><td style="padding:5px 0;"><?= e($member->date_of_birth ?? '—') ?></td></tr>
                <tr><td style="padding:5px 0;color:#6B7280;">المحمول</td><td style="padding:5px 0;direction:ltr;text-align:right;"><?= e($member->phone_mobile ?? '—') ?></td></tr>
                <tr><td style="padding:5px 0;color:#6B7280;">الجنسية</td><td style="padding:5px 0;"><?= e($member->nationality ?? '—') ?></td></tr>
                <tr><td style="padding:5px 0;color:#6B7280;">المؤهل</td><td style="padding:5px 0;"><?= e($qualificationName) ?></td></tr>
                <tr><td style="padding:5px 0;color:#6B7280;">الحالة الاجتماعية</td><td style="padding:5px 0;"><?= e($member->marital_status ?? '—') ?></td></tr>
                <?php if ($member->email): ?><tr><td style="padding:5px 0;color:#6B7280;">البريد</td><td style="padding:5px 0;direction:ltr;text-align:right;"><?= e($member->email) ?></td></tr><?php endif; ?>
                <?php if ($member->residence_address): ?><tr><td style="padding:5px 0;color:#6B7280;">العنوان</td><td style="padding:5px 0;"><?= e($member->residence_address) ?></td></tr><?php endif; ?>
                <?php if ($member->occupation): ?><tr><td style="padding:5px 0;color:#6B7280;">المهنة</td><td style="padding:5px 0;"><?= e($member->occupation) ?></td></tr><?php endif; ?>
            </table>
        </div>
    </div>
    <div class="card">
        <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;"><h3 style="margin:0;color:#0D7377;font-size:15px;">💰 الملخص المالي</h3></div>
        <div style="padding:15px 20px;">
            <div style="display:grid;grid-template-columns:<?= !empty($specialDiscount) ? '1fr 1fr 1fr' : '1fr 1fr' ?>;gap:15px;">
                <div style="background:#F0FDF4;padding:15px;border-radius:8px;text-align:center;"><div style="font-size:20px;font-weight:700;color:#059669;"><?= money($member->membership_value ?? '0') ?></div><div style="color:#6B7280;font-size:11px;">قيمة العضوية</div></div>
                <?php if (!empty($specialDiscount)): ?>
                <div style="background:#FFF7ED;padding:15px;border-radius:8px;text-align:center;">
                    <div style="font-size:20px;font-weight:700;color:#D97706;">-<?= money($member->discount_amount ?? '0') ?></div>
                    <div style="color:#6B7280;font-size:11px;">خصم: <?= e($specialDiscount['name_ar']) ?> (<?= e($specialDiscount['discount_percentage']) ?>%)</div>
                    <?php if ($specialDiscount['requires_document'] && !empty($member->special_discount_document)): ?>
                    <a href="/<?= e($member->special_discount_document) ?>" target="_blank" style="font-size:11px;color:#0D7377;">📄 عرض الإثبات</a>
                    <?php endif; ?>
                </div>
                <?php endif; ?>
                <div style="background:#EFF6FF;padding:15px;border-radius:8px;text-align:center;"><div style="font-size:20px;font-weight:700;color:#0284C7;"><?= money($bill['total_paid']) ?></div><div style="color:#6B7280;font-size:11px;">إجمالي المدفوع</div></div>
            </div>
            <a href="/payments/member/<?= (int) $member->id ?>" class="btn btn-outline" style="width:100%;text-align:center;margin-top:10px;">📜 سجل المدفوعات</a>
        </div>
    </div>
</div>

<!-- ═══════════════════════════════════════════════ -->
<!-- ALL ACTIONS — organized by category -->
<!-- ═══════════════════════════════════════════════ -->
<div class="card" style="margin-bottom:20px;">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;">
        <h3 style="margin:0;color:#0D7377;font-size:15px;">⚡ الإجراءات</h3>
    </div>
    <div style="padding:15px 20px;">

        <!-- Registration & Form -->
        <?php if ($bill['form_fee_paid'] && !$formFilled): ?>
        <div style="margin-bottom:15px;">
            <div style="font-size:12px;color:#6B7280;font-weight:600;margin-bottom:8px;text-transform:uppercase;">📋 الاستمارة</div>
            <div style="display:flex;gap:8px;flex-wrap:wrap;">
                <a href="/members/<?= (int) $member->id ?>/fill-form" class="btn btn-primary">📝 ملء الاستمارة</a>
            </div>
        </div>
        <?php endif; ?>

        <!-- Family Management -->
        <?php if ($formFilled): ?>
        <div style="margin-bottom:15px;">
            <div style="font-size:12px;color:#6B7280;font-weight:600;margin-bottom:8px;text-transform:uppercase;">👨‍👩‍👧‍👦 الأسرة</div>
            <div style="display:flex;gap:8px;flex-wrap:wrap;">
                <a href="/members/<?= (int) $member->id ?>/spouses/create" class="btn btn-sm btn-outline">💍 إضافة زوج/ة</a>
                <a href="/members/<?= (int) $member->id ?>/children/create" class="btn btn-sm btn-outline">👶 إضافة ابن/ة</a>
                <a href="/members/<?= (int) $member->id ?>/temporary/create" class="btn btn-sm btn-outline">👤 إضافة عضو مؤقت</a>
            </div>
        </div>
        <?php endif; ?>

        <!-- Interview & Review -->
        <?php if (in_array($member->status, ['under_review', 'interview_scheduled', 'potential'])): ?>
        <div style="margin-bottom:15px;">
            <div style="font-size:12px;color:#6B7280;font-weight:600;margin-bottom:8px;text-transform:uppercase;">🗓️ المقابلة والمراجعة</div>
            <div style="display:flex;gap:8px;flex-wrap:wrap;">
                <a href="/interviews/schedule/<?= (int) $member->id ?>" class="btn btn-sm btn-outline">🗓️ تحديد مقابلة</a>
            </div>
        </div>
        <?php endif; ?>

        <!-- Financial -->
        <div style="margin-bottom:15px;">
            <div style="font-size:12px;color:#6B7280;font-weight:600;margin-bottom:8px;text-transform:uppercase;">💰 المالية</div>
            <div style="display:flex;gap:8px;flex-wrap:wrap;">
                <a href="/payments/process/<?= (int) $member->id ?>" class="btn btn-sm btn-outline">💰 تسجيل دفعة</a>
                <a href="/payments/member/<?= (int) $member->id ?>" class="btn btn-sm btn-outline">📜 سجل المدفوعات</a>
                <?php if ($isActive): ?>
                    <a href="/installments/create/<?= (int) $member->id ?>" class="btn btn-sm btn-outline">📅 خطة تقسيط</a>
                    <a href="/subscriptions/member/<?= (int) $member->id ?>" class="btn btn-sm btn-outline">🔄 الاشتراكات</a>
                <?php endif; ?>
            </div>
        </div>

        <!-- Documents & Carnet -->
        <?php if ($formFilled): ?>
        <div style="margin-bottom:15px;">
            <div style="font-size:12px;color:#6B7280;font-weight:600;margin-bottom:8px;text-transform:uppercase;">📁 المستندات والكارنيه</div>
            <div style="display:flex;gap:8px;flex-wrap:wrap;">
                <a href="/documents/upload/<?= (int) $member->id ?>" class="btn btn-sm btn-outline">📁 رفع مستندات</a>
                <a href="/documents/member/<?= (int) $member->id ?>" class="btn btn-sm btn-outline">📂 عرض المستندات</a>
                <?php if ($isActive): ?>
                    <a href="/carnets/issue/<?= (int) $member->id ?>" class="btn btn-sm btn-outline">🪪 إصدار كارنيه</a>
                <?php endif; ?>
            </div>
        </div>
        <?php endif; ?>

        <!-- Special Memberships (active members only) -->
        <?php if ($isActive): ?>
        <div style="margin-bottom:15px;">
            <div style="font-size:12px;color:#6B7280;font-weight:600;margin-bottom:8px;text-transform:uppercase;">🏅 عضويات خاصة</div>
            <div style="display:flex;gap:8px;flex-wrap:wrap;">
                <a href="/seasonal/create/<?= (int) $member->id ?>" class="btn btn-sm btn-outline">☀️ عضوية موسمية</a>
                <a href="/members/<?= (int) $member->id ?>/sports/create" class="btn btn-sm btn-outline">🏆 عضوية رياضية</a>
                <a href="/members/<?= (int) $member->id ?>/honorary/create" class="btn btn-sm btn-outline">⭐ عضوية شرفية</a>
                <a href="/members/<?= (int) $member->id ?>/foreign/create" class="btn btn-sm btn-outline">🌍 عضوية أجنبية</a>
            </div>
        </div>
        <?php endif; ?>

        <!-- Violations & Fines (active members only) -->
        <?php if ($isActive): ?>
        <div style="margin-bottom:15px;">
            <div style="font-size:12px;color:#6B7280;font-weight:600;margin-bottom:8px;text-transform:uppercase;">⚠️ المخالفات والغرامات</div>
            <div style="display:flex;gap:8px;flex-wrap:wrap;">
                <a href="/violations/create/<?= (int) $member->id ?>" class="btn btn-sm btn-outline" style="color:#DC2626;border-color:#DC2626;">⚠️ تسجيل مخالفة</a>
            </div>
        </div>
        <?php endif; ?>

        <!-- Life Events (active members only) -->
        <?php if ($isActive): ?>
        <div style="margin-bottom:15px;">
            <div style="font-size:12px;color:#6B7280;font-weight:600;margin-bottom:8px;text-transform:uppercase;">🔀 التحويلات وأحداث الحياة</div>
            <div style="display:flex;gap:8px;flex-wrap:wrap;">
                <a href="/transfers/create/<?= (int) $member->id ?>" class="btn btn-sm btn-outline">🔀 طلب تحويل/فصل</a>
                <a href="/divorce/create/<?= (int) $member->id ?>" class="btn btn-sm btn-outline">💔 تسجيل طلاق</a>
                <a href="/death/create/<?= (int) $member->id ?>" class="btn btn-sm btn-outline" style="color:#6B7280;">🕊️ تسجيل وفاة</a>
                <a href="/waiver/create/<?= (int) $member->id ?>" class="btn btn-sm btn-outline">📤 طلب تنازل</a>
            </div>
        </div>
        <?php endif; ?>

        <!-- Archive & Audit -->
        <div style="margin-bottom:0;">
            <div style="font-size:12px;color:#6B7280;font-weight:600;margin-bottom:8px;text-transform:uppercase;">🗄️ الأرشيف والمراجعة</div>
            <div style="display:flex;gap:8px;flex-wrap:wrap;">
                <a href="/audit/entity/members/<?= (int) $member->id ?>" class="btn btn-sm btn-outline">📜 سجل المراجعة</a>
                <a href="/archive/entity/members/<?= (int) $member->id ?>" class="btn btn-sm btn-outline">🗄️ اللقطات الأرشيفية</a>
                <?php if ($member->membership_number): ?>
                    <a href="/archive/number-chain/<?= urlencode($member->membership_number) ?>" class="btn btn-sm btn-outline">🔗 سلسلة رقم العضوية</a>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>

<!-- Spouses -->
<?php if (!empty($spouses)): ?>
<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;font-size:15px;">💍 الزوجات (<?= count($spouses) ?>)</h3>
        <a href="/members/<?= (int) $member->id ?>/spouses/create" class="btn btn-sm btn-outline">+ إضافة</a>
    </div>
    <div class="table-responsive"><table class="data-table"><thead><tr><th>#</th><th>الاسم</th><th>الرقم القومي</th><th>الرسوم</th><th>الحالة</th><th>الإجراءات</th></tr></thead><tbody>
    <?php foreach ($spouses as $s): ?>
    <tr>
        <td><?= (int) $s['spouse_order'] ?></td>
        <td style="font-weight:600;"><?= e($s['full_name_ar']) ?></td>
        <td style="direction:ltr;text-align:right;font-size:12px;"><?= e($s['national_id'] ?? '—') ?></td>
        <td style="font-weight:600;"><?php
            $fee = $s['addition_fee'] ?? '0.00';
            if (bccomp($fee, '0', 2) <= 0 && (int) $s['spouse_order'] === 1) echo '<span style="color:#059669;">مشمولة</span>';
            else echo money($fee);
        ?></td>
        <td><span style="color:<?= $s['status'] === 'active' ? '#059669' : '#DC2626' ?>;font-weight:600;">● <?= $s['status'] === 'active' ? 'نشط' : $s['status'] ?></span></td>
        <td><a href="/members/<?= (int) $member->id ?>/spouses/<?= (int) $s['id'] ?>" class="btn btn-sm btn-outline">عرض</a></td>
    </tr>
    <?php endforeach; ?>
    </tbody></table></div>
</div>
<?php endif; ?>

<!-- Children -->
<?php if (!empty($children)): ?>
<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;font-size:15px;">👨‍👩‍👧‍👦 الأبناء (<?= count($children) ?>)</h3>
        <a href="/members/<?= (int) $member->id ?>/children/create" class="btn btn-sm btn-outline">+ إضافة</a>
    </div>
    <div class="table-responsive"><table class="data-table"><thead><tr><th>#</th><th>الاسم</th><th>النوع</th><th>السن</th><th>التصنيف</th><th>الرسوم</th><th>الإجراءات</th></tr></thead><tbody>
    <?php foreach ($children as $c): ?>
    <tr>
        <td><?= (int) $c['child_order'] ?></td>
        <td style="font-weight:600;"><?= e($c['full_name_ar']) ?></td>
        <td><?= $c['gender'] === 'male' ? 'ذكر' : 'أنثى' ?></td>
        <td><?= (int) ($c['age_years'] ?? 0) ?></td>
        <td style="font-size:12px;"><?= match($c['classification'] ?? '') { 'included' => '<span style="color:#059669;">مشمول</span>', 'dependent_with_fee' => '<span style="color:#D97706;">برسوم</span>', default => e($c['classification'] ?? '') } ?></td>
        <td style="font-weight:600;"><?php
            $fee = $c['addition_fee'] ?? '0.00';
            echo bccomp($fee, '0', 2) <= 0 ? '<span style="color:#059669;">مشمول</span>' : money($fee);
        ?></td>
        <td><a href="/members/<?= (int) $member->id ?>/children/<?= (int) $c['id'] ?>" class="btn btn-sm btn-outline">عرض</a></td>
    </tr>
    <?php endforeach; ?>
    </tbody></table></div>
</div>
<?php endif; ?>

<!-- Status Change -->
<div class="card" style="margin-bottom:20px;">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;"><h3 style="margin:0;color:#0D7377;font-size:15px;">🔄 تغيير الحالة</h3></div>
    <div style="padding:15px 20px;">
        <form method="POST" action="/members/<?= (int) $member->id ?>/status" style="display:flex;gap:10px;align-items:center;">
            <?= csrf_field() ?>
            <select name="status" class="form-select" style="width:auto;">
                <?php foreach (\App\Modules\Members\Models\Member::getStatusOptions() as $val => $label): ?>
                    <option value="<?= e($val) ?>" <?= $member->status === $val ? 'selected' : '' ?>><?= e($label) ?></option>
                <?php endforeach; ?>
            </select>
            <button type="submit" class="btn btn-outline" onclick="return confirm('تغيير حالة العضو؟')">تحديث</button>
        </form>
    </div>
</div>

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