<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>إدارة الأعضاء<?php $__template->endSection(); ?>
<?php $__template->section('page_actions'); ?>
<?php if (can('member.create')): ?>
<a href="/members/create" class="btn btn-primary">+ عضو جديد</a>
<?php endif; ?>
<?php $__template->endSection(); ?>
<?php $__template->section('content'); ?>

<div class="card" style="margin-bottom:20px;padding:15px;">
    <form method="GET" action="/members" 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['q'] ?? '') ?>" placeholder="الاسم، الرقم القومي، رقم العضوية..." class="form-input" style="min-width:250px;">
        </div>
        <div>
            <label class="form-label" style="font-size:12px;">الحالة</label>
            <select name="status" class="form-select" style="min-width:120px;">
                <option value="">الكل</option>
                <?php foreach ($statuses as $val => $label): ?>
                    <option value="<?= e($val) ?>" <?= ($filters['status'] ?? '') === $val ? 'selected' : '' ?>><?= e($label) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div>
            <label class="form-label" style="font-size:12px;">الفرع</label>
            <select name="branch_id" class="form-select" style="min-width:120px;">
                <option value="">الكل</option>
                <?php foreach ($branches as $b): ?>
                    <option value="<?= (int) $b['id'] ?>" <?= ($filters['branch_id'] ?? '') == $b['id'] ? 'selected' : '' ?>><?= e($b['name_ar']) ?></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>
        <button type="submit" class="btn btn-outline">بحث</button>
        <a href="/members" 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>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($members as $m): ?>
                <tr>
                    <td style="font-weight:600;"><?= e($m['membership_number'] ?? '—') ?></td>
                    <td>
                        <a href="/members/<?= (int) $m['id'] ?>" style="color:#0D7377;font-weight:600;"><?= e($m['full_name_ar']) ?></a>
                        <?php if ($m['full_name_en']): ?>
                            <div style="font-size:11px;color:#9CA3AF;"><?= e($m['full_name_en']) ?></div>
                        <?php endif; ?>
                    </td>
                    <td style="direction:ltr;text-align:right;font-size:13px;"><?= e($m['national_id'] ?? '—') ?></td>
                    <td style="direction:ltr;text-align:right;font-size:13px;"><?= e($m['phone_mobile']) ?></td>
                    <td style="font-size:13px;"><?= e($m['branch_name'] ?? '—') ?></td>
                    <td>
                        <?php
                        $statusLabels = [
                            'potential' => 'محتمل', 'under_review' => 'مراجعة', 'interview_scheduled' => 'مقابلة',
                            'accepted' => 'مقبول', 'rejected' => 'مرفوض', 'payment_pending' => 'انتظار سداد',
                            'active' => 'فعال', 'frozen' => 'مجمد', 'suspended' => 'موقوف',
                            'dropped' => 'مسقط', 'expired' => 'منتهي',
                        ];
                        $statusColors = [
                            'potential' => '#6B7280', 'under_review' => '#D97706', 'interview_scheduled' => '#0284C7',
                            'accepted' => '#059669', 'rejected' => '#DC2626', 'payment_pending' => '#D97706',
                            'active' => '#059669', 'frozen' => '#6B7280', 'suspended' => '#DC2626',
                            'dropped' => '#DC2626', 'expired' => '#9CA3AF',
                        ];
                        $color = $statusColors[$m['status']] ?? '#6B7280';
                        $label = $statusLabels[$m['status']] ?? $m['status'];
                        ?>
                        <span style="color:<?= $color ?>;font-weight:600;font-size:13px;">● <?= e($label) ?></span>
                    </td>
                    <td style="font-size:12px;"><?= e(substr($m['created_at'], 0, 10)) ?></td>
                    <td>
                        <div style="display:flex;gap:5px;">
                            <?php if (can('member.view')): ?>
                            <a href="/members/<?= (int) $m['id'] ?>" class="btn btn-sm btn-outline">عرض</a>
                            <?php endif; ?>
                            <?php if (can('member.edit')): ?>
                            <a href="/members/<?= (int) $m['id'] ?>/edit" class="btn btn-sm btn-outline">تعديل</a>
                            <?php endif; ?>
                        </div>
                    </td>
                </tr>
                <?php endforeach; ?>
                <?php if (empty($members)): ?>
                <tr><td colspan="8" style="text-align:center;padding:40px;color:#6B7280;">لا يوجد أعضاء</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
    <?php if (isset($pagination) && ($pagination['last_page'] ?? 1) > 1): ?>
    <div style="padding:15px;">
        <?php $__template->include('Shared.Components.pagination', ['pagination' => $pagination]); ?>
    </div>
    <?php endif; ?>
</div>
<?php $__template->endSection(); ?>