<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>تقرير أبناء يقتربون من السن القانوني<?php $__template->endSection(); ?>
<?php $__template->section('content'); ?>

<div class="card" style="margin-bottom:20px;padding:15px;">
    <form method="GET" action="/reports/children-aging" style="display:flex;gap:10px;flex-wrap:wrap;align-items:end;">
        <div>
            <label class="form-label" style="font-size:12px;">العمر من</label>
            <input type="number" name="min_age" value="<?= (int) $minAge ?>" min="0" max="99" class="form-input" style="width:80px;">
        </div>
        <div>
            <label class="form-label" style="font-size:12px;">العمر إلى</label>
            <input type="number" name="max_age" value="<?= (int) $maxAge ?>" min="0" max="99" class="form-input" style="width:80px;">
        </div>
        <button type="submit" class="btn btn-outline">تصفية</button>
        <a href="/reports/children-aging" class="btn btn-sm btn-outline" style="color:#6B7280;">مسح</a>
    </form>
</div>

<?php if (!empty($nearThreshold)): ?>
<div class="card" style="margin-bottom:20px;padding:15px;border-right:4px solid #F59E0B;">
    <h4 style="margin:0 0 10px;color:#D97706;">أبناء خلال 6 أشهر من سن 25 (يتطلبون إجراء)</h4>
    <div class="table-responsive">
        <table class="data-table">
            <thead><tr><th>الابن</th><th>تاريخ الميلاد</th><th>العمر</th><th>ولي الأمر</th><th>رقم العضوية</th></tr></thead>
            <tbody>
            <?php foreach ($nearThreshold as $c): ?>
                <tr>
                    <td><?= e($c['full_name_ar']) ?></td>
                    <td><?= e($c['date_of_birth']) ?></td>
                    <td><?= (int) $c['current_age'] ?> سنة</td>
                    <td><a href="/members/<?= (int) $c['member_id'] ?>"><?= e($c['parent_name']) ?></a></td>
                    <td><?= e($c['membership_number'] ?? '—') ?></td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
<?php endif; ?>

<div class="card">
    <div style="padding:15px;border-bottom:1px solid #E5E7EB;">
        <strong>النتائج:</strong> <?= count($children) ?> ابن/ابنة بين <?= (int) $minAge ?> و <?= (int) $maxAge ?> سنة
    </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>
                    <th>الهاتف</th>
                </tr>
            </thead>
            <tbody>
            <?php if (empty($children)): ?>
                <tr><td colspan="8" style="text-align:center;padding:30px;color:#6B7280;">لا توجد نتائج</td></tr>
            <?php else: ?>
                <?php foreach ($children as $c): ?>
                <tr>
                    <td><?= e($c['full_name_ar']) ?></td>
                    <td><?= e($c['date_of_birth']) ?></td>
                    <td><?= (int) $c['current_age'] ?> سنة</td>
                    <td><?= $c['gender'] === 'male' ? 'ذكر' : 'أنثى' ?></td>
                    <td>
                        <?php if ($c['is_frozen']): ?>
                            <span class="badge badge-warning">مجمد</span>
                        <?php elseif ($c['child_status'] === 'active'): ?>
                            <span class="badge badge-success">نشط</span>
                        <?php else: ?>
                            <span class="badge badge-secondary"><?= e($c['child_status'] ?? '—') ?></span>
                        <?php endif; ?>
                    </td>
                    <td><a href="/members/<?= (int) $c['member_id'] ?>"><?= e($c['parent_name']) ?></a></td>
                    <td><?= e($c['membership_number'] ?? '—') ?></td>
                    <td style="direction:ltr;text-align:right;"><?= e($c['phone_mobile'] ?? '—') ?></td>
                </tr>
                <?php endforeach; ?>
            <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

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