<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>الاشتراكات الشهرية<?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<form method="POST" action="/sa/subscriptions/generate" style="display:inline-flex;gap:8px;align-items:center;">
    <?= csrf_field() ?>
    <input type="month" name="year_month" value="<?= e($filters['month'] ?? date('Y-m')) ?>" class="form-input" style="width:160px;">
    <button type="submit" class="btn btn-primary" onclick="return confirm('هل تريد توليد الاشتراكات لهذا الشهر؟');">
        <i data-lucide="refresh-cw" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> توليد الاشتراكات
    </button>
</form>
<?php $__template->endSection(); ?>

<?php $__template->section('content'); ?>

<!-- Filters -->
<div class="card" style="margin-bottom:20px;padding:15px;">
    <form method="GET" action="/sa/subscriptions" style="display:flex;gap:10px;align-items:end;flex-wrap:wrap;">
        <div style="min-width:160px;">
            <label class="form-label" style="font-size:12px;">الشهر</label>
            <input type="month" name="month" value="<?= e($filters['month'] ?? '') ?>" class="form-input">
        </div>
        <div style="min-width:180px;">
            <label class="form-label" style="font-size:12px;">المجموعة</label>
            <select name="group_id" class="form-select">
                <option value="">-- الكل --</option>
                <?php foreach ($groups as $g): ?>
                <option value="<?= (int) $g['id'] ?>" <?= ($filters['group_id'] ?? '') === (string) $g['id'] ? 'selected' : '' ?>><?= e($g['name_ar']) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div style="min-width:150px;">
            <label class="form-label" style="font-size:12px;">حالة الدفع</label>
            <select name="payment_status" class="form-select">
                <option value="">-- الكل --</option>
                <?php foreach ($statusOptions as $key => $label): ?>
                <option value="<?= e($key) ?>" <?= ($filters['payment_status'] ?? '') === $key ? 'selected' : '' ?>><?= e($label) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <button type="submit" class="btn btn-outline"><i data-lucide="search" style="width:16px;height:16px;vertical-align:middle;"></i> بحث</button>
        <a href="/sa/subscriptions" class="btn btn-sm btn-outline" style="color:#6B7280;">مسح</a>
    </form>
</div>

<!-- Subscriptions Table -->
<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>
                </tr>
            </thead>
            <tbody>
                <?php if (!empty($subscriptions)): ?>
                <?php foreach ($subscriptions as $sub): ?>
                <tr>
                    <td><code style="font-size:11px;background:#F3F4F6;padding:2px 6px;border-radius:4px;"><?= e($sub['subscription_number'] ?? '') ?></code></td>
                    <td><?= e($sub['player_name'] ?? '—') ?></td>
                    <td><?= e($sub['group_name'] ?? '—') ?></td>
                    <td style="font-size:12px;"><?= e($sub['period_start'] ?? '') ?> - <?= e($sub['period_end'] ?? '') ?></td>
                    <td style="font-weight:600;"><?= money((float) ($sub['final_amount'] ?? 0)) ?></td>
                    <td>
                        <?php
                        $statusColors = [
                            'unpaid' => 'background:#FEF3C7;color:#92400E;',
                            'paid' => 'background:#ECFDF5;color:#059669;',
                            'partial' => 'background:#DBEAFE;color:#1D4ED8;',
                            'exempt' => 'background:#F3E8FF;color:#7C3AED;',
                            'overdue' => 'background:#FEE2E2;color:#DC2626;',
                        ];
                        $statusStyle = $statusColors[$sub['payment_status'] ?? ''] ?? 'background:#F3F4F6;color:#374151;';
                        ?>
                        <span style="<?= $statusStyle ?>padding:3px 10px;border-radius:10px;font-size:12px;font-weight:600;">
                            <?= e($statusOptions[$sub['payment_status'] ?? ''] ?? $sub['payment_status'] ?? '') ?>
                        </span>
                    </td>
                    <td>
                        <a href="/sa/subscriptions/<?= (int) $sub['id'] ?>" class="btn btn-sm btn-outline" style="font-size:12px;padding:4px 10px;">
                            <i data-lucide="eye" style="width:13px;height:13px;vertical-align:middle;"></i> عرض
                        </a>
                    </td>
                </tr>
                <?php endforeach; ?>
                <?php else: ?>
                <tr>
                    <td colspan="7" style="text-align:center;padding:40px;color:#6B7280;">
                        <i data-lucide="credit-card" style="width:36px;height:36px;color:#D1D5DB;display:block;margin:0 auto 10px;"></i>
                        لا توجد اشتراكات
                    </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, 'baseUrl' => '/sa/subscriptions?' . http_build_query(array_filter($filters))]); ?>
    </div>
    <?php endif; ?>
</div>

<script>
document.addEventListener('DOMContentLoaded', function() {
    if (typeof lucide !== 'undefined') { lucide.createIcons(); }
});
</script>
<?php $__template->endSection(); ?>
