<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>الأرشيف<?php $__template->endSection(); ?>
<?php $__template->section('content'); ?>
<?php
$entityTypeLabels = ['members' => 'أعضاء', 'employees' => 'موظفين', 'roles' => 'أدوار', 'spouses' => 'زوجات', 'children' => 'أبناء', 'temporary_members' => 'مؤقتين'];
$reasonLabels = ['transfer' => 'تحويل', 'full_transfer' => 'تحويل كامل', 'separation' => 'فصل', 'child_separation' => 'فصل ابن', 'child_mandatory_25' => 'فصل إلزامي (25 سنة)', 'divorce' => 'طلاق', 'death' => 'وفاة', 'waiver' => 'تنازل', 'sports_conversion' => 'تحويل رياضي', 'cross_branch' => 'نقل بين فروع', 'status_change' => 'تغيير حالة', 'data_correction' => 'تصحيح بيانات', 'manual' => 'يدوي'];
?>
<div class="card" style="margin-bottom:20px;padding:15px;">
    <form method="GET" action="/archive" style="display:flex;gap:10px;flex-wrap:wrap;align-items:end;">
        <div>
            <label class="form-label" style="font-size:12px;">بحث</label>
            <input type="text" name="q" value="<?= e($filters['search'] ?? '') ?>" placeholder="بحث..." class="form-input" style="min-width:150px;">
        </div>
        <div>
            <label class="form-label" style="font-size:12px;">نوع الكيان</label>
            <select name="entity_type" class="form-select" style="min-width:120px;">
                <option value="">الكل</option>
                <?php foreach ($entityTypes as $et): ?>
                    <option value="<?= e($et) ?>" <?= ($filters['entity_type'] ?? '') === $et ? 'selected' : '' ?>><?= e($entityTypeLabels[$et] ?? $et) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div>
            <label class="form-label" style="font-size:12px;">السبب</label>
            <select name="snapshot_reason" class="form-select" style="min-width:120px;">
                <option value="">الكل</option>
                <?php foreach ($reasons as $r): ?>
                    <option value="<?= e($r) ?>" <?= ($filters['snapshot_reason'] ?? '') === $r ? 'selected' : '' ?>><?= e($reasonLabels[$r] ?? $r) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div>
            <label class="form-label" style="font-size:12px;">رقم العضوية</label>
            <input type="text" name="membership_number" value="<?= e($filters['membership_number'] ?? '') ?>" placeholder="رقم العضوية" class="form-input" style="min-width:120px;">
        </div>
        <div>
            <label class="form-label" style="font-size:12px;">من</label>
            <input type="date" name="date_from" value="<?= e($filters['date_from'] ?? '') ?>" class="form-input">
        </div>
        <div>
            <label class="form-label" style="font-size:12px;">إلى</label>
            <input type="date" name="date_to" value="<?= e($filters['date_to'] ?? '') ?>" class="form-input">
        </div>
        <button type="submit" class="btn btn-outline">بحث</button>
        <a href="/archive" class="btn btn-sm btn-outline" style="color:#6B7280;">مسح</a>
    </form>
</div>

