<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>تسجيل حالة وفاة — <?= e($member['full_name_ar']) ?><?php $__template->endSection(); ?>
<?php $__template->section('content'); ?>

<div class="card" style="margin-bottom:15px;padding:15px;display:flex;justify-content:space-between;align-items:center;">
    <div>
        <strong>العضو:</strong> <?= e($member['full_name_ar']) ?>
        &nbsp;|&nbsp; <strong>رقم العضوية:</strong> <?= e($member['membership_number'] ?? 'لم يُحدد') ?>
    </div>
    <a href="/members/<?= (int) $member['id'] ?>" class="btn btn-outline">← العودة للعضو</a>
</div>

<form method="POST" action="/death/store/<?= (int) $member['id'] ?>" enctype="multipart/form-data">
    <?= csrf_field() ?>

    <!-- Mandatory documents — shown/required only when primary_member is selected -->
    <div id="docs-section" style="display:none;">
        <div class="card" style="margin-bottom:20px;border:2px solid #DC2626;">
            <div style="padding:15px 20px;border-bottom:1px solid #FCA5A5;background:#FEF2F2;">
                <h3 style="margin:0;color:#DC2626;font-size:15px;">المستندات المطلوبة — إلزامية لنقل العضوية</h3>
                <p style="margin:4px 0 0;font-size:12px;color:#6B7280;">PDF أو صورة (JPG/PNG) — لن يتم تسجيل الحالة بدونهما</p>
            </div>
            <div style="padding:20px;display:grid;grid-template-columns:1fr 1fr;gap:15px;">
                <div class="form-group">
                    <label class="form-label">شهادة الوفاة <span style="color:#DC2626;">*</span></label>
                    <input type="file" name="death_certificate" id="death_certificate" class="form-input" accept=".pdf,.png,.jpg,.jpeg">
                    <p style="font-size:11px;color:#9CA3AF;margin:4px 0 0;">PDF أو JPG أو PNG</p>
                </div>
                <div class="form-group">
                    <label class="form-label">إعلام الوراثة <span style="color:#DC2626;">*</span></label>
                    <input type="file" name="inheritance_notice" id="inheritance_notice" class="form-input" accept=".pdf,.png,.jpg,.jpeg">
                    <p style="font-size:11px;color:#9CA3AF;margin:4px 0 0;">PDF أو JPG أو PNG</p>
                </div>
            </div>
        </div>
    </div>

    <div class="card" style="margin-bottom:20px;">
        <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;"><h3 style="margin:0;color:#1F2937;">بيانات الوفاة</h3></div>
        <div style="padding:20px;display:grid;grid-template-columns:1fr 1fr;gap:15px;">
            <div class="form-group">
                <label class="form-label">المتوفى <span style="color:#DC2626;">*</span></label>
                <select name="deceased_type" id="deceased_type" class="form-select" required>
                    <option value="">-- اختر --</option>
                    <option value="primary_member">العضو الرئيسي (<?= e($member['full_name_ar']) ?>)</option>
                    <?php foreach ($spouses as $s): ?>
                    <option value="spouse" data-spouse-id="<?= (int) $s['id'] ?>">الزوج/ة: <?= e($s['full_name_ar']) ?></option>
                    <?php endforeach; ?>
                    <?php foreach ($children as $c): ?>
                    <option value="child" data-child-id="<?= (int) $c['id'] ?>">ابن/ابنة: <?= e($c['full_name_ar']) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="form-group">
                <label class="form-label">تاريخ الوفاة <span style="color:#DC2626;">*</span></label>
                <input type="date" name="death_date" class="form-input" required max="<?= e(date('Y-m-d')) ?>">
            </div>
            <div class="form-group">
                <label class="form-label">رقم شهادة الوفاة</label>
                <input type="text" name="death_certificate_number" class="form-input">
            </div>
            <input type="hidden" name="spouse_id" id="hidden_spouse_id">
            <input type="hidden" name="child_id" id="hidden_child_id">
        </div>
    </div>

    <!-- Primary member death: spouse selection + children assignment -->
    <div id="primary-death-section" style="display:none;">
        <?php if (count($spouses) > 0): ?>
        <div class="card" style="margin-bottom:20px;border:2px solid #0D7377;">
            <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;background:#F0FDFA;">
                <h3 style="margin:0;color:#0D7377;font-size:15px;">نقل العضوية — اختيار الزوجة الأساسية</h3>
                <p style="margin:5px 0 0;font-size:12px;color:#6B7280;">الزوجة الأساسية ترث نفس رقم العضوية وتملأ استمارة جديدة (570 + اشتراك سنوي)</p>
            </div>
            <div style="padding:20px;">
                <div class="form-group" style="margin-bottom:15px;">
                    <label class="form-label">الزوجة الأساسية (تأخذ نفس رقم العضوية) <span style="color:#DC2626;">*</span></label>
                    <select name="primary_spouse_id" id="primary_spouse_id" class="form-select">
                        <option value="">-- اختر الزوجة الأولى --</option>
                        <?php foreach ($spouses as $s): ?>
                        <option value="<?= (int) $s['id'] ?>"><?= e($s['full_name_ar']) ?> (ترتيب: <?= (int) $s['spouse_order'] ?>)</option>
                        <?php endforeach; ?>
                    </select>
                </div>

                <?php if (count($spouses) > 1): ?>
                <div class="form-group" style="margin-bottom:15px;">
                    <label class="form-label">الزوجات الإضافيات (عضويات منفصلة — 570 + اشتراك لكل واحدة)</label>
                    <?php foreach ($spouses as $s): ?>
                    <label style="display:flex;align-items:center;gap:8px;padding:5px 0;font-size:14px;" class="secondary-spouse-label" data-id="<?= (int) $s['id'] ?>">
                        <input type="checkbox" name="secondary_spouse_ids[]" value="<?= (int) $s['id'] ?>" class="secondary-spouse-cb">
                        <?= e($s['full_name_ar']) ?>
                    </label>
                    <?php endforeach; ?>
                </div>
                <?php endif; ?>
            </div>
        </div>

        <?php if (count($children) > 0): ?>
        <div class="card" style="margin-bottom:20px;">
            <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;">
                <h3 style="margin:0;color:#1F2937;font-size:15px;">توزيع الأبناء</h3>
                <p style="margin:5px 0 0;font-size:12px;color:#6B7280;">حدد لأي عضوية ينتقل كل ابن/ابنة (الافتراضي: الزوجة الأساسية)</p>
            </div>
            <div style="padding:20px;">
                <?php foreach ($children as $c): ?>
                <div style="display:flex;align-items:center;gap:10px;padding:8px 0;border-bottom:1px solid #F3F4F6;">
                    <span style="font-weight:600;min-width:150px;"><?= e($c['full_name_ar']) ?></span>
                    <select name="children_assignment[<?= (int) $c['id'] ?>]" class="form-select" style="max-width:250px;">
                        <option value="primary">الزوجة الأساسية</option>
                        <?php foreach ($spouses as $s): ?>
                        <option value="<?= (int) $s['id'] ?>"><?= e($s['full_name_ar']) ?> (منفصلة)</option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <?php endforeach; ?>
            </div>
        </div>
        <?php endif; ?>
        <?php endif; ?>
    </div>

    <!-- Fee Summary -->
    <div id="fee-section" style="display:none;">
        <div class="card" style="padding:20px;margin-bottom:20px;background:#FFF7ED;border:2px solid #F59E0B;">
            <h4 style="margin:0 0 15px;color:#D97706;">رسوم الإجراء</h4>
            <table style="font-size:14px;margin-bottom:15px;" id="fee-table">
                <tr><td style="padding:4px 20px 4px 0;color:#6B7280;">رسوم استمارة (570)</td><td style="font-weight:600;"><?= money($form_fee) ?></td></tr>
                <tr><td style="padding:4px 20px 4px 0;color:#6B7280;">اشتراك سنوي</td><td style="font-weight:600;"><?= money($annual_sub) ?></td></tr>
                <tr style="border-top:2px solid #D97706;" id="fee-total-row"><td style="padding:8px 20px 4px 0;font-weight:700;">الإجمالي</td><td style="font-weight:700;font-size:18px;color:#DC2626;"><?= money($total_fee) ?></td></tr>
            </table>
        </div>
    </div>

    <div class="form-group" style="margin-bottom:20px;">
        <label class="form-label">ملاحظات</label>
        <textarea name="notes" class="form-textarea" rows="2"></textarea>
    </div>

    <div style="display:flex;gap:10px;">
        <button type="submit" class="btn btn-primary" onclick="return confirm('سيتم تسجيل حالة الوفاة. متأكد؟')">تسجيل حالة الوفاة</button>
        <a href="/members/<?= (int) $member['id'] ?>" class="btn btn-outline">إلغاء</a>
    </div>
