<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>إدارة الكارنيهات<?php $__template->endSection(); ?>
<?php $__template->section('page_actions'); ?>
<div style="padding:8px 16px;background:#EFF6FF;border:1px solid #BFDBFE;border-radius:6px;font-size:13px;color:#0284C7;">
    💡 لإصدار كارنيه جديد، اذهب لصفحة العضو واختر "🪪 إصدار كارنيه"
</div>
<?php $__template->endSection(); ?>
<?php $__template->section('content'); ?>
<div class="card" style="margin-bottom:20px;padding:15px;">
    <form method="GET" action="/carnets" 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:200px;"></div>
        <div><label class="form-label" style="font-size:12px;">الحالة</label><select name="is_active" class="form-select"><option value="">الكل</option><option value="1" <?= ($filters['is_active'] ?? '') === '1' ? 'selected' : '' ?>>نشط</option><option value="0" <?= ($filters['is_active'] ?? '') === '0' ? 'selected' : '' ?>>ملغى</option></select></div>
        <div><label class="form-label" style="font-size:12px;">النوع</label><select name="type" class="form-select"><option value="">الكل</option><option value="regular" <?= ($filters['type'] ?? '') === 'regular' ? 'selected' : '' ?>>عادي</option><option value="seasonal" <?= ($filters['type'] ?? '') === 'seasonal' ? 'selected' : '' ?>>موسمي</option></select></div>
        <button type="submit" class="btn btn-outline">بحث</button>
        <a href="/carnets" 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><th>الإجراءات</th></tr></thead><tbody>
<?php foreach ($rows as $r): ?>
<tr>
    <td style="font-weight:600;direction:ltr;text-align:right;"><?= e($r['carnet_number']) ?></td>
    <td><a href="/members/<?= (int) $r['member_id'] ?>" style="color:#0D7377;font-weight:600;"><?= e($r['member_name']) ?></a></td>
    <td style="font-size:13px;"><?= e($r['membership_number'] ?? '—') ?></td>
    <td style="font-size:13px;"><?= $r['carnet_type'] === 'seasonal' ? 'موسمي' : 'عادي' ?></td>
    <td style="font-size:12px;"><?= e(substr($r['issued_at'] ?? '', 0, 10)) ?></td>
    <td style="font-size:13px;"><?= e($r['issued_by_name'] ?? '—') ?></td>
    <td style="text-align:center;"><?= (int) $r['print_count'] ?></td>
    <td style="text-align:center;"><?= (int) $r['replacement_count'] ?></td>
    <td>
        <?php if ($r['is_active']): ?>
            <span style="color:#059669;font-weight:600;">● نشط</span>
        <?php else: ?>
            <span style="color:#DC2626;font-weight:600;">● ملغى</span>
        <?php endif; ?>
    </td>
    <td>
        <div style="display:flex;gap:5px;">
            <?php if ($r['is_active']): ?>
                <?php if (can('carnet.print')): ?>
                <a href="/carnets/<?= (int) $r['id'] ?>/print" class="btn btn-sm btn-primary" target="_blank">طباعة</a>
                <?php endif; ?>
                <?php if (can('carnet.deactivate')): ?>
                <form method="POST" action="/carnets/<?= (int) $r['id'] ?>/deactivate" style="display:inline;"><?= csrf_field() ?><input type="hidden" name="reason" value="إلغاء يدوي"><button type="submit" class="btn btn-sm btn-outline" style="color:#DC2626;" onclick="return confirm('إلغاء الكارنيه؟')">إلغاء</button></form>
                <?php endif; ?>
            <?php else: ?>
                <span style="color:#9CA3AF;font-size:12px;"><?= e($r['deactivated_reason'] ?? '—') ?></span>
            <?php endif; ?>
        </div>
    </td>
</tr>
<?php endforeach; ?>
<?php if (empty($rows)): ?><tr><td colspan="10" style="text-align:center;padding:40px;color:#6B7280;">لا توجد كارنيهات</td></tr><?php endif; ?>
</tbody></table></div></div>
<?php $__template->endSection(); ?>