<div class="card">
    <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>
                    <th>الإجراءات</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($rows as $r): ?>
                <tr>
                    <td><?= (int) $r['id'] ?></td>
                    <td style="font-size:12px;white-space:nowrap;"><?= e($r['snapshot_taken_at']) ?></td>
                    <td><code style="font-size:12px;"><?= e($entityTypeLabels[$r['entity_type']] ?? $r['entity_type']) ?></code></td>
                    <td><?= (int) $r['entity_id'] ?></td>
                    <td>
                        <?php if ($r['membership_number']): ?>
                            <a href="/archive/number-chain/<?= urlencode($r['membership_number']) ?>" style="color:#0D7377;font-weight:600;"><?= e($r['membership_number']) ?></a>
                        <?php else: ?>
                            —
                        <?php endif; ?>
                    </td>
                    <td>
                        <?php
                        $reasonColors = [
                            'transfer' => '#0284C7', 'separation' => '#D97706', 'divorce' => '#DC2626',
                            'death' => '#6B7280', 'waiver' => '#7C3AED', 'status_change' => '#059669',
                            'data_correction' => '#0D7377', 'manual' => '#9CA3AF',
                        ];
                        $color = $reasonColors[$r['snapshot_reason']] ?? '#6B7280';
                        ?>
                        <span style="color:<?= $color ?>;font-weight:600;font-size:13px;"><?= e($reasonLabels[$r['snapshot_reason']] ?? $r['snapshot_reason']) ?></span>
                    </td>
                    <td style="font-size:13px;"><?= $r['snapshot_taken_by'] ? '#' . (int) $r['snapshot_taken_by'] : 'النظام' ?></td>
                    <td style="font-size:12px;color:#6B7280;max-width:200px;overflow:hidden;text-overflow:ellipsis;"><?= e(mb_substr($r['notes'] ?? '', 0, 80)) ?: '—' ?></td>
                    <td>
                        <div style="display:flex;gap:5px;">
                            <a href="/archive/<?= (int) $r['id'] ?>" class="btn btn-sm btn-outline">عرض</a>
                        </div>
                    </td>
                </tr>
                <?php endforeach; ?>
                <?php if (empty($rows)): ?>
                <tr><td colspan="9" style="text-align:center;padding:40px;color:#6B7280;">لا توجد لقطات أرشيفية</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
    <?php if (isset($pagination['last_page']) && $pagination['last_page'] > 1): ?>
    <div style="padding:15px;">
        <nav class="pagination-wrapper">
            <ul class="pagination" style="display:flex;gap:5px;list-style:none;padding:0;justify-content:center;">
                <?php if ($pagination['has_prev'] ?? false): ?>
                    <li><a href="?page=<?= $pagination['prev_page'] ?>&q=<?= urlencode($filters['search'] ?? '') ?>&entity_type=<?= urlencode($filters['entity_type'] ?? '') ?>&snapshot_reason=<?= urlencode($filters['snapshot_reason'] ?? '') ?>&membership_number=<?= urlencode($filters['membership_number'] ?? '') ?>&date_from=<?= urlencode($filters['date_from'] ?? '') ?>&date_to=<?= urlencode($filters['date_to'] ?? '') ?>" class="btn btn-sm btn-outline">السابق</a></li>
                <?php endif; ?>
                <?php foreach ($pagination['pages'] ?? [] as $p): ?>
                    <?php if ($p === '...'): ?>
                        <li style="padding:5px;">...</li>
                    <?php else: ?>
                        <li><a href="?page=<?= $p ?>&q=<?= urlencode($filters['search'] ?? '') ?>&entity_type=<?= urlencode($filters['entity_type'] ?? '') ?>&snapshot_reason=<?= urlencode($filters['snapshot_reason'] ?? '') ?>&membership_number=<?= urlencode($filters['membership_number'] ?? '') ?>&date_from=<?= urlencode($filters['date_from'] ?? '') ?>&date_to=<?= urlencode($filters['date_to'] ?? '') ?>" class="btn btn-sm <?= $p === ($pagination['current_page'] ?? 1) ? 'btn-primary' : 'btn-outline' ?>"><?= $p ?></a></li>
                    <?php endif; ?>
                <?php endforeach; ?>
                <?php if ($pagination['has_next'] ?? false): ?>
                    <li><a href="?page=<?= $pagination['next_page'] ?>&q=<?= urlencode($filters['search'] ?? '') ?>&entity_type=<?= urlencode($filters['entity_type'] ?? '') ?>&snapshot_reason=<?= urlencode($filters['snapshot_reason'] ?? '') ?>&membership_number=<?= urlencode($filters['membership_number'] ?? '') ?>&date_from=<?= urlencode($filters['date_from'] ?? '') ?>&date_to=<?= urlencode($filters['date_to'] ?? '') ?>" class="btn btn-sm btn-outline">التالي</a></li>
                <?php endif; ?>
            </ul>
        </nav>
    </div>
    <?php endif; ?>
</div>
<?php $__template->endSection(); ?>