<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>حالة وفاة #<?= (int) $case['id'] ?><?php $__template->endSection(); ?>
<?php $__template->section('content'); ?>

<?php
$statusLabels = [
    'recorded'          => 'مسجّل — في انتظار الدفع',
    'fee_paid'          => 'تم الدفع',
    'pending_form_fill' => 'في انتظار ملء الاستمارة',
    'completed'         => 'مكتمل',
];
$statusColors = [
    'recorded'          => '#D97706',
    'fee_paid'          => '#2563EB',
    'pending_form_fill' => '#7C3AED',
    'completed'         => '#059669',
];
$statusLabel = $statusLabels[$case['status']] ?? $case['status'];
$statusColor = $statusColors[$case['status']] ?? '#6B7280';
$deceasedLabel = match($case['deceased_type']) {
    'primary_member' => 'العضو الرئيسي',
    'spouse'         => 'الزوج/ة',
    'child'          => 'ابن/ابنة',
    default          => $case['deceased_type'],
};
?>

<!-- Case Info -->
<div class="card" style="padding:20px;margin-bottom:20px;">
    <div style="display:flex;justify-content:space-between;align-items:start;">
        <table style="width:100%;max-width:600px;font-size:14px;">
            <tr><td style="padding:6px 0;color:#6B7280;width:35%;">العضو</td><td style="padding:6px 0;font-weight:600;"><?= e($case['member_name']) ?> (<?= e($case['membership_number'] ?? '—') ?>)</td></tr>
            <tr><td style="padding:6px 0;color:#6B7280;">المتوفى</td><td style="padding:6px 0;font-weight:600;"><?= $deceasedLabel ?></td></tr>
            <tr><td style="padding:6px 0;color:#6B7280;">تاريخ الوفاة</td><td style="padding:6px 0;"><?= e($case['death_date']) ?></td></tr>
            <?php if ($case['death_certificate_number']): ?>
            <tr><td style="padding:6px 0;color:#6B7280;">شهادة الوفاة</td><td style="padding:6px 0;"><?= e($case['death_certificate_number']) ?></td></tr>
            <?php endif; ?>
            <tr><td style="padding:6px 0;color:#6B7280;">الحالة</td><td style="padding:6px 0;font-weight:700;color:<?= $statusColor ?>;">● <?= $statusLabel ?></td></tr>
            <?php if ($primarySpouse): ?>
            <tr><td style="padding:6px 0;color:#6B7280;">الزوجة الأساسية</td><td style="padding:6px 0;font-weight:600;"><?= e($primarySpouse['full_name_ar']) ?></td></tr>
            <?php endif; ?>
            <?php if ($newMember): ?>
            <tr><td style="padding:6px 0;color:#6B7280;">نُقلت العضوية إلى</td><td style="padding:6px 0;"><a href="/members/<?= (int) $newMember['id'] ?>" style="color:#1e3a5f;font-weight:700;font-size:16px;"><?= e($newMember['full_name_ar']) ?> — رقم <?= e($newMember['membership_number'] ?? '—') ?></a></td></tr>
            <?php endif; ?>
        </table>
        <a href="/death" class="btn btn-outline">← القائمة</a>
    </div>
</div>

<?php if (!empty($secondarySpouses)): ?>
<div class="card" style="margin-bottom:20px;padding:20px;">
    <h4 style="margin:0 0 10px;color:#6B7280;font-size:14px;">الزوجات الإضافيات (عضويات منفصلة)</h4>
    <?php foreach ($secondarySpouses as $ss): ?>
    <div style="padding:5px 0;font-size:14px;">• <?= e($ss['full_name_ar']) ?></div>
    <?php endforeach; ?>
</div>
<?php endif; ?>

