<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>أرشيف الأعضاء<?php $__template->endSection(); ?>
<?php $__template->section('content'); ?>

<?php
$statusLabels = ['deceased' => 'وفاة', 'transferred' => 'تحويل', 'waived' => 'تنازل', 'divorced' => 'طلاق', 'cancelled' => 'إلغاء'];
$statusColors = ['deceased' => '#6B7280', 'transferred' => '#0284C7', 'waived' => '#7C3AED', 'divorced' => '#DC2626', 'cancelled' => '#D97706'];
?>

<!-- Header -->
<div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:20px;">
    <div>
        <h2 style="margin:0;color:#1A1A2E;">أرشيف الأعضاء</h2>
        <p style="margin:4px 0 0;font-size:13px;color:#6B7280;">جميع العضويات المؤرشفة — وفاة، تنازل، تحويل، طلاق، إلغاء (<?= number_format($total) ?> سجل)</p>
    </div>
</div>

<!-- Search & Filters -->
<div class="card" style="margin-bottom:20px;padding:15px;">
    <form method="GET" action="/members/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>
            <input type="text" name="number" value="<?= e($filters['number'] ?? '') ?>" placeholder="1001" class="form-input" style="min-width:100px;">
        </div>
        <div>
            <label class="form-label" style="font-size:12px;">الرقم القومي</label>
            <input type="text" name="nid" value="<?= e($filters['nid'] ?? '') ?>" placeholder="الرقم القومي" class="form-input" style="min-width:130px;">
        </div>
        <div>
            <label class="form-label" style="font-size:12px;">الهاتف</label>
            <input type="text" name="phone" value="<?= e($filters['phone'] ?? '') ?>" placeholder="رقم الهاتف" class="form-input" style="min-width:110px;">
        </div>
        <div>
            <label class="form-label" style="font-size:12px;">نوع الأرشفة</label>
            <select name="status" class="form-select" style="min-width:100px;">
                <option value="">الكل</option>
                <?php foreach ($statusLabels as $k => $v): ?>
                    <option value="<?= e($k) ?>" <?= ($filters['status'] ?? '') === $k ? 'selected' : '' ?>><?= e($v) ?></option>
                <?php endforeach; ?>
            </select>
        </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>
        <div>
            <label class="form-label" style="font-size:12px;">الموظف المنفذ</label>
            <input type="text" name="operator" value="<?= e($filters['operator'] ?? '') ?>" placeholder="اسم الموظف" class="form-input" style="min-width:120px;">
        </div>
        <button type="submit" class="btn btn-primary">بحث</button>
        <a href="/members/archive" class="btn btn-outline" style="color:#6B7280;">مسح</a>
    </form>
</div>

<!-- Results Table -->
<div class="card">
    <div class="table-responsive">
        <table class="data-table">
            <thead>
                <tr>
                    <th style="width:50px;">#</th>
                    <th>العضو</th>
                    <th>رقم العضوية</th>
                    <th>الرقم القومي</th>
                    <th>نوع الأرشفة</th>
                    <th>السبب</th>
                    <th>تاريخ الأرشفة</th>
                    <th>بواسطة</th>
                    <th style="width:100px;">الإجراءات</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($rows as $r): ?>
                <?php $color = $statusColors[$r['status']] ?? '#6B7280'; ?>
                <tr>
                    <td><?= (int) $r['id'] ?></td>
                    <td>
                        <a href="/members/archive/<?= (int) $r['id'] ?>" style="color:#0D7377;font-weight:600;text-decoration:none;">
                            <?= e($r['full_name_ar'] ?? '') ?>
                        </a>
                        <?php if ($r['phone_mobile']): ?>
                            <div style="font-size:11px;color:#6B7280;"><?= e($r['phone_mobile']) ?></div>
                        <?php endif; ?>
                    </td>
                    <td>
                        <?php if ($r['archived_membership_number']): ?>
                            <a href="/archive/number-chain/<?= urlencode($r['archived_membership_number']) ?>" style="color:#0D7377;font-weight:700;"><?= e($r['archived_membership_number']) ?></a>
                        <?php else: ?>
                            <span style="color:#9CA3AF;">—</span>
                        <?php endif; ?>
                    </td>
                    <td style="font-size:12px;font-family:monospace;"><?= e($r['national_id'] ?? '—') ?></td>
                    <td>
                        <span style="color:<?= $color ?>;font-weight:600;font-size:13px;display:inline-flex;align-items:center;gap:4px;">
                            <span style="width:8px;height:8px;border-radius:50%;background:<?= $color ?>;display:inline-block;"></span>
                            <?= e($statusLabels[$r['status']] ?? $r['status']) ?>
                        </span>
                    </td>
                    <td style="font-size:12px;color:#6B7280;"><?= e($r['snapshot_reason'] ?? '—') ?></td>
                    <td style="font-size:12px;white-space:nowrap;"><?= $r['archived_at'] ? arabic_date($r['archived_at']) : '—' ?></td>
                    <td style="font-size:12px;"><?= e($r['operator_name'] ?? 'النظام') ?></td>
                    <td>
                        <a href="/members/archive/<?= (int) $r['id'] ?>" class="btn btn-sm btn-outline">عرض</a>
                    </td>
                </tr>
                <?php endforeach; ?>
                <?php if (empty($rows)): ?>
                <tr><td colspan="9" style="text-align:center;padding:50px;color:#6B7280;">لا توجد سجلات مؤرشفة مطابقة لمعايير البحث</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>

    <?php if (($pagination['last_page'] ?? 1) > 1): ?>
    <div style="padding:15px;display:flex;justify-content:center;">
        <nav>
            <ul style="display:flex;gap:5px;list-style:none;padding:0;margin:0;">
                <?php if ($pagination['has_prev'] ?? false): ?>
                    <li><a href="?page=<?= $pagination['prev_page'] ?>&<?= http_build_query(array_diff_key($filters, ['page' => ''])) ?>" class="btn btn-sm btn-outline">السابق</a></li>
                <?php endif; ?>
                <?php foreach ($pagination['pages'] ?? [] as $p): ?>
                    <?php if ($p === '...'): ?>
                        <li style="padding:6px 10px;color:#6B7280;">...</li>
                    <?php else: ?>
                        <li><a href="?page=<?= $p ?>&<?= http_build_query(array_diff_key($filters, ['page' => ''])) ?>" 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'] ?>&<?= http_build_query(array_diff_key($filters, ['page' => ''])) ?>" class="btn btn-sm btn-outline">التالي</a></li>
                <?php endif; ?>
            </ul>
        </nav>
    </div>
    <?php endif; ?>
</div>
<?php $__template->endSection(); ?>