</form>
<?php $__template->endSection(); ?>

<?php $__template->section('scripts'); ?>
<script>
document.addEventListener('DOMContentLoaded', function() {
    var deceasedType = document.getElementById('deceased_type');
    var primarySection = document.getElementById('primary-death-section');
    var feeSection = document.getElementById('fee-section');
    var hiddenSpouse = document.getElementById('hidden_spouse_id');
    var hiddenChild = document.getElementById('hidden_child_id');
    var primarySelect = document.getElementById('primary_spouse_id');

    var docsSection = document.getElementById('docs-section');

    deceasedType.addEventListener('change', function() {
        var selected = this.options[this.selectedIndex];
        hiddenSpouse.value = selected.dataset.spouseId || '';
        hiddenChild.value = selected.dataset.childId || '';

        if (this.value === 'primary_member') {
            primarySection.style.display = 'block';
            feeSection.style.display = 'block';
            docsSection.style.display = 'block';
            document.getElementById('death_certificate').required = true;
            document.getElementById('inheritance_notice').required = true;
        } else {
            primarySection.style.display = 'none';
            feeSection.style.display = 'none';
            docsSection.style.display = 'none';
            document.getElementById('death_certificate').required = false;
            document.getElementById('inheritance_notice').required = false;
        }
    });

    // Hide secondary spouse checkbox that matches primary selection
    if (primarySelect) {
        primarySelect.addEventListener('change', function() {
            var primaryId = this.value;
            document.querySelectorAll('.secondary-spouse-label').forEach(function(label) {
                if (label.dataset.id === primaryId) {
                    label.style.display = 'none';
                    label.querySelector('input').checked = false;
                } else {
                    label.style.display = 'flex';
                }
            });
        });
    }
});
</script>
<?php $__template->endSection(); ?>