<!-- Fee Breakdown -->
<?php if (bccomp($case['fee_amount'] ?? '0', '0', 2) > 0): ?>
<div class="card" style="margin-bottom:20px;">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;">
        <h3 style="margin:0;color:#D97706;font-size:15px;">تفصيل الرسوم</h3>
    </div>
    <div style="padding:20px;">
        <table style="width:100%;max-width:500px;font-size:14px;">
            <tr><td style="padding:8px 0;color:#6B7280;">رسوم الاستمارة (570)</td><td style="padding:8px 0;font-weight:600;direction:ltr;text-align:left;"><?= money($fees['formFee']) ?></td></tr>
            <tr><td style="padding:8px 0;color:#6B7280;">اشتراك سنوي</td><td style="padding:8px 0;font-weight:600;direction:ltr;text-align:left;"><?= money($fees['annualSubBase']) ?></td></tr>
            <tr><td style="padding:8px 0;color:#6B7280;">رسوم تنمية</td><td style="padding:8px 0;font-weight:600;direction:ltr;text-align:left;"><?= money($fees['devFee']) ?></td></tr>
            <?php
            $secondaryCount = 0;
            if (!empty($case['secondary_spouses_json'])) {
                $secondaryCount = count(json_decode($case['secondary_spouses_json'], true));
            }
            if ($secondaryCount > 0): ?>
            <tr><td style="padding:8px 0;color:#6B7280;">× <?= $secondaryCount + 1 ?> (أساسية + <?= $secondaryCount ?> إضافية)</td><td style="padding:8px 0;font-weight:600;"></td></tr>
            <?php endif; ?>
            <tr style="border-top:2px solid #1e3a5f;">
                <td style="padding:12px 0;font-weight:700;font-size:16px;">الإجمالي المطلوب</td>
                <td style="padding:12px 0;font-weight:800;font-size:22px;color:#DC2626;direction:ltr;text-align:left;"><?= money($case['fee_amount']) ?></td>
            </tr>
        </table>
    </div>
</div>
<?php endif; ?>

<!-- Action Buttons -->
<?php if ($case['status'] === 'recorded' && bccomp($case['fee_amount'] ?? '0', '0', 2) > 0): ?>
<div class="card" style="padding:20px;margin-bottom:20px;background:#FFF7ED;border:2px solid #F59E0B;">
    <h4 style="margin:0 0 15px;color:#D97706;">دفع رسوم نقل العضوية</h4>
    <form method="POST" action="/death/<?= (int) $case['id'] ?>/pay">
        <?= csrf_field() ?>
        <div style="display:flex;gap:10px;align-items:end;">
            <div class="form-group"><label class="form-label">المبلغ</label><input type="text" value="<?= money($case['fee_amount']) ?>" class="form-input" style="background:#F3F4F6;font-weight:700;font-size:18px;" readonly></div>
            <div class="form-group"><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="padding:10px 25px;" onclick="return confirm('تأكيد الدفع؟')">ادفع</button>
        </div>
    </form>
</div>
<?php endif; ?>

<?php if ($case['status'] === 'pending_form_fill'): ?>
<div class="card" style="padding:20px;margin-bottom:20px;background:#F5F3FF;border:2px solid #7C3AED;">
    <h4 style="margin:0 0 10px;color:#7C3AED;">ملء استمارة العضوية الجديدة</h4>
    <p style="font-size:13px;color:#6B7280;margin-bottom:15px;">يجب ملء استمارة العضوية الكاملة للزوجة قبل إتمام نقل العضوية.</p>
    <?php if ($case['primary_spouse_form_filled']): ?>
    <p style="color:#059669;font-weight:600;margin-bottom:10px;">✓ تم ملء الاستمارة — يمكن إتمام النقل</p>
    <?php else: ?>
    <a href="/death/<?= (int) $case['id'] ?>/fill-form" class="btn btn-primary">ملء الاستمارة الآن</a>
    <?php endif; ?>
</div>
<?php endif; ?>

<?php if ($case['status'] === 'pending_form_fill' && $case['primary_spouse_form_filled']): ?>
<form method="POST" action="/death/<?= (int) $case['id'] ?>/complete">
    <?= csrf_field() ?>
    <button type="submit" class="btn btn-primary" onclick="return confirm('سيتم نقل العضوية بنفس الرقم. متأكد؟')">إتمام نقل العضوية</button>
</form>
<?php elseif ($case['status'] === 'fee_paid' && $case['deceased_type'] !== 'primary_member'): ?>
<form method="POST" action="/death/<?= (int) $case['id'] ?>/complete">
    <?= csrf_field() ?>
    <button type="submit" class="btn btn-primary" onclick="return confirm('إتمام حالة الوفاة؟')">إتمام الإجراء</button>
</form>
<?php elseif ($case['status'] === 'recorded' && bccomp($case['fee_amount'] ?? '0', '0', 2) <= 0): ?>
<form method="POST" action="/death/<?= (int) $case['id'] ?>/complete">
    <?= csrf_field() ?>
    <button type="submit" class="btn btn-primary" onclick="return confirm('إتمام حالة الوفاة؟')">إتمام الإجراء</button>
</form>
<?php endif; ?>

